Search in sources :

Example 6 with RuleOperation

use of de.be4.classicalb.core.parser.rules.RuleOperation in project probparsers by bendisposto.

the class RulesTransformation method translateGetRuleCounterExamplesOperator.

private void translateGetRuleCounterExamplesOperator(AOperatorExpression node) {
    final PExpression pExpression = node.getIdentifiers().get(0);
    final AIdentifierExpression id = (AIdentifierExpression) pExpression;
    final String ruleName = id.getIdentifier().get(0).getText();
    final AbstractOperation operation = allOperations.get(ruleName);
    if (operation == null || !(operation instanceof RuleOperation)) {
        errorList.add(new CheckException(String.format("'%s' does not match any rule visible to this machine.", ruleName), node));
        return;
    }
    final RuleOperation rule = (RuleOperation) operation;
    final String name = id.getIdentifier().get(0).getText() + RULE_COUNTER_EXAMPLE_VARIABLE_SUFFIX;
    if (node.getIdentifiers().size() == 1) {
        final AIdentifierExpression ctVariable = createIdentifier(name, pExpression);
        final ARangeExpression range = createPositinedNode(new ARangeExpression(ctVariable), node);
        node.replaceBy(range);
    } else {
        PExpression funcCall = getSetOfErrorMessagesByErrorType(name, node.getIdentifiers().get(1), rule.getNumberOfErrorTypes());
        node.replaceBy(funcCall);
    }
    return;
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException)

Example 7 with RuleOperation

use of de.be4.classicalb.core.parser.rules.RuleOperation in project prob2 by bendisposto.

the class RulesChecker method evalOperations.

public Map<AbstractOperation, OperationStatus> evalOperations(State state, Collection<AbstractOperation> operations) {
    ArrayList<IEvalElement> formulas = new ArrayList<>();
    for (AbstractOperation abstractOperation : operations) {
        if (abstractOperation instanceof ComputationOperation || abstractOperation instanceof RuleOperation) {
            formulas.add(rulesModel.getEvalElement(abstractOperation));
        }
    }
    state.getStateSpace().subscribe(this, formulas);
    Map<IEvalElement, AbstractEvalResult> values = state.getValues();
    final Map<AbstractOperation, OperationStatus> states = new HashMap<>();
    for (AbstractOperation op : operations) {
        if (op instanceof RuleOperation) {
            states.put(op, RuleStatus.valueOf(values.get(rulesModel.getEvalElement(op))));
        } else if (op instanceof ComputationOperation) {
            states.put(op, ComputationStatus.valueOf(values.get(rulesModel.getEvalElement(op))));
        }
    }
    return states;
}
Also used : RuleOperation(de.be4.classicalb.core.parser.rules.RuleOperation) AbstractOperation(de.be4.classicalb.core.parser.rules.AbstractOperation) HashMap(java.util.HashMap) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) IEvalElement(de.prob.animator.domainobjects.IEvalElement) ArrayList(java.util.ArrayList) ComputationOperation(de.be4.classicalb.core.parser.rules.ComputationOperation)

Example 8 with RuleOperation

use of de.be4.classicalb.core.parser.rules.RuleOperation in project prob2 by bendisposto.

the class RulesMachineTest method testSimpleRulesMachine.

