Search in sources :

Example 1 with RulesProject

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

the class RulesUtil method getRulesMachineAsBMachine.

public static String getRulesMachineAsBMachine(final String content) {
    RulesProject rulesProject = new RulesProject();
    rulesProject.parseRulesMachines(content, new String[] {});
    rulesProject.checkAndTranslateProject();
    List<IModel> bModels = rulesProject.getBModels();
    List<BException> bExceptionList = rulesProject.getBExceptionList();
    if (!bExceptionList.isEmpty()) {
        throw new RuntimeException(bExceptionList.get(0));
    }
    IModel model = bModels.get(bModels.size() - 2);
    PrettyPrinter pp = new PrettyPrinter();
    model.getStart().apply(pp);
    return pp.getPrettyPrint();
}
Also used : PrettyPrinter(de.be4.classicalb.core.parser.util.PrettyPrinter) BException(de.be4.classicalb.core.parser.exceptions.BException)

Example 2 with RulesProject

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

the class RulesMachineFilesTest method testRulesMachineConfiguration.

@Test
public void testRulesMachineConfiguration() throws Exception {
    File file = new File("src/test/resources/rules/project/RulesMachineConfigurationTest.rmch");
    ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
    parsingBehaviour.setAddLineNumbers(true);
    parsingBehaviour.setPrologOutput(true);
    RulesProject project = new RulesProject();
    project.parseProject(file);
    project.checkAndTranslateProject();
    RulesMachineRunConfiguration rulesMachineRunConfiguration = project.getRulesMachineRunConfiguration();
    Set<RuleGoalAssumption> rulesGoalAssumptions = rulesMachineRunConfiguration.getRulesGoalAssumptions();
    assertEquals(2, rulesGoalAssumptions.size());
    for (Iterator<RuleGoalAssumption> iterator = rulesGoalAssumptions.iterator(); iterator.hasNext(); ) {
        RuleGoalAssumption next = iterator.next();
        if ("rule1".equals(next.getRuleName())) {
            assertEquals(new HashSet<Integer>(Arrays.asList(1)), next.getErrorTypesAssumedToSucceed());
            assertEquals(true, next.isCheckedForCounterexamples());
            assertEquals("rule1", next.getRuleOperation().getName());
        } else {
            assertEquals("rule2", next.getRuleName());
            assertEquals(new HashSet<Integer>(Arrays.asList(1)), next.getErrorTypesAssumedToFail());
            assertEquals(false, next.isCheckedForCounterexamples());
        }
    }
}
Also used : RuleGoalAssumption(de.be4.classicalb.core.parser.rules.RulesMachineRunConfiguration.RuleGoalAssumption) File(java.io.File) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour) Test(org.junit.Test)

Example 3 with RulesProject

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

the class RulesMachineRun method parseAndTranslateRulesProject.

private boolean parseAndTranslateRulesProject() {
    this.rulesProject = new RulesProject();
    ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
    parsingBehaviour.setAddLineNumbers(true);
    rulesProject.setParsingBehaviour(parsingBehaviour);
    rulesProject.parseProject(runnerFile);
    for (Entry<String, String> pair : constantValuesToBeInjected.entrySet()) {
        rulesProject.addConstantValue(pair.getKey(), pair.getValue());
    }
    /*
		 * parse errors and errors from semantic checks are stored in the
		 * rulesProject
		 */
    rulesProject.checkAndTranslateProject();
    if (rulesProject.hasErrors()) {
        BException bException = rulesProject.getBExceptionList().get(0);
        String message = bException.getMessage();
        logger.error("Parse error:  {}", message);
        this.errors.add(new Error(ERROR_TYPES.PARSE_ERROR, message, bException));
    }
    return rulesProject.hasErrors();
}
Also used : ProBError(de.prob.exception.ProBError) StateError(de.prob.animator.domainobjects.StateError) RulesProject(de.be4.classicalb.core.parser.rules.RulesProject) BException(de.be4.classicalb.core.parser.exceptions.BException) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 4 with RulesProject

use of de.be4.classicalb.core.parser.rules.RulesProject 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 5 with RulesProject

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

the class RulesUtil method getRulesProjectAsPrologTerm.

public static String getRulesProjectAsPrologTerm(final String content) {
    RulesProject rulesProject = new RulesProject();
    ParsingBehaviour pb = new ParsingBehaviour();
    pb.setAddLineNumbers(false);
    rulesProject.setParsingBehaviour(pb);
    rulesProject.parseRulesMachines(content, new String[] {});
    rulesProject.checkAndTranslateProject();
    List<IModel> bModels = rulesProject.getBModels();
    List<BException> bExceptionList = rulesProject.getBExceptionList();
    if (bExceptionList.isEmpty()) {
        IModel model = bModels.get(bModels.size() - 2);
        PrettyPrinter pp = new PrettyPrinter();
        model.getStart().apply(pp);
        System.out.println(pp.getPrettyPrint());
    }
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    rulesProject.printPrologOutput(new PrintStream(output), new PrintStream(output));
    return output.toString();
}
Also used : PrintStream(java.io.PrintStream) PrettyPrinter(de.be4.classicalb.core.parser.util.PrettyPrinter) OutputStream(java.io.OutputStream) BException(de.be4.classicalb.core.parser.exceptions.BException) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Aggregations

ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)4 BException (de.be4.classicalb.core.parser.exceptions.BException)4 RulesProject (de.be4.classicalb.core.parser.rules.RulesProject)3 PrettyPrinter (de.be4.classicalb.core.parser.util.PrettyPrinter)3 ProBError (de.prob.exception.ProBError)2 File (java.io.File)2 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)1 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)1 AbstractOperation (de.be4.classicalb.core.parser.rules.AbstractOperation)1 RuleOperation (de.be4.classicalb.core.parser.rules.RuleOperation)1 RuleGoalAssumption (de.be4.classicalb.core.parser.rules.RulesMachineRunConfiguration.RuleGoalAssumption)1 StateError (de.prob.animator.domainobjects.StateError)1 RuleResult (de.prob.model.brules.RuleResult)1 RuleResults (de.prob.model.brules.RuleResults)1 RulesModelFactory (de.prob.model.brules.RulesModelFactory)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1