use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testNumbers.
@Test
public void testNumbers() throws Exception {
// test boolean
createLiteralRule(new ExprConstraintDescr("booleanPrimitive == true "));
// test boolean
createLiteralRule(new ExprConstraintDescr("booleanPrimitive == false "));
// test char
createLiteralRule(new ExprConstraintDescr("charPrimitive == 'a' "));
createLiteralRule(new ExprConstraintDescr("charPrimitive == \"a\" "));
// test byte
createLiteralRule(new ExprConstraintDescr("bytePrimitive == 1 "));
createLiteralRule(new ExprConstraintDescr("bytePrimitive == 0 "));
createLiteralRule(new ExprConstraintDescr("bytePrimitive == -1 "));
// test short
createLiteralRule(new ExprConstraintDescr("shortPrimitive == 1 "));
createLiteralRule(new ExprConstraintDescr("shortPrimitive == 0 "));
createLiteralRule(new ExprConstraintDescr("shortPrimitive == -1 "));
// test int
createLiteralRule(new ExprConstraintDescr("intPrimitive == 1"));
createLiteralRule(new ExprConstraintDescr("intPrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("intPrimitive == -1"));
// test long
createLiteralRule(new ExprConstraintDescr("longPrimitive == 1"));
createLiteralRule(new ExprConstraintDescr("longPrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("longPrimitive == -1"));
// test float
createLiteralRule(new ExprConstraintDescr("floatPrimitive == 1.1"));
createLiteralRule(new ExprConstraintDescr("floatPrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("floatPrimitive == -1.1"));
// test double
createLiteralRule(new ExprConstraintDescr("doublePrimitive == 1.1"));
createLiteralRule(new ExprConstraintDescr("doublePrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("doublePrimitive == -1.1"));
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class QueryBuilderTest method testQuery.
@Test
public void testQuery() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final QueryDescr queryDescr = new QueryDescr("query1");
queryDescr.addParameter("String", "$type");
packageDescr.addRule(queryDescr);
final AndDescr lhs = new AndDescr();
queryDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
pattern.addConstraint(new ExprConstraintDescr("type == $type"));
// Another query, no parameters
QueryDescr queryDescr2 = new QueryDescr("query2");
packageDescr.addRule(queryDescr2);
AndDescr lhs2 = new AndDescr();
queryDescr2.setLhs(lhs2);
PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
lhs2.addDescr(pattern2);
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
final KieSession session = kbase.newKieSession();
session.insert(new Cheese("stilton", 15));
QueryResults results = session.getQueryResults("query1", "stilton");
assertEquals(1, results.size());
Object object = results.iterator().next().get("stilton");
assertEquals(new Cheese("stilton", 15), object);
results = session.getQueryResults("query1", new Object[] { "cheddar" });
assertEquals(0, results.size());
session.insert(new Cheese("dolcelatte", 20));
results = session.getQueryResults("query2", new Object[] {});
assertEquals(2, results.size());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parseConstraint.
private void parseConstraint(final RuleModel m, final FactPattern factPattern, final ConditionalElementDescr constraint, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
for (BaseDescr descr : constraint.getDescrs()) {
if (descr instanceof ExprConstraintDescr) {
ExprConstraintDescr exprConstraint = (ExprConstraintDescr) descr;
Expr expr = parseExpr(exprConstraint.getExpression(), isJavaDialect, boundParams, dmo);
factPattern.addConstraint(expr.asFieldConstraint(m, factPattern));
}
}
}
use of org.drools.compiler.lang.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<Declaration>();
for (String identifier : analysisResult.getIdentifiers()) {
Declaration declaration = declarationResolver.getDeclaration(identifier);
if (declaration != null) {
declarations.add(declaration);
}
}
if (declarations.size() == analysisResult.getIdentifiers().size()) {
arguments[pos] = new QueryArgument.Expression(declarations, expression, getParserContext(context));
} else {
arguments[pos] = getLiteralQueryArgument(context, base, result);
}
}
}
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class QueryElementBuilder method build.
@SuppressWarnings("unchecked")
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<Declaration>();
ObjectType argsObjectType = ClassObjectType.ObjectArray_ObjectType;
InternalReadAccessor arrayReader = new SelfReferenceClassFieldReader(Object[].class);
Pattern pattern = new Pattern(context.getNextPatternId(), 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<Integer>();
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