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;
}
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()));
}
}
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;
}
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;
}
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();
}
Aggregations