use of de.be4.classicalb.core.parser.rules.AbstractOperation in project probparsers by bendisposto.
the class RulesProject method findTransitiveDependencies.
private Set<AbstractOperation> findTransitiveDependencies(final AbstractOperation operation, final List<AbstractOperation> ancestors) {
ancestors.add(operation);
List<TIdentifierLiteral> directDependencies = new ArrayList<>();
directDependencies.addAll(convertAIdentifierListToTIdentifierLiteralList(operation.getDependsOnComputationList()));
directDependencies.addAll(convertAIdentifierListToTIdentifierLiteralList(operation.getDependsOnRulesList()));
directDependencies.addAll(operation.getImplicitDependenciesToComputations());
directDependencies.addAll(operation.getFunctionCalls());
// check for cycle
Set<AbstractOperation> transitiveDependenciesFound = new HashSet<>();
boolean cycleDetected = checkForCycles(operation, directDependencies, ancestors);
if (cycleDetected) {
operation.setTransitiveDependencies(transitiveDependenciesFound);
return transitiveDependenciesFound;
}
for (TIdentifierLiteral tIdentifierLiteral : directDependencies) {
String opName = tIdentifierLiteral.getText();
AbstractOperation nextOperation = allOperations.get(opName);
transitiveDependenciesFound.add(nextOperation);
if (nextOperation.getTransitiveDependencies() != null) {
transitiveDependenciesFound.addAll(nextOperation.getTransitiveDependencies());
} else {
transitiveDependenciesFound.addAll(findTransitiveDependencies(nextOperation, new ArrayList<>(ancestors)));
}
}
operation.setTransitiveDependencies(transitiveDependenciesFound);
return new HashSet<>(transitiveDependenciesFound);
}
use of de.be4.classicalb.core.parser.rules.AbstractOperation in project probparsers by bendisposto.
the class RulesProject method findImplicitDependenciesToComputations.
private void findImplicitDependenciesToComputations() {
HashMap<String, ComputationOperation> variableToComputation = new HashMap<>();
for (AbstractOperation operation : allOperations.values()) {
if (operation instanceof ComputationOperation && !operation.replacesOperation()) {
ComputationOperation comp = (ComputationOperation) operation;
for (String defName : comp.getDefineVariables()) {
variableToComputation.put(defName, comp);
}
}
}
for (AbstractOperation operation : allOperations.values()) {
final Set<String> readVariables = operation.getReadVariables();
readVariables.retainAll(variableToComputation.keySet());
final HashSet<String> variablesInScope = new HashSet<>();
// defined before read.
if (operation instanceof ComputationOperation) {
variablesInScope.addAll(((ComputationOperation) operation).getDefineVariables());
}
for (AIdentifierExpression aIdentifier : operation.getDependsOnComputationList()) {
String opName = aIdentifier.getIdentifier().get(0).getText();
AbstractOperation abstractOperation = allOperations.get(opName);
if (abstractOperation instanceof ComputationOperation) {
variablesInScope.addAll(((ComputationOperation) abstractOperation).getDefineVariables());
}
}
readVariables.removeAll(variablesInScope);
if (!readVariables.isEmpty()) {
List<ComputationOperation> inferredDependenciesToComputations = new ArrayList<>();
for (String varName : readVariables) {
ComputationOperation computationOperation = variableToComputation.get(varName);
inferredDependenciesToComputations.add(computationOperation);
List<String> machineReferences = operation.getMachineReferencesAsString();
machineReferences.add(operation.getMachineName());
if (!machineReferences.contains(computationOperation.getMachineName())) {
this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Missing reference to RULES_MACHINE '" + computationOperation.getMachineName() + "' in order to use variable '" + varName + "'.", operation.getVariableReadByName(varName))));
}
operation.setImplicitComputationDependencies(inferredDependenciesToComputations);
}
} else {
operation.setImplicitComputationDependencies(Collections.<ComputationOperation>emptyList());
}
}
}
use of de.be4.classicalb.core.parser.rules.AbstractOperation in project prob2 by bendisposto.
the class RulesChecker method getExecutableOperations.
private Set<AbstractOperation> getExecutableOperations() {
final Set<AbstractOperation> todo = new HashSet<>();
for (Entry<AbstractOperation, OperationStatus> eval : operationStatuses.entrySet()) {
AbstractOperation op = eval.getKey();
OperationStatus value = eval.getValue();
if (value.isNotExecuted() && !value.isDisabled()) {
boolean canBeExecuted = true;
// in case of rules
for (AbstractOperation pred : predecessors.get(op)) {
OperationStatus predState = operationStatuses.get(pred);
if (predState.isNotExecuted() || predState == RuleStatus.FAIL) {
canBeExecuted = false;
break;
}
}
if (canBeExecuted) {
todo.add(op);
}
}
}
return todo;
}
use of de.be4.classicalb.core.parser.rules.AbstractOperation in project prob2 by bendisposto.
the class RulesChecker method executeAllOperations.
public void executeAllOperations() {
init();
// determine all operations that can be executed in this state
Set<AbstractOperation> executableOperations = getExecutableOperations();
while (!executableOperations.isEmpty()) {
for (AbstractOperation op : executableOperations) {
executeOperation(op);
}
executableOperations = getExecutableOperations();
}
}
use of de.be4.classicalb.core.parser.rules.AbstractOperation in project prob2 by bendisposto.
the class RulesCheckerTest method testExecuteAllOperations.
@Test
public void testExecuteAllOperations() throws IOException {
StateSpace s = api.brules_load(dir + "RulesMachineExample.rmch");
Trace trace = new Trace(s);
RulesChecker rulesChecker = new RulesChecker(trace);
rulesChecker.init();
rulesChecker.executeAllOperations();
for (Entry<AbstractOperation, OperationStatus> entry : rulesChecker.getOperationStates().entrySet()) {
OperationStatus state = entry.getValue();
if (entry.getKey().getName().equals("RULE_BasedOnRuleWithViolations")) {
assertEquals(RuleStatus.NOT_CHECKED, state);
} else {
assertTrue(state.isExecuted());
}
}
}
Aggregations