Search in sources :

Example 1 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class Misc2Test method testSelfChangingRuleSet.

@Test
public void testSelfChangingRuleSet() {
    // DROOLS-92
    String str = "package org.drools.compiler.integrationtests;\n" + "" + "import org.drools.compiler.integrationtests.Misc2Test.Foo2; \n" + "" + "global java.util.List list; \n" + "\n" + "rule \"Prep\" \n" + "when \n" + "  $packs : java.util.Collection() \n" + "then \n" + "   ((org.drools.core.impl.InternalKnowledgeBase)drools.getKieRuntime().getKieBase()).addPackages( $packs );" + "end \n" + "" + "rule \"Self-change\"\n" + "when\n" + "  String( this == \"go\" )\n" + "then\n" + "   ((org.drools.core.impl.InternalKnowledgeBase)drools.getKieRuntime().getKieBase()).removeRule( \"org.drools.compiler.integrationtests\", \"React\" ); \n" + "end\n" + "\n" + "\n" + "rule \"Insert\"\n" + "when\n" + "  $i : Integer()\n" + "then\n" + "  Foo2 foo = new Foo2();\n " + "  foo.setX( $i ); \n" + "  insert( foo );\n" + "end\n" + "" + "";
    String str2 = "package org.drools.compiler.integrationtests;\n" + "" + "import org.drools.compiler.integrationtests.Misc2Test.Foo2; \n" + "global java.util.List list;\n " + "rule \"React\"\n" + "when\n" + "  $b : Foo2( x < 10 )\n" + "then\n" + "  System.out.println( \" Foo2 is in \" + $b.getX() );" + "  list.add( $b ); \n" + "end\n";
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(str2.getBytes()), ResourceType.DRL);
    System.out.println(knowledgeBuilder.getErrors());
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(knowledgeBuilder.getKnowledgePackages());
    ksession.insert(new Integer(1));
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    ksession.insert(new Integer(2));
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 2 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class ExtendsTest method testMultipleInheritanceWithPosition6.

@Test
public void testMultipleInheritanceWithPosition6() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 //@position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(6, sw.getFields().size());
    assertEquals("field0", sw.getFields().get(0).getName());
    assertEquals("field1", sw.getFields().get(1).getName());
    assertEquals("field2", sw.getFields().get(2).getName());
    assertEquals("field3", sw.getFields().get(3).getName());
    assertEquals("mfield0", sw.getFields().get(4).getName());
    assertEquals("mfield1", sw.getFields().get(5).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 3 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class ExtendsTest method testMultipleInheritanceWithPosition2.

@Test
public void testMultipleInheritanceWithPosition2() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(5, sw.getFields().size());
    assertEquals("mfield0", sw.getFields().get(0).getName());
    assertEquals("field0", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("field1", sw.getFields().get(3).getName());
    assertEquals("field2", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 4 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class ExtendsTest method testDeclareExtendsMissingJavaParent.

@Test
public void testDeclareExtendsMissingJavaParent() {
    String drl = "package org.drools.test; \n" + "import org.drools.compiler.integrationtests.ExtendsTest.Y; \n" + "declare Student extends Y end \n" + "";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertTrue(kBuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 5 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class ExtendsTest method testDeclareInheritance.

@Test
public void testDeclareInheritance() throws Exception {
    String s1 = "package org.drools;\n" + "import org.drools.compiler.integrationtests.ExtendsTest.*;\n" + "\n" + "declare A \n" + " @role( event )" + " @typesafe( false )\n" + "end\n" + "" + "declare C @role( event ) @typesafe( false ) end \n" + "" + "rule R \n" + "when " + "   $x : C( this.x == 1 ) \n" + "then\n" + "   System.out.println( $x ); \n" + "end\n" + "";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    FactHandle h = knowledgeSession.insert(new X());
    assertTrue(((InternalFactHandle) h).isEvent());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)79 Test (org.junit.Test)67 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)67 KieSession (org.kie.api.runtime.KieSession)33 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 ArrayList (java.util.ArrayList)27 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)25 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)20 List (java.util.List)19 FactType (org.kie.api.definition.type.FactType)16 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)5 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)5 BitSet (java.util.BitSet)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)4 KieBase (org.kie.api.KieBase)4 StringReader (java.io.StringReader)3 LinkedList (java.util.LinkedList)3