use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class KiePackagesBuilder method getClassObjectType.
private ObjectType getClassObjectType(Class<?> patternClass) {
return objectTypeCache.computeIfAbsent(patternClass.getCanonicalName(), name -> {
boolean isEvent = false;
if (!name.startsWith("java.") && !patternClass.isPrimitive()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(patternClass.getPackage().getName(), this::createKiePackage);
TypeDeclaration typeDeclaration = pkg.getTypeDeclaration(patternClass);
if (typeDeclaration == null) {
typeDeclaration = createTypeDeclaration(patternClass);
pkg.addTypeDeclaration(typeDeclaration);
}
isEvent = typeDeclaration.getRole() == Role.Type.EVENT;
}
return new ClassObjectType(patternClass, isEvent);
});
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class TraitTest method testAlphaNodeSharing.
@Test
public void testAlphaNodeSharing() {
String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + " name : String\n" + "end\n" + "rule Init " + "when " + "then " + " don( new Entity(), Person.class ); " + "end\n" + "rule One when" + " $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + " $core: Entity( this isA Person ) " + "then " + "end " + "\n";
final KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession kSession = kbase.newKieSession();
assertEquals(3, kSession.fireAllRules());
NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
assertNotNull(otn);
assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class CepEspTest method testEventExpiration3.
@Test(timeout = 10000)
public void testEventExpiration3() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration3.drl");
Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
assertNotNull(node);
// the expiration policy @expires(10m) should override the temporal operator usage
assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PatternBuilder method buildXPathDescr.
private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
String expression = ((ExpressionDescr) descr).getExpression();
XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
if (xpathAnalysis.hasError()) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
return null;
}
XpathConstraint xpathConstraint = new XpathConstraint();
ObjectType objectType = pattern.getObjectType();
Class<?> patternClass = objectType.getClassType();
List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
backReferenceClasses.add(patternClass);
XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
pattern.setBackRefDeclarations(backRef);
ObjectType originalType = pattern.getObjectType();
ObjectType currentObjectType = originalType;
mvelCtx.setInXpath(true);
try {
for (XpathAnalysis.XpathPart part : xpathAnalysis) {
XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
// make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
if (xpathChunk == null) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
pattern.setObjectType(originalType);
return null;
}
if (part.getInlineCast() != null) {
try {
patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
} catch (ClassNotFoundException e) {
registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
return null;
}
part.addInlineCastConstraint(patternClass);
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
xpathChunk.setReturnedType(currentObjectType);
} else {
patternClass = xpathChunk.getReturnedClass();
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
}
pattern.setObjectType(currentObjectType);
backReferenceClasses.add(0, patternClass);
backRef.reset();
for (String constraint : part.getConstraints()) {
ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
if (result == null) {
continue;
}
for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
xpathChunk.addConstraint(c);
}
}
}
} finally {
mvelCtx.setInXpath(false);
pattern.setBackRefDeclarations(null);
pattern.setObjectType(originalType);
}
xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
if (descr instanceof BindingDescr) {
xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
}
return xpathConstraint;
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PatternBuilder method isDateType.
private boolean isDateType(RuleBuildContext context, Pattern pattern, String leftValue) {
Declaration declaration = pattern.getDeclarations().get(leftValue);
if (declaration != null && declaration.getExtractor() != null) {
return declaration.getValueType() == ValueType.DATE_TYPE;
}
if (pattern.getObjectType() instanceof FactTemplateObjectType) {
return ((FactTemplateObjectType) pattern.getObjectType()).getFactTemplate().getFieldTemplate(leftValue).getValueType() == ValueType.DATE_TYPE;
}
Class<?> clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
Class<?> fieldType = context.getPkg().getClassFieldAccessorStore().getFieldType(clazz, leftValue);
return fieldType != null && ValueType.isDateType(fieldType);
}
Aggregations