Search in sources :

Example 41 with ExprConstraintDescr

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

the class QueryCall method buildPattern.

@Override
public void buildPattern() {
    MethodCallExpr callMethod = new MethodCallExpr(new NameExpr(queryDef), QUERY_INVOCATION_CALL);
    callMethod.addArgument("" + !pattern.isQuery());
    List<QueryParameter> parameters = packageModel.getQueryDefWithType().get(queryDef).getContext().getQueryParameters();
    for (int i = 0; i < parameters.size(); i++) {
        String queryName = context.getQueryName().orElseThrow(RuntimeException::new);
        ExprConstraintDescr variableExpr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(i);
        String variableName = variableExpr.toString();
        int unifPos = variableName.indexOf(":=");
        if (unifPos > 0) {
            variableName = variableName.substring(0, unifPos).trim();
        }
        Optional<String> unificationId = context.getUnificationId(variableName);
        int queryIndex = i + 1;
        Expression parameterCall = unificationId.map(name -> (Expression) new NameExpr(toVar(name))).orElseGet(() -> new MethodCallExpr(new NameExpr(queryName), QueryGenerator.toQueryArg(queryIndex)));
        callMethod.addArgument(parameterCall);
    }
    context.addExpression(callMethod);
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Expression(org.drools.javaparser.ast.expr.Expression) QUERY_INVOCATION_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.QUERY_INVOCATION_CALL) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) NameExpr(org.drools.javaparser.ast.expr.NameExpr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DSLNode(org.drools.modelcompiler.builder.generator.visitor.DSLNode) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) Optional(java.util.Optional) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) Expression(org.drools.javaparser.ast.expr.Expression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 42 with ExprConstraintDescr

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

the class PredicateHandler 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();
    if (expression == null || expression.trim().equals("")) {
        throw new SAXParseException("<predicate> must have some content", parser.getLocator());
    }
    final PatternDescr patternDescr = (PatternDescr) parser.getParent();
    ExprConstraintDescr expr = new ExprConstraintDescr("eval(" + expression + ")");
    patternDescr.addConstraint(expr);
    return expr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 43 with ExprConstraintDescr

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

the class PatternDescrBuilderImpl method constraint.

public PatternDescrBuilder<P> constraint(String constraint, boolean positional) {
    ExprConstraintDescr constr = new ExprConstraintDescr(constraint);
    constr.setType(positional ? ExprConstraintDescr.Type.POSITIONAL : ExprConstraintDescr.Type.NAMED);
    constr.setPosition(descr.getConstraint().getDescrs().size());
    constr.setResource(descr.getResource());
    descr.addConstraint(constr);
    return this;
}
Also used : ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 44 with ExprConstraintDescr

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

the class PatternDescrBuilderImpl method constraint.

public PatternDescrBuilder<P> constraint(String constraint) {
    ExprConstraintDescr constr = new ExprConstraintDescr(constraint);
    constr.setType(ExprConstraintDescr.Type.NAMED);
    constr.setPosition(descr.getConstraint().getDescrs().size());
    constr.setResource(descr.getResource());
    descr.addConstraint(constr);
    return this;
}
Also used : ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 45 with ExprConstraintDescr

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

the class RuleBuilderTest method testBuildBigIntegerLiteralConstraint.

@Test
public void testBuildBigIntegerLiteralConstraint() 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.BigInteger", "$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.BigInteger. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigInteger);
    }
}
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) BigInteger(java.math.BigInteger) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) 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