Search in sources :

Example 6 with ComputationOperation

use of de.be4.classicalb.core.parser.rules.ComputationOperation 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)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)3 ArrayList (java.util.ArrayList)3 BException (de.be4.classicalb.core.parser.exceptions.BException)2 AComputationOperation (de.be4.classicalb.core.parser.node.AComputationOperation)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2 HashMap (java.util.HashMap)2 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)1 AbstractOperation (de.be4.classicalb.core.parser.rules.AbstractOperation)1 ComputationOperation (de.be4.classicalb.core.parser.rules.ComputationOperation)1 RuleOperation (de.be4.classicalb.core.parser.rules.RuleOperation)1 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)1 IEvalElement (de.prob.animator.domainobjects.IEvalElement)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1