<commit_before>/*
 * Copyright 2010-2012 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.processor.block;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.processor.OComposableProcessor;
import com.orientechnologies.orient.core.processor.OProcessException;
import com.orientechnologies.orient.core.record.impl.ODocument;

public class OExecuteBlock extends OAbstractBlock {
  public static final String NAME = "execute";
  private Object             returnValue;

  @Override
  public Object processBlock(OComposableProcessor iManager, final OCommandContext iContext, final ODocument iConfig,
      ODocument iOutput, final boolean iReadOnly) {

    final Object foreach = getField(iContext, iConfig, "foreach");
    String returnType = (String) getFieldOfClass(iContext, iConfig, "returnType", String.class);

    returnValue = null;
    if (returnType == null)
      returnType = "last";
    else if ("list".equalsIgnoreCase(returnType))
      returnValue = new ArrayList<Object>();
    else if ("set".equalsIgnoreCase(returnType))
      returnValue = new HashSet<Object>();

    int iterated = 0;

    final Object beginClause = getField(iContext, iConfig, "begin");
    if (beginClause != null)
      executeBlock(iManager, iContext, "begin", beginClause, iOutput, iReadOnly, returnType, returnValue);

    if (foreach != null) {
      Object result;
      if (foreach instanceof ODocument)
        result = delegate("foreach", iManager, (ODocument) foreach, iContext, iOutput, iReadOnly);
      else if (foreach instanceof Map)
        result = ((Map<?, ?>) foreach).values();
      else
        result = foreach;

      if (!OMultiValue.isIterable(result))
        throw new OProcessException("Result of 'foreach' block (" + foreach + ") must be iterable but found " + result.getClass());

      for (Object current : OMultiValue.getMultiValueIterable(result)) {
        if (current instanceof Map.Entry)
          current = ((Entry<?, ?>) current).getValue();

        assignVariable(iContext, "current", current);

        debug(iContext, "Executing...");
        final Object doClause = getRequiredField(iContext, iConfig, "do");

        returnValue = executeDo(iManager, iContext, doClause, returnType, returnValue, iOutput, iReadOnly);

        debug(iContext, "Done");

        iterated++;
      }

    } else {
      debug(iContext, "Executing...");
      final Object doClause = getRequiredField(iContext, iConfig, "do");
      returnValue = executeDo(iManager, iContext, doClause, returnType, returnValue, iOutput, iReadOnly);
      debug(iContext, "Done");
    }

    final Object endClause = getField(iContext, iConfig, "end");
    if (endClause != null)
      executeBlock(iManager, iContext, "end", endClause, iOutput, iReadOnly, returnType, returnValue);<commit_msg>OPLA: execute now stores in context the "currentIndex" variable as counter<commit_after>/*
 * Copyright 2010-2012 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.processor.block;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.processor.OComposableProcessor;
import com.orientechnologies.orient.core.processor.OProcessException;
import com.orientechnologies.orient.core.record.impl.ODocument;

public class OExecuteBlock extends OAbstractBlock {
  public static final String NAME = "execute";
  private Object             returnValue;

  @Override
  public Object processBlock(OComposableProcessor iManager, final OCommandContext iContext, final ODocument iConfig,
      ODocument iOutput, final boolean iReadOnly) {

    final Object foreach = getField(iContext, iConfig, "foreach");
    String returnType = (String) getFieldOfClass(iContext, iConfig, "returnType", String.class);

    returnValue = null;
    if (returnType == null)
      returnType = "last";
    else if ("list".equalsIgnoreCase(returnType))
      returnValue = new ArrayList<Object>();
    else if ("set".equalsIgnoreCase(returnType))
      returnValue = new HashSet<Object>();

    int iterated = 0;

    final Object beginClause = getField(iContext, iConfig, "begin");
    if (beginClause != null)
      executeBlock(iManager, iContext, "begin", beginClause, iOutput, iReadOnly, returnType, returnValue);

    if (foreach != null) {
      Object result;
      if (foreach instanceof ODocument)
        result = delegate("foreach", iManager, (ODocument) foreach, iContext, iOutput, iReadOnly);
      else if (foreach instanceof Map)
        result = ((Map<?, ?>) foreach).values();
      else
        result = foreach;

      if (!OMultiValue.isIterable(result))
        throw new OProcessException("Result of 'foreach' block (" + foreach + ") must be iterable but found " + result.getClass());

      for (Object current : OMultiValue.getMultiValueIterable(result)) {
        if (current instanceof Map.Entry)
          current = ((Entry<?, ?>) current).getValue();

        assignVariable(iContext, "current", current);
        assignVariable(iContext, "currentIndex", iterated);

        debug(iContext, "Executing...");
        final Object doClause = getRequiredField(iContext, iConfig, "do");

        returnValue = executeDo(iManager, iContext, doClause, returnType, returnValue, iOutput, iReadOnly);

        debug(iContext, "Done");

        iterated++;
      }

    } else {
      debug(iContext, "Executing...");
      final Object doClause = getRequiredField(iContext, iConfig, "do");
      returnValue = executeDo(iManager, iContext, doClause, returnType, returnValue, iOutput, iReadOnly);
      debug(iContext, "Done");
    }

    final Object endClause = getField(iContext, iConfig, "end");
    if (endClause != null)
      executeBlock(iManager, iContext, "end", endClause, iOutput, iReadOnly, returnType, returnValue);
{'fraction_non_alphanumeric': 0.1074949358541526, 'fraction_numerical': 0.003511141120864281, 'mean_word_length': 3.930758988015979, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '21674', 'n_tokens_mistral': 2051, 'n_tokens_neox': 2030, 'n_words': 610}