Search in sources :

Example 1 with Opposites

use of org.drools.verifier.report.components.Opposites in project drools by kiegroup.

the class OppositePatternsTest method testPatternsPossibilitiesOpposite.

@Test
public void testPatternsPossibilitiesOpposite() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
    Collection<Object> data = new ArrayList<Object>();
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    /*
         * Working pair
         */
    SubPattern pp1 = new SubPattern(pattern, 0);
    SubPattern pp2 = new SubPattern(pattern, 1);
    Restriction r1 = LiteralRestriction.createRestriction(pattern, "");
    pp1.add(r1);
    Restriction r2 = LiteralRestriction.createRestriction(pattern, "");
    pp2.add(r2);
    Restriction r3 = LiteralRestriction.createRestriction(pattern, "");
    pp1.add(r3);
    Restriction r4 = LiteralRestriction.createRestriction(pattern, "");
    pp2.add(r4);
    Opposites o1 = new Opposites(r1, r2);
    Opposites o2 = new Opposites(r3, r4);
    /*
         * Pair that doesn't work.
         */
    SubPattern pp3 = new SubPattern(pattern, 2);
    SubPattern pp4 = new SubPattern(pattern, 3);
    Restriction r5 = LiteralRestriction.createRestriction(pattern, "");
    pp3.add(r5);
    Restriction r6 = LiteralRestriction.createRestriction(pattern, "");
    pp4.add(r6);
    Restriction r7 = LiteralRestriction.createRestriction(pattern, "");
    pp3.add(r7);
    Restriction r8 = LiteralRestriction.createRestriction(pattern, "");
    pp4.add(r8);
    Opposites o3 = new Opposites(r5, r6);
    data.add(r1);
    data.add(r2);
    data.add(r3);
    data.add(r4);
    data.add(r5);
    data.add(r6);
    data.add(r7);
    data.add(r8);
    data.add(pp1);
    data.add(pp2);
    data.add(pp3);
    data.add(pp4);
    data.add(o1);
    data.add(o2);
    data.add(o3);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Opposite Patterns"));
    Map<Cause, Set<Cause>> map = createOppositesMap(VerifierComponentType.SUB_PATTERN, (Iterator<Object>) session.getObjects().iterator());
    assertTrue((TestBaseOld.causeMapContains(map, pp1, pp2) ^ TestBaseOld.causeMapContains(map, pp2, pp1)));
    if (!map.isEmpty()) {
        fail("More opposites than was expected.");
    }
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Set(java.util.Set) Opposites(org.drools.verifier.report.components.Opposites) Cause(org.drools.verifier.report.components.Cause) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with Opposites

use of org.drools.verifier.report.components.Opposites in project drools by kiegroup.

the class OppositesBase method createOppositesMap.

/**
 * Creates opposites map from Opposites objects, one rule may have several
 * opposing dependencies.
 *
 * @param iter
 * @return
 */
protected Map<Cause, Set<Cause>> createOppositesMap(VerifierComponentType type, Iterator<Object> iter) {
    Map<Cause, Set<Cause>> map = new HashMap<Cause, Set<Cause>>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof Opposites) {
            Opposites r = (Opposites) o;
            if (((VerifierComponent) r.getLeft()).getVerifierComponentType().equals(type)) {
                Cause left = r.getLeft();
                Cause right = r.getRight();
                if (map.containsKey(left)) {
                    Set<Cause> set = map.get(left);
                    set.add(right);
                } else {
                    Set<Cause> set = new HashSet<Cause>();
                    set.add(right);
                    map.put(left, set);
                }
            }
        }
    }
    return map;
}
Also used : Cause(org.drools.verifier.report.components.Cause) Opposites(org.drools.verifier.report.components.Opposites)

Aggregations

Cause (org.drools.verifier.report.components.Cause)2 Opposites (org.drools.verifier.report.components.Opposites)2 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1