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);
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations