Search in sources :

Example 1 with QueryElement

use of org.drools.core.rule.QueryElement in project drools by kiegroup.

the class QueryElementNodeTest method testAttach.

@Test
public void testAttach() throws Exception {
    QueryElement queryElement = new QueryElement(null, null, new QueryArgument[0], null, null, false, false);
    final MockTupleSource source = new MockTupleSource(12);
    final QueryElementNode node = new QueryElementNode(18, source, queryElement, true, false, buildContext);
    assertEquals(18, node.getId());
    assertEquals(0, source.getAttached());
    node.attach(buildContext);
    assertEquals(1, source.getAttached());
}
Also used : QueryElement(org.drools.core.rule.QueryElement) Test(org.junit.Test)

Example 2 with QueryElement

use of org.drools.core.rule.QueryElement in project drools by kiegroup.

the class QueryElementBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final QueryElement qe = (QueryElement) rce;
    context.pushRuleComponent(qe);
    Pattern resultPattern = qe.getResultPattern();
    final int tupleIndex = context.getTupleSource() == null ? 0 : context.getTupleSource().getPathIndex() + 1;
    resultPattern.setTupleIndex(tupleIndex);
    resultPattern.setObjectIndex((context.getTupleSource() != null) ? context.getTupleSource().getObjectCount() : 0);
    context.setTupleSource(utils.attachNode(context, CoreComponentFactory.get().getNodeFactoryService().buildQueryElementNode(context.getNextNodeId(), context.getTupleSource(), qe, context.isTupleMemoryEnabled(), qe.isOpenQuery(), context)));
    context.popRuleComponent();
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryElement(org.drools.core.rule.QueryElement)

Example 3 with QueryElement

use of org.drools.core.rule.QueryElement in project drools by kiegroup.

the class QueryElementNodeTest method testAttach.

@Test
public void testAttach() throws Exception {
    QueryElement queryElement = new QueryElement(null, null, new QueryArgument[0], null, null, false, false);
    final MockTupleSource source = new MockTupleSource(12, buildContext);
    final QueryElementNode node = new QueryElementNode(18, source, queryElement, true, false, buildContext);
    assertEquals(18, node.getId());
    assertEquals(0, source.getAttached());
    node.attach(buildContext);
    assertEquals(1, source.getAttached());
}
Also used : MockTupleSource(org.drools.core.reteoo.MockTupleSource) QueryElementNode(org.drools.core.reteoo.QueryElementNode) QueryElement(org.drools.core.rule.QueryElement) Test(org.junit.Test)

Example 4 with QueryElement

use of org.drools.core.rule.QueryElement 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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) QueryArgument(org.drools.core.rule.QueryArgument) PatternDescr(org.drools.drl.ast.descr.PatternDescr) ArrayList(java.util.ArrayList) QueryElement(org.drools.core.rule.QueryElement) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ArrayList(java.util.ArrayList) List(java.util.List) Declaration(org.drools.core.rule.Declaration) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) Pattern(org.drools.core.rule.Pattern) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 5 with QueryElement

use of org.drools.core.rule.QueryElement in project drools by kiegroup.

the class KiePackagesBuilder method buildQueryPattern.

private RuleConditionElement buildQueryPattern(RuleContext ctx, QueryCallPattern queryPattern) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // tupleIndex is 0 by default
    0, // patternIndex is 0 by default
    0, ClassObjectType.ObjectArray_ObjectType, null);
    QueryArgument[] arguments = new QueryArgument[queryPattern.getArguments().length];
    List<Integer> varIndexList = new ArrayList<>();
    List<Declaration> requiredDeclarations = new ArrayList<>();
    for (int i = 0; i < queryPattern.getArguments().length; i++) {
        Argument arg = queryPattern.getArguments()[i];
        if (arg instanceof Variable) {
            Variable var = ((Variable) arg);
            Declaration decl = ctx.getDeclaration(var);
            if (decl != null) {
                requiredDeclarations.add(decl);
                arguments[i] = new QueryArgument.Declr(decl);
            } else {
                ArrayElementReader reader = new ArrayElementReader(new SelfReferenceClassFieldReader(Object[].class), i, arg.getType());
                Declaration varDeclaration = pattern.addDeclaration(var.getName());
                varDeclaration.setReadAccessor(reader);
                ctx.addDeclaration(var, varDeclaration);
                arguments[i] = QueryArgument.VAR;
                varIndexList.add(i);
            }
        } else if (arg instanceof Value) {
            arguments[i] = new QueryArgument.Literal(((Value) arg).getValue());
        } else {
            throw new UnsupportedOperationException();
        }
    }
    return new QueryElement(pattern, queryPattern.getQuery().getName(), arguments, varIndexList.stream().mapToInt(i -> i).toArray(), requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), queryPattern.isOpen(), // TODO: query.isAbductive() );
    false);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) QueryArgument(org.drools.core.rule.QueryArgument) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) QueryArgument(org.drools.core.rule.QueryArgument) Argument(org.drools.model.Argument) ArrayList(java.util.ArrayList) QueryElement(org.drools.core.rule.QueryElement) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) AbstractConstraint(org.drools.modelcompiler.constraints.AbstractConstraint) Constraint(org.drools.model.Constraint) CombinedConstraint(org.drools.modelcompiler.constraints.CombinedConstraint) AbstractSingleConstraint(org.drools.model.constraints.AbstractSingleConstraint) DSL.entryPoint(org.drools.model.DSL.entryPoint) SalienceInteger(org.drools.core.base.SalienceInteger) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) Value(org.drools.model.Value) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

QueryElement (org.drools.core.rule.QueryElement)5 Pattern (org.drools.core.rule.Pattern)3 ArrayList (java.util.ArrayList)2 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)2 Declaration (org.drools.core.rule.Declaration)2 QueryArgument (org.drools.core.rule.QueryArgument)2 Test (org.junit.Test)2 List (java.util.List)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 SalienceInteger (org.drools.core.base.SalienceInteger)1 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)1 MockTupleSource (org.drools.core.reteoo.MockTupleSource)1 QueryElementNode (org.drools.core.reteoo.QueryElementNode)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 WindowDeclaration (org.drools.core.rule.WindowDeclaration)1 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)1 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)1 ObjectType (org.drools.core.spi.ObjectType)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1