use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class CrossProductTest method setUp.
@Before
public void setUp() throws Exception {
final ObjectType list1ObjectType = new ClassObjectType(String.class);
final ObjectType list2ObjectType = new ClassObjectType(String.class);
final RuleImpl rule = new RuleImpl("rule-1");
final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
rule.addPattern(list1Pattern);
rule.addPattern(list2Pattern);
final Declaration s1Declaration = rule.getDeclaration("s1");
final Declaration s2Declaration = rule.getDeclaration("s2");
this.values = new ArrayList();
rule.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
final String s1 = (String) knowledgeHelper.get(s1Declaration);
final String s2 = (String) knowledgeHelper.get(s2Declaration);
CrossProductTest.this.values.add(new String[] { s1, s2 });
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
this.pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
this.pkg.addRule(rule);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class AccumulateVisitor method replaceBindingWithPatternBinding.
private MethodCallExpr replaceBindingWithPatternBinding(MethodCallExpr bindExpression, MethodCallExpr lastPattern) {
// This method links a binding expression, used to evaluate the accumulated value,
// to the last pattern in a multi-pattern accumulate like the following
//
// accumulate( $c : Child( age < 10 ) and $a : Adult( name == $c.parent ) and $s : String( this == $a.name ),
// $sum : sum($a.getAge() + $c.getAge() + $s.length()) )
//
// In the case the bindExpression, that will have to be linked to the $s pattern, is originally generated as
//
// bind(var_$sum, var_$a, var_$c, var_$s, (Adult $a, Child $c, String $s) -> $a.getAge() + $c.getAge() + $s.length())
final Expression bindingId = lastPattern.getArgument(0);
bindExpression.findFirst(NameExpr.class, e -> e.equals(bindingId)).ifPresent(name -> {
// since the bind has to be linked to $s, the corresponding variable should be removed from the arguments list so it becomes
// bind(var_$sum, var_$a, var_$c, (Adult $a, Child $c, String $s) -> $a.getAge() + $c.getAge() + $s.length())
bindExpression.remove(name);
// also the first formal parameter in the binding lambda has to be $s so it becomes
// bind(var_$sum, var_$a, var_$c, (String $s, Adult $a, Child $c) -> $a.getAge() + $c.getAge() + $s.length())
LambdaExpr lambda = (LambdaExpr) bindExpression.getArgument(bindExpression.getArguments().size() - 1);
if (lambda.getParameters().size() > 1) {
String formalArg = context.fromVar(name.getNameAsString());
for (Parameter param : lambda.getParameters()) {
if (param.getNameAsString().equals(formalArg)) {
lambda.getParameters().remove(param);
lambda.getParameters().add(0, param);
break;
}
}
}
});
return bindExpression;
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class LogicTransformerTest method testNotExistsTransformation.
/**
* Exists inside a not is redundant and should be eliminated
*
* (Not (exists (A) ) )
*
* <pre>
* Not
* |
* Exists
* |
* And
* / \
* a b
* </pre>
*
* Should become:
*
* <pre>
* Not
* |
* And
* / \
* a b
* </pre>
*/
@Test
public void testNotExistsTransformation() throws InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final GroupElement not = GroupElementFactory.newNotInstance();
final GroupElement exists = GroupElementFactory.newExistsInstance();
final GroupElement and = GroupElementFactory.newAndInstance();
not.addChild(exists);
exists.addChild(and);
and.addChild(a);
and.addChild(b);
GroupElement[] transformed = LogicTransformer.getInstance().transform(not, Collections.EMPTY_MAP);
GroupElement wrapper = transformed[0];
GroupElement notR = (GroupElement) wrapper.getChildren().get(0);
assertTrue(notR.isNot());
assertEquals(1, notR.getChildren().size());
assertTrue(notR.getChildren().get(0) instanceof GroupElement);
GroupElement andR = (GroupElement) notR.getChildren().get(0);
assertTrue(andR.isAnd());
assertEquals(2, andR.getChildren().size());
assertTrue(andR.getChildren().get(0) instanceof Pattern);
assertTrue(andR.getChildren().get(1) instanceof Pattern);
final Pattern a1 = (Pattern) andR.getChildren().get(0);
final Pattern b1 = (Pattern) andR.getChildren().get(1);
assertEquals(a, a1);
assertEquals(b, b1);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class LogicTransformerTest method testSingleOrAndOrTransformation.
/**
* (a||b)&&c
*
* <pre>
* and
* / \
* or c
* / \
* a b
* </pre>
*
* Should become (a&&c)||(b&&c)
*
* <pre>
*
* or
* / \
* / \
* / \
* and and
* / \ / \
* a c b c
* </pre>
*/
@Test
public void testSingleOrAndOrTransformation() throws InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final Pattern c = new Pattern(2, type, "c");
final GroupElement or = GroupElementFactory.newOrInstance();
or.addChild(a);
or.addChild(b);
final GroupElement parent = GroupElementFactory.newAndInstance();
parent.addChild(or);
parent.addChild(c);
LogicTransformer.getInstance().applyOrTransformation(parent);
assertLength(2, parent.getChildren());
assertEquals(GroupElement.class, parent.getChildren().get(0).getClass());
assertEquals(GroupElement.class, parent.getChildren().get(1).getClass());
final GroupElement and1 = (GroupElement) parent.getChildren().get(0);
assertTrue(and1.isAnd());
// transformation MUST keep the order
assertEquals(a, and1.getChildren().get(0));
assertEquals(c, and1.getChildren().get(1));
final GroupElement and2 = (GroupElement) parent.getChildren().get(1);
assertEquals(b, and2.getChildren().get(0));
assertEquals(c, and2.getChildren().get(1));
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class LogicTransformerTest method testTransform.
/**
* /**
*
* <pre>
* _/|\_
* __/ | \__
* / | \
* __/ | \__
* / | \
* And or And
* / \ / \ / \
* a Or d e Not OR
* / \ | / |
* b c f g Not
* |
* h
*
* </pre>
*
* Each And is a Rete sub rule
*
* <pre>
*
* And___ And___ And___ And___ And__ And___ And___ And___
* ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \
* abd Not g abd Not Not abe Not g abe Not Not acd Not g acd Not Not ace Not g ace Not Not
* | | | | | | | | | | | |
* f f h f f h f f h f f h
*
* </pre>
*
* @throws IOException
* @throws ClassNotFoundException
*
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void testTransform() throws IOException, ClassNotFoundException, InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final Pattern c = new Pattern(2, type, "c");
final Pattern d = new Pattern(3, type, "d");
final Pattern e = new Pattern(4, type, "e");
final Pattern f = new Pattern(5, type, "f");
final Pattern g = new Pattern(6, type, "g");
final Pattern h = new Pattern(7, type, "h");
final GroupElement and = GroupElementFactory.newAndInstance();
final GroupElement and1 = GroupElementFactory.newAndInstance();
and1.addChild(a);
final GroupElement or1 = GroupElementFactory.newOrInstance();
or1.addChild(b);
or1.addChild(c);
and1.addChild(or1);
and.addChild(and1);
final GroupElement or2 = GroupElementFactory.newOrInstance();
or2.addChild(d);
or2.addChild(e);
and.addChild(or2);
final GroupElement and2 = GroupElementFactory.newAndInstance();
final GroupElement not1 = GroupElementFactory.newNotInstance();
not1.addChild(f);
final GroupElement or3 = GroupElementFactory.newOrInstance();
or3.addChild(g);
final GroupElement not2 = GroupElementFactory.newNotInstance();
not2.addChild(h);
or3.addChild(not2);
and2.addChild(not1);
and2.addChild(or3);
and.addChild(and2);
final GroupElement[] ands = LogicTransformer.getInstance().transform(and, Collections.EMPTY_MAP);
// Uncomment this when you need to output a new known correct tree
// result
final File testFile = new File("target/test/LogicTransformerTest_correct_transform1.dat");
testFile.getParentFile().mkdirs();
DroolsStreamUtils.streamOut(new FileOutputStream(testFile), ands);
// Now check the main tree
// Get known correct tree
// The binary stream was created from a handchecked correct output
final GroupElement[] correctResultAnds = (GroupElement[]) DroolsStreamUtils.streamIn(new FileInputStream(testFile));
for (int j = 0; j < ands.length; j++) {
assertEquals(correctResultAnds[j], ands[j]);
}
}
Aggregations