Java Code Examples for org.kie.api.runtime.KieSession#update()
The following examples show how to use
org.kie.api.runtime.KieSession#update() .
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: JBRULESTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testJBRules2369() { final KieBase kbase = loadKnowledgeBase("test_JBRules2369.drl"); final KieSession ksession = createKnowledgeSession(kbase); final List<String> results = new ArrayList<String>(); ksession.setGlobal("results", results); final FactA a = new FactA(); final FactB b = new FactB(Integer.valueOf(0)); final FactHandle aHandle = ksession.insert(a); final FactHandle bHandle = ksession.insert(b); ksession.fireAllRules(); assertEquals(1, results.size()); ksession.update(aHandle, a); ksession.fireAllRules(); assertEquals(2, results.size()); }
Example 2
Source File: NullCheckOnExistentialNodeTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private void check( String drl, int fire1, int fire2 ) { KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL ).build().newKieSession(); A a1 = new A(); A a2 = new A(); FactHandle fhA1 = kieSession.insert( a1 ); FactHandle fhA2 = kieSession.insert( a2 ); kieSession.insert( "xxx" ); assertEquals( fire1, kieSession.fireAllRules() ); a1.setB( null ); kieSession.update( fhA1, a1 ); a2.setB( new B( null ) ); kieSession.update( fhA2, a2 ); assertEquals( fire2, kieSession.fireAllRules() ); }
Example 3
Source File: PropertyReactivityTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testUpdateOnNonExistingProperty() { // DROOLS-2170 final String str1 = "import " + Klass.class.getCanonicalName() + "\n" + "rule R when\n" + " Klass( b == 2 )\n" + "then\n" + "end\n"; final KieSession ksession = new KieHelper().addContent( str1, ResourceType.DRL ) .build() .newKieSession(); final Klass bean = new Klass( 1, 2, 3, 4, 5, 6 ); final FactHandle fh = ksession.insert( bean ); ksession.fireAllRules(); try { ksession.update( fh, bean, "z" ); fail("Trying to update not existing property must fail"); } catch (Exception e) { assertTrue( e.getMessage().contains( Klass.class.getName() ) ); } }
Example 4
Source File: PropertyReactivityTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testSetterInConcreteClass() { // DROOLS-1368 final String drl = "import " + BaseFact.class.getCanonicalName() + ";\n" + "import " + MyFact.class.getCanonicalName() + ";\n" + "rule R when\n" + " $b : BaseFact( $n : name, name != null )\n" + "then end\n"; final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(); final MyFact f = new MyFact(); final FactHandle fh = ksession.insert(f); assertEquals( 0, ksession.fireAllRules() ); f.setName("hello"); ksession.update(fh, f, "name"); assertEquals( 1, ksession.fireAllRules() ); }
Example 5
Source File: WrongServiceRulesJUnitTest.java From drools-workshop with Apache License 2.0 | 6 votes |
@Test public void testServiceCallInRHS() { Assert.assertNotNull(kBase); KieSession kSession = kBase.newKieSession(); kSession.setGlobal("myDataProviderService", new MyDataProviderServiceImpl()); System.out.println(" ---- Starting testServiceCallInRHS() Test ---"); University university = new University("My Uni"); FactHandle univFactHandle = kSession.insert(university); Assert.assertEquals(50, kSession.fireAllRules()); university.setName("My Uni (updated)"); kSession.update(univFactHandle, university); Assert.assertEquals(50, kSession.fireAllRules()); System.out.println(" ---- Finished testServiceCallInRHS() Test ---"); kSession.dispose(); }
Example 6
Source File: ParallelEvaluationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private Callable<Void> getMultipleParallelKieSessionsWithUpdatesCallable(KieBase kBase) { return new Callable<Void>() { @Override public Void call() { KieSession ksession = kBase.newKieSession(); assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).as("Parallel agenda has to be enabled").isTrue(); List<Integer> list = new DebugList<Integer>(); ksession.setGlobal( "list", list ); FactHandle[] fhs = new FactHandle[10]; fhs = insertFacts(ksession, 10); ksession.fireAllRules(); assertThat(list.size()).isEqualTo(10); list.clear(); for (int i = 0; i < 10; i++) { ksession.update( fhs[i], i ); } ksession.fireAllRules(); assertThat(list.size()).isEqualTo(10); return null; } }; }
Example 7
Source File: ParallelEvaluationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testWithUpdates() { StringBuilder sb = new StringBuilder( 400 ); sb.append( "global java.util.List list;\n" ); for (int i = 0; i < 10; i++) { sb.append( getRule( i, "" ) ); } KieSession ksession = new KieHelper().addContent( sb.toString(), ResourceType.DRL ) .build( MultithreadEvaluationOption.YES ) .newKieSession(); assertTrue( ( (InternalWorkingMemory) ksession ).getAgenda().isParallelAgenda() ); List<Integer> list = new DebugList<Integer>(); ksession.setGlobal( "list", list ); FactHandle[] fhs = new FactHandle[10]; for (int i = 0; i < 10; i++) { fhs[i] = ksession.insert( i ); ksession.insert( "" + i ); } ksession.fireAllRules(); assertEquals(10, list.size()); list.clear(); for (int i = 0; i < 10; i++) { ksession.update( fhs[i], i ); } ksession.fireAllRules(); assertEquals(10, list.size()); }
Example 8
Source File: AbstractConcurrentInsertionsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
protected Callable<Boolean> getTask(final int objectCount, final KieSession ksession, final boolean disposeSession, final boolean updateFacts) { return () -> { try { for (int j = 0; j < 10; j++) { final FactHandle[] facts = new FactHandle[objectCount]; final FactHandle[] stringFacts = new FactHandle[objectCount]; for (int i = 0; i < objectCount; i++) { facts[i] = ksession.insert(new AtomicInteger(i)); stringFacts[i] = ksession.insert("test_" + i); } if (updateFacts) { for (int i = 0; i < objectCount; i++) { ksession.update(facts[i], new AtomicInteger(-i)); ksession.update(stringFacts[i], "updated_test_" + i); } } for (int i = 0; i < objectCount; i++) { ksession.delete(facts[i]); ksession.delete(stringFacts[i]); } ksession.fireAllRules(); } return true; } catch (final Exception e) { e.printStackTrace(); return false; } finally { if (disposeSession) { ksession.dispose(); } } }; }
Example 9
Source File: OOPathBenchmarkTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public static long[] testRelational(KieBase kbase, int n) { final long[] result = new long[2]; final KieSession ksession = kbase.newKieSession(); final List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); final List<Man> model = generateModel(n); final List<Child> toBeModified = getChildToBeModified(model); long start = System.nanoTime(); final List<InternalFactHandle> fhs = insertFullModel(ksession, model); ksession.fireAllRules(); result[0] = System.nanoTime() - start; list.clear(); start = System.nanoTime(); for (Child child : toBeModified) { child.setAge(11); } for (InternalFactHandle fh : fhs) { ksession.update(fh, fh.getObject()); } ksession.fireAllRules(); result[1] = System.nanoTime() - start; assertThat(n).isEqualTo(list.size()); ksession.dispose(); return result; }
Example 10
Source File: PropertyReactivityBlockerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test() public void testA_NotWorking() { // DROOLS-644 String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $p1 : Person( name == \"Mario\" ) \n" + " $p2 : Person( age > $p1.age ) \n" + "then\n" + " list.add(\"t0\");\n" + "end\n"; KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL) .build() .newKieSession(); ReteDumper.dumpRete(ksession); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); Person mario = new Person("Mario", 40); Person mark = new Person("Mark", 37); FactHandle fh_mario = ksession.insert(mario); ksession.insert(mark); ksession.fireAllRules(); mario.setAge(35); ksession.update(fh_mario, mario, "age"); int x = ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals("t0", list.get(0)); }
Example 11
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testFromInsideNotAndExists() throws Exception { KieBase kbase = loadKnowledgeBase("test_FromInsideNotAndExists.drl"); KieSession workingMemory = createKnowledgeSession(kbase); final List list = new ArrayList(); workingMemory.setGlobal( "results", list ); final Cheese cheddar = new Cheese( "cheddar", 7 ); final Cheese provolone = new Cheese( "provolone", 5 ); final Cheesery cheesery = new Cheesery(); cheesery.addCheese( cheddar ); cheesery.addCheese( provolone ); FactHandle handle = (FactHandle) workingMemory.insert( cheesery ); workingMemory.fireAllRules(); assertEquals( 0, list.size() ); cheesery.addCheese( new Cheese( "stilton", 10 ) ); cheesery.removeCheese( cheddar ); workingMemory.update( handle, cheesery ); workingMemory.fireAllRules(); assertEquals( 2, list.size() ); }
Example 12
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testApplyBlockerSecondAfterUpdate() { KieSession ksession = getStatefulKnowledgeSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go1 = ksession.insert( "go1" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); list.clear(); FactHandle go2 = ksession.insert( "go2" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.update( go1, "go1" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); }
Example 13
Source File: DynamicRulesTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDynamicRuleRemovalsSubNetwork() throws Exception { Collection<KiePackage> kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages( "test_DynamicRulesWithSubnetwork1.drl", "test_DynamicRulesWithSubnetwork.drl" ) ); InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase(); kbase.addPackages( kpkgs ); kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages( "test_DynamicRulesWithSubnetwork2.drl" ) ); kbase.addPackages( kpkgs ); KieSession session = createKnowledgeSession( kbase ); final List<?> list = new ArrayList<Object>(); session.setGlobal( "results", list ); Order order = new Order(); OrderItem item1 = new OrderItem( order, 1, "Adventure Guide Brazil", OrderItem.TYPE_BOOK, 24 ); order.addItem( item1 ); FactHandle item1Fh = session.insert( item1 ); OrderItem item2 = new OrderItem( order, 2, "Prehistoric Britain", OrderItem.TYPE_BOOK, 15 ); order.addItem( item2 ); FactHandle item2Fh = session.insert( item2 ); OrderItem item3 = new OrderItem( order, 3, "Holiday Music", OrderItem.TYPE_CD, 9 ); order.addItem( item3 ); FactHandle item3Fh = session.insert( item3 ); OrderItem item4 = new OrderItem( order, 4, "Very Best of Mick Jagger", OrderItem.TYPE_CD, 11 ); order.addItem( item4 ); FactHandle item4Fh = session.insert( item4 ); session.insert( order ); session.fireAllRules(); assertEquals( 11, list.size() ); kbase.removeRule( "org.drools.compiler", "Apply Discount on all books" ); list.clear(); session.update( item1Fh, item1 ); session.update( item2Fh, item2 ); session.update( item3Fh, item3 ); session.update( item4Fh, item4 ); session.fireAllRules(); assertEquals( 10, list.size() ); kbase.removeRule( "org.drools.compiler", "like book" ); list.clear(); session.update( item1Fh, item1 ); session.update( item2Fh, item2 ); session.update( item3Fh, item3 ); session.update( item4Fh, item4 ); session.fireAllRules(); assertEquals( 8, list.size() ); final OrderItem item5 = new OrderItem( order, 5, "Sinatra : Vegas", OrderItem.TYPE_CD, 5 ); FactHandle item5Fh = session.insert( item5 ); session.fireAllRules(); assertEquals( 10, list.size() ); kbase.removeKiePackage( "org.drools.compiler" ); list.clear(); session.update( item1Fh, item1 ); session.update( item2Fh, item2 ); session.update( item3Fh, item3 ); session.update( item4Fh, item4 ); session.update( item5Fh, item5 ); session.fireAllRules(); assertEquals( 0, list.size() ); }
Example 14
Source File: UpdateTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testNotIterativeModifyBug() { // JBRULES-2809 // This bug occurs when a tuple is modified, the remove/add puts it onto the memory end // However before this was done it would attempt to find the next tuple, starting from itself // This meant it would just re-add itself as the blocker, but then be moved to end of the memory // If this tuple was then removed or changed, the blocked was unable to check previous tuples. String str = ""; str += "package org.simple \n"; str += "import " + AFact.class.getCanonicalName() + "\n"; str += "global java.util.List list \n"; str += "rule xxx \n"; str += "when \n"; str += " $f1 : AFact() \n"; str += " not AFact(this != $f1, eval(field2 == $f1.getField2())) \n"; str += " eval( !$f1.getField1().equals(\"1\") ) \n"; str += "then \n"; str += " list.add($f1); \n"; str += "end \n"; final KieBase kbase = loadKnowledgeBaseFromString(str); final KieSession ksession = createKnowledgeSession(kbase); final List list = new ArrayList(); ksession.setGlobal("list", list); final AFact a1 = new AFact("2", "2"); final AFact a2 = new AFact("1", "2"); final AFact a3 = new AFact("1", "2"); final FactHandle fa1 = ksession.insert(a1); final FactHandle fa2 = ksession.insert(a2); final FactHandle fa3 = ksession.insert(a3); ksession.fireAllRules(); // a1 is blocked by a2 assertEquals(0, list.size()); // modify a2, so that a1 is now blocked by a3 a2.setField2("1"); // Do ksession.update(fa2, a2); a2.setField2("2"); // Undo ksession.update(fa2, a2); // modify a3 to cycle, so that it goes on the memory end, but in a previous bug still blocked a1 ksession.update(fa3, a3); a3.setField2("1"); // Do ksession.update(fa3, a3); ksession.fireAllRules(); assertEquals(0, list.size()); // this should still now blocked by a2, but bug from previous update hanging onto blocked ksession.dispose(); }
Example 15
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 16
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testTMSWithLateUpdate() { // JBRULES-3416 String str =""+ "package org.drools.compiler.test;\n" + "\n" + "import org.drools.compiler.Father;\n" + "import org.drools.compiler.YoungestFather;\n" + "\n" + "rule \"findMarriedCouple\"\n" + "when\n" + " $h: Father()\n" + " not Father(father == $h)\n" + "then\n" + " insertLogical(new YoungestFather($h));\n" + "end"; KieBase kbase = loadKnowledgeBaseFromString( str ); KieSession kSession = createKnowledgeSession(kbase); try { Father abraham = new Father("abraham"); Father bart = new Father("bart"); Collection<? extends Object> youngestFathers; bart.setFather(abraham); FactHandle abrahamHandle = kSession.insert(abraham); FactHandle bartHandle = kSession.insert(bart); kSession.fireAllRules(); youngestFathers = kSession.getObjects( new ClassObjectFilter(YoungestFather.class) ); assertEquals(1, youngestFathers.size()); assertEquals(bart, ((YoungestFather) youngestFathers.iterator().next()).getMan()); Father homer = new Father("homer"); FactHandle homerHandle = kSession.insert(homer); homer.setFather(abraham); // If we do kSession.update(homerHandle, homer) here instead of after bart.setFather(homer) it works // But in some use cases we cannot do this because fact fields are actually called // while the facts are in an invalid temporary state bart.setFather(homer); // Late update call for homer, after bart has been changed too, but before fireAllRules kSession.update(homerHandle, homer); kSession.update(bartHandle, bart); kSession.fireAllRules(); youngestFathers = kSession.getObjects( new ClassObjectFilter(YoungestFather.class) ); assertEquals(1, youngestFathers.size()); assertEquals(bart, ((YoungestFather) youngestFathers.iterator().next()).getMan()); } finally { kSession.dispose(); } }
Example 17
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test // public void testLogicalInsertions3() throws Exception { KieBase kbase = loadKnowledgeBase("test_logicalInsertions3.drl"); KieSession ksession = kbase.newKieSession(); try { final List list = new ArrayList(); ksession.setGlobal( "events", list ); // asserting the sensor object final Sensor sensor = new Sensor( 150, 100 ); FactHandle sensorHandle = ksession.insert( sensor ); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession( ksession, true ); // alarm must sound assertEquals(2, list.size()); assertEquals(2, ksession.getObjects().size()); // modifying sensor sensor.setTemperature( 125 ); sensorHandle = getFactHandle( sensorHandle, ksession ); ksession.update( sensorHandle, sensor ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); // alarm must continue to sound assertEquals(3, list.size()); assertEquals(2, ksession.getObjects().size()); // modifying sensor sensor.setTemperature( 80 ); sensorHandle = getFactHandle( sensorHandle, ksession ); ksession.update( sensorHandle, sensor ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); // no alarms anymore assertEquals(3, list.size()); assertEquals(1, ksession.getObjects().size()); } finally { ksession.dispose(); } }
Example 18
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test //@Disabled("in Java 8, the byte[] generated by serialization are not the same and requires investigation") public void testLogicalInsertionsWithExists() throws Exception { KieBase kbase = loadKnowledgeBase("test_LogicalInsertionWithExists.drl"); KieSession ksession = kbase.newKieSession(); try { final Person p1 = new Person( "p1", "stilton", 20 ); p1.setStatus( "europe" ); FactHandle c1FactHandle = ksession.insert( p1 ); final Person p2 = new Person( "p2", "stilton", 30 ); p2.setStatus( "europe" ); FactHandle c2FactHandle = ksession.insert( p2 ); final Person p3 = new Person( "p3", "stilton", 40 ); p3.setStatus( "europe" ); FactHandle c3FactHandle = ksession.insert( p3 ); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); // all 3 in europe, so, 2 cheese Collection cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(2, cheeseList.size()); // europe=[ 1, 2 ], america=[ 3 ] p3.setStatus( "america" ); c3FactHandle = getFactHandle( c3FactHandle, ksession ); ksession.update( c3FactHandle, p3 ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(1, cheeseList.size()); // europe=[ 1 ], america=[ 2, 3 ] p2.setStatus( "america" ); c2FactHandle = getFactHandle( c2FactHandle, ksession ); ksession.update( c2FactHandle, p2 ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(1, cheeseList.size()); // europe=[ ], america=[ 1, 2, 3 ] p1.setStatus( "america" ); c1FactHandle = getFactHandle( c1FactHandle, ksession ); ksession.update( c1FactHandle, p1 ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(2, cheeseList.size()); // europe=[ 2 ], america=[ 1, 3 ] p2.setStatus( "europe" ); c2FactHandle = getFactHandle( c2FactHandle, ksession ); ksession.update( c2FactHandle, p2 ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(1, cheeseList.size()); // europe=[ 1, 2 ], america=[ 3 ] p1.setStatus( "europe" ); c1FactHandle = getFactHandle( c1FactHandle, ksession ); ksession.update( c1FactHandle, p1 ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(1, cheeseList.size()); // europe=[ 1, 2, 3 ], america=[ ] p3.setStatus( "europe" ); c3FactHandle = getFactHandle( c3FactHandle, ksession ); ksession.update( c3FactHandle, p3 ); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); ksession.fireAllRules(); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class)); assertEquals(2, cheeseList.size()); } finally { ksession.dispose(); } }
Example 19
Source File: DynamicRulesTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDynamicRulesAddRemove() { try { InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase( "test_DynamicRulesTom.drl" ); KieSession session = createKnowledgeSession( kbase ); List<?> results = new ArrayList<Object>(); session.setGlobal( "results", results ); InternalFactHandle h1 = (InternalFactHandle) session.insert( new Person( "tom", 1 ) ); InternalFactHandle h2 = (InternalFactHandle) session.insert( new Person( "fred", 2 ) ); InternalFactHandle h3 = (InternalFactHandle) session.insert( new Person( "harry", 3 ) ); InternalFactHandle h4 = (InternalFactHandle) session.insert( new Person( "fred", 4 ) ); InternalFactHandle h5 = (InternalFactHandle) session.insert( new Person( "ed", 5 ) ); InternalFactHandle h6 = (InternalFactHandle) session.insert( new Person( "tom", 6 ) ); InternalFactHandle h7 = (InternalFactHandle) session.insert( new Person( "sreeni", 7 ) ); InternalFactHandle h8 = (InternalFactHandle) session.insert( new Person( "jill", 8 ) ); InternalFactHandle h9 = (InternalFactHandle) session.insert( new Person( "ed", 9 ) ); InternalFactHandle h10 = (InternalFactHandle) session.insert( new Person( "tom", 10 ) ); session.fireAllRules(); assertEquals( 3, results.size() ); assertTrue( results.contains( h1.getObject() ) ); assertTrue( results.contains( h6.getObject() ) ); assertTrue( results.contains( h10.getObject() ) ); results.clear(); kbase.addPackages( loadKnowledgePackages( "test_DynamicRulesFred.drl" ) ); session.fireAllRules(); assertEquals( 2, results.size() ); assertTrue( results.contains( h2.getObject() ) ); assertTrue( results.contains( h4.getObject() ) ); results.clear(); kbase.removeKiePackage( "tom" ); kbase.addPackages( loadKnowledgePackages( "test_DynamicRulesEd.drl" ) ); session.fireAllRules(); assertEquals( 2, results.size() ); assertTrue( results.contains( h5.getObject() ) ); assertTrue( results.contains( h9.getObject() ) ); results.clear(); ((Person) h3.getObject()).setName( "ed" ); session.update( h3, h3.getObject() ); session.fireAllRules(); assertEquals( 1, results.size() ); assertTrue( results.contains( h3.getObject() ) ); } catch ( Exception e ) { e.printStackTrace(); fail( "Should not raise any exception: " + e.getMessage() ); } }
Example 20
Source File: PhreakLiaNodeTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void test2() { 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 + \" : \" + $b );" + " modify($a) { setObject(3) }; \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---" ); // InternalFactHandle fhB2 = ( InternalFactHandle ) ksession.insert( B.b(2) ); InternalFactHandle fhB3 = ( InternalFactHandle ) ksession.insert( B.b(3) ); ksession.update( fhA, A.a(2) ); ksession.fireAllRules(); System.out.println( "---3---" ); // ksession.update( fhA, a(2) ); // ksession.fireAllRules(); // System.out.println( "---4---" ); // // ksession.update( fhA, a(3) ); // ksession.fireAllRules(); // System.out.println( "---5---" ); // // ksession.update( fhB, b(1) ); // // ksession.update( fhA, a(3) ); // ksession.fireAllRules(); // ksession.update( fhA, a(1) ); // ksession.fireAllRules(); // // ksession.update( fhA, a(1) ); // ksession.fireAllRules(); ksession.dispose(); }