Search in sources :

Example 1 with VariableRestriction

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

the class FieldConstraintDescrVisitor method visit.

/**
 * End
 * <p/>
 * Foo( bar == $bar )<br>
 * $bar is a VariableRestrictionDescr
 *
 * @param descr
 */
private void visit(VariableRestrictionDescr descr) {
    PatternVariable patternVariable = (PatternVariable) data.getVariableByRuleAndVariableName(pattern.getRuleName(), descr.getIdentifier());
    VariableRestriction restriction = new VariableRestriction(pattern);
    restriction.setPatternIsNot(pattern.isPatternNot());
    restriction.setFieldPath(field.getPath());
    restriction.setOperator(Operator.determineOperator(descr.getEvaluator(), descr.isNegated()));
    restriction.setVariable(patternVariable);
    restriction.setOrderNumber(orderNumber);
    restriction.setParentPath(pattern.getPath());
    restriction.setParentType(pattern.getVerifierComponentType());
    // Set field value, if it is unset.
    field.setFieldType(Field.VARIABLE);
    data.add(restriction);
    solvers.addPatternComponent(restriction);
}
Also used : VariableRestriction(org.drools.verifier.components.VariableRestriction) PatternVariable(org.drools.verifier.components.PatternVariable)

Example 2 with VariableRestriction

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

the class MissingEqualityTest method testMissingEqualityInVariableRestrictions2.

@Test
public void testMissingEqualityInVariableRestrictions2() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("MissingEquality.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(this.getClass().getResourceAsStream("MissingEqualityTest.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Missing restriction in VariableRestrictions, unequal operator"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.WARNING).iterator();
    Set<String> ruleNames = new HashSet<String>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            Cause cause = ((VerifierMessage) o).getFaulty();
            String name = ((VariableRestriction) cause).getRuleName();
            ruleNames.add(name);
        }
    }
    assertTrue(ruleNames.remove("Missing equality 7"));
    if (!ruleNames.isEmpty()) {
        for (String string : ruleNames) {
            fail("Rule " + string + " caused an error.");
        }
    }
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) VerifierMessage(org.drools.verifier.report.components.VerifierMessage) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VariableRestriction(org.drools.verifier.components.VariableRestriction) VerifierReport(org.drools.verifier.data.VerifierReport) Cause(org.drools.verifier.report.components.Cause) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with VariableRestriction

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

the class OppositeRestrictionsTest method testVariableRestrictionOpposite.

