Search in sources :

Example 76 with ByteArrayResource

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

the class TraitTest method traitSimpleTypes.

@Test
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();
    TraitFactoryImpl.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 TraitProxyImpl) {
            TraitProxyImpl tp = (TraitProxyImpl) o;
            assertEquals(0, tp.listAssignedOtnTypeCodes().size());
        }
    }
}
Also used : TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) 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.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 77 with ByteArrayResource

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

the class TraitTest method testTraitEncodeExtendingNonTrait.

@Test
public void testTraitEncodeExtendingNonTrait() {
    String s1 = "package test;\n" + "import " + IntfParent.class.getCanonicalName() + ";\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();
    TraitFactoryImpl.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.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 78 with ByteArrayResource

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

the class TraitTest method testTraitModifyCoreWithPropertyReactivity.

@Test
public void testTraitModifyCoreWithPropertyReactivity() {
    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 " + "   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
    TraitFactoryImpl.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.kiesession.rulebase.InternalKnowledgeBase) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 79 with ByteArrayResource

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

the class ProcessUpgradeTest method testCompositeMappingUpgrade.

@Test
public void testCompositeMappingUpgrade() throws Exception {
    String rule = "package org.test;\n";
    rule += "import org.jbpm.integrationtests.test.Person\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "  ruleflow-group \"hello\"\n";
    rule += "when\n";
    rule += "    $p : Person( ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(new ByteArrayResource(rule.getBytes()), ResourceType.DRL);
    String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "    xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "    type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + "  <header>\n" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <composite id=\"2\" name=\"Composite\" >\n" + "      <nodes>\n" + "        <workItem id=\"1\" name=\"Hello\" >\n" + "          <work name=\"Human Task\" >\n" + "          </work>\n" + "        </workItem>\n" + "      </nodes>\n" + "      <connections>\n" + "      </connections>\n" + "      <in-ports>\n" + "        <in-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeInType=\"DROOLS_DEFAULT\" />\n" + "      </in-ports>\n" + "      <out-ports>\n" + "        <out-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + "      </out-ports>\n" + "    </composite>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\"/>\n" + "    <connection from=\"2\" to=\"3\"/>\n" + "  </connections>\n" + "</process>";
    builder.add(new ReaderResource(new StringReader(process)), ResourceType.DRF);
    // RuleBaseConfiguration config = new RuleBaseConfiguration();
    // config.setRuleBaseUpdateHandler(null);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(builder.getKnowledgePackages());
    KieSession session = kbase.newKieSession();
    TestWorkItemHandler handler = new TestWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    List<String> list = new ArrayList<String>();
    session.setGlobal("list", list);
    Person p = new Person("bobba fet", 32);
    session.insert(p);
    ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.test.ruleflow");
    assertEquals(1, session.getProcessInstances().size());
    String process2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "    xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "    type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow2\" package-name=\"org.test\" >\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <composite id=\"2\" name=\"Composite\" >\n" + "      <nodes>\n" + "        <workItem id=\"101\" name=\"Hello\" >\n" + "          <work name=\"Human Task\" >\n" + "          </work>\n" + "        </workItem>\n" + "        <actionNode id=\"2\" name=\"Action\" >" + "          <action type=\"expression\" dialect=\"java\">System.out.println();\n" + "list.add(\"Executed\");</action>\n" + "        </actionNode>\n" + "      </nodes>\n" + "      <connections>\n" + "        <connection from=\"101\" to=\"2\"/>\n" + "      </connections>\n" + "      <in-ports>\n" + "        <in-port type=\"DROOLS_DEFAULT\" nodeId=\"101\" nodeInType=\"DROOLS_DEFAULT\" />\n" + "      </in-ports>\n" + "      <out-ports>\n" + "        <out-port type=\"DROOLS_DEFAULT\" nodeId=\"2\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + "      </out-ports>\n" + "    </composite>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\"/>\n" + "    <connection from=\"2\" to=\"3\"/>\n" + "  </connections>\n" + "</process>";
    builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(new ReaderResource(new StringReader(process2)), ResourceType.DRF);
    kbase.addPackages(builder.getKnowledgePackages());
    Map<String, Long> mapping = new HashMap<String, Long>();
    mapping.put("2:1", 101L);
    WorkflowProcessInstanceUpgrader.upgradeProcessInstance(session, processInstance.getId(), "org.test.ruleflow2", mapping);
    assertEquals("org.test.ruleflow2", processInstance.getProcessId());
    session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
    assertEquals(1, list.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ReaderResource(org.drools.core.io.impl.ReaderResource) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

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