use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testNull.
@Test
public void testNull() {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testReload.
@Test
public void testReload() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addGlobal("map", Map.class);
final KieSession workingMemory = kBase.newKieSession();
final HashMap map = new HashMap();
workingMemory.setGlobal("map", map);
final LeftTupleImpl tuple = new MockTuple(new HashMap());
tuple.setLeftTupleSink(new RuleTerminalNode(1, new CompositeObjectSinkAdapterTest.MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase)));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
pkg.removeRule(rule);
// Make sure the compiled classes are also removed
assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
builder.addPackage(packageDescr);
pkg = builder.getPackage(packageDescr.getName());
rule = pkg.getRule("rule-1");
knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(2), map.get("value"));
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testEval.
@Test
public void testEval() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
lhs.addDescr(evalDescr);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
final List list = invoker.getMethodBytecode();
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method createRule.
private RuleImpl createRule(final ConditionalElementDescr ceDescr, final KnowledgeBuilderImpl builder, final String consequence) throws Exception {
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
lhs.addDescr((BaseDescr) ceDescr);
final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
patternDescr.addConstraint(new ExprConstraintDescr("type == \"stilton\" "));
ceDescr.addDescr(patternDescr);
ruleDescr.setConsequence(consequence);
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertEquals("rule-1", rule.getName());
return rule;
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBaseImpl method internalAddPackages.
private void internalAddPackages(List<InternalKnowledgePackage> clonedPkgs) {
for (InternalWorkingMemory wm : getWorkingMemories()) {
wm.flushPropagations();
}
// we need to merge all byte[] first, so that the root classloader can resolve classes
for (InternalKnowledgePackage newPkg : clonedPkgs) {
newPkg.checkValidity();
this.eventSupport.fireBeforePackageAdded(newPkg);
if (newPkg.hasTraitRegistry()) {
getTraitRegistry().merge(newPkg.getTraitRegistry());
}
InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
if (pkg == null) {
pkg = new KnowledgePackageImpl(newPkg.getName());
// @TODO we really should have a single root cache
pkg.setClassFieldAccessorCache(this.classFieldAccessorCache);
pkgs.put(pkg.getName(), pkg);
}
// first merge anything related to classloader re-wiring
pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader, true);
}
processAllTypesDeclaration(clonedPkgs);
for (InternalKnowledgePackage newPkg : clonedPkgs) {
// Add functions
JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
for (Function function : newPkg.getFunctions().values()) {
String functionClassName = function.getClassName();
try {
registerFunctionClassAndInnerClasses(functionClassName, runtime, this::registerAndLoadTypeDefinition);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to compile function '" + function.getName() + "'", e);
}
}
}
// now iterate again, this time onBeforeExecute will handle any wiring or cloader re-creating that needs to be done as part of the merge
for (InternalKnowledgePackage newPkg : clonedPkgs) {
InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
// this needs to go here, as functions will set a java dialect to dirty
if (newPkg.getFunctions() != null) {
for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
pkg.addFunction(entry.getValue());
}
}
pkg.getDialectRuntimeRegistry().onBeforeExecute();
// with the classloader recreated for all byte[] classes, we should now merge and wire any new accessors
pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
}
for (InternalKnowledgePackage newPkg : clonedPkgs) {
InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
// now merge the new package into the existing one
mergePackage(pkg, newPkg);
// add the window declarations to the kbase
for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
this.reteooBuilder.addNamedWindow(window);
}
// add entry points to the kbase
for (String id : newPkg.getEntryPointIds()) {
this.reteooBuilder.addEntryPoint(id);
}
// add the rules to the RuleBase
for (Rule r : newPkg.getRules()) {
RuleImpl rule = (RuleImpl) r;
checkMultithreadedEvaluation(rule);
internalAddRule(rule);
}
// add the flows to the RuleBase
if (newPkg.getRuleFlows() != null) {
final Map<String, Process> flows = newPkg.getRuleFlows();
for (Process process : flows.values()) {
internalAddProcess(process);
}
}
if (!newPkg.getResourceTypePackages().isEmpty()) {
KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
ResourceType rt = rtkKpg.getResourceType();
KieWeaverService factory = weavers.getWeavers().get(rt);
factory.weave(this, newPkg, rtkKpg);
}
}
ruleUnitRegistry.add(newPkg.getRuleUnitRegistry());
this.eventSupport.fireAfterPackageAdded(newPkg);
}
if (config.isMultithreadEvaluation() && !hasMultiplePartitions()) {
disableMultithreadEvaluation("The rete network cannot be partitioned: disabling multithread evaluation");
}
}
Aggregations