@Test
public void testVariableRestrictionOpposite() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Restrictions.drl"));
    Collection<Object> data = new ArrayList<Object>();
    VerifierRule rule = VerifierComponentMockFactory.createRule1();
    Pattern pattern1 = VerifierComponentMockFactory.createPattern(1);
    Pattern pattern2 = VerifierComponentMockFactory.createPattern(2);
    Pattern pattern3 = VerifierComponentMockFactory.createPattern(3);
    /*
         * Working pair
         */
    PatternVariable variable1 = new PatternVariable(rule);
    variable1.setParentPath("1");
    variable1.setParentType(VerifierComponentType.FIELD);
    variable1.setOrderNumber(-1);
    VariableRestriction r1 = new VariableRestriction(pattern1);
    r1.setFieldPath("0");
    r1.setOperator(Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator());
    r1.setVariable(variable1);
    r1.setOrderNumber(0);
    VariableRestriction r2 = new VariableRestriction(pattern1);
    r2.setFieldPath("0");
    r2.setOperator(Operator.BuiltInOperator.LESS.getOperator());
    r2.setVariable(variable1);
    r2.setOrderNumber(1);
    String containsOperator = "contains";
    PatternVariable variable2 = new PatternVariable(rule);
    variable2.setParentPath("2");
    variable2.setParentType(VerifierComponentType.FIELD);
    variable2.setOrderNumber(3);
    VariableRestriction r3 = new VariableRestriction(pattern2);
    r3.setFieldPath("1");
    r3.setOperator(Operator.determineOperator(containsOperator, false));
    r3.setVariable(variable2);
    r3.setOrderNumber(4);
    VariableRestriction r4 = new VariableRestriction(pattern2);
    r4.setFieldPath("1");
    r4.setOperator(Operator.determineOperator(containsOperator, true));
    r4.setVariable(variable2);
    r4.setOrderNumber(5);
    /*
         * Pair that doesn't work.
         */
    PatternVariable variable3 = new PatternVariable(rule);
    variable3.setParentPath("3");
    variable3.setParentType(VerifierComponentType.FIELD);
    variable3.setOrderNumber(6);
    VariableRestriction r5 = new VariableRestriction(pattern3);
    r5.setFieldPath("1");
    r5.setOperator(Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator());
    r5.setVariable(variable3);
    r5.setOrderNumber(7);
    VariableRestriction r6 = new VariableRestriction(pattern3);
    r6.setFieldPath("1");
    r6.setOperator(Operator.BuiltInOperator.EQUAL.getOperator());
    r6.setVariable(variable3);
    r6.setOrderNumber(8);
    data.add(r1);
    data.add(r2);
    data.add(r3);
    data.add(r4);
    data.add(r5);
    data.add(r6);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Opposite VariableRestrictions"));
    Map<Cause, Set<Cause>> map = createOppositesMap(VerifierComponentType.RESTRICTION, (Iterator<Object>) session.getObjects().iterator());
    assertTrue((TestBaseOld.causeMapContains(map, r1, r2) ^ TestBaseOld.causeMapContains(map, r2, r1)));
    assertTrue((TestBaseOld.causeMapContains(map, r3, r4) ^ TestBaseOld.causeMapContains(map, r4, r3)));
    if (!map.isEmpty()) {
        fail("More opposites than was expected.");
    }
}
Also used : Pattern(org.drools.verifier.components.Pattern) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) PatternVariable(org.drools.verifier.components.PatternVariable) Set(java.util.Set) ArrayList(java.util.ArrayList) VerifierRule(org.drools.verifier.components.VerifierRule) VariableRestriction(org.drools.verifier.components.VariableRestriction) Cause(org.drools.verifier.report.components.Cause) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with VariableRestriction

use of org.drools.verifier.components.VariableRestriction 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 5 with VariableRestriction

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

the class ExprConstraintDescrVisitor method createVariableRestriction.

private void createVariableRestriction(int orderNumber, String value, Operator operator) {
    Variable variable = data.getVariableByRuleAndVariableName(pattern.getRuleName(), value);
    VariableRestriction restriction = new VariableRestriction(pattern);
    restriction.setPatternIsNot(pattern.isPatternNot());
    restriction.setFieldPath(field.getPath());
    restriction.setOperator(operator);
    restriction.setVariable(variable);
    restriction.setOrderNumber(orderNumber);
    restriction.setParentPath(pattern.getPath());
    restriction.setParentType(pattern.getVerifierComponentType());
    // Set field value, if it is unset.
    field.setFieldType(Field.VARIABLE);
    data.add(restriction);
    solvers.addPatternComponent(restriction);
}
Also used : VariableRestriction(org.drools.verifier.components.VariableRestriction) Variable(org.drools.verifier.components.Variable) FieldVariable(org.drools.verifier.components.FieldVariable)

Aggregations

VariableRestriction (org.drools.verifier.components.VariableRestriction)9 Test (org.junit.Test)6 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)5 Cause (org.drools.verifier.report.components.Cause)5 KieSession (org.kie.api.runtime.KieSession)5 Pattern (org.drools.verifier.components.Pattern)3 PatternVariable (org.drools.verifier.components.PatternVariable)3 VerifierReport (org.drools.verifier.data.VerifierReport)3 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)3 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 VerifierRule (org.drools.verifier.components.VerifierRule)2 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1 EnumRestriction (org.drools.verifier.components.EnumRestriction)1 FieldVariable (org.drools.verifier.components.FieldVariable)1 NumberRestriction (org.drools.verifier.components.NumberRestriction)1 ObjectType (org.drools.verifier.components.ObjectType)1 QualifiedIdentifierRestriction (org.drools.verifier.components.QualifiedIdentifierRestriction)1 Restriction (org.drools.verifier.components.Restriction)1