Search in sources :

Example 21 with ByteArrayResource

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

the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition3.

@Test
public void testMultipleInheritanceWithPosition3() 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("field0", sw.getFields().get(0).getName());
    assertEquals("field2", sw.getFields().get(1).getName());
    assertEquals("field1", sw.getFields().get(2).getName());
    assertEquals("mfield1", sw.getFields().get(3).getName());
    assertEquals("mfield0", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 22 with ByteArrayResource

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

the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition5.

@Test
public void testMultipleInheritanceWithPosition5() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(2)\n" + " field1 : int = 101 @position(1)\n" + " field2 : int = 102 @position(8)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(7)\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("field1", sw.getFields().get(0).getName());
    assertEquals("field0", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("mfield0", 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.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 23 with ByteArrayResource

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

the class TraitTest method testTMSConsistencyWithNonTraitableBeans.

@Test
public void testTMSConsistencyWithNonTraitableBeans() {
    String s1 = "package org.drools.test;\n" + "import org.drools.traits.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + "  insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + "  age  : int\n" + "  name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + "    $p : Person( )\n" + "then\n" + "    don( $p, Student.class, true );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    TraitFactoryImpl.setMode(mode, ksession.getKieBase());
    ksession.fireAllRules();
    FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
    InternalFactHandle h = ((InternalFactHandle) personHandle);
    ObjectTypeConfigurationRegistry reg = h.getEntryPoint((InternalWorkingMemory) ksession).getObjectTypeConfigurationRegistry();
    ObjectTypeConf conf = reg.getOrCreateObjectTypeConf(h.getEntryPointId(), ((InternalFactHandle) personHandle).getObject());
    assertTrue(conf.isTMSEnabled());
    ksession.dispose();
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.traits.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 24 with ByteArrayResource

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

the class TraitTest method testShedThing.

@Test
public void testShedThing() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "import org.drools.traits.core.factmodel.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + "   insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    $x : Core( id == 0 )\n" + "then\n" + "    don( $x, A.class );\n" + "    don( $x, B.class );\n" + "    don( $x, C.class );\n" + "    don( $x, D.class );\n" + "    don( $x, E.class );\n" + "end\n" + "\n" + "\n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + "  $g : String( this == \"go\" ) \n" + "  $x : Core( id == 0 ) \n" + "then \n" + "  shed( $x, Thing.class ); " + "  delete( $g ); \n\n" + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // not relevant
    TraitFactoryImpl.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List list = new ArrayList();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(1, ksession.getObjects().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 25 with ByteArrayResource

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

the class TraitTest method testHierarchyEncodeOnPackageMerge.

@Test
public void testHierarchyEncodeOnPackageMerge() {
    String drl0 = "package org.drools.test; " + "declare trait X end ";
    String drl1 = "package org.drools.test; " + "import org.drools.core.factmodel.traits.*; " + "global java.util.List list; " + "declare trait A end " + "declare trait B extends A end " + "declare trait C extends B end " + "";
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactoryImpl.setMode(mode, knowledgeBase);
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl0.getBytes()), ResourceType.DRL);
    assertFalse(kb.hasErrors());
    knowledgeBase.addPackages(kb.getKnowledgePackages());
    KnowledgeBuilder kb2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb2.add(new ByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    System.out.print(kb2.getErrors());
    assertFalse(kb2.hasErrors());
    knowledgeBase.addPackages(kb2.getKnowledgePackages());
    HierarchyEncoder<String> hier = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(knowledgeBase).getHierarchy();
    BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone();
    BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone();
    c.and(b);
    assertEquals(b, c);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) BitSet(java.util.BitSet) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) 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