Search in sources :

Example 36 with ByteArrayResource

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

the class TraitTest method traitSimpleTypes.

@Test(timeout = 10000)
public void traitSimpleTypes() {
    String s1 = "package org.drools.factmodel.traits;\n" + "\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "declare trait PassMark\n" + "end\n" + "\n" + "declare ExamMark \n" + "@Traitable\n" + "value : long \n" + "end\n" + "" + "rule \"testTraitFieldTypePrimitive\"\n" + "when\n" + "    $mark : ExamMark()\n" + "then\n" + "    don($mark, PassMark.class);\n" + "end\n" + "" + "rule \"Init\" when then insert( new ExamMark() ); 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();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        if (o instanceof TraitableBean) {
            TraitableBean tb = (TraitableBean) o;
            assertEquals(1, tb._getTraitMap().size());
            BitSet bs = new BitSet();
            bs.set(0);
            assertEquals(bs, tb.getCurrentTypeCode());
        }
        if (o instanceof TraitProxy) {
            TraitProxy tp = (TraitProxy) o;
            assertEquals(0, tp.listAssignedOtnTypeCodes().size());
        }
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) BitSet(java.util.BitSet) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 37 with ByteArrayResource

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

the class TraitTest method testTraitEncodeExtendingNonTrait.

@Test(timeout = 10000)
public void testTraitEncodeExtendingNonTrait() {
    String s1 = "package test;\n" + "import org.drools.compiler.factmodel.traits.TraitTest.IntfParent;\n" + "" + "declare IntfParent end\n" + "" + "declare trait TChild extends IntfParent end \n" + "";
    String s2 = "package test; declare trait SomeThing end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s2.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder2.hasErrors()) {
        fail(kbuilder2.getErrors().toString());
    }
    kbase.addPackages(kbuilder2.getKnowledgePackages());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 38 with ByteArrayResource

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

the class TraitTest method testTraitModifyCoreWithPropertyReactivity.

@Test(timeout = 10000)
public void testTraitModifyCoreWithPropertyReactivity() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "global java.util.List list;\n" + "" + "declare trait Student @propertyReactive " + "   name : String " + "   age : int " + "   grades : double " + "   school : String " + "   aaa : boolean " + "end\n" + "declare trait Worker @propertyReactive " + "   name : String " + "   wage : double " + "end\n" + "declare trait StudentWorker extends Student, Worker @propertyReactive " + "   hours : int " + "end\n" + "declare trait Assistant extends Student, Worker @propertyReactive " + "   address : String " + "end\n" + "declare Person @propertyReactive @Traitable " + "   wage : double " + "   name : String " + "   age : int  " + "end\n" + "" + "rule \"Init\" \n" + "when \n" + "then \n" + "  Person p = new Person( 109.99, \"john\", 18 ); \n" + "  insert( p ); \n" + "end \n" + "" + "rule \"Don\" \n" + "when \n" + "  $p : Person( name == \"john\" ) \n" + "then \n" + "  System.out.println( $p ); \n" + "  don( $p, StudentWorker.class ); \n" + "  don( $p, Assistant.class ); \n" + "end \n" + "" + "rule \"Log S\" \n" + "when \n" + "  $t : Student( age == 44 ) \n" + "then \n" + "  list.add( 1 );\n " + "  System.out.println( \"Student >> \" +  $t ); \n" + "end \n" + "rule \"Log W\" \n" + "when \n" + "  $t : Worker( name == \"alan\" ) \n" + "then \n" + "  list.add( 2 );\n " + "  System.out.println( \"Worker >> \" + $t ); \n" + "end \n" + "rule \"Log SW\" \n" + "when \n" + "  $t : StudentWorker( age == 44 ) \n" + "then \n" + "  list.add( 3 );\n " + "  System.out.println( \"StudentWorker >> \" + $t ); \n" + "end \n" + "rule \"Log Pers\" \n" + "when \n" + "  $t : Person( age == 44 ) \n" + "then \n" + "  list.add( 4 );\n " + "  System.out.println( \"Person >> \" + $t ); \n" + "end \n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + "  String( this == \"go\" ) \n" + "  $p : Student( name == \"john\" ) \n" + "then \n" + "  System.out.println( \" ------------------------------------------------------------------------------ \" + $p ); \n" + "  modify ( $p ) { setSchool( \"myschool\" ), setAge( 44 ), setName( \"alan\" ); } " + "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
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<Integer> list = new ArrayList<Integer>();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    int k = ksession.fireAllRules();
    ksession.insert("go");
    k = ksession.fireAllRules();
    assertEquals(5, k);
    assertEquals(4, list.size());
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
    assertTrue(list.contains(3));
    assertTrue(list.contains(4));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) 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.core.impl.InternalKnowledgeBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Test(org.junit.Test)

