Search in sources :

Example 1 with ForallDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.ForallDescr)

Example 2 with ForallDescr

use of org.drools.compiler.lang.descr.ForallDescr in project drools by kiegroup.

the class RuleParserTest method testForall.

@Test
public void testForall() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forall.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());
    final List<BaseDescr> remaining = forall.getRemainingPatterns();
    assertEquals(1, remaining.size());
    final PatternDescr cheese = (PatternDescr) remaining.get(0);
    assertEquals("Cheese", cheese.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ForallDescr(org.drools.compiler.lang.descr.ForallDescr) Test(org.junit.Test)

Example 3 with ForallDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) ForallDescr(org.drools.compiler.lang.descr.ForallDescr) Test(org.junit.Test)

Example 4 with ForallDescr

use of org.drools.compiler.lang.descr.ForallDescr in project drools by kiegroup.

the class ForallBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final ForallDescr forallDescr = (ForallDescr) descr;
    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 : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Forall(org.drools.core.rule.Forall) ForallDescr(org.drools.compiler.lang.descr.ForallDescr)

Example 5 with ForallDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.ConditionalElementDescr) ForallDescr(org.drools.compiler.lang.descr.ForallDescr)

Aggregations

ForallDescr (org.drools.compiler.lang.descr.ForallDescr)6 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)4 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)3 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 RuleDescr (org.drools.compiler.lang.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 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)1 FromDescr (org.drools.compiler.lang.descr.FromDescr)1 Forall (org.drools.core.rule.Forall)1 Pattern (org.drools.core.rule.Pattern)1 Element (org.w3c.dom.Element)1