Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and evaluate throttle input #1392

Merged
merged 4 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public interface SlangTextualKeys {
String PARALLEL_LOOP_KEY = "parallel_loop";
String WORKER_GROUP = "worker_group"; //&& flow
String ROBOT_GROUP = "robot_group";
String MAX_THROTTLE_KEY = "max_throttle";

//seq step
String SEQ_STEP_ID_KEY = "id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
import static ch.lambdaj.Lambda.filter;
import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static io.cloudslang.lang.compiler.SlangTextualKeys.DO_EXTERNAL_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.DO_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.FOR_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.INPUTS_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.MAX_THROTTLE_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.NAVIGATION_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.ON_FAILURE_KEY;
import static io.cloudslang.lang.compiler.SlangTextualKeys.PARALLEL_LOOP_KEY;
Expand Down Expand Up @@ -122,7 +124,9 @@ public class ExecutableBuilder {

private List<String> stepAdditionalKeyWords = asList(LOOP_KEY, DO_KEY, DO_EXTERNAL_KEY, NAVIGATION_KEY,
WORKER_GROUP, ROBOT_GROUP);
private List<String> parallelLoopValidKeywords = asList(DO_KEY, DO_EXTERNAL_KEY, FOR_KEY, WORKER_GROUP);
private List<String> parallelLoopValidKeywords = asList(DO_KEY, DO_EXTERNAL_KEY, FOR_KEY,
WORKER_GROUP, MAX_THROTTLE_KEY);
private List<String> parallelLoopConstructKeywords = asList(FOR_KEY, MAX_THROTTLE_KEY);

private List<String> seqSupportedResults = asList(SUCCESS_RESULT, WARNING_RESULT, FAILURE_RESULT);

Expand Down Expand Up @@ -184,15 +188,15 @@ public ExecutableModellingResult transformToExecutable(ParsedSlang parsedSlang,
SensitivityLevel sensitivityLevel) {
List<RuntimeException> errors = new ArrayList<>();
String execName = preCompileValidator.validateExecutableRawData(parsedSlang, executableRawData, errors);
String workerGroup = (String)executableRawData.get(SlangTextualKeys.WORKER_GROUP);
String workerGroup = (String) executableRawData.get(SlangTextualKeys.WORKER_GROUP);
errors.addAll(preCompileValidator.checkKeyWords(
execName,
"",
executableRawData,
ListUtils.union(preExecTransformers, postExecTransformers),
ParsedSlang.Type.DECISION.equals(parsedSlang.getType()) ?
executableAdditionalKeywords : allExecutableAdditionalKeywords,
executableConstraintGroups
execName,
"",
executableRawData,
ListUtils.union(preExecTransformers, postExecTransformers),
ParsedSlang.Type.DECISION.equals(parsedSlang.getType()) ?
executableAdditionalKeywords : allExecutableAdditionalKeywords,
executableConstraintGroups
)
);

Expand Down Expand Up @@ -292,8 +296,8 @@ public ExecutableModellingResult transformToExecutable(ParsedSlang parsedSlang,
} else {
preCompileValidator.validateResultsHaveNoExpression(results, execName, errors);
preCompileValidator.validateResultsWithWhitelist(results, seqSupportedResults, execName, errors);
seqSteps = (List)((Map) actionRawData.get(SlangTextualKeys.SEQ_ACTION_KEY))
.get(SEQ_STEPS_KEY);
seqSteps = (List) ((Map) actionRawData.get(SlangTextualKeys.SEQ_ACTION_KEY))
.get(SEQ_STEPS_KEY);
@SuppressWarnings("unchecked")
Map<String, Object> settings = (Map<String, Object>) ((Map) actionRawData
.get(SlangTextualKeys.SEQ_ACTION_KEY))
Expand Down Expand Up @@ -550,7 +554,13 @@ private WorkflowModellingResult compileWorkFlow(List<Map<String, Map<String, Obj
)
);

parallelLoopRawData.put(PARALLEL_LOOP_KEY, parallelLoopRawData.remove(FOR_KEY));
Map<String, Object> filteredParallelLoopData =
newHashMapWithExpectedSize(parallelLoopConstructKeywords.size());
for (String keyword : parallelLoopConstructKeywords) {
filteredParallelLoopData.put(keyword, parallelLoopRawData.remove(keyword));
}

parallelLoopRawData.put(PARALLEL_LOOP_KEY, filteredParallelLoopData);
stepRawDataValue.putAll(parallelLoopRawData);
}
}
Expand Down Expand Up @@ -676,7 +686,7 @@ private String computeWorkerGroupString(Map<String, Object> stepRawData) {
workerGroup = (String) stepRawData.get(SlangTextualKeys.WORKER_GROUP);
} else if (stepRawData.get(SlangTextualKeys.WORKER_GROUP) instanceof Map) {
workerGroup = String.valueOf(
((Map<String, Object>)stepRawData.get(SlangTextualKeys.WORKER_GROUP)).get(SlangTextualKeys.VALUE));
((Map<String, Object>) stepRawData.get(SlangTextualKeys.WORKER_GROUP)).get(SlangTextualKeys.VALUE));
}
return workerGroup;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,101 +9,20 @@
*******************************************************************************/
package io.cloudslang.lang.compiler.modeller.transformers;

