Search in sources :

Example 1 with ExprConstraintDescr

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

the class ExprConstraintHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final String expression = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
    emptyContentCheck(localName, expression, parser);
    PatternDescr p = (PatternDescr) parser.getParent();
    ExprConstraintDescr descr = new ExprConstraintDescr(expression);
    p.getConstraint().addDescr(descr);
    return descr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) Element(org.w3c.dom.Element) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 2 with ExprConstraintDescr

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

the class FieldConstraintHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final ConnectiveDescr c = (ConnectiveDescr) parser.getCurrent();
    Object p = parser.getParent();
    if (p instanceof PatternDescr) {
        StringBuilder sb = new StringBuilder();
        c.buildExpression(sb);
        ExprConstraintDescr expr = new ExprConstraintDescr();
        expr.setExpression(sb.toString());
        final PatternDescr patternDescr = (PatternDescr) parser.getParent();
        patternDescr.addConstraint(expr);
    } else if (p instanceof ConnectiveDescr) {
        ((ConnectiveDescr) p).add(c);
    }
    return c;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) Element(org.w3c.dom.Element) ConnectiveDescr(org.drools.compiler.lang.descr.ConnectiveDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 3 with ExprConstraintDescr

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

the class RuleBuilderTest method testBuildBigDecimalLiteralConstraint.

@Test
public void testBuildBigDecimalLiteralConstraint() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    AndDescr andDescr = new AndDescr();
    PatternDescr patDescr = new PatternDescr("java.math.BigDecimal", "$bd");
    ExprConstraintDescr fcd = new ExprConstraintDescr("this == 10");
    patDescr.addConstraint(fcd);
    andDescr.addDescr(patDescr);
    ruleDescr.setLhs(andDescr);
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
    final GroupElement and = rule.getLhs();
    final Pattern pat = (Pattern) and.getChildren().get(0);
    if (pat.getConstraints().get(0) instanceof MvelConstraint) {
        final MvelConstraint fc = (MvelConstraint) pat.getConstraints().get(0);
        assertTrue("Wrong class. Expected java.math.BigDecimal. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigDecimal);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with ExprConstraintDescr

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

the class RuleParserTest method testIsQuery.

@Test
public void testIsQuery() throws Exception {
    final String text = "rule X when ?person( \"Mark\", 42; ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertTrue(pattern.isQuery());
    assertEquals(2, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("\"Mark\"", fcd.getExpression());
    assertEquals(0, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.POSITIONAL, fcd.getType());
    fcd = (ExprConstraintDescr) pattern.getDescrs().get(1);
    assertEquals("42", fcd.getExpression());
    assertEquals(1, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.POSITIONAL, fcd.getType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 5 with ExprConstraintDescr

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

the class RuleParserTest method testSlidingWindow.

@Test
public void testSlidingWindow() throws Exception {
    final String text = "rule X when StockTick( symbol==\"ACME\") over window:length(10) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("symbol==\"ACME\"", fcd.getExpression());
    List<BehaviorDescr> behaviors = pattern.getBehaviors();
    assertNotNull(behaviors);
    assertEquals(1, behaviors.size());
    BehaviorDescr descr = behaviors.get(0);
    assertEquals("window", descr.getType());
    assertEquals("length", descr.getSubType());
    assertEquals("10", descr.getParameters().get(0));
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)65 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)57 Test (org.junit.Test)49 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)37 AndDescr (org.drools.compiler.lang.descr.AndDescr)27 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)23 Cheese (org.drools.compiler.Cheese)9 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)9 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)5 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)5 List (java.util.List)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)3 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)3 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)3 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Declaration (org.drools.core.rule.Declaration)3