Search in sources :

Example 6 with ExistsDescr

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());
}
Also used : CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 7 with ExistsDescr

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

Example 8 with 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;
}
Also used : ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 9 with 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;
}
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 10 with ExistsDescr

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

Aggregations

ExistsDescr (org.drools.drl.ast.descr.ExistsDescr)10 PatternDescr (org.drools.drl.ast.descr.PatternDescr)6 Test (org.junit.Test)6 NotDescr (org.drools.drl.ast.descr.NotDescr)4 AndDescr (org.drools.drl.ast.descr.AndDescr)3 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)3 GroupElement (org.drools.core.rule.GroupElement)2 Pattern (org.drools.core.rule.Pattern)2 BaseDescr (org.drools.drl.ast.descr.BaseDescr)2 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)2 PackageDescr (org.drools.drl.ast.descr.PackageDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)2 InputStreamReader (java.io.InputStreamReader)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Forall (org.drools.core.rule.Forall)1 ForallDescr (org.drools.drl.ast.descr.ForallDescr)1 OrDescr (org.drools.drl.ast.descr.OrDescr)1