Search in sources :

Example 1 with QueryNameConstraint

use of org.drools.core.rule.constraint.QueryNameConstraint in project drools by kiegroup.

the class SegmentUtilities method getQueryLiaNode.

private static LeftInputAdapterNode getQueryLiaNode(String queryName, ObjectTypeNode queryOtn) {
    for (ObjectSink sink : queryOtn.getObjectSinkPropagator().getSinks()) {
        AlphaNode alphaNode = (AlphaNode) sink;
        QueryNameConstraint nameConstraint = (QueryNameConstraint) alphaNode.getConstraint();
        if (queryName.equals(nameConstraint.getQueryName())) {
            return (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
        }
    }
    throw new RuntimeException("Unable to find query '" + queryName + "'");
}
Also used : QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ObjectSink(org.drools.core.reteoo.ObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 2 with QueryNameConstraint

use of org.drools.core.rule.constraint.QueryNameConstraint in project drools by kiegroup.

the class QueryBuilder method build.

public Pattern build(final RuleBuildContext context, final QueryDescr queryDescr) {
    ObjectType queryObjectType = ClassObjectType.DroolsQuery_ObjectType;
    final Pattern pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
    0, queryObjectType, null);
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, pattern, "name", null, true);
    final QueryNameConstraint constraint = new QueryNameConstraint(extractor, queryDescr.getName());
    PatternBuilder.registerReadAccessor(context, queryObjectType, "name", constraint);
    // adds appropriate constraint to the pattern
    pattern.addConstraint(constraint);
    ObjectType argsObjectType = ClassObjectType.DroolsQuery_ObjectType;
    InternalReadAccessor arrayExtractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, null, argsObjectType, "elements", null, true);
    QueryImpl query = ((QueryImpl) context.getRule());
    String[] params;
    String[] types;
    int numParams = queryDescr.getParameters().length;
    if (query.isAbductive()) {
        params = Arrays.copyOf(queryDescr.getParameters(), queryDescr.getParameters().length + 1);
        types = Arrays.copyOf(queryDescr.getParameterTypes(), queryDescr.getParameterTypes().length + 1);
    } else {
        params = queryDescr.getParameters();
        types = queryDescr.getParameterTypes();
    }
    Declaration[] declarations = new Declaration[params.length];
    Class<?> abductionReturnKlass = null;
    if (query.isAbductive()) {
        Abductive abductive = queryDescr.getTypedAnnotation(Abductive.class);
        abductionReturnKlass = abductive.target();
        params[numParams] = "";
        types[numParams] = abductionReturnKlass.getName();
    }
    int i = 0;
    try {
        for (i = 0; i < params.length; i++) {
            Declaration declr = pattern.addDeclaration(params[i]);
            // this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
            ArrayElementReader reader = new ArrayElementReader(arrayExtractor, i, context.getDialect().getTypeResolver().resolveType(types[i]));
            PatternBuilder.registerReadAccessor(context, argsObjectType, "elements", reader);
            declr.setReadAccessor(reader);
            declarations[i] = declr;
        }
        query.setParameters(declarations);
    } catch (ClassNotFoundException e) {
        context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve type '" + types[i] + " for parameter" + params[i]));
    }
    context.setPrefixPattern(pattern);
    if (query.isAbductive()) {
        String returnName = "";
        try {
            AnnotationDescr ann = queryDescr.getAnnotation(Abductive.class);
            Object[] argsVal = ((Object[]) ann.getValue("args"));
            String[] args = argsVal != null ? Arrays.copyOf(argsVal, argsVal.length, String[].class) : null;
            returnName = types[numParams];
            ObjectType objectType = new ClassObjectType(abductionReturnKlass, false);
            objectType = context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, (AbductiveQuery) query);
            ((AbductiveQuery) query).setReturnType(objectType, params, args, declarations);
        } catch (NoSuchMethodException e) {
            context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve abducible constructor for type : " + returnName + " with types " + Arrays.toString(types)));
        } catch (IllegalArgumentException e) {
            context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, e.getMessage()));
        }
    }
    return pattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ClassObjectType(org.drools.core.base.ClassObjectType) Abductive(org.drools.core.beliefsystem.abductive.Abductive) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) QueryImpl(org.drools.core.rule.QueryImpl) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) AbductiveQuery(org.drools.core.rule.AbductiveQuery)

Example 3 with QueryNameConstraint

use of org.drools.core.rule.constraint.QueryNameConstraint in project drools by kiegroup.

the class KiePackagesBuilder method addQueryPattern.

private void addQueryPattern(Query query, QueryImpl queryImpl, RuleContext ctx) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset is 0 by default
    0, ClassObjectType.DroolsQuery_ObjectType, null);
    InternalReadAccessor extractor = new LambdaReadAccessor(DroolsQuery.class, q -> ((DroolsQuery) q).getName());
    QueryNameConstraint constraint = new QueryNameConstraint(extractor, query.getName());
    pattern.addConstraint(constraint);
    queryImpl.getLhs().addChild(pattern);
    Variable<?>[] args = query.getArguments();
    Declaration[] declarations = new Declaration[args.length];
    for (int i = 0; i < args.length; i++) {
        int index = i;
        LambdaReadAccessor accessor = new LambdaReadAccessor(index, args[index].getType(), obj -> ((DroolsQuery) obj).getElements()[index]);
        declarations[i] = new Declaration(args[i].getName(), accessor, pattern, true);
        pattern.addDeclaration(declarations[i]);
        ctx.addQueryDeclaration(args[i], declarations[i]);
    }
    queryImpl.setParameters(declarations);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) 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) LambdaReadAccessor(org.drools.modelcompiler.constraints.LambdaReadAccessor) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint) DroolsQuery(org.drools.core.base.DroolsQuery)

Aggregations

QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)3 Declaration (org.drools.core.rule.Declaration)2 Pattern (org.drools.core.rule.Pattern)2 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 DroolsQuery (org.drools.core.base.DroolsQuery)1 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)1 Abductive (org.drools.core.beliefsystem.abductive.Abductive)1 AlphaNode (org.drools.core.reteoo.AlphaNode)1 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)1 ObjectSink (org.drools.core.reteoo.ObjectSink)1 AbductiveQuery (org.drools.core.rule.AbductiveQuery)1 QueryImpl (org.drools.core.rule.QueryImpl)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 WindowDeclaration (org.drools.core.rule.WindowDeclaration)1 ObjectType (org.drools.core.spi.ObjectType)1 AccumulatePattern (org.drools.model.AccumulatePattern)1 Constraint (org.drools.model.Constraint)1