use of org.drools.drl.ast.descr.ForallDescr in project drools by kiegroup.
the class RuleParserTest method testForallWithFrom.
@Test
public void testForallWithFrom() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forallwithfrom.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final ForallDescr forall = (ForallDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, forall.getDescrs().size());
final PatternDescr pattern = forall.getBasePattern();
assertEquals("Person", pattern.getObjectType());
assertEquals("$village", ((FromDescr) pattern.getSource()).getDataSource().toString());
final List<BaseDescr> remaining = forall.getRemainingPatterns();
assertEquals(1, remaining.size());
final PatternDescr cheese = (PatternDescr) remaining.get(0);
assertEquals("Cheese", cheese.getObjectType());
assertEquals("$cheesery", ((FromDescr) cheese.getSource()).getDataSource().toString());
}
use of org.drools.drl.ast.descr.ForallDescr in project drools by kiegroup.
the class ForallHandler method start.
/* (non-Javadoc)
* @see org.kie.xml.Handler#start(java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
final ForallDescr forallDescr = new ForallDescr();
return forallDescr;
}
use of org.drools.drl.ast.descr.ForallDescr in project drools by kiegroup.
the class ForallHandler method end.
/* (non-Javadoc)
* @see org.kie.xml.Handler#end(java.lang.String, java.lang.String)
*/
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final ForallDescr forallDescr = (ForallDescr) parser.getCurrent();
final Object parent = parser.getParent();
final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
parentDescr.addDescr(forallDescr);
return null;
}
use of org.drools.drl.ast.descr.ForallDescr 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.ForallDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseForall.
@Test
public void testParseForall() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseForall.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
ForallDescr forall = (ForallDescr) obj.getLhs().getDescrs().get(0);
List forallPaterns = forall.getDescrs();
PatternDescr pattarnState = (PatternDescr) forallPaterns.get(0);
PatternDescr personState = (PatternDescr) forallPaterns.get(1);
PatternDescr cheeseState = (PatternDescr) forallPaterns.get(2);
assertEquals(pattarnState.getObjectType(), "State");
assertEquals(personState.getObjectType(), "Person");
assertEquals(cheeseState.getObjectType(), "Cheese");
}
Aggregations