Search in sources :

Example 1 with Pattern

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

the class PatternSolverTest method testAddAndOrOr.

/**
 * <pre>
 *            and
 *         /        \
 *       or          or
 *      /  \         / \
 * descr descr2 descr3 descr4
 * </pre>
 *
 * result:<br>
 * descr && descr3<br>
 * or<br>
 * descr && descr4<br>
 * or<br>
 * descr2 && descr3<br>
 * or<br>
 * descr2 && descr4
 */
@Test
public void testAddAndOrOr() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction3 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction4 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.AND);
    solver.addOperator(OperatorDescrType.OR);
    solver.add(literalRestriction);
    solver.add(literalRestriction2);
    solver.end();
    solver.addOperator(OperatorDescrType.OR);
    solver.add(literalRestriction3);
    solver.add(literalRestriction4);
    solver.end();
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(4, list.size());
    assertEquals(2, list.get(0).size());
    assertTrue(list.get(0).contains(literalRestriction));
    assertTrue(list.get(0).contains(literalRestriction3));
    assertEquals(2, list.get(1).size());
    assertTrue(list.get(1).contains(literalRestriction));
    assertTrue(list.get(1).contains(literalRestriction4));
    assertEquals(2, list.get(2).size());
    assertTrue(list.get(2).contains(literalRestriction2));
    assertTrue(list.get(2).contains(literalRestriction3));
    assertEquals(2, list.get(3).size());
    assertTrue(list.get(3).contains(literalRestriction2));
    assertTrue(list.get(3).contains(literalRestriction4));
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Example 2 with Pattern

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

the class PatternSolverTest method testAddOrAndAnd.

/**
 * <pre>
 *             or
 *         /        \
 *       and         and
 *      /  \         / \
 * descr descr2 descr3 descr4
 * </pre>
 *
 * result:<br>
 * descr && descr2<br>
 * or<br>
 * descr3 && descr4
 */
@Test
public void testAddOrAndAnd() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction3 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction4 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.OR);
    solver.addOperator(OperatorDescrType.AND);
    solver.add(literalRestriction);
    solver.add(literalRestriction2);
    solver.end();
    solver.addOperator(OperatorDescrType.AND);
    solver.add(literalRestriction3);
    solver.add(literalRestriction4);
    solver.end();
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(2, list.size());
    assertEquals(2, list.get(0).size());
    assertTrue(list.get(0).contains(literalRestriction));
    assertTrue(list.get(0).contains(literalRestriction2));
    assertEquals(2, list.get(1).size());
    assertTrue(list.get(1).contains(literalRestriction3));
    assertTrue(list.get(1).contains(literalRestriction4));
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Example 3 with Pattern

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

the class PatternSolverTest method testAddAndOr.

/**
 * <pre>
 *       and
 *      /  \
 *  descr   or
 *         / \
 *    descr2 descr3
 * </pre>
 *
 * result:<br>
 * descr && descr2 <br>
 * or<br>
 * descr && descr3
 */
@Test
public void testAddAndOr() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction3 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.AND);
    solver.add(literalRestriction);
    solver.addOperator(OperatorDescrType.OR);
    solver.add(literalRestriction2);
    solver.add(literalRestriction3);
    solver.end();
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(2, list.size());
    assertEquals(2, list.get(0).size());
    assertTrue(list.get(0).contains(literalRestriction));
    assertTrue(list.get(0).contains(literalRestriction2));
    assertEquals(2, list.get(1).size());
    assertTrue(list.get(1).contains(literalRestriction));
    assertTrue(list.get(1).contains(literalRestriction3));
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Example 4 with Pattern

use of org.drools.verifier.components.Pattern 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.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 5 with Pattern

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

the class PatternDescrVisitor method visitPatternDescr.

private Pattern visitPatternDescr(PatternDescr descr, VerifierComponent parent, int orderNumber) throws UnknownDescriptionException {
    objectType = data.getObjectTypeByFullName(descr.getObjectType());
    if (objectType == null) {
        Import objectImport = data.getImportByName(descr.getObjectType());
        if (objectImport != null) {
            objectType = ObjectTypeFactory.createObjectType(descr, objectImport);
        } else {
            objectType = ObjectTypeFactory.createObjectType(descr, descr.getObjectType());
        }
        data.add(objectType);
    }
    pattern = new Pattern(descr, rule);
    if (parent != null) {
        pattern.setParentPath(parent.getPath());
        pattern.setParentType(parent.getVerifierComponentType());
    }
    pattern.setObjectTypePath(objectType.getPath());
    pattern.setName(objectType.getName());
    pattern.setPatternNot(solvers.getRuleSolver().isChildNot());
    pattern.setPatternExists(solvers.getRuleSolver().isExists());
    pattern.setPatternForall(solvers.getRuleSolver().isForall());
    pattern.setOrderNumber(orderNumber);
    if (descr.getIdentifier() != null) {
        PatternVariable patternVariable = new PatternVariable(rule);
        patternVariable.setName(descr.getIdentifier());
        patternVariable.setParentPath(pattern.getPath());
        patternVariable.setParentType(pattern.getVerifierComponentType());
        data.add(patternVariable);
    }
    // visit source.
    if (descr.getSource() != null) {
        visit(descr.getSource());
    } else {
        if (workingMemory == null) {
            workingMemory = new WorkingMemory();
            data.add(workingMemory);
        }
        pattern.setSourcePath(workingMemory.getPath());
        pattern.setSourceType(workingMemory.getVerifierComponentType());
    }
    solvers.startPatternSolver(pattern);
    visit(descr.getConstraint());
    solvers.endPatternSolver();
    data.add(pattern);
    return pattern;
}
Also used : Pattern(org.drools.verifier.components.Pattern) PatternVariable(org.drools.verifier.components.PatternVariable) Import(org.drools.verifier.components.Import) WorkingMemory(org.drools.verifier.components.WorkingMemory)

Aggregations

Pattern (org.drools.verifier.components.Pattern)25 Test (org.junit.Test)22 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)10 Set (java.util.Set)8 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)8 VerifierReport (org.drools.verifier.data.VerifierReport)8 KieSession (org.kie.api.runtime.KieSession)8 HashSet (java.util.HashSet)7 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)7 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)7 SubPattern (org.drools.verifier.components.SubPattern)6 VerifierRule (org.drools.verifier.components.VerifierRule)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 SubRule (org.drools.verifier.components.SubRule)4 ArrayList (java.util.ArrayList)3 Restriction (org.drools.verifier.components.Restriction)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)2 ObjectType (org.drools.verifier.components.ObjectType)2 PatternVariable (org.drools.verifier.components.PatternVariable)2 RulePackage (org.drools.verifier.components.RulePackage)2