use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSignedSerialization3.
/*
* Deserializing a signed rulebase without the proper public key
* should fail.
*/
@Test
public void testSignedSerialization3() throws Exception {
try {
// set only the serialisation properties, but not the deserialization
setPrivateKeyProperties();
// create the kpkgs, but do not let them serialize
Collection<KiePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
try {
kBase = SerializationHelper.serializeObject(kBase);
fail("Deserialisation should have failed.");
} catch (Exception e) {
// success
}
} finally {
unsetPrivateKeyProperties();
}
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class FromTest method testFromSharing.
@Test
public void testFromSharing() {
// Keeping original test as non-property reactive by default, just allowed.
String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + " ListsContainer( $list : list1 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output1.add($s);\n" + "end\n" + "rule R2 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n" + "rule R3 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n";
KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(kbase);
List<String> output1 = new ArrayList<String>();
ksession.setGlobal("output1", output1);
List<String> output2 = new ArrayList<String>();
ksession.setGlobal("output2", output2);
FactHandle fh = ksession.insert(new ListsContainer());
ksession.fireAllRules();
assertEquals("bb", output1.get(0));
assertEquals("22", output2.get(0));
assertEquals("22", output2.get(1));
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
// There is only 1 LIA
assertEquals(1, otn.getObjectSinkPropagator().size());
LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
// There are only 2 FromNodes since R2 and R3 are sharing the second From
LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
assertEquals(2, sinks.length);
// The first from has R1 has sink
assertEquals(1, sinks[0].getSinkPropagator().size());
// The second from has both R2 and R3 as sinks
assertEquals(2, sinks[1].getSinkPropagator().size());
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class QueryTest2 method testEvalRewrite.
@Test
public void testEvalRewrite() throws Exception {
String str = "" + "package org.drools.compiler;\n" + "global java.util.List results;\n" + "rule \"eval rewrite\"\n" + " when\n" + " $o1 : OrderItem( order.number == 11, $seq : seq == 1 )\n" + // " $o2 : OrderItem( order.number == $o1.order.number, seq != $seq )\n" +
" $o2 : Order( items[(Integer) 1] == $o1 ) \n" + " then\n" + " System.out.println( $o1 + \":\" + $o2 );\n" + "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final Order order1 = new Order(11, "Bob");
final OrderItem item11 = new OrderItem(order1, 1);
final OrderItem item12 = new OrderItem(order1, 2);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = createKnowledgeSession(kbase);
ksession.insert(order1);
ksession.insert(item11);
ksession.insert(item12);
ksession.fireAllRules();
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class RuleExtensionTest method testRuleExtendsWithCompositeKBuilder.
@Test
public void testRuleExtendsWithCompositeKBuilder() {
// DROOLS-100
String str = "package org.drools.test;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Base\"\n" + "when\n" + " String( this == \"go\" )\n" + "then\n" + "end\n" + "";
String str2 = "package org.drools.test;\n" + "global java.util.List list;\n" + "\n" + "rule \"ExtYes\" extends \"Base\"\n" + "when\n" + "then\n" + " list.add( 1 );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
CompositeKnowledgeBuilder ckb = kbuilder.batch();
ckb.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL).build();
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
KieSession knowledgeSession = kb.newKieSession();
List list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.fireAllRules();
assertEquals(0, list.size());
knowledgeSession.insert("go");
knowledgeSession.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class AbstractKieModule method createKieBase.
public InternalKnowledgeBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, ResultsImpl messages, KieBaseConfiguration conf) {
Collection<KiePackage> pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName());
if (pkgs == null) {
KnowledgeBuilder kbuilder = kieProject.buildKnowledgePackages(kBaseModel, messages);
if (kbuilder.hasErrors()) {
// Messages already populated by the buildKnowlegePackages
return null;
}
}
// if we get to here, then we know the pkgs is now cached
pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName());
if (kBaseModel.getEventProcessingMode() == EventProcessingOption.CLOUD && (conf == null || conf.getOption(EventProcessingOption.class) == EventProcessingOption.CLOUD)) {
for (KiePackage kpkg : pkgs) {
if (((KnowledgePackageImpl) kpkg).needsStreamMode()) {
throw new RuntimeException("The requested KieBase \"" + kBaseModel.getName() + "\" has been set to run in CLOUD mode but requires features only available in STREAM mode");
}
}
}
ClassLoader cl = kieProject.getClassLoader();
if (conf == null) {
conf = getKnowledgeBaseConfiguration(kBaseModel, cl);
} else if (conf instanceof RuleBaseConfiguration) {
((RuleBaseConfiguration) conf).setClassLoader(cl);
}
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kBaseModel.getName(), conf);
kBase.addPackages(pkgs);
return kBase;
}
Aggregations