Java Code Examples for org.kie.internal.builder.KnowledgeBuilder#add()
The following examples show how to use
org.kie.internal.builder.KnowledgeBuilder#add() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoreBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 7 votes |
@Test public void testQuery() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(kbuilder.getKnowledgePackages()); ksession = createKnowledgeSession(kbase); ksession.insert( new Cheese( "stinky", 5 ) ); ksession.insert( new Cheese( "smelly", 7 ) ); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query")); Command<?> cmds = CommandFactory.newBatchExecution(commands); ExecutionResults result = (ExecutionResults) ksession.execute(cmds); assertNotNull(result, "Batch execution result is null!"); Object queryResultsObject = result.getValue("numStinkyCheeses"); assertTrue(queryResultsObject != null && queryResultsObject instanceof QueryResults, "Retrieved object is null or incorrect!"); assertEquals( 1, ((QueryResults) queryResultsObject).size() ); }
Example 2
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testTypeReDeclarationPojoLessFields() { String str1 = "" + "package org.drools \n" + "import " + TypeDeclarationTest.class.getName() + ".ClassC; \n" + "" + "declare " + TypeDeclarationTest.class.getName() + ".ClassC \n" + " name : String \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str1.getBytes() ), ResourceType.DRL ); if ( ! kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } }
Example 3
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * 2 resources (containing different declarations of the same type ) are added * to the kbuilder. * The expectation here is that the compilation fails because we are * trying to add an incompatible re-definition of the declared type: * it introduces a new field 'lastName' */ @Test public void testTypeReDeclarationWithExtraField2() { //same package, different resources String str1 = ""; str1 += "package org.drools.compiler \n" + "declare org.drools.ClassA \n" + " name : String \n" + " age : Integer \n" + "end \n"; String str2 = ""; str2 += "package org.drools.compiler \n" + "declare org.drools.ClassA \n" + " name : String \n" + " lastName : String \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str1.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( str2.getBytes() ), ResourceType.DRL ); if (!kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } }
Example 4
Source File: LegacyTraitTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private KieSession getSessionFromString(String drl) { KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL); if (knowledgeBuilder.hasErrors()) { throw new RuntimeException(knowledgeBuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(knowledgeBuilder.getKnowledgePackages()); KieSession session = kbase.newKieSession(); return session; }
Example 5
Source File: UnmarshallingTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private KieBase initializeKnowledgeBase( String rule ) { // Setup knowledge base KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL ); if ( kbuilder.hasErrors() ) { throw new RuntimeException( kbuilder.getErrors().toString() ); } KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase( config ); knowledgeBase.addPackages( kbuilder.getKnowledgePackages() ); return knowledgeBase; }
Example 6
Source File: TypeDeclarationMergingTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testNotOverwritePOJOMetadata() { final String eventClassName = PositionAnnotatedEvent.class.getCanonicalName(); // should add metadata to metadata already defined in POJO String str = "package org.drools.compiler.test \n" + "declare " + eventClassName + "\n" + " @role(event)\n" + "end \n" + "rule 'sample rule' \n" + "when \n" + " " + eventClassName + "( 'value1', 'value2'; ) \n" + "then \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); try { kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL ); } catch (IndexOutOfBoundsException e) { final String msg = e.getMessage(); if ( "Error trying to access field at position 0".equals( msg ) ) { fail( "@Position declared in POJO was ignored." ); } else { fail( "Check the test, unexpected error message: " + msg ); } } assertFalse(kbuilder.hasErrors(), "Check the test, unexpected error message: " + kbuilder.getErrors()); }
Example 7
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * 2 resources (containing different declarations of the same type ) are added * to the kbuilder. * The expectation here is that compilation fails because we are changing * the type of a field */ @Test public void testClashingTypeDeclarationInDifferentResources() { //same package, different resources String str1 = ""; str1 += "package org.drools.compiler \n" + "declare org.drools.ClassA \n" + " name : String \n" + " age : Integer \n" + "end \n"; String str2 = ""; str2 += "package org.drools.compiler \n" + "declare org.drools.ClassA \n" + " name : String \n" + " age : String \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str1.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( str2.getBytes() ), ResourceType.DRL ); if (!kbuilder.hasErrors() ) { fail( "An error should have been generated, redefinition of ClassA is not allowed" ); } }
Example 8
Source File: MVELTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testTokensInString(){ //should query antldr DFA63 class but don't know how final String [] operators = {"," ,"=" , "|=", "*"}; //test various in consequence final String strBegin = "" + "package org.kie \n" + "import org.drools.compiler.Cheese \n" + "dialect \"mvel\"\n" + "rule rule1 \n" + "when \n" + "$c:Cheese(type==\"swiss\") \n" + "then \n"+ "modify($c){ type = \"swiss"; final String strEnd = "good\"};\n" + "end\n"; final StringBuilder failures = new StringBuilder(); for (final String oper : operators) { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); final String rule = strBegin + oper + strEnd; System.out.print(rule); kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { failures.append(kbuilder.getErrors().toString()); } } final String failStr = failures.toString(); if (failStr.length() > 0) { fail(failStr); } }
Example 9
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 5 votes |
protected void testInvalidDrl(final String drl) { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { logger.warn(kbuilder.getErrors().toString()); } assertTrue(kbuilder.hasErrors()); }
Example 10
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public Collection<KiePackage> loadKnowledgePackagesFromString(KnowledgeBuilderConfiguration kbuilderConf, String... content) { if (kbuilderConf == null) { kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConf); for (String r : content) { kbuilder.add(ResourceFactory.newByteArrayResource(r.getBytes()),ResourceType.DRL); } if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } Collection<KiePackage> knowledgePackages = kbuilder.getKnowledgePackages(); return knowledgePackages; }
Example 11
Source File: JBRULESTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testJBRULES3030() { final String str = "package org.drools.compiler\n" + "rule X\n" + "when\n" + " $gp : GrandParent()" + " $ch : ChildHolder( child == $gp )\n" + "then\n" + "end\n"; final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); }
Example 12
Source File: SessionInspectorTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testGetSessionInfoWithCustomTemplate() { if ( System.getProperty("java.vendor").toUpperCase().contains("IBM") ) { return; //Does not work in the IBM JDK due to a bug in MVEL } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_SubNetworks.drl" ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_AccumulateWithFromChaining.drl"), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_CollectResultsBetaConstraint.drl" ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_QueryMemoryLeak.drl"), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = createKnowledgeSession(kbase); ksession.setGlobal( "results", new ArrayList<Object>() ); ksession.insert( new Dimension( 100, 50 ) ); ksession.insert( new Dimension( 130, 80 ) ); ksession.insert( new Dimension( 50, 40 ) ); ksession.insert( new Dimension( 50, 40 ) ); Cheesery cheesery = new Cheesery(); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); ksession.insert( cheesery ); ksession.insert( new Person( "Bob", "muzzarella") ); ksession.insert( new Person( "Mark", "brie") ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Double( 10 ) ); ksession.insert( new Double( 11 ) ); ksession.insert( new Double( 12 ) ); ksession.insert( new Double( 13 ) ); ksession.insert( new Double( 14 ) ); ksession.insert( new Integer( 15 ) ); ksession.insert( new Integer( 16 ) ); ksession.insert( new Integer( 17 ) ); ksession.insert( new Integer( 18 ) ); FactHandle handle = ksession.insert( new Worker( ) ); ksession.retract( handle ); SessionInspector inspector = new SessionInspector( ksession ); StatefulKnowledgeSessionInfo info = inspector.getSessionInfo(); SessionReporter.addNamedTemplate( "topten", getClass().getResourceAsStream( "customreports.mvel" ) ); String report = SessionReporter.generateReport( "topten", info, null ); assertNotNull( report ); }
Example 13
Source File: ProcessActionTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testOnEntryExit() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); 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.actions\" package-name=\"org.drools\" version=\"1\" >\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" + " <workItem id=\"2\" name=\"HumanTask\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>John Doe</value>\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something</value>\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " <onEntry>\n" + " <action type=\"expression\" name=\"Print\" dialect=\"mvel\" >list.add(\"Executing on entry action\");</action>\n" + " </onEntry>\n" + " <onExit>\n" + " <action type=\"expression\" name=\"Print\" dialect=\"java\" >list.add(\"Executing on exit action1\");</action>\n" + " <action type=\"expression\" name=\"Print\" dialect=\"java\" >list.add(\"Executing on exit action2\");</action>\n" + " </onExit>\n" + " </workItem>\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>"); kbuilder.add(new ReaderResource(source), ResourceType.DRF); KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); TestWorkItemHandler handler = new TestWorkItemHandler(); ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ProcessInstance processInstance = ksession.startProcess("org.drools.actions"); assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState()); WorkItem workItem = handler.getWorkItem(); assertNotNull(workItem); assertEquals(1, list.size()); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); assertEquals(3, list.size()); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); }
Example 14
Source File: ProcessDynamicNodeTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testAddDynamicSubProcess() { 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" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <dynamic id=\"2\" name=\"DynamicNode\" >\n" + " <completionCondition>autocomplete</completionCondition>\n" + " <nodes>\n" + " <actionNode id=\"1\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action\");</action>\n" + " </actionNode>\n" + " </nodes>\n" + " <connections>\n" + " </connections>\n" + " <in-ports/>\n" + " <out-ports/>\n" + " </dynamic>\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" + "</process>"); Reader source2 = 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=\"subflow\" id=\"org.drools.subflow\" package-name=\"org.drools\" >\n" + "\n" + " <header>\n" + " <variables>\n" + " <variable name=\"x\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>SomeText</value>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <actionNode id=\"2\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(x);</action>\n" + " </actionNode>\n" + " <workItem id=\"3\" name=\"Work\" >\n" + " <work name=\"Human Task\" />\n" + " </workItem>\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>"); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF); kbuilder.add(ResourceFactory.newReaderResource(source2), ResourceType.DRF); KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test"); TestWorkItemHandler handler = new TestWorkItemHandler(); ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler); // start a new process instance ProcessInstance processInstance = (ProcessInstance) ksession.startProcess("org.drools.dynamic"); DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next(); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("x", "NewValue"); assertNull(handler.getWorkItem()); assertEquals(0, dynamicContext.getNodeInstances().size()); DynamicUtils.addDynamicSubProcess(dynamicContext, ksession, "org.drools.subflow", parameters); assertNotNull(handler.getWorkItem()); assertEquals(1, dynamicContext.getNodeInstances().size()); logger.close(); }
Example 15
Source File: ProcessDynamicNodeTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testAddDynamicWorkItem() { 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" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <dynamic id=\"2\" name=\"DynamicNode\" >\n" + " <completionCondition>autocomplete</completionCondition>\n" + " <nodes>\n" + " <actionNode id=\"1\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action\");</action>\n" + " </actionNode>\n" + " </nodes>\n" + " <connections>\n" + " </connections>\n" + " <in-ports/>\n" + " <out-ports/>\n" + " </dynamic>\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" + "</process>"); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF); KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test"); TestWorkItemHandler handler = new TestWorkItemHandler(); ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler); // start a new process instance ProcessInstance processInstance = (ProcessInstance) ksession.startProcess("org.drools.dynamic"); DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next(); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("TaskName", "Dynamic Task"); assertNull(handler.getWorkItem()); assertEquals(0, dynamicContext.getNodeInstances().size()); DynamicUtils.addDynamicWorkItem(dynamicContext, ksession, "Human Task", parameters); assertNotNull(handler.getWorkItem()); assertEquals(1, dynamicContext.getNodeInstances().size()); logger.close(); }
Example 16
Source File: PhreakLiaNodeTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void test() { String str = "package org.drools.compiler.test\n" + "\n" + "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "\n" + "rule r1 \n" + " when \n" + " $a : A( object == 1 )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n" + "rule r2 \n" + " when \n" + " $a : A( object == 2 )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n " + "rule r3 \n" + " when \n" + " $a : A( object == 2 )\n" + " $b : B( )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n " + "rule r4 \n" + " when \n" + " $a : A( object == 3 )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n"; KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add( ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if ( builder.hasErrors() ) { throw new RuntimeException(builder.getErrors().toString()); } InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages(builder.getKnowledgePackages()); KieSession ksession = knowledgeBase.newKieSession(); InternalFactHandle fhB = ( InternalFactHandle ) ksession.insert( B.b(1) ); InternalFactHandle fhA = ( InternalFactHandle ) ksession.insert( A.a(1) ); ksession.fireAllRules(); System.out.println( "---1---" ); // ksession.update( fhA, a(1) ); // ksession.fireAllRules(); // System.out.println( "---2---" ); // ksession.update( fhA, A.a(2) ); ksession.fireAllRules(); System.out.println( "---3---" ); ksession.update( fhA, A.a(2) ); ksession.fireAllRules(); System.out.println( "---4---" ); ksession.update( fhA, A.a(3) ); ksession.fireAllRules(); System.out.println( "---5---" ); ksession.update( fhB, B.b(1) ); ksession.update( fhA, A.a(3) ); ksession.fireAllRules(); // ksession.update( fhA, a(1) ); // ksession.fireAllRules(); // // ksession.update( fhA, a(1) ); // ksession.fireAllRules(); ksession.dispose(); }
Example 17
Source File: LinkingTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testJoinNodes() throws Exception { String str = ""; str += "package org.kie \n"; str += "import " + A.class.getCanonicalName() + "\n" ; str += "import " + B.class.getCanonicalName() + "\n" ; str += "import " + C.class.getCanonicalName() + "\n" ; str += "import " + D.class.getCanonicalName() + "\n" ; str += "import " + E.class.getCanonicalName() + "\n" ; str += "import " + F.class.getCanonicalName() + "\n" ; str += "import " + G.class.getCanonicalName() + "\n" ; str += "global java.util.List list \n"; str += "rule rule1 when \n"; str += " $a : A() \n"; str += " $b : B() \n"; str += " $c : C() \n"; str += "then \n"; str += " list.add( $a.getValue() + \":\"+ $b.getValue() + \":\" + $c.getValue() ); \n"; str += "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL ); assertFalse( kbuilder.hasErrors(), kbuilder.getErrors().toString()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class ); ObjectTypeNode botn = getObjectTypeNode(kbase, B.class ); ObjectTypeNode cotn = getObjectTypeNode(kbase, C.class ); InternalWorkingMemory wm = (InternalWorkingMemory)kbase.newKieSession(); List list = new ArrayList(); wm.setGlobal( "list", list ); for ( int i = 0; i < 3; i++ ) { wm.insert( new A(i) ); } for ( int i = 0; i < 3; i++ ) { wm.insert( new B(i) ); } for ( int i = 0; i < 29; i++ ) { wm.insert( new C(i) ); } wm.flushPropagations(); LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0]; JoinNode bNode = ( JoinNode) aNode.getSinkPropagator().getSinks()[0]; JoinNode cNode = ( JoinNode) bNode.getSinkPropagator().getSinks()[0]; LiaNodeMemory amem = ( LiaNodeMemory ) wm.getNodeMemory( aNode ); BetaMemory bmem = ( BetaMemory ) wm.getNodeMemory( bNode ); BetaMemory cmem = ( BetaMemory ) wm.getNodeMemory( cNode ); // amem.getSegmentMemory().getStagedLeftTuples().insertSize() == 3 assertNotNull( amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst() ); assertNotNull( amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext() ); assertNotNull( amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext() ); assertNull( amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext() ); //assertEquals( 3, bmem.getStagedRightTuples().insertSize() ); assertNotNull( bmem.getStagedRightTuples().getInsertFirst() ); assertNotNull( bmem.getStagedRightTuples().getInsertFirst().getStagedNext() ); assertNotNull( bmem.getStagedRightTuples().getInsertFirst().getStagedNext().getStagedNext() ); assertNull( bmem.getStagedRightTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext() ); wm.fireAllRules(); assertNull( amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst() ); assertNull( bmem.getStagedRightTuples().getInsertFirst() ); assertNull( cmem.getStagedRightTuples().getInsertFirst() ); assertEquals( 261, list.size() ); assertTrue( list.contains( "2:2:14") ); assertTrue( list.contains( "1:0:6") ); assertTrue( list.contains( "0:1:1") ); assertTrue( list.contains( "2:2:14") ); assertTrue( list.contains( "0:0:25") ); }
Example 18
Source File: LinkingTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testSubNetworkSharing() throws Exception { // Checks the network is correctly formed, with sharing String str = ""; str += "package org.kie \n"; str += "import " + A.class.getCanonicalName() + "\n" ; str += "import " + B.class.getCanonicalName() + "\n" ; str += "import " + C.class.getCanonicalName() + "\n" ; str += "import " + D.class.getCanonicalName() + "\n" ; str += "import " + E.class.getCanonicalName() + "\n" ; str += "global java.util.List list \n"; str += "rule rule1 when \n"; str += " A() \n"; str += " B() \n"; str += " C() \n"; str += " D() \n"; str += "then \n"; str += "end \n"; str += "rule rule2 when \n"; str += " A() \n"; str += " exists( B() and C() ) \n"; str += " D() \n"; str += "then \n"; str += "end \n"; str += "rule rule3 when \n"; str += " A() \n"; str += " exists( B() and C() and D() ) \n"; str += " E() \n"; str += "then \n"; str += "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL ); assertFalse( kbuilder.hasErrors(), kbuilder.getErrors().toString()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); ObjectTypeNode node = getObjectTypeNode(kbase, A.class ); InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl)kbase.newKieSession()); LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0]; assertEquals( 3, liaNode.getSinkPropagator().size() ); ExistsNode existsNode2 = ( ExistsNode) liaNode.getSinkPropagator().getSinks()[1]; ExistsNode existsNode3 = ( ExistsNode) liaNode.getSinkPropagator().getSinks()[2]; JoinNode joinNodeB = ( JoinNode) liaNode.getSinkPropagator().getSinks()[0]; assertSame( joinNodeB.getRightInput(), getObjectTypeNode(kbase, B.class ) ); JoinNode joinNodeC = ( JoinNode) joinNodeB.getSinkPropagator().getSinks()[0]; assertSame( joinNodeC.getRightInput(), getObjectTypeNode(kbase, C.class ) ); assertEquals( 2, joinNodeC.getSinkPropagator().size() ); JoinNode joinNodeD = ( JoinNode) joinNodeC.getSinkPropagator().getSinks()[0]; assertSame( joinNodeD.getRightInput(), getObjectTypeNode(kbase, D.class ) ); assertEquals( 2, joinNodeD.getSinkPropagator().size() ); assertSame( existsNode2, (( RightInputAdapterNode) joinNodeC.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0] ); assertSame( existsNode3, (( RightInputAdapterNode) joinNodeD.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0] ); }
Example 19
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDeclareFieldArray() { String str1 = "" + "package org.drools " + "declare Test end " + "declare Pet " + " owners : Owner[] " + " twoDimArray : Foo[][] " + " friends : Pet[] " + " ages : int[] " + "end " + "declare Owner " + " name : String " + "end " + "declare Foo end " + ""; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str1.getBytes() ), ResourceType.DRL ); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } for( KiePackage kp : kbuilder.getKnowledgePackages() ) { if ( kp.getName().equals( "org.drools" ) ) { Collection<FactType> types = kp.getFactTypes(); for ( FactType type : types ) { if ( "org.drools.Pet".equals( type.getName() ) ) { assertEquals( 4, type.getFields().size() ); FactField owners = type.getField( "owners" ); assertTrue( owners != null && owners.getType().getSimpleName().equals( "Owner[]" ) && owners.getType().isArray() ); FactField twoDim = type.getField( "twoDimArray" ); assertTrue( twoDim != null && twoDim.getType().getSimpleName().equals( "Foo[][]" ) && twoDim.getType().isArray() ); FactField friends = type.getField( "friends" ); assertTrue( friends != null && friends.getType().getSimpleName().equals( "Pet[]" ) && friends.getType().isArray() ); FactField ages = type.getField( "ages" ); assertTrue( ages != null && ages.getType().getSimpleName().equals( "int[]" ) && ages.getType().isArray() ); } } } } }
Example 20
Source File: MVELTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDuplicateLocalVariableMVELConsequence() { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_DuplicateLocalVariableMVELConsequence.drl")), ResourceType.DRL); assertTrue( kbuilder.hasErrors() ); }