use of org.drools.drl.ast.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.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class QueryCall method getVariableName.
private String getVariableName(int i) {
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();
}
return variableName;
}
use of org.drools.drl.ast.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);
}
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class QueryElementBuilder method processPositional.
private void processPositional(RuleBuildContext context, QueryImpl query, Declaration[] params, QueryArgument[] arguments, List<Declaration> requiredDeclarations, InternalReadAccessor arrayReader, Pattern pattern, BaseDescr base, String expression, ConstraintConnectiveDescr result) {
int pos = ((ExprConstraintDescr) base).getPosition();
if (pos >= arguments.length) {
context.addError(new DescrBuildError(context.getParentDescr(), base, null, "Unable to parse query '" + query.getName() + "', as postion " + pos + " for expression '" + expression + "' does not exist on query size " + arguments.length));
return;
}
boolean isVariable = isVariable(expression);
DeclarationScopeResolver declarationResolver = context.getDeclarationResolver();
Declaration declr = isVariable ? declarationResolver.getDeclaration(expression) : null;
if (declr != null) {
// it exists, so it's an input
requiredDeclarations.add(declr);
arguments[pos] = new QueryArgument.Declr(declr);
} else if (isVariable && expression.indexOf('.') < 0) {
arguments[pos] = getVariableQueryArgument(arrayReader, params, pos, pattern, expression);
} else {
// it's an expression and thus an input
AnalysisResult analysisResult = analyzeExpression(context, base, expression);
if (analysisResult == null || analysisResult.getIdentifiers().isEmpty()) {
arguments[pos] = getLiteralQueryArgument(context, base, result);
} else {
List<Declaration> declarations = new ArrayList<>();
for (String identifier : analysisResult.getIdentifiers()) {
Declaration declaration = declarationResolver.getDeclaration(identifier);
if (declaration != null) {
declarations.add(declaration);
}
}
if (declarations.size() == analysisResult.getIdentifiers().size()) {
arguments[pos] = ConstraintBuilder.get().buildExpressionQueryArgument(context, declarations, expression);
} else {
arguments[pos] = getLiteralQueryArgument(context, base, result);
}
}
}
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class QueryElementBuilder method build.
public RuleConditionElement build(RuleBuildContext context, BaseDescr descr, QueryImpl query) {
PatternDescr patternDescr = (PatternDescr) descr;
Declaration[] params = query.getParameters();
List<BaseDescr> args = (List<BaseDescr>) patternDescr.getDescrs();
List<Declaration> requiredDeclarations = new ArrayList<>();
ObjectType argsObjectType = ClassObjectType.ObjectArray_ObjectType;
InternalReadAccessor arrayReader = new SelfReferenceClassFieldReader(Object[].class);
Pattern pattern = new Pattern(context.getNextPatternId(), // tupleIndex is 0 by default
0, // patternIndex is 0 by default
0, argsObjectType, null);
if (!StringUtils.isEmpty(patternDescr.getIdentifier())) {
if (query.isAbductive()) {
Declaration declr = context.getDeclarationResolver().getDeclaration(patternDescr.getIdentifier());
if (declr != null && !patternDescr.isUnification()) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Duplicate declaration " + patternDescr.getIdentifier() + ", unable to bind abducted value"));
}
} else {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query binding is not supported by non-abductive queries : " + patternDescr.getIdentifier()));
}
}
boolean addAbductiveReturnArgument = query.isAbductive() && !StringUtils.isEmpty(patternDescr.getIdentifier()) && args.size() < params.length;
if (addAbductiveReturnArgument) {
ExprConstraintDescr extraDescr = new ExprConstraintDescr(patternDescr.getIdentifier());
extraDescr.setPosition(patternDescr.getConstraint().getDescrs().size());
extraDescr.setType(ExprConstraintDescr.Type.POSITIONAL);
args.add(extraDescr);
}
QueryArgument[] arguments = new QueryArgument[params.length];
// Deal with the constraints, both positional and bindings
for (BaseDescr base : args) {
String expression = null;
boolean isPositional = false;
boolean isBinding = false;
BindingDescr bind = null;
ConstraintConnectiveDescr result = null;
if (base instanceof BindingDescr) {
bind = (BindingDescr) base;
expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
isBinding = true;
} else {
if (base instanceof ExprConstraintDescr) {
ExprConstraintDescr ecd = (ExprConstraintDescr) base;
expression = ecd.getExpression();
isPositional = ecd.getType() == ExprConstraintDescr.Type.POSITIONAL;
} else {
expression = base.getText();
}
result = parseExpression(context, patternDescr, expression);
if (result == null) {
// error, can't parse expression.
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to parse constraint: \n" + expression));
continue;
}
isBinding = result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr;
if (isBinding) {
bind = (BindingDescr) result.getDescrs().get(0);
}
}
if ((!isPositional) && (!isBinding)) {
// error, can't have non binding slots.
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query's must use positional or bindings, not field constraints:\n" + expression));
} else if (isPositional && isBinding) {
// error, can't have positional binding slots.
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query's can't use positional bindings:\n" + expression));
} else if (isPositional) {
processPositional(context, query, params, arguments, requiredDeclarations, arrayReader, pattern, base, expression, result);
} else {
// it is binding
processBinding(context, descr, params, arguments, requiredDeclarations, arrayReader, pattern, bind);
}
}
List<Integer> varIndexList = new ArrayList<>();
for (int i = 0; i < arguments.length; i++) {
if (!(arguments[i] instanceof QueryArgument.Declr)) {
if (arguments[i] instanceof QueryArgument.Var) {
varIndexList.add(i);
}
continue;
}
Class actual = ((QueryArgument.Declr) arguments[i]).getArgumentClass();
Declaration formalArgument = query.getParameters()[i];
Class formal = formalArgument.getDeclarationClass();
// input argument require a broader type, while output types require a narrower type, so we check for both.
if (!ClassUtils.isTypeCompatibleWithArgumentType(actual, formal) && !ClassUtils.isTypeCompatibleWithArgumentType(formal, actual)) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query is being invoked with known argument of type " + actual + " at position " + i + ", but the expected query argument is of type " + formal));
}
}
return new QueryElement(pattern, query.getName(), arguments, toIntArray(varIndexList), requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), !patternDescr.isQuery(), query.isAbductive());
}
Aggregations