use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class KieContainerImpl method update.
private Results update(final InternalKieModule currentKM, final ReleaseId newReleaseId) {
final InternalKieModule newKM = (InternalKieModule) kr.getKieModule(newReleaseId);
if (newKM == null) {
return null;
}
final KieJarChangeSet cs = currentKM.getChanges(newKM);
List<String> modifiedClassNames = getModifiedClasses(cs);
final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader());
final List<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader());
final List<String> unchangedResources = getUnchangedResources(newKM, cs);
Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
final ResultsImpl results = new ResultsImpl();
List<String> kbasesToRemove = new ArrayList<String>();
for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) {
String kbaseName = kBaseEntry.getKey();
KieBaseModelImpl newKieBaseModel = (KieBaseModelImpl) kProject.getKieBaseModel(kbaseName);
KieBaseModelImpl currentKieBaseModel = (KieBaseModelImpl) currentKieBaseModels.get(kbaseName);
// if a kbase no longer exists, just remove it from the cache
if (newKieBaseModel == null) {
// have to save for later removal to avoid iteration errors
kbasesToRemove.add(kbaseName);
} else {
final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue();
KieBaseUpdateContext context = new KieBaseUpdateContext(kProject, kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel);
kBase.enqueueModification(currentKM.createKieBaseUpdater(context));
}
}
for (String kbaseToRemove : kbasesToRemove) {
kBases.remove(kbaseToRemove);
}
// remove sessions that no longer exist
this.kSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
this.statelessKSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
return results;
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class KieContainerImpl method createKieBase.
private KieBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, ResultsImpl messages, KieBaseConfiguration conf) {
InternalKieModule kModule = kieProject.getKieModuleForKBase(kBaseModel.getName());
InternalKnowledgeBase kBase = kModule.createKieBase(kBaseModel, kieProject, messages, conf);
if (kBase == null) {
return null;
}
kBase.setResolvedReleaseId(containerReleaseId);
kBase.setContainerId(containerId);
kBase.setKieContainer(this);
kBase.initMBeans();
return kBase;
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class BayesBeliefSystemTest method getSession.
protected KieSession getSession(String ruleFile) {
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(ResourceFactory.newClassPathResource(ruleFile), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
fail();
}
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kBuilder.getKnowledgePackages());
KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
KieSession kSession = kBase.newKieSession(ksConf, null);
return kSession;
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class JavaDialectBinaryEqualityTest method getKnowledgePackage1.
public KiePackage getKnowledgePackage1() {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import " + Person.class.getName() + ";\n";
str += "global java.util.List list\n";
str += "rule rule1 dialect\"java\" \n";
str += "when\n";
str += " $p : Person( age : age == ( 17 + 17 ), eval( age == 34 ))\n";
str += " eval( $p.getAge() == 34 )\n";
str += "then\n";
str += " list.add( $p );\n";
str += "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
List<Person> list = new ArrayList<Person>();
KieSession ksession = kbase.newKieSession();
ksession.setGlobal("list", list);
ksession.insert(new Person("darth", 34));
ksession.fireAllRules();
assertEquals(new Person("darth", 34), list.get(0));
return kbase.getPackage("org.drools.compiler.test");
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class JavaDialectTest method testEvalDetectionInBetaNode.
@Test
public void testEvalDetectionInBetaNode() {
// Tests evals are generated and executed with Java dialect
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $s : String()\n";
drl += " $p1 : Person( eval( name \n != $s ), name == ( new String($s+\"xxx\") ) )\n";
drl += "then\n";
drl += "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
BetaNode betaanode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
BetaNodeFieldConstraint[] constraint = betaanode.getConstraints();
PredicateConstraint c = (PredicateConstraint) constraint[0];
assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
}
Aggregations