Search in sources :

Example 36 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project jbpm by kiegroup.

the class MemoryLeakTest method createKnowledgeBase.

private KieBase createKnowledgeBase() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("memory/BPMN2-RuleTaskWithInsertProcessInstance.bpmn2"), ResourceType.BPMN2);
    kbuilder.add(ResourceFactory.newClassPathResource("memory/ProcessInstanceRule.drl"), ResourceType.DRL);
    if (!kbuilder.getErrors().isEmpty()) {
        Iterator<KnowledgeBuilderError> errIter = kbuilder.getErrors().iterator();
        while (errIter.hasNext()) {
            KnowledgeBuilderError err = errIter.next();
            StringBuilder lines = new StringBuilder("");
            if (err.getLines().length > 0) {
                lines.append(err.getLines()[0]);
                for (int i = 1; i < err.getLines().length; ++i) {
                    lines.append(", " + err.getLines()[i]);
                }
            }
            logger.warn(err.getMessage() + " (" + lines.toString() + ")");
        }
        throw new IllegalArgumentException("Errors while parsing knowledge base");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 37 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project spring-boot-student by wyh-spring-ecosystem-student.

the class DdLoadTest method main.

public static void main(String[] args) {
    // rule,rule2可以放在数据库中,有个唯一code和他们对于,代码要执行规则的时候,根据code从数据库获取出来就OK了,这样自己开发的规则管理系统那边对数据库里的规则进行维护就行了
    String rule = "package com.xiaolyuh.drools\r\n";
    rule += "import com.xiaolyuh.model.Message;\r\n";
    rule += "rule \"rule1\"\r\n";
    rule += "\twhen\r\n";
    rule += "Message( status == 1, myMessage : msg )";
    rule += "\tthen\r\n";
    rule += "\t\tSystem.out.println( 1+\":\"+myMessage );\r\n";
    rule += "end\r\n";
    String rule2 = "package com.xiaolyuh.drools\r\n";
    rule += "import com.xiaolyuh.model.Message;\r\n";
    rule += "rule \"rule2\"\r\n";
    rule += "\twhen\r\n";
    rule += "Message( status == 2, myMessage : msg )";
    rule += "\tthen\r\n";
    rule += "\t\tSystem.out.println( 2+\":\"+myMessage );\r\n";
    rule += "end\r\n";
    StatefulKnowledgeSession kSession = null;
    try {
        long startTime = System.currentTimeMillis();
        KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
        // 装入规则,可以装入多个
        kb.add(ResourceFactory.newByteArrayResource(rule.getBytes("utf-8")), ResourceType.DRL);
        kb.add(ResourceFactory.newByteArrayResource(rule2.getBytes("utf-8")), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kb.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(error);
        }
        KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
        kBase.addKnowledgePackages(kb.getKnowledgePackages());
        kSession = kBase.newStatefulKnowledgeSession();
        long endTime = System.currentTimeMillis();
        System.out.println("获取kSession耗时:" + (endTime - startTime));
        Message message1 = new Message();
        message1.setStatus(1);
        message1.setMsg("hello world!");
        Message message2 = new Message();
        message2.setStatus(2);
        message2.setMsg("hi world!");
        kSession.insert(message1);
        kSession.insert(message2);
        kSession.fireAllRules();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } finally {
        if (kSession != null)
            kSession.dispose();
    }
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBase(org.kie.internal.KnowledgeBase) Message(com.xiaolyuh.model.Message) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 38 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project kogito-runtimes by kiegroup.

the class ProcessDynamicNodeTest method TODOtestDynamicActions.

@Test
@Disabled("Not done yet")
public void TODOtestDynamicActions() {
    Reader source = new StringReader("<?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.drools.dynamic\" package-name=\"org.drools\" >\n" + "\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <dynamic id=\"2\" name=\"DynamicNode\" >\n" + "      <nodes>\n" + "        <actionNode id=\"1\" name=\"Action1\" >\n" + "          <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action1\");\n" + "list.add(\"Action1\");</action>\n" + "        </actionNode>\n" + "        <actionNode id=\"2\" name=\"Action2\" >\n" + "          <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action2\");\n" + "list.add(\"Action2\");</action>\n" + "        </actionNode>\n" + "        <actionNode id=\"3\" name=\"Action3\" >\n" + "          <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action3\");\n" + "list.add(\"Action3\");</action>\n" + "        </actionNode>\n" + "      </nodes>\n" + "      <connections>\n" + "        <connection from=\"1\" to=\"3\" />\n" + "      </connections>\n" + "      <in-ports/>\n" + "      <out-ports/>\n" + "    </dynamic>\n" + "    <actionNode id=\"3\" name=\"Action4\" >\n" + "      <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action4\");\n" + "list.add(\"Action4\");</action>\n" + "    </actionNode>\n" + "    <end id=\"4\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "    <connection from=\"3\" to=\"4\" />\n" + "  </connections>\n" + "</process>");
    builder.add(new ReaderResource(source), ResourceType.DRF);
    for (KnowledgeBuilderError error : builder.getErrors()) {
        logger.error(error.toString());
    }
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    List<String> list = new ArrayList<String>();
    kruntime.getKieSession().setGlobal("list", list);
    KogitoProcessInstance processInstance = kruntime.startProcess("org.drools.dynamic");
    assertEquals(KogitoProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals(4, list.size());
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.util.io.ReaderResource) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Disabled(org.junit.jupiter.api.Disabled)

Example 39 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project kogito-runtimes by kiegroup.

the class ProcessStartTest method testStartConstraintTrigger.

@Test
public void testStartConstraintTrigger() throws Exception {
    Reader source = new StringReader("<?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=\"flow\" id=\"org.drools.start\" package-name=\"org.jbpm\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"SomeVar\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "      <variable name=\"SomeOtherVar\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" >\n" + "      <triggers>" + "        <trigger type=\"constraint\" >\n" + "          <constraint type=\"rule\" dialect=\"mvel\" >p:Person()</constraint>\n" + "          <mapping type=\"in\" from=\"p.getName()\" to=\"SomeVar\" />\n" + "          <mapping type=\"in\" from=\"&quot;SomeString&quot;\" to=\"SomeOtherVar\" />\n" + "        </trigger>\n " + "      </triggers>\n" + "    </start>\n" + "    <actionNode id=\"2\" name=\"Action\" >\n" + "      <action type=\"expression\" dialect=\"java\" >myList.add(kcontext.getVariable(\"SomeVar\"));\n" + "myList.add(kcontext.getVariable(\"SomeOtherVar\"));</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    builder.addRuleFlow(source);
    if (!builder.getErrors().isEmpty()) {
        for (KnowledgeBuilderError error : builder.getErrors()) {
            logger.error(error.toString());
        }
        fail("Could not build process");
    }
    /*
         * This test cannot be migrated to use KogitoProcessRuntime because during ProcessRuntime initialization
         * multiple listeners are registered and this break the semantic of this test:
         * see ProcessRuntimeImpl#initProcessActivationListener() -> startProcessWithParamsAndTrigger (same for LightProcessRuntime)
         * This listener triggers a start process when the insert is performed (but no rules are fired) that is an expected behavior
         * for BPMN2 processes (see org.jbpm.bpmn2.StartEventTest#testConditionalStart() ) while for DRF processes produces an additional
         * unexpected (broken) execution
         */
    KieSession ksession = createKieSession();
    List<Message> myList = new ArrayList<>();
    ksession.setGlobal("myList", myList);
    assertEquals(0, myList.size());
    Person jack = new Person();
    jack.setName("Jack");
    ksession.insert(jack);
    ksession.fireAllRules();
    assertEquals(2, myList.size());
    assertEquals("Jack", String.valueOf(myList.get(0)));
    assertEquals("SomeString", String.valueOf(myList.get(1)));
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) Message(org.jbpm.integrationtests.test.Message) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 40 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project kogito-runtimes by kiegroup.

the class ProcessStartTest method testStartEventTrigger.

@Test
public void testStartEventTrigger() throws Exception {
    Reader source = new StringReader("<?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=\"flow\" id=\"org.drools.start\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"SomeVar\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "      <variable name=\"SomeOtherVar\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" >\n" + "      <triggers>" + "        <trigger type=\"event\" >\n" + "          <eventFilters>" + "            <eventFilter type=\"eventType\" eventType=\"myEvent\" />\n" + "          </eventFilters>" + "          <mapping type=\"in\" from=\"event\" to=\"SomeVar\" />\n" + "          <mapping type=\"in\" from=\"SomeString\" to=\"SomeOtherVar\" />\n" + "        </trigger>\n " + "      </triggers>\n" + "    </start>\n" + "    <actionNode id=\"2\" name=\"Action\" >\n" + "      <action type=\"expression\" dialect=\"java\" >myList.add(kcontext.getVariable(\"SomeVar\"));\n" + "myList.add(kcontext.getVariable(\"SomeOtherVar\"));</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    builder.add(new ReaderResource(source), ResourceType.DRF);
    if (!builder.getErrors().isEmpty()) {
        for (KnowledgeBuilderError error : builder.getErrors()) {
            logger.error(error.toString());
        }
        fail("Could not build process");
    }
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    List<Message> myList = new ArrayList<Message>();
    kruntime.getKieSession().setGlobal("myList", myList);
    assertEquals(0, myList.size());
    kruntime.signalEvent("myEvent", "Jack");
    kruntime.getKieSession().fireAllRules();
    assertEquals(2, myList.size());
    assertEquals("Jack", String.valueOf(myList.get(0)));
    assertEquals("SomeString", String.valueOf(myList.get(1)));
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) Message(org.jbpm.integrationtests.test.Message) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.util.io.ReaderResource) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)41 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)26 KnowledgeBuilderErrors (org.kie.internal.builder.KnowledgeBuilderErrors)16 StringReader (java.io.StringReader)10 ArrayList (java.util.ArrayList)9 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)9 Test (org.junit.Test)9 Reader (java.io.Reader)8 Test (org.junit.jupiter.api.Test)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 Resource (org.kie.api.io.Resource)6 ReaderResource (org.drools.util.io.ReaderResource)5 KogitoProcessRuntime (org.kie.kogito.internal.process.runtime.KogitoProcessRuntime)5 HashMap (java.util.HashMap)4 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)4 KieSession (org.kie.api.runtime.KieSession)4 ClassPathResource (org.drools.core.io.impl.ClassPathResource)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 CompositeKnowledgeBuilder (org.kie.internal.builder.CompositeKnowledgeBuilder)3