Example 39 with ByteArrayResource

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

the class TraitTest method testdeleteThings.

@Test(timeout = 10000)
public void testdeleteThings() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\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" + "  delete( $x ); \n\n" + "  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();
    TraitFactory.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(0, 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.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 40 with ByteArrayResource

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

the class TraitTest method testTraitModifyCore.

@Test(timeout = 10000)
public void testTraitModifyCore() {
    String s1 = "package test; " + "import org.drools.core.factmodel.traits.*; " + "" + "global java.util.List list; " + "" + "declare trait Student @PropertyReactive name : String end " + "declare trait Worker @PropertyReactive name : String end " + "declare trait StudentWorker extends Student, Worker @PropertyReactive name : String end " + "declare trait Assistant extends Student, Worker @PropertyReactive name : String end " + "declare Person @Traitable name : String end " + "" + "rule \"Init\"  " + "when  " + "then  " + "  Person p = new Person( \"john\" );  " + "  insert( p );  " + "end  " + "" + "rule \"Don\"  " + "no-loop  " + "when  " + "  $p : Person( name == \"john\" )  " + "then  " + "  System.out.println( $p );  " + "" + "  System.out.println( \" ----------------------------------------------------------------------------------- Don student\" );  " + "  don( $p, Student.class );  " + "  System.out.println( \" ----------------------------------------------------------------------------------- Don worker\" );  " + "  don( $p, Worker.class );  " + "  System.out.println( \" ----------------------------------------------------------------------------------- Don studentworker\" );  " + "  don( $p, StudentWorker.class );  " + "  System.out.println( \" ----------------------------------------------------------------------------------- Don assistant\" );  " + "  don( $p, Assistant.class );  " + "end  " + "" + "rule \"Log S\"  " + "when  " + "  $t : Student() @Watch( name ) " + "then  " + "  System.out.println( \"Student >> \" +  $t ); " + "  list.add( $t.getName() );  " + "end  " + "rule \"Log W\"  " + "when  " + "  $t : Worker() @Watch( name ) " + "then  " + "  System.out.println( \"Worker >> \" + $t );  " + "  list.add( $t.getName() );  " + "end  " + "rule \"Log SW\"  " + "when  " + "  $t : StudentWorker() @Watch( name ) " + "then  " + "  System.out.println( \"StudentWorker >> \" + $t );  " + "  list.add( $t.getName() );  " + "end  " + "rule \"Log RA\"  " + "when  " + "  $t : Assistant() @Watch( name ) " + "then  " + "  System.out.println( \"Assistant >> \" + $t );  " + "  list.add( $t.getName() );  " + "end  " + "" + "rule \"Mod\"  " + "salience -10  " + "when  " + "  $p : Person( name == \"john\" ) " + "then  " + "   System.out.println( \"-----------------------------\" ); " + "   modify ( $p ) { setName( \"alan\" ); } " + "end  " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    int k = ksession.fireAllRules();
    assertEquals(Arrays.asList("john", "john", "john", "john", "alan", "alan", "alan", "alan"), list);
    assertEquals(11, k);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) 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