Search in sources :

Example 1 with KnowledgeBuilderConfiguration

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);
}
Also used : JarInputStream(java.util.jar.JarInputStream) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MapBackedClassLoader(org.drools.core.rule.MapBackedClassLoader) JarEntry(java.util.jar.JarEntry) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 2 with KnowledgeBuilderConfiguration

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));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 3 with KnowledgeBuilderConfiguration

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();
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 4 with KnowledgeBuilderConfiguration

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);
}
Also used : KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 5 with KnowledgeBuilderConfiguration

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());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Aggregations

KnowledgeBuilderConfiguration (org.kie.internal.builder.KnowledgeBuilderConfiguration)25 Test (org.junit.Test)21 KieSession (org.kie.api.runtime.KieSession)16 KieBase (org.kie.api.KieBase)15 ArrayList (java.util.ArrayList)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)7 List (java.util.List)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Arrays.asList (java.util.Arrays.asList)4 URLClassLoader (java.net.URLClassLoader)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KiePackage (org.kie.api.definition.KiePackage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JarEntry (java.util.jar.JarEntry)2 JarInputStream (java.util.jar.JarInputStream)2 MapBackedClassLoader (org.drools.core.rule.MapBackedClassLoader)2 FactType (org.kie.api.definition.type.FactType)2 Resource (org.kie.api.io.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1