Search in sources :

Example 71 with ByteArrayResource

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

the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition1.

@Test
public void testMultipleInheritanceWithPosition1() 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("field1", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("field2", 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 72 with ByteArrayResource

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

the class TraitTest method testTraitModifyCore2.

@Test
public void testTraitModifyCore2() {
    String s1 = "package test; " + "import org.drools.core.factmodel.traits.*; " + "" + "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 StudentWorker2 extends StudentWorker @propertyReactive name : String end " + "declare trait Assistant extends Student, Worker @propertyReactive name : String end " + "declare Person @Traitable @propertyReactive name : String end " + "" + "rule \"Init\"  " + "when  " + "then  " + "  Person p = new Person( \"john\" );  " + "  insert( p );  " + "end  " + "" + "rule \"Don\"  " + "when  " + "  $p : Person( name == \"john\" )  " + "then  " + "  System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON WORKER \" + $p  );  " + "  don( $p, Worker.class );  " + "  System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON STUDWORKER-2 \" + $p );  " + "  don( $p, StudentWorker2.class );  " + "  System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON ASSISTANT \" + $p );  " + "  don( $p, Assistant.class );  " + "end  " + "" + "rule \"Log S\"  " + "when  " + "  $t : Student() @watch( name )  " + "then  " + "  System.err.println( \"@@Student >> \" +  $t );  " + "end  " + "rule \"Log W\"  " + "when  " + "  $t : Worker() @watch( name )  " + "then  " + "  System.err.println( \"@@Worker >> \" + $t );  " + "end  " + "rule \"Log SW\"  " + "when  " + "  $t : StudentWorker() @watch( name )  " + "then  " + "  System.err.println( \"@@StudentWorker >> \" + $t );  " + "end  " + "rule \"Log RA\"  " + "when  " + "  $t : Assistant() @watch( name )  " + "then  " + "  System.err.println( \"@@Assistant >> \" + $t );  " + "end  " + "rule \"Log Px\"  " + "salience -1  " + "when  " + "  $p : Person() @watch( name )  " + "then  " + "  System.err.println( \"Poor Core Person >> \" + $p );  " + "end  " + "" + "rule \"Mod\"  " + "salience -10  " + "when  " + "  String( this == \"go\" )  " + "  $p : Student( name == \"john\" )  " + "then  " + "  System.out.println( \" ------------------------------------------------------------------------------ \" + $p );  " + "  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();
    // not relevant
    TraitFactoryImpl.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    int k = ksession.fireAllRules();
    assertEquals(7, k);
    ksession.insert("go");
    k = ksession.fireAllRules();
    assertEquals(6, k);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) 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) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 73 with ByteArrayResource

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

the class TraitTest method traitsInBatchExecution.

// BZ #748752
@Test
public void traitsInBatchExecution() {
    String str = "package org.jboss.qa.brms.traits\n" + "import org.drools.traits.compiler.Person;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "global java.util.List list;" + "" + "declare Person \n" + "  @Traitable \n" + "end \n" + "" + "declare trait Student\n" + "  school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + "  when\n" + "    $student : Person( age < 26 )\n" + "  then\n" + "    Student s = don( $student, Student.class );\n" + "    s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + "  when\n" + "    student : Person( this isA Student )\n" + "  then" + "    list.add( 1 );\n" + "    System.out.println(\"Person is a student: \" + student);\n" + "end\n" + "\n" + "rule \"print school\"\n" + "  when\n" + "    Student( $school : school )\n" + "  then\n" + "    list.add( 2 );\n" + "    System.out.println(\"Student is attending \" + $school);\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    List list = new ArrayList();
    KieBase kbase = kbuilder.newKieBase();
    TraitFactoryImpl.setMode(mode, kbase);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    Person student = new Person("student", 18);
    commands.add(CommandFactory.newInsert(student));
    System.out.println("Starting execution...");
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    System.out.println("Finished...");
    assertEquals(2, list.size());
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Person(org.drools.traits.compiler.Person) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 74 with ByteArrayResource

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

the class TraitTest method testTraitModifyCore.

@Test
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();
    TraitFactoryImpl.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.kiesession.rulebase.InternalKnowledgeBase) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 75 with ByteArrayResource

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

the class TraitTest method testTraitModifyCore2a.

@Test
public void testTraitModifyCore2a() {
    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 Student @propertyReactive name : String end\n" + "declare trait Worker @propertyReactive name : String end\n" + "declare trait StudentWorker extends Student, Worker @propertyReactive name : String end\n" + "declare trait Assistant extends Student, Worker @propertyReactive name : String end\n" + "declare Person @Traitable @propertyReactive name : String end\n" + "" + "rule \"Init\" \n" + "when \n" + "then \n" + "  Person p = new Person( \"john\" ); \n" + "  insert( p ); \n" + "end \n" + "" + "rule \"Don\" \n" + "when \n" + "  $p : Person( name == \"john\" ) \n" + "then \n" + "  System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON WORKER \" + $p  ); \n" + "  don( $p, Worker.class ); \n" + "  System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON STUDWORKER \" + $p ); \n" + "  don( $p, StudentWorker.class ); \n" + "end \n" + "" + "rule \"Log W\" \n" + "when \n" + "  $t : Worker( this isA StudentWorker ) @watch( name ) \n" + "then \n" + "  System.out.println( \"@@Worker >> \" + $t ); \n" + "  list.add( true ); \n" + "end \n" + "rule \"Log SW\" \n" + "when \n" + "  $t : StudentWorker() @watch( name ) \n" + "then \n" + "  System.out.println( \"@@StudentWorker >> \" + $t ); \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());
    KieSession ksession = kbase.newKieSession();
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);
    int k = ksession.fireAllRules();
    assertTrue(list.contains(true));
    assertEquals(1, list.size());
}
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.kiesession.rulebase.InternalKnowledgeBase) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) 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