Search in sources :

Example 1 with Restriction

use of org.drools.verifier.components.Restriction in project drools by kiegroup.

the class SolversTest method testNotAnd.

/**
 * <pre>
 * when
 *     Foo( r &amp;&amp; r2 )
 *     and
 *     not Foo( r3 &amp;&amp; r4 )
 * </pre>
 *
 * result:<br>
 * r && r2<br>
 * r3 && r4
 */
@Test
public void testNotAnd() {
    PackageDescr descr = new PackageDescr("testPackage");
    RulePackage rulePackage = new RulePackage(descr);
    rulePackage.setName("testPackage");
    VerifierRule rule = new VerifierRule(descr, rulePackage, new HashMap<String, Object>());
    rule.setName("testRule");
    Pattern pattern = new Pattern(new PatternDescr(), rule);
    Restriction r = LiteralRestriction.createRestriction(pattern, "");
    Restriction r2 = LiteralRestriction.createRestriction(pattern, "");
    Restriction r3 = LiteralRestriction.createRestriction(pattern, "");
    Restriction r4 = LiteralRestriction.createRestriction(pattern, "");
    Solvers solvers = new Solvers();
    solvers.startRuleSolver(rule);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.startPatternSolver(pattern);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.addPatternComponent(r);
    solvers.addPatternComponent(r2);
    solvers.endOperator();
    solvers.endPatternSolver();
    solvers.startNot();
    solvers.startPatternSolver(pattern);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.addPatternComponent(r3);
    solvers.addPatternComponent(r4);
    solvers.endOperator();
    solvers.endPatternSolver();
    solvers.endNot();
    solvers.endOperator();
    solvers.endRuleSolver();
    List<SubRule> list = solvers.getRulePossibilities();
    assertEquals(1, list.size());
    assertEquals(2, list.get(0).getItems().size());
    List<Restriction> result = new ArrayList<Restriction>();
    result.add(r);
    result.add(r2);
    List<Restriction> result2 = new ArrayList<Restriction>();
    result2.add(r3);
    result2.add(r4);
    Object[] possibilies = list.get(0).getItems().toArray();
    SubPattern p1 = (SubPattern) possibilies[0];
    SubPattern p2 = (SubPattern) possibilies[1];
    /*
         * Order may change but it doesn't matter.
         */
    if (p1.getItems().containsAll(result)) {
        assertTrue(p2.getItems().containsAll(result2));
    } else if (p1.getItems().containsAll(result2)) {
        assertTrue(p2.getItems().containsAll(result));
    } else {
        fail("No items found.");
    }
}
Also used : Pattern(org.drools.verifier.components.Pattern) SubPattern(org.drools.verifier.components.SubPattern) PatternDescr(org.drools.drl.ast.descr.PatternDescr) SubRule(org.drools.verifier.components.SubRule) ArrayList(java.util.ArrayList) VerifierRule(org.drools.verifier.components.VerifierRule) SubPattern(org.drools.verifier.components.SubPattern) RulePackage(org.drools.verifier.components.RulePackage) Restriction(org.drools.verifier.components.Restriction) LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Solvers(org.drools.verifier.solver.Solvers) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 2 with Restriction

use of org.drools.verifier.components.Restriction in project drools by kiegroup.

the class ExprConstraintDescrVisitorTest method assertContainsNumberRestriction.

private void assertContainsNumberRestriction(Operator operator, Number value) {
    Collection<Restriction> allRestrictions = verifierData.getAll(VerifierComponentType.RESTRICTION);
    for (Restriction restriction : allRestrictions) {
        if (restriction instanceof NumberRestriction) {
            NumberRestriction numberRestriction = (NumberRestriction) restriction;
            if (value.equals(numberRestriction.getValue()) && operator.equals(numberRestriction.getOperator())) {
                return;
            }
        }
    }
    fail(String.format("Could not find NumberRestriction: Operator : %s Value: %s", operator, value));
}
Also used : Restriction(org.drools.verifier.components.Restriction) NumberRestriction(org.drools.verifier.components.NumberRestriction) StringRestriction(org.drools.verifier.components.StringRestriction) VariableRestriction(org.drools.verifier.components.VariableRestriction) NumberRestriction(org.drools.verifier.components.NumberRestriction)

Example 3 with Restriction

use of org.drools.verifier.components.Restriction in project drools by kiegroup.

the class ExprConstraintDescrVisitorTest method assertContainsVariableRestriction.

private void assertContainsVariableRestriction(Operator operator, String variableName) {
    Collection<Restriction> allRestrictions = verifierData.getAll(VerifierComponentType.RESTRICTION);
    for (Restriction restriction : allRestrictions) {
        if (restriction instanceof VariableRestriction) {
            VariableRestriction variableRestriction = (VariableRestriction) restriction;
            if (variableName.equals(variableRestriction.getVariable().getName()) && operator.equals(variableRestriction.getOperator())) {
                return;
            }
        }
    }
    fail(String.format("Could not find VariableRestriction: Operator : %s Variable name: %s", operator, variableName));
}
Also used : Restriction(org.drools.verifier.components.Restriction) NumberRestriction(org.drools.verifier.components.NumberRestriction) StringRestriction(org.drools.verifier.components.StringRestriction) VariableRestriction(org.drools.verifier.components.VariableRestriction) VariableRestriction(org.drools.verifier.components.VariableRestriction)

