Search in sources :

Example 1 with AcceptsClassObjectType

use of org.drools.core.spi.AcceptsClassObjectType in project drools by kiegroup.

the class PatternBuilder method buildPattern.

private Pattern buildPattern(RuleBuildContext context, PatternDescr patternDescr, ObjectType objectType) {
    String patternIdentifier = patternDescr.getIdentifier();
    boolean duplicateBindings = patternIdentifier != null && objectType instanceof ClassObjectType && context.getDeclarationResolver().isDuplicated(context.getRule(), patternIdentifier, objectType.getClassName());
    Pattern pattern;
    if (!StringUtils.isEmpty(patternIdentifier) && !duplicateBindings) {
        pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
        0, objectType, patternIdentifier, patternDescr.isInternalFact(context));
        if (objectType instanceof ClassObjectType) {
            // make sure PatternExtractor is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, (AcceptsClassObjectType) pattern.getDeclaration().getExtractor());
        }
    } else {
        pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
        0, objectType, null);
    }
    pattern.setPassive(patternDescr.isPassive(context));
    if (ClassObjectType.Match_ObjectType.isAssignableFrom(pattern.getObjectType())) {
        PropertyHandler handler = PropertyHandlerFactory.getPropertyHandler(RuleTerminalNodeLeftTuple.class);
        if (handler == null) {
            PropertyHandlerFactoryFixer.getPropertyHandlerClass().put(RuleTerminalNodeLeftTuple.class, new ActivationPropertyHandler());
        }
    }
    // adding the newly created pattern to the build stack this is necessary in case of local declaration usage
    context.getDeclarationResolver().pushOnBuildStack(pattern);
    if (duplicateBindings) {
        processDuplicateBindings(patternDescr.isUnification(), patternDescr, pattern, patternDescr, "this", patternDescr.getIdentifier(), context);
    }
    return pattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ActivationPropertyHandler(org.drools.core.base.mvel.ActivationPropertyHandler) PropertyHandler(org.mvel2.integration.PropertyHandler) ActivationPropertyHandler(org.drools.core.base.mvel.ActivationPropertyHandler)

Example 2 with AcceptsClassObjectType

use of org.drools.core.spi.AcceptsClassObjectType in project drools by kiegroup.

the class PatternBuilderForAbductiveQuery method postBuild.

@Override
protected void postBuild(RuleBuildContext context, QueryDescr queryDescr, QueryImpl query, String[] params, String[] types, Declaration[] declarations) {
    int numParams = queryDescr.getParameters().length;
    String returnName = "";
    try {
        AnnotationDescr ann = queryDescr.getAnnotation(query.getAbductiveAnnotationClass());
        Object[] argsVal = ((Object[]) ann.getValue("args"));
        String[] args = argsVal != null ? Arrays.copyOf(argsVal, argsVal.length, String[].class) : null;
        returnName = types[numParams];
        Class<?> abductionReturnKlass = query.getAbductionClass(annotationClass -> getTypedAnnotation(queryDescr, annotationClass));
        ObjectType objectType = context.getPkg().wireObjectType(new ClassObjectType(abductionReturnKlass, false), (AcceptsClassObjectType) query);
        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()));
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) ClassObjectType(org.drools.core.base.ClassObjectType) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 3 with AcceptsClassObjectType

use of org.drools.core.spi.AcceptsClassObjectType in project drools by kiegroup.

the class JavaRuleBuilderHelper method createConsequenceContext.

public static Map<String, Object> createConsequenceContext(final RuleBuildContext context, String consequenceName, String className, String consequenceText, Map<String, Declaration> decls, final BoundIdentifiers usedIdentifiers) {
    final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
    String[] declrStr = new String[declarations.length];
    int j = 0;
    for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
        declrStr[j] = str;
        declarations[j++] = decls.get(str);
    }
    Arrays.sort(declarations, RuleTerminalNode.SortDeclarations.instance);
    for (int i = 0; i < declrStr.length; i++) {
        declrStr[i] = declarations[i].getIdentifier();
    }
    context.getRule().setRequiredDeclarationsForConsequence(consequenceName, declrStr);
    final Map<String, Object> map = createVariableContext(className, consequenceText, context, declarations, null, usedIdentifiers.getGlobals());
    map.put("consequenceName", consequenceName);
    // final int[] indexes = new int[declarations.length];
    final Integer[] indexes = new Integer[declarations.length];
    final Boolean[] notPatterns = new Boolean[declarations.length];
    for (int i = 0, length = declarations.length; i < length; i++) {
        indexes[i] = i;
        notPatterns[i] = (declarations[i].getExtractor() instanceof AcceptsClassObjectType) ? Boolean.FALSE : Boolean.TRUE;
        if (indexes[i] == -1) {
            context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Internal Error : Unable to find declaration in list while generating the consequence invoker"));
        }
    }
    map.put("indexes", indexes);
    map.put("notPatterns", notPatterns);
    return map;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) Declaration(org.drools.core.rule.Declaration)

