use of org.drools.compiler.lang.descr.PatternDescr 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.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class AlwaysTruePatternTest method testPatterns.
@Test
@Ignore
public void testPatterns() throws Exception {
KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
VerifierReport result = VerifierReportFactory.newVerifierReport();
Collection<Object> data = new ArrayList<Object>();
session.setGlobal("result", result);
VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
// This pattern is always true.
Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
SubPattern pp1 = new SubPattern(pattern1, 0);
AlwaysTrue alwaysTrue1 = new AlwaysTrue(pp1);
SubPattern pp2 = new SubPattern(pattern1, 1);
AlwaysTrue alwaysTrue2 = new AlwaysTrue(pp2);
// This pattern is okay.
Pattern pattern2 = new Pattern(new PatternDescr("objectType2", "testPattern2"), rule1);
pattern2.setName("testPattern2");
SubPattern pp3 = new SubPattern(pattern2, 0);
SubPattern pp4 = new SubPattern(pattern2, 1);
AlwaysTrue alwaysTrue4 = new AlwaysTrue(pp4);
data.add(rule1);
data.add(pattern1);
data.add(pp1);
data.add(pp2);
data.add(alwaysTrue1);
data.add(alwaysTrue2);
data.add(pattern2);
data.add(pp3);
data.add(pp4);
data.add(alwaysTrue4);
for (Object o : data) {
session.insert(o);
}
session.fireAllRules(new RuleNameMatchesAgendaFilter("Pattern that is always true"));
Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.NOTE).iterator();
boolean works = false;
while (iter.hasNext()) {
Object o = (Object) iter.next();
if (o instanceof VerifierMessage) {
VerifierMessage message = (VerifierMessage) o;
if (message.getFaulty().equals(pattern1)) {
works = true;
} else {
fail("There can be only one. (And this is not the one)");
}
}
}
assertEquals(0, result.getBySeverity(Severity.ERROR).size());
assertEquals(0, result.getBySeverity(Severity.WARNING).size());
assertEquals(1, result.getBySeverity(Severity.NOTE).size());
assertTrue(works);
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parseBaseDescr.
private IPattern parseBaseDescr(final RuleModel m, final BaseDescr descr, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
if (descr instanceof PatternDescr) {
return parsePatternDescr(m, (PatternDescr) descr, isJavaDialect, boundParams, dmo);
} else if (descr instanceof AndDescr) {
AndDescr andDescr = (AndDescr) descr;
return parseBaseDescr(m, andDescr.getDescrs().get(0), isJavaDialect, boundParams, dmo);
} else if (descr instanceof EvalDescr) {
FreeFormLine freeFormLine = new FreeFormLine();
freeFormLine.setText("eval( " + ((EvalDescr) descr).getContent() + " )");
return freeFormLine;
} else if (descr instanceof ConditionalElementDescr) {
return parseExistentialElementDescr(m, (ConditionalElementDescr) descr, isJavaDialect, boundParams, dmo);
}
return null;
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class VerifierComponentMockFactory method createPattern.
public static Pattern createPattern(int i) {
Pattern pattern = new Pattern(new PatternDescr("objectType" + i, "testPattern" + i), createRule(i));
pattern.setObjectTypePath("objectType" + i);
pattern.setName("testPattern" + i);
return pattern;
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class ForallBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final ForallDescr forallDescr = (ForallDescr) descr;
final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
final Pattern basePattern = (Pattern) patternBuilder.build(context, forallDescr.getBasePattern());
if (basePattern == null) {
return null;
}
final Forall forall = new Forall(basePattern);
// adding the newly created forall CE to the build stack
// this is necessary in case of local declaration usage
context.getDeclarationResolver().pushOnBuildStack(forall);
for (BaseDescr baseDescr : forallDescr.getRemainingPatterns()) {
final Pattern anotherPattern = (Pattern) patternBuilder.build(context, (PatternDescr) baseDescr);
forall.addRemainingPattern(anotherPattern);
}
if (forallDescr.getDescrs().size() == 1) {
// An optimization for unlinking, where we allow unlinking if the resulting 'not' node has no constraints
// we need to record this here, due to getRemainingPatterns injecting "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
// which we wish to ignore
PatternDescr p = (PatternDescr) forallDescr.getDescrs().get(0);
if (p.getConstraint().getDescrs().isEmpty()) {
forall.setEmptyBetaConstraints(true);
}
}
// poping the forall
context.getDeclarationResolver().popBuildStack();
return forall;
}
Aggregations