Search in sources :

Example 31 with ExprConstraintDescr

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

the class PatternBuilder method buildXPathDescr.

private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
    String expression = ((ExpressionDescr) descr).getExpression();
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
        return null;
    }
    XpathConstraint xpathConstraint = new XpathConstraint();
    ObjectType objectType = pattern.getObjectType();
    Class<?> patternClass = objectType.getClassType();
    List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
    backReferenceClasses.add(patternClass);
    XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
    pattern.setBackRefDeclarations(backRef);
    ObjectType originalType = pattern.getObjectType();
    ObjectType currentObjectType = originalType;
    mvelCtx.setInXpath(true);
    try {
        for (XpathAnalysis.XpathPart part : xpathAnalysis) {
            XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
            // make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
            if (xpathChunk == null) {
                registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
                pattern.setObjectType(originalType);
                return null;
            }
            if (part.getInlineCast() != null) {
                try {
                    patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
                } catch (ClassNotFoundException e) {
                    registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
                    return null;
                }
                part.addInlineCastConstraint(patternClass);
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
                xpathChunk.setReturnedType(currentObjectType);
            } else {
                patternClass = xpathChunk.getReturnedClass();
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
            }
            pattern.setObjectType(currentObjectType);
            backReferenceClasses.add(0, patternClass);
            backRef.reset();
            for (String constraint : part.getConstraints()) {
                ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
                if (result == null) {
                    continue;
                }
                for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
                    xpathChunk.addConstraint(c);
                }
            }
        }
    } finally {
        mvelCtx.setInXpath(false);
        pattern.setBackRefDeclarations(null);
        pattern.setObjectType(originalType);
    }
    xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
    if (descr instanceof BindingDescr) {
        xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
    }
    return xpathConstraint;
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) ArrayList(java.util.ArrayList) XpathBackReference(org.drools.core.rule.XpathBackReference) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ExpressionDescr(org.drools.compiler.lang.descr.ExpressionDescr) XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 32 with ExprConstraintDescr

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

the class PatternBuilder method processConstraintsAndBinds.

/**
 * Process all constraints and bindings on this pattern
 */
protected void processConstraintsAndBinds(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern) {
    MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext().setRuleContext(context);
    for (BaseDescr b : patternDescr.getDescrs()) {
        String expression;
        boolean isPositional = false;
        if (b instanceof BindingDescr) {
            BindingDescr bind = (BindingDescr) b;
            expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
        } else if (b instanceof ExprConstraintDescr) {
            ExprConstraintDescr descr = (ExprConstraintDescr) b;
            expression = descr.getExpression();
            isPositional = descr.getType() == ExprConstraintDescr.Type.POSITIONAL;
        } else {
            expression = b.getText();
        }
        ConstraintConnectiveDescr result = parseExpression(context, patternDescr, b, expression);
        if (result == null) {
            return;
        }
        isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
        if (isPositional) {
            processPositional(context, patternDescr, pattern, (ExprConstraintDescr) b);
        } else {
            // need to build the actual constraint
            List<Constraint> constraints = build(context, patternDescr, pattern, result, mvelCtx);
            pattern.addConstraints(constraints);
        }
    }
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
        for (String field : context.getRuleDescr().lookAheadFieldsOfIdentifier(patternDescr)) {
            addFieldToPatternWatchlist(pattern, typeDeclaration, field);
        }
    }
    combineConstraints(context, pattern, mvelCtx);
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) MVELDumper(org.drools.compiler.lang.MVELDumper) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 33 with ExprConstraintDescr

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

the class RestrictionConnectiveHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    Object op = parser.getParent();
    ConnectiveDescr c = (ConnectiveDescr) parser.getCurrent();
    if (op instanceof PatternDescr) {
        StringBuilder sb = new StringBuilder();
        c.buildExpression(sb);
        ExprConstraintDescr expr = new ExprConstraintDescr();
        expr.setExpression(sb.toString());
        final PatternDescr patternDescr = (PatternDescr) op;
        patternDescr.addConstraint(expr);
    } else {
        ConnectiveDescr p = (ConnectiveDescr) op;
        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) RestrictionConnectiveDescr(org.drools.compiler.lang.descr.RestrictionConnectiveDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 34 with ExprConstraintDescr

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

the class XmlPackageReaderTest method testParseSimpleRule.

@Test
public void testParseSimpleRule() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_SimpleRule1.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.List", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.compiler.Person", ((ImportDescr) imports.get(1)).getTarget());
    RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("simple_rule1", ruleDescr.getName());
    AndDescr lhs = ruleDescr.getLhs();
    PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    ExprConstraintDescr expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == \"darth\"", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(1);
    assertEquals("simple_rule2", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || == -3.5", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(2);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || (!= 7.0 && != -70)", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(3);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == $s", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(4);
    assertEquals("simple_rule4", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || (age == 35 || (!= 7.0 && != -70))", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(5);
    assertEquals("simple_rule5", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || ((age != 34) && (age != 37) && (name != \"yoda\"))", expr.getExpression());
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 35 with ExprConstraintDescr

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

the class KnowledgeBuilderTest method testLiteral.

@Test
public void testLiteral() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == 'stilton'"));
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) 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