Example 4 with AcceptsClassObjectType

use of org.drools.core.spi.AcceptsClassObjectType in project drools by kiegroup.

the class PatternBuilder method buildPattern.

private Pattern buildPattern(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, ObjectType objectType) {
    String patternIdentifier = patternDescr.getIdentifier();
    boolean duplicateBindings = patternIdentifier != null && objectType instanceof ClassObjectType && context.getDeclarationResolver().isDuplicated(context.getRule(), patternIdentifier, objectType.getClassName());
    Pattern pattern;
    if (!StringUtils.isEmpty(patternIdentifier) && !duplicateBindings) {
        pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
        0, // offset is 0 by default
        0, objectType, patternIdentifier, isInternalFact(patternDescr, context));
        if (objectType instanceof ClassObjectType) {
            // make sure PatternExtractor is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().wireObjectType(objectType, (AcceptsClassObjectType) pattern.getDeclaration().getExtractor());
        }
    } else {
        pattern = new Pattern(context.getNextPatternId(), // tupleIndex is 0 by default
        0, // patternIndex is 0 by default
        0, objectType, null);
    }
    pattern.setPassive(isPassivePattern(patternDescr, context));
    // adding the newly created pattern to the build stack this is necessary in case of local declaration usage
    context.getDeclarationResolver().pushOnBuildStack(pattern);
    if (duplicateBindings) {
        processDuplicateBindings(patternDescr.isUnification(), patternDescr, xpathStartDeclaration, pattern, patternDescr, "this", patternDescr.getIdentifier(), context);
    }
    return pattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType)

Example 5 with AcceptsClassObjectType

use of org.drools.core.spi.AcceptsClassObjectType in project drools by kiegroup.

the class ClassFieldAccessorStore method wireObjectType.

public ObjectType wireObjectType(ObjectType objectType, AcceptsClassObjectType target) {
    if (!(objectType instanceof ClassObjectType)) {
        return objectType;
    }
    AccessorKey key = new AccessorKey(objectType.getClassName(), objectType.isEvent() ? "$$DROOLS__isEvent__" : null, AccessorKey.AccessorType.ClassObjectType);
    ClassObjectTypeLookupEntry entry = (ClassObjectTypeLookupEntry) this.lookup.computeIfAbsent(key, k -> new ClassObjectTypeLookupEntry(cache.getClassObjectType((ClassObjectType) objectType, false)));
    if (target != null) {
        target.setClassObjectType(entry.getClassObjectType());
    }
    return entry.getClassObjectType();
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ObjectType(org.drools.core.spi.ObjectType) ObjectOutput(java.io.ObjectOutput) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) AccessorType(org.drools.core.base.AccessorKey.AccessorType) AccessorKey(org.drools.core.base.AccessorKey) Map(java.util.Map) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactField(org.kie.api.definition.type.FactField) Externalizable(java.io.Externalizable) Collection(java.util.Collection) WriteAccessor(org.drools.core.spi.WriteAccessor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BaseClassFieldWriter(org.drools.core.base.BaseClassFieldWriter) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) ClassWireable(org.drools.core.spi.ClassWireable) IOException(java.io.IOException) Field(java.lang.reflect.Field) UncheckedIOException(java.io.UncheckedIOException) CoreComponentsBuilder(org.drools.core.base.CoreComponentsBuilder) AcceptsReadAccessor(org.drools.core.spi.AcceptsReadAccessor) Entry(java.util.Map.Entry) ObjectInput(java.io.ObjectInput) BaseClassFieldReader(org.drools.core.base.BaseClassFieldReader) ReadAccessorSupplier(org.drools.core.base.ReadAccessorSupplier) Collections(java.util.Collections) ClassFieldInspector(org.drools.core.base.ClassFieldInspector) ClassObjectType(org.drools.core.base.ClassObjectType) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) AccessorKey(org.drools.core.base.AccessorKey)

Aggregations

AcceptsClassObjectType (org.drools.core.spi.AcceptsClassObjectType)6 ClassObjectType (org.drools.core.base.ClassObjectType)4 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)3 Declaration (org.drools.core.rule.Declaration)2 Pattern (org.drools.core.rule.Pattern)2 ObjectType (org.drools.core.spi.ObjectType)2 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 UncheckedIOException (java.io.UncheckedIOException)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AccessorKey (org.drools.core.base.AccessorKey)1 AccessorType (org.drools.core.base.AccessorKey.AccessorType)1 BaseClassFieldReader (org.drools.core.base.BaseClassFieldReader)1