Search in sources :

Example 1 with ForallDescr

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());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ForallDescr(org.drools.drl.ast.descr.ForallDescr) Test(org.junit.Test)

Example 2 with ForallDescr

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;
}
Also used : ForallDescr(org.drools.drl.ast.descr.ForallDescr)

Example 3 with 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;
}
Also used : Element(org.w3c.dom.Element) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr) ForallDescr(org.drools.drl.ast.descr.ForallDescr)

Example 4 with ForallDescr

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;
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) Pattern(org.drools.core.rule.Pattern) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) GroupElement(org.drools.core.rule.GroupElement) BaseDescr(org.drools.drl.ast.descr.BaseDescr) Forall(org.drools.core.rule.Forall) ForallDescr(org.drools.drl.ast.descr.ForallDescr)

Example 5 with ForallDescr

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");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) ForallDescr(org.drools.drl.ast.descr.ForallDescr) Test(org.junit.Test)

Aggregations

ForallDescr (org.drools.drl.ast.descr.ForallDescr)6 PatternDescr (org.drools.drl.ast.descr.PatternDescr)4 BaseDescr (org.drools.drl.ast.descr.BaseDescr)3 PackageDescr (org.drools.drl.ast.descr.PackageDescr)3 RuleDescr (org.drools.drl.ast.descr.RuleDescr)3 Test (org.junit.Test)3 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 Forall (org.drools.core.rule.Forall)1 GroupElement (org.drools.core.rule.GroupElement)1 Pattern (org.drools.core.rule.Pattern)1 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)1 ExistsDescr (org.drools.drl.ast.descr.ExistsDescr)1 FromDescr (org.drools.drl.ast.descr.FromDescr)1 NotDescr (org.drools.drl.ast.descr.NotDescr)1 Element (org.w3c.dom.Element)1