use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KnowledgeBuilderTest method testReturnValueMethodCompare.
@Test
public void testReturnValueMethodCompare() {
final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = new PackageDescr("package1");
createReturnValueRule(packageDescr1, " x + y ");
builder1.addPackage(packageDescr1);
if (builder1.hasErrors()) {
fail(builder1.getErrors().toString());
}
final Pattern pattern1 = (Pattern) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
final Constraint returnValue1 = pattern1.getConstraints().get(0);
final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr2 = new PackageDescr("package2");
createReturnValueRule(packageDescr2, " x + y ");
builder2.addPackage(packageDescr2);
final Pattern pattern2 = (Pattern) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
final Constraint returnValue2 = pattern2.getConstraints().get(0);
final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr3 = new PackageDescr("package3");
createReturnValueRule(packageDescr3, " x - y ");
builder3.addPackage(packageDescr3);
final Pattern pattern3 = (Pattern) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
final Constraint returnValue3 = pattern3.getConstraints().get(0);
assertEquals(returnValue1, returnValue2);
assertFalse(returnValue1.equals(returnValue3));
assertFalse(returnValue2.equals(returnValue3));
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KnowledgeBuilderTest method testExists.
@Test
public void testExists() throws Exception {
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
// Make sure we can't accessa variable bound inside the not node
RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
assertTrue(builder.hasErrors());
builder = new KnowledgeBuilderImpl();
rule = createRule(new ExistsDescr(), builder, "");
assertEquals(0, builder.getErrors().getErrors().length);
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
assertLength(1, exists.getChildren());
final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class PatternBuilder method build.
private List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ConstraintConnectiveDescr descr, DumperContext mvelCtx) {
List<Constraint> constraints = new ArrayList<>();
List<BaseDescr> initialDescrs = new ArrayList<>(descr.getDescrs());
for (BaseDescr d : initialDescrs) {
boolean isXPath = isXPathDescr(d);
if (isXPath && pattern.hasXPath()) {
registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
return constraints;
}
context.setXpathOffsetadjustment(xpathStartDeclaration == null ? 0 : -1);
Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, xpathStartDeclaration, pattern, (ExpressionDescr) d, mvelCtx) : buildCcdDescr(context, patternDescr, xpathStartDeclaration, pattern, d, descr, mvelCtx);
if (constraint != null) {
Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
if (declCorrXpath == null) {
constraints.add(constraint);
} else {
// A constraint is using a declration bound to an xpath in the same pattern
// Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
Pattern modifiedPattern = pattern.clone();
modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
constraint = buildCcdDescr(context, patternDescr, xpathStartDeclaration, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
if (constraint != null) {
pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
}
}
}
}
if (descr.getDescrs().size() > initialDescrs.size()) {
// The initial build process may have generated other constraint descrs.
// This happens when null-safe references or inline-casts are used
// These additional constraints must be built, and added as
List<BaseDescr> additionalDescrs = new ArrayList<>(descr.getDescrs());
additionalDescrs.removeAll(initialDescrs);
if (!additionalDescrs.isEmpty()) {
List<Constraint> additionalConstraints = new ArrayList<>();
for (BaseDescr d : additionalDescrs) {
Constraint constraint = buildCcdDescr(context, patternDescr, xpathStartDeclaration, pattern, d, descr, mvelCtx);
if (constraint != null) {
additionalConstraints.add(constraint);
}
}
constraints.addAll(0, additionalConstraints);
}
}
return constraints;
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class CollectBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final CollectDescr collectDescr = (CollectDescr) descr;
final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
final Pattern sourcePattern = (Pattern) patternBuilder.build(context, collectDescr.getInputPattern());
if (sourcePattern == null) {
return null;
}
final String className = "collect" + context.getNextId();
collectDescr.setClassMethodName(className);
Pattern resultPattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
return new Collect(sourcePattern, resultPattern);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class AnnotationsTest method testNestedAnnotations.
@Test
public void testNestedAnnotations() {
String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Outer( value = @Inner( text = \"world\" ) ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
Map<String, AnnotationDefinition> defs = p.getAnnotations();
assertEquals(1, defs.size());
AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
assertNotNull(outer);
Object val = outer.getPropertyValue("value");
assertNotNull(val);
assertTrue(val instanceof AnnotationDefinition);
AnnotationDefinition inner = (AnnotationDefinition) val;
assertEquals("world", inner.getPropertyValue("text"));
}
Aggregations