Search in sources :

Example 16 with AbstractOperation

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);
}
Also used : ArrayList(java.util.ArrayList) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) HashSet(java.util.HashSet)

Example 17 with AbstractOperation

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());
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) ArrayList(java.util.ArrayList) BException(de.be4.classicalb.core.parser.exceptions.BException) HashSet(java.util.HashSet)

Example 18 with AbstractOperation

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;
}
Also used : AbstractOperation(de.be4.classicalb.core.parser.rules.AbstractOperation) HashSet(java.util.HashSet)

Example 19 with AbstractOperation

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();
    }
}
Also used : AbstractOperation(de.be4.classicalb.core.parser.rules.AbstractOperation)

Example 20 with AbstractOperation

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());
        }
    }
}
Also used : Trace(de.prob.statespace.Trace) StateSpace(de.prob.statespace.StateSpace) AbstractOperation(de.be4.classicalb.core.parser.rules.AbstractOperation) OperationStatus(de.prob.model.brules.OperationStatus) RulesChecker(de.prob.model.brules.RulesChecker) Test(org.junit.Test)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)9 AbstractOperation (de.be4.classicalb.core.parser.rules.AbstractOperation)8 ArrayList (java.util.ArrayList)8 BException (de.be4.classicalb.core.parser.exceptions.BException)7 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)5 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)5 HashSet (java.util.HashSet)5 RuleOperation (de.be4.classicalb.core.parser.rules.RuleOperation)3 HashMap (java.util.HashMap)3 ComputationOperation (de.be4.classicalb.core.parser.rules.ComputationOperation)2 FunctionOperation (de.be4.classicalb.core.parser.rules.FunctionOperation)2 RulesProject (de.be4.classicalb.core.parser.rules.RulesProject)2 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)2 IEvalElement (de.prob.animator.domainobjects.IEvalElement)2 Trace (de.prob.statespace.Trace)2 OperationStatus (de.prob.model.brules.OperationStatus)1 RuleResult (de.prob.model.brules.RuleResult)1 RuleResults (de.prob.model.brules.RuleResults)1 RulesChecker (de.prob.model.brules.RulesChecker)1 State (de.prob.statespace.State)1