@Test
public void testSimpleRulesMachine() throws IOException {
    RulesMachineRun rulesMachineRun = startRulesMachineRun(dir + "SimpleRulesMachine.rmch");
    assertEquals(false, rulesMachineRun.hasError());
    assertTrue(rulesMachineRun.getErrorList().isEmpty());
    assertEquals(null, rulesMachineRun.getFirstError());
    RuleResults ruleResults = rulesMachineRun.getRuleResults();
    ResultSummary summary = ruleResults.getSummary();
    // summary is created only once
    assertEquals(summary, ruleResults.getSummary());
    assertEquals(4, ruleResults.getRuleResultList().size());
    RuleResult rule1Result = ruleResults.getRuleResult("Rule1");
    assertEquals(RuleStatus.SUCCESS, rule1Result.getRuleState());
    RuleOperation rule1Operation = rule1Result.getRuleOperation();
    assertEquals("Rule1", rule1Operation.getName());
    assertTrue("Should be empty", rule1Result.getNotCheckedDependencies().isEmpty());
    RuleResult result2 = ruleResults.getRuleResult("Rule2");
    assertEquals(RuleStatus.FAIL, result2.getRuleState());
    String message = result2.getCounterExamples().get(0).getMessage();
    assertEquals("ERROR2", message);
    assertEquals(NOT_CHECKED, ruleResults.getRuleResult("Rule3").getRuleState());
    assertEquals("Rule2", ruleResults.getRuleResult("Rule3").getFailedDependencies().get(0));
}
Also used : RulesMachineRun(de.prob.model.brules.RulesMachineRun) RuleResults(de.prob.model.brules.RuleResults) RuleOperation(de.be4.classicalb.core.parser.rules.RuleOperation) ResultSummary(de.prob.model.brules.RuleResults.ResultSummary) RuleResult(de.prob.model.brules.RuleResult) Test(org.junit.Test)

Example 9 with RuleOperation

use of de.be4.classicalb.core.parser.rules.RuleOperation in project prob2 by bendisposto.

the class RulesTestUtil method checkRulesMachineRunForConsistency.

public static void checkRulesMachineRunForConsistency(RulesMachineRun rulesMachineRun) {
    RulesProject rulesProject = rulesMachineRun.getRulesProject();
    RuleResults ruleResults = rulesMachineRun.getRuleResults();
    Map<String, RuleResult> ruleResultMap = ruleResults.getRuleResultMap();
    for (AbstractOperation abstractOperation : rulesProject.getOperationsMap().values()) {
        if (abstractOperation instanceof RuleOperation) {
            String ruleName = abstractOperation.getName();
            assertTrue(String.format("Rule operation '%s' is not contained in the result map.", ruleName), ruleResultMap.containsKey(ruleName));
            RuleResult ruleResult = ruleResultMap.get(ruleName);
            if (ruleResult.getRuleState() == RuleStatus.FAIL) {
                assertTrue(String.format("No violation found but rule failed: '%s'", ruleName), ruleResult.getNumberOfViolations() > 0);
            }
            if (ruleResult.getRuleState() == RuleStatus.NOT_CHECKED) {
                List<String> notCheckedCauses = ruleResult.getFailedDependencies();
                assertTrue(String.format("There is no cause why rule '%s' is not checked.", ruleName), !notCheckedCauses.isEmpty());
            }
        }
    }
}
Also used : RuleResults(de.prob.model.brules.RuleResults) RuleOperation(de.be4.classicalb.core.parser.rules.RuleOperation) AbstractOperation(de.be4.classicalb.core.parser.rules.AbstractOperation) RulesProject(de.be4.classicalb.core.parser.rules.RulesProject) RuleResult(de.prob.model.brules.RuleResult)

Example 10 with RuleOperation

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

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)8 ARuleOperation (de.be4.classicalb.core.parser.node.ARuleOperation)4 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)3 RuleOperation (de.be4.classicalb.core.parser.rules.RuleOperation)3 BException (de.be4.classicalb.core.parser.exceptions.BException)2 AbstractOperation (de.be4.classicalb.core.parser.rules.AbstractOperation)2 RuleResult (de.prob.model.brules.RuleResult)2 RuleResults (de.prob.model.brules.RuleResults)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AFunctionOperation (de.be4.classicalb.core.parser.node.AFunctionOperation)1 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)1 APredicateAttributeOperationAttribute (de.be4.classicalb.core.parser.node.APredicateAttributeOperationAttribute)1 PPredicate (de.be4.classicalb.core.parser.node.PPredicate)1 ComputationOperation (de.be4.classicalb.core.parser.rules.ComputationOperation)1 RulesProject (de.be4.classicalb.core.parser.rules.RulesProject)1 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)1 IEvalElement (de.prob.animator.domainobjects.IEvalElement)1 ResultSummary (de.prob.model.brules.RuleResults.ResultSummary)1 RulesMachineRun (de.prob.model.brules.RulesMachineRun)1