use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PatternBuilder method processPositional.
protected void processPositional(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final ExprConstraintDescr descr) {
if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
Class<?> klazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(klazz);
if (tDecl == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + klazz + "\n");
return;
}
ClassDefinition clsDef = tDecl.getTypeClassDef();
if (clsDef == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
return;
}
FieldDefinition field = clsDef.getField(descr.getPosition());
if (field == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
return;
}
String expr = descr.getExpression();
boolean isSimpleIdentifier = isIdentifier(expr);
if (isSimpleIdentifier) {
// create a binding
BindingDescr binder = new BindingDescr();
binder.setUnification(true);
binder.setExpression(field.getName());
binder.setVariable(descr.getExpression());
buildRuleBindings(context, patternDescr, pattern, binder);
} else {
// create a constraint
build(context, patternDescr, pattern, descr, field.getName() + " == " + descr.getExpression());
}
}
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PatternBuilder method build.
protected List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, ConstraintConnectiveDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
List<Constraint> constraints = new ArrayList<Constraint>();
List<BaseDescr> initialDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
for (BaseDescr d : initialDescrs) {
boolean isXPath = isXPathDescr(d);
if (isXPath && pattern.hasXPath()) {
registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
return constraints;
}
Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, pattern, d, mvelCtx) : buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
if (constraint != null) {
Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
if (declCorrXpath == null) {
constraints.add(constraint);
} else {
// A constraint is using a declration bound to an xpath in the same pattern
// Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
Pattern modifiedPattern = pattern.clone();
modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
constraint = buildCcdDescr(context, patternDescr, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
if (constraint != null) {
pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
}
}
}
}
if (descr.getDescrs().size() > initialDescrs.size()) {
// The initial build process may have generated other constraint descrs.
// This happens when null-safe references or inline-casts are used
// These additional constraints must be built, and added as
List<BaseDescr> additionalDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
additionalDescrs.removeAll(initialDescrs);
if (!additionalDescrs.isEmpty()) {
List<Constraint> additionalConstraints = new ArrayList<Constraint>();
for (BaseDescr d : additionalDescrs) {
Constraint constraint = buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
if (constraint != null) {
additionalConstraints.add(constraint);
}
}
constraints.addAll(0, additionalConstraints);
}
}
return constraints;
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PatternBuilder method getSettableProperties.
protected List<String> getSettableProperties(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
ObjectType patternType = pattern.getObjectType();
if (!(patternType instanceof ClassObjectType)) {
return null;
}
Class<?> patternClass = patternType.getClassType();
TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
if (!typeDeclaration.isPropertyReactive()) {
registerDescrBuildError(context, patternDescr, "Wrong usage of @" + Watch.class.getSimpleName() + " annotation on class " + patternClass.getName() + " that is not annotated as @PropertyReactive");
}
typeDeclaration.setTypeClass(patternClass);
return typeDeclaration.getAccessibleProperties();
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class MarshallingTest method testDynamicEmptyRule.
@Test
public void testDynamicEmptyRule() throws Exception {
String rule1 = "package org.drools.compiler.test;\n";
rule1 += "global java.util.List list\n";
rule1 += "rule \"Rule 1\"\n";
rule1 += "when\n";
rule1 += "then\n";
rule1 += " list.add( \"fired1\" );\n";
rule1 += "end";
String rule2 = "package org.drools.compiler.test;\n";
rule2 += "global java.util.List list\n";
rule2 += "rule \"Rule 2\"\n";
rule2 += "when\n";
rule2 += "then\n";
rule2 += " list.add( \"fired2\" );\n";
rule2 += "end";
InternalKnowledgeBase kBase = (InternalKnowledgeBase) loadKnowledgeBaseFromString(rule1);
// Make sure the rete node map is created correctly
Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
// Make sure the rete node map is created correctly
assertEquals(2, nodes.size());
assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
KieSession session1 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, false);
session1.fireAllRules();
assertEquals(1, ((List) session1.getGlobal("list")).size());
KieSession session2 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session1, kBase, false);
session.dispose();
session1.dispose();
Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(rule2);
kBase.addPackages(kpkgs);
session2.fireAllRules();
System.out.println(session2.getGlobal("list"));
assertEquals(2, ((List) session2.getGlobal("list")).size());
assertEquals("fired1", ((List) session2.getGlobal("list")).get(0));
assertEquals("fired2", ((List) session2.getGlobal("list")).get(1));
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class ParallelEvaluationTest method test.
@Test(timeout = 10000L)
public void test() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
for (int i = 0; i < 10; i++) {
sb.append(getRule(i, ""));
}
KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Integer.class));
assertTrue(((CompositePartitionAwareObjectSinkAdapter) otn.getObjectSinkPropagator()).isHashed());
KieSession ksession = kbase.newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(i);
ksession.insert("" + i);
}
ksession.fireAllRules();
assertEquals(10, list.size());
}
Aggregations