use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class StreamsTest method loadKnowledgeBase.
private KieBase loadKnowledgeBase(final String fileName, KieBaseConfiguration kconf) throws IOException, DroolsParserException, Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(fileName, getClass()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors());
return null;
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
kbase.addPackages(kbuilder.getKnowledgePackages());
return SerializationHelper.serializeObject(kbase);
// return kbase;
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class SegmentMemoryPrototypeTest method testSegmentMemoryPrototype.
@Test
public void testSegmentMemoryPrototype() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(DRL.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
checkKieSession(ksession);
// Create a 2nd KieSession (that will use segment memory prototype) and check that it works as the former one
KieSession ksession2 = kbase.newKieSession();
checkKieSession(ksession2);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class SegmentMemoryPrototypeTest method testSessionCache.
@Test
public void testSessionCache() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(DRL.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
checkKieSession(ksession);
ksession.dispose();
ksession.reset();
checkKieSession(ksession);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class SerializedPackageMergeTest method testBuildAndSerializePackagesWithSamePackageName.
@Test
public void testBuildAndSerializePackagesWithSamePackageName() throws IOException, ClassNotFoundException {
// RHBRMS-2773
String str1 = "package com.sample\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R1 when\n" + " $p : Person( name == \"John\" )\n" + "then\n" + " list.add($p);" + "end\n";
String str2 = "package com.sample\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + " $p : Person( name == \"Paul\" )\n" + "then\n" + " list.add($p);" + "end\n";
// Create 2 knowledgePackages separately (but these rules have the same package name)
KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder1.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages();
KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder2.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages();
// Combine the knowledgePackages
InternalKnowledgeBase knowledgeBase1 = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase1.addPackages(knowledgePackages1);
knowledgeBase1.addPackages(knowledgePackages2);
Collection<KiePackage> knowledgePackagesCombined = knowledgeBase1.getKiePackages();
// serialize
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new DroolsObjectOutputStream(baos);
out.writeObject(knowledgePackagesCombined);
out.flush();
out.close();
// deserialize
ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Collection<KiePackage> deserializedPackages = (Collection<KiePackage>) in.readObject();
// Use the deserialized knowledgePackages
InternalKnowledgeBase knowledgeBase2 = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase2.addPackages(deserializedPackages);
KieSession ksession = knowledgeBase2.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new org.drools.compiler.Person("John"));
ksession.insert(new org.drools.compiler.Person("Paul"));
ksession.fireAllRules();
assertEquals(2, list.size());
ksession.dispose();
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class StrEvaluatorTest method readKnowledgeBase.
private KieBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("strevaluator_test.drl")), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge." + errors.toArray());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
return kbase;
}
Aggregations