use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.
the class MarshallingTest method testSerializabilityWithJarFacts.
/**
* In this case we are dealing with facts which are not on the systems classpath.
*/
@Test
public void testSerializabilityWithJarFacts() throws Exception {
MapBackedClassLoader loader = new MapBackedClassLoader(this.getClass().getClassLoader());
JarInputStream jis = new JarInputStream(this.getClass().getResourceAsStream("/billasurf.jar"));
JarEntry entry = null;
byte[] buf = new byte[1024];
int len = 0;
while ((entry = jis.getNextJarEntry()) != null) {
if (!entry.isDirectory()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = jis.read(buf)) >= 0) {
out.write(buf, 0, len);
}
loader.addResource(entry.getName(), out.toByteArray());
}
}
String drl = "package foo.bar \n" + "import com.billasurf.Board\n" + "rule 'MyGoodRule' \n dialect 'mvel' \n when " + " Board() " + "then \n" + " System.err.println(42); \n" + "end\n";
KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl);
kpkgs = SerializationHelper.serializeObject(kpkgs, loader);
}
use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.
the class EvalTest method testJaninoEval.
@Test
public void testJaninoEval() throws Exception {
final KnowledgeBuilderConfiguration kbconf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
kbconf.setProperty(JavaDialectConfiguration.JAVA_COMPILER_PROPERTY, "JANINO");
KieBase kbase = loadKnowledgeBase(kbconf, "eval_rule_test.drl");
kbase = SerializationHelper.serializeObject(kbase);
KieSession ksession = kbase.newKieSession();
ksession.setGlobal("five", 5);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Cheese stilton = new Cheese("stilton", 5);
ksession.insert(stilton);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(stilton, ((List) ksession.getGlobal("list")).get(0));
}
use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.
the class Misc2Test method testNoLoopWithNamedConsequences.
@Test
public void testNoLoopWithNamedConsequences() {
// DROOLS-327
String drl = "import org.drools.compiler.Message\n" + "rule \"Hello World\" no-loop\n" + " when\n" + " m : Message( myMessage : message )\n" + " if (status == 0) do[sayHello]\n" + " then\n" + " System.out.println( myMessage );\n" + " m.setMessage( \"Goodbye cruel world\" );\n" + " m.setStatus( Message.GOODBYE );\n" + " update( m );\n" + " then[sayHello]\n" + " System.out.println(\"Hello, I'm here!\");\n" + "end\n";
KnowledgeBuilderConfiguration kbConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
KieBase kbase = loadKnowledgeBaseFromString(kbConf, drl);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Message("Hello World"));
ksession.fireAllRules();
}
use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.
the class Misc2Test method testManyAccumulatesWithSubnetworks.
@Test
public void testManyAccumulatesWithSubnetworks() {
String drl = "package org.drools.compiler.tests; \n" + "" + "declare FunctionResult\n" + " father : Applied\n" + "end\n" + "\n" + "declare Field\n" + " applied : Applied\n" + "end\n" + "\n" + "declare Applied\n" + "end\n" + "\n" + "\n" + "rule \"Seed\"\n" + "when\n" + "then\n" + " Applied app = new Applied();\n" + " Field fld = new Field();\n" + "\n" + " insert( app );\n" + " insert( fld );\n" + "end\n" + "\n" + "\n" + "\n" + "\n" + "rule \"complexSubNetworks\"\n" + "when\n" + " $fld : Field( $app : applied )\n" + " $a : Applied( this == $app )\n" + " accumulate (\n" + " $res : FunctionResult( father == $a ),\n" + " $args : collectList( $res )\n" + " )\n" + " accumulate (\n" + " $res : FunctionResult( father == $a ),\n" + " $deps : collectList( $res )\n" + " )\n" + " accumulate (\n" + " $x : String()\n" + " and\n" + " not String( this == $x ),\n" + " $exprFieldList : collectList( $x )\n" + " )\n" + "then\n" + "end\n" + "\n";
KnowledgeBuilderConfiguration kbConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
KieBase kbase = loadKnowledgeBaseFromString(kbConf, drl);
KieSession ksession = kbase.newKieSession();
int num = ksession.fireAllRules();
// only one rule should fire, but the partial propagation of the asserted facts should not cause a runtime NPE
assertEquals(1, num);
}
use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.
the class Misc2Test method testLinkRiaNodesWithSubSubNetworks.
@Test
public void testLinkRiaNodesWithSubSubNetworks() {
String drl = "package org.drools.compiler.tests; \n" + "" + "import java.util.*; \n" + "" + "global List list; \n" + "" + "declare MyNode\n" + "end\n" + "" + "rule Init\n" + "when\n" + "then\n" + " insert( new MyNode() );\n" + " insert( new MyNode() );\n" + "end\n" + "" + "" + "rule \"Init tree nodes\"\n" + "salience -10\n" + "when\n" + " accumulate (\n" + " MyNode(),\n" + " $x : count( 1 )\n" + " )\n" + " accumulate (\n" + " $n : MyNode()\n" + " and\n" + " accumulate (\n" + " $val : Double( ) from Arrays.asList( 1.0, 2.0, 3.0 ),\n" + " $rc : count( $val );\n" + " $rc == 3 \n" + " ),\n" + " $y : count( $n )\n" + " )\n" + "then\n" + " list.add( $x ); \n" + " list.add( $y ); \n" + " System.out.println( $x ); \n" + " System.out.println( $y ); \n" + "end\n";
KnowledgeBuilderConfiguration kbConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
KieBase kbase = loadKnowledgeBaseFromString(kbConf, drl);
KieSession ksession = kbase.newKieSession();
List<Long> list = new ArrayList<Long>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals(2, list.get(0).intValue());
assertEquals(2, list.get(1).intValue());
}
Aggregations