Search in sources :

Example 61 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException 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 62 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class ExpressionTest method testProverComprehensionSets.

@Test
public void testProverComprehensionSets() throws Exception {
    final String expression = "SET(i).(i>0)";
    parser.getOptions().setRestrictProverExpressions(false);
    final String expected = "AProverComprehensionSetExpression([AIdentifierExpression([i])],AGreaterPredicate(AIdentifierExpression([i]),AIntegerExpression(0)))";
    final String prover = getExpressionAsString(expression);
    assertEquals(expected, prover);
    parser.getOptions().setRestrictProverExpressions(true);
    try {
        getExpressionAsString(expression);
        fail("exception expected");
    } catch (BCompoundException e) {
        assertTrue(e.getCause() instanceof CheckException);
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Ast2String(util.Ast2String) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Example 63 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class RulesMachineChecker method checkErrorTypesAttribute.

private void checkErrorTypesAttribute(POperationAttribute pOperationAttribute, LinkedList<PExpression> arguments) {
    if (currentOperation instanceof RuleOperation) {
        final RuleOperation rule = (RuleOperation) currentOperation;
        if (arguments.size() == 1 && arguments.get(0) instanceof AIntegerExpression) {
            AIntegerExpression intExpr = (AIntegerExpression) arguments.get(0);
            rule.setErrrorTypes(intExpr);
        } else {
            errorList.add(new CheckException("Expected exactly one integer after ERROR_TYPES.", pOperationAttribute));
        }
    } else {
        errorList.add(new CheckException("ERROR_TYPES is not an attribute of a FUNCTION or COMPUTATION operation.", pOperationAttribute));
    }
    return;
}
Also used : ARuleOperation(de.be4.classicalb.core.parser.node.ARuleOperation) AIntegerExpression(de.be4.classicalb.core.parser.node.AIntegerExpression) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException)

Example 64 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class RulesMachineChecker method caseAExpressionDefinitionDefinition.

@Override
public void caseAExpressionDefinitionDefinition(AExpressionDefinitionDefinition node) {
    final String name = node.getName().getText();
    this.definitions.add(name);
    if ("GOAL".equals(name)) {
        errorList.add(new CheckException("The GOAL definition must be a predicate.", node));
        return;
    }
    this.identifierScope.createNewScope(new LinkedList<>(node.getParameters()));
    node.getRhs().apply(this);
    this.identifierScope.removeScope();
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException)

Example 65 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class RulesMachineChecker method caseARuleOperation.

@Override
public void caseARuleOperation(ARuleOperation node) {
    currentOperation = new RuleOperation(node.getRuleName(), this.fileName, this.machineName, machineReferences);
    if (containsRule(currentOperation.getName())) {
        errorList.add(new CheckException("Duplicate operation name '" + currentOperation.getName() + "'.", node.getRuleName()));
    }
    RuleOperation ruleOp = (RuleOperation) currentOperation;
    rulesMap.put(node, ruleOp);
    visitOperationAttributes(node.getAttributes());
    node.getRuleBody().apply(this);
    checkAllErrorTypesImplemented(ruleOp);
    currentOperation = null;
}
Also used : ARuleOperation(de.be4.classicalb.core.parser.node.ARuleOperation) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)78 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)19 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)17 PExpression (de.be4.classicalb.core.parser.node.PExpression)16 Test (org.junit.Test)15 BException (de.be4.classicalb.core.parser.exceptions.BException)14 ArrayList (java.util.ArrayList)13 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)12 Ast2String (util.Ast2String)10 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)9 Node (de.be4.classicalb.core.parser.node.Node)8 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 File (java.io.File)8 HashSet (java.util.HashSet)8 IOException (java.io.IOException)7 ARuleOperation (de.be4.classicalb.core.parser.node.ARuleOperation)4 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)4 Helpers.getTreeAsString (util.Helpers.getTreeAsString)4 Type (de.be4.classicalb.core.parser.IDefinitions.Type)3 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)3