Example 4 with Restriction

use of org.drools.verifier.components.Restriction in project drools by kiegroup.

the class NestedPatternsTest method runVisitor.

@Test
public void runVisitor() throws Exception {
    VerifierData data = VerifierReportFactory.newVerifierData();
    PackageDescrVisitor visitor = new PackageDescrVisitor(data, Collections.EMPTY_LIST);
    assertNotNull(data);
    Reader drlReader = new InputStreamReader(getClass().getResourceAsStream("NestedPatterns.drl"));
    PackageDescr packageDescr = new DrlParser(LanguageLevelOption.DRL5).parse(drlReader);
    assertNotNull(packageDescr);
    visitor.visitPackageDescr(packageDescr);
    Collection<VerifierComponent> all = data.getAll();
    int patternCount = 0;
    for (VerifierComponent verifierComponent : all) {
        if (verifierComponent.getVerifierComponentType().equals(VerifierComponentType.PATTERN)) {
            patternCount++;
        }
    }
    assertEquals(4, patternCount);
    Collection<Pattern> patterns = data.getAll(VerifierComponentType.PATTERN);
    // for ( Pattern pattern : patterns ) {
    // System.out.println( pattern.getPath() + " " + pattern );
    // }
    assertNotNull(patterns);
    assertEquals(4, patterns.size());
    Collection<Restriction> restrictions = data.getAll(VerifierComponentType.RESTRICTION);
    // for ( Restriction restriction : restrictions ) {
    // System.out.println( restriction.getPath() + " " + restriction );
    // }
    assertNotNull(restrictions);
    assertEquals(3, restrictions.size());
}
Also used : Pattern(org.drools.verifier.components.Pattern) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) VerifierComponent(org.drools.verifier.data.VerifierComponent) Restriction(org.drools.verifier.components.Restriction) VerifierData(org.drools.verifier.data.VerifierData) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 5 with Restriction

use of org.drools.verifier.components.Restriction in project drools by kiegroup.

the class ComponentsReportVisitor method visitField.

public static String visitField(String sourceFolder, Field field, VerifierReport result) {
    VerifierData data = result.getVerifierData();
    ObjectType objectType = data.getVerifierObject(VerifierComponentType.OBJECT_TYPE, field.getObjectTypePath());
    Collection<VerifierRule> rules = data.getRulesByFieldPath(field.getPath());
    Map<String, Object> map = new HashMap<>();
    map.put("sourceFolder", sourceFolder);
    map.put("ruleFolder", UrlFactory.RULE_FOLDER);
    map.put("objectTypeFolder", UrlFactory.OBJECT_TYPE_FOLDER);
    map.put("fieldFolder", UrlFactory.FIELD_FOLDER);
    map.put("field", field);
    map.put("objectType", objectType);
    map.put("rules", rules);
    if (field.getFieldType().equals(Field.DOUBLE) || field.getFieldType().equals(Field.DATE) || field.getFieldType().equals(Field.INT)) {
        Collection<MissingRange> causes = result.getRangeCheckCausesByFieldPath(field.getPath());
        Collection<Restriction> restrictions = data.getRestrictionsByFieldPath(field.getPath());
        map.put("ranges", "Ranges:" + MissingRangesReportVisitor.visitRanges(UrlFactory.PREVIOUS_FOLDER, restrictions, causes));
    } else {
        map.put("ranges", "");
    }
    String myTemplate = readFile("field.htm");
    return String.valueOf(TemplateRuntime.eval(myTemplate, map));
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) Restriction(org.drools.verifier.components.Restriction) MissingRange(org.drools.verifier.report.components.MissingRange) VerifierData(org.drools.verifier.data.VerifierData) HashMap(java.util.HashMap) VerifierRule(org.drools.verifier.components.VerifierRule)

Aggregations

Restriction (org.drools.verifier.components.Restriction)9 NumberRestriction (org.drools.verifier.components.NumberRestriction)5 ArrayList (java.util.ArrayList)3 Pattern (org.drools.verifier.components.Pattern)3 StringRestriction (org.drools.verifier.components.StringRestriction)3 VariableRestriction (org.drools.verifier.components.VariableRestriction)3 VerifierRule (org.drools.verifier.components.VerifierRule)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 PackageDescr (org.drools.drl.ast.descr.PackageDescr)2 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)2 SubPattern (org.drools.verifier.components.SubPattern)2 SubRule (org.drools.verifier.components.SubRule)2 VerifierData (org.drools.verifier.data.VerifierData)2 MissingRange (org.drools.verifier.report.components.MissingRange)2 Solvers (org.drools.verifier.solver.Solvers)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 DrlParser (org.drools.drl.parser.DrlParser)1