use of org.drools.verifier.components.Restriction in project drools by kiegroup.
the class SolversTest method testNotAnd.
/**
* <pre>
* when
* Foo( r && r2 )
* and
* not Foo( r3 && 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.");
}
}
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));
}
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));
}
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());
}
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));
}
Aggregations