import io.cloudslang.lang.compiler.modeller.result.BasicTransformModellingResult;
import io.cloudslang.lang.compiler.modeller.result.TransformModellingResult;
import io.cloudslang.lang.compiler.validator.ExecutableValidator;
import io.cloudslang.lang.entities.ListLoopStatement;
import io.cloudslang.lang.entities.LoopStatement;
import io.cloudslang.lang.entities.MapLoopStatement;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

/**
* Date: 3/25/2015
*
* @author Bonczidai Levente
*/
public abstract class AbstractForTransformer extends AbstractInOutForTransformer {

private ExecutableValidator executableValidator;

// case: value in variable_name
private static final String FOR_REGEX = "^(\\s+)?(\\w+)\\s+(in)\\s+(\\w+)(\\s+)?$";
protected static final String FOR_REGEX = "^(\\s+)?(\\w+)\\s+(in)\\s+(\\w+)(\\s+)?$";
// case: key, value
private static final String KEY_VALUE_PAIR_REGEX = "^(\\s+)?(\\w+)(\\s+)?(,)(\\s+)?(\\w+)(\\s+)?$";
private static final String FOR_IN_KEYWORD = " in ";

public TransformModellingResult<LoopStatement> transformToLoopStatement(String rawData, boolean isParallelLoop) {
List<RuntimeException> errors = new ArrayList<>();
Accumulator dependencyAccumulator = extractFunctionData("${" + rawData + "}");
if (StringUtils.isEmpty(rawData)) {
errors.add(new RuntimeException("For statement is empty."));
return new BasicTransformModellingResult<>(null, errors);
}

LoopStatement loopStatement = null;
String varName;
String collectionExpression;

Pattern regexSimpleFor = Pattern.compile(FOR_REGEX);
Matcher matcherSimpleFor = regexSimpleFor.matcher(rawData);

try {
if (matcherSimpleFor.find()) {
// case: value in variable_name
varName = matcherSimpleFor.group(2);
collectionExpression = matcherSimpleFor.group(4);
loopStatement = createLoopStatement(varName, collectionExpression,
dependencyAccumulator, isParallelLoop);
} else {
String beforeInKeyword = StringUtils.substringBefore(rawData, FOR_IN_KEYWORD);
collectionExpression = StringUtils.substringAfter(rawData, FOR_IN_KEYWORD).trim();

Pattern regexKeyValueFor = Pattern.compile(KEY_VALUE_PAIR_REGEX);
Matcher matcherKeyValueFor = regexKeyValueFor.matcher(beforeInKeyword);

if (matcherKeyValueFor.find()) {
// case: key, value
String keyName = matcherKeyValueFor.group(2);
String valueName = matcherKeyValueFor.group(6);
loopStatement = createMapForLoopStatement(keyName, valueName,
collectionExpression, dependencyAccumulator);
} else {
// case: value in expression_other_than_variable_name
varName = beforeInKeyword.trim();
loopStatement = createLoopStatement(varName, collectionExpression,
dependencyAccumulator, isParallelLoop);
}
}
} catch (RuntimeException rex) {
errors.add(rex);
}

return new BasicTransformModellingResult<>(loopStatement, errors);
}

private LoopStatement createMapForLoopStatement(String keyName, String valueName,
String collectionExpression, Accumulator dependencyAccumulator) {
executableValidator.validateLoopStatementVariable(keyName);
executableValidator.validateLoopStatementVariable(valueName);
return new MapLoopStatement(
keyName,
valueName,
collectionExpression,
dependencyAccumulator.getFunctionDependencies(),
dependencyAccumulator.getSystemPropertyDependencies());
}
protected static final String KEY_VALUE_PAIR_REGEX = "^(\\s+)?(\\w+)(\\s+)?(,)(\\s+)?(\\w+)(\\s+)?$";
protected static final String FOR_IN_KEYWORD = " in ";

private LoopStatement createLoopStatement(String varName, String collectionExpression,
Accumulator dependencyAccumulator, boolean isParallelLoop) {
executableValidator.validateLoopStatementVariable(varName);
return new ListLoopStatement(varName, collectionExpression,
dependencyAccumulator.getFunctionDependencies(),
dependencyAccumulator.getSystemPropertyDependencies(), isParallelLoop);
protected void validateLoopStatementVariable(String name) {
executableValidator.validateLoopStatementVariable(name);
}

public void setExecutableValidator(ExecutableValidator executableValidator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@

import io.cloudslang.lang.compiler.CompilerConstants;
import io.cloudslang.lang.compiler.SlangTextualKeys;
import io.cloudslang.lang.compiler.modeller.result.BasicTransformModellingResult;
import io.cloudslang.lang.compiler.modeller.result.TransformModellingResult;
import io.cloudslang.lang.entities.ListLoopStatement;
import io.cloudslang.lang.entities.LoopStatement;
import io.cloudslang.lang.entities.MapLoopStatement;
import io.cloudslang.lang.entities.SensitivityLevel;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ForTransformer extends AbstractForTransformer implements Transformer<String, LoopStatement> {

Expand All @@ -27,7 +34,7 @@ public TransformModellingResult<LoopStatement> transform(String rawData) {

@Override
public TransformModellingResult<LoopStatement> transform(String rawData, SensitivityLevel sensitivityLevel) {
return transformToLoopStatement(rawData, false);
return transformToLoopStatement(rawData);
}

@Override
Expand All @@ -40,4 +47,72 @@ public String keyToTransform() {
return SlangTextualKeys.FOR_KEY;
}

private TransformModellingResult<LoopStatement> transformToLoopStatement(String rawData) {
List<RuntimeException> errors = new ArrayList<>();
Accumulator dependencyAccumulator = extractFunctionData("${" + rawData + "}");
if (StringUtils.isEmpty(rawData)) {
errors.add(new RuntimeException("For statement is empty."));
return new BasicTransformModellingResult<>(null, errors);
}

LoopStatement loopStatement = null;
String varName;
String collectionExpression;

Pattern regexSimpleFor = Pattern.compile(FOR_REGEX);
Matcher matcherSimpleFor = regexSimpleFor.matcher(rawData);

try {
if (matcherSimpleFor.find()) {
// case: value in variable_name
varName = matcherSimpleFor.group(2);
collectionExpression = matcherSimpleFor.group(4);
loopStatement = createLoopStatement(varName, collectionExpression, dependencyAccumulator);
} else {
String beforeInKeyword = StringUtils.substringBefore(rawData, FOR_IN_KEYWORD);
collectionExpression = StringUtils.substringAfter(rawData, FOR_IN_KEYWORD).trim();

Pattern regexKeyValueFor = Pattern.compile(KEY_VALUE_PAIR_REGEX);
Matcher matcherKeyValueFor = regexKeyValueFor.matcher(beforeInKeyword);

if (matcherKeyValueFor.find()) {
// case: key, value
String keyName = matcherKeyValueFor.group(2);
String valueName = matcherKeyValueFor.group(6);
loopStatement = createMapForLoopStatement(keyName, valueName,
collectionExpression, dependencyAccumulator);
} else {
// case: value in expression_other_than_variable_name
varName = beforeInKeyword.trim();
loopStatement = createLoopStatement(varName, collectionExpression, dependencyAccumulator);
}
}
} catch (RuntimeException rex) {
errors.add(rex);
}

return new BasicTransformModellingResult<>(loopStatement, errors);
}

private LoopStatement createMapForLoopStatement(String keyName, String valueName,
String collectionExpression, Accumulator dependencyAccumulator) {
super.validateLoopStatementVariable(keyName);
super.validateLoopStatementVariable(valueName);
return new MapLoopStatement(
keyName,
valueName,
collectionExpression,
dependencyAccumulator.getFunctionDependencies(),
dependencyAccumulator.getSystemPropertyDependencies());
}

private LoopStatement createLoopStatement(String varName, String collectionExpression,
Accumulator dependencyAccumulator) {
super.validateLoopStatementVariable(varName);
return new ListLoopStatement(
varName,
collectionExpression,
dependencyAccumulator.getFunctionDependencies(),
dependencyAccumulator.getSystemPropertyDependencies());
}
}
Loading