use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method commonDeclarePatternWithConstraint.
@Test
public void commonDeclarePatternWithConstraint() {
String patternType = "TEMPERATURE";
String constraintsString = "value < 35";
final CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<RuleDescrBuilder, AndDescr>, NotDescr>, ExistsDescr> existsBuilder = lhsBuilder.not().exists();
KiePMMLDescrLhsFactory.factory(lhsBuilder).commonDeclarePatternWithConstraint(existsBuilder, patternType, constraintsString);
assertNotNull(existsBuilder.getDescr());
final List<BaseDescr> descrs = existsBuilder.getDescr().getDescrs();
assertNotNull(descrs);
assertEquals(1, descrs.size());
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.get(0);
assertEquals(patternType, patternDescr.getObjectType());
assertNull(patternDescr.getIdentifier());
assertTrue(patternDescr.getConstraint() instanceof AndDescr);
AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
assertEquals(1, andDescr.getDescrs().size());
assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
assertFalse(exprConstraintDescr.isNegated());
assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
assertEquals(constraintsString, exprConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class ExistsHandler method start.
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
final ExistsDescr existsDescr = new ExistsDescr();
return existsDescr;
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class ExistsHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final ExistsDescr existsDescr = (ExistsDescr) parser.getCurrent();
if ((existsDescr.getDescrs().size() != 1) && (existsDescr.getDescrs().get(0).getClass() != PatternDescr.class)) {
throw new SAXParseException("<exists> can only have a single <pattern...> as a child element", parser.getLocator());
}
final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parser.getParent();
parentDescr.addDescr(existsDescr);
return existsDescr;
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class ForallBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr) {
final ForallDescr forallDescr = (ForallDescr) descr;
if (forallDescr.isSinglePattern()) {
PatternDescr pattern = (PatternDescr) forallDescr.getDescrs().get(0);
NotDescr notDescr = new NotDescr(pattern.negateConstraint());
RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(notDescr.getClass());
return builder.build(context, notDescr);
}
BaseDescr selfJoin = forallDescr.getSelfJoinConstraint();
if (selfJoin != null) {
// transforms a self join forall in the form
// forall( $t : Type( constraints1 ) Type( this == $t, constraints2 ) )
// into
// exists( Type( constraints1 ) ) and not( Type( constraints1, !constraints2 ) )
GroupElement transformedForall = new GroupElement();
PatternDescr p1 = (PatternDescr) forallDescr.getDescrs().get(0);
PatternDescr p2 = (PatternDescr) forallDescr.getDescrs().get(1);
ExistsDescr existDescr = new ExistsDescr(p1);
RuleConditionBuilder existsBuilder = (RuleConditionBuilder) context.getDialect().getBuilder(existDescr.getClass());
transformedForall.addChild(existsBuilder.build(context, existDescr));
NotDescr notDescr = new NotDescr(p1);
p2.removeConstraint(selfJoin);
p2.negateConstraint().getConstraint().getDescrs().forEach(p1::addConstraint);
RuleConditionBuilder notBuilder = (RuleConditionBuilder) context.getDialect().getBuilder(notDescr.getClass());
transformedForall.addChild(notBuilder.build(context, notDescr));
return transformedForall;
}
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;
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class RuleParserTest method testNotExistWithBrackets.
@Test
public void testNotExistWithBrackets() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
final AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
assertEquals(1, not.getDescrs().size());
final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
assertEquals("Cheese", pattern.getObjectType());
final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
assertEquals(1, ex.getDescrs().size());
final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
assertEquals("Foo", exPattern.getObjectType());
}
Aggregations