Java Code Examples for org.kie.api.runtime.KieSession#insert()
The following examples show how to use
org.kie.api.runtime.KieSession#insert() .
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: WorkingMemoryLoggerTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testRetraction() throws Exception { // RHBRMS-2641 final String drl = "import " + AnyType.class.getCanonicalName() + ";\n" + "rule \"retract\" when\n" + " $any : AnyType( $typeId :typeId, typeName in (\"Standard\", \"Extended\") )\n" + " $any_c1 : AnyType( typeId == $typeId, typeName not in (\"Standard\", \"Extended\") ) \r\n" + "then\n" + " delete($any);\n" + " $any.setTypeId(null);\n" + "end"; final KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL ) .build() .newKieSession(); ksession.insert(new AnyType(1, "Standard")); ksession.insert(new AnyType(1, "Extended")); ksession.insert(new AnyType(1, "test")); assertEquals( 2, ksession.fireAllRules() ); }
Example 2
Source File: UpdateTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testModifySimple() { final String str = "package org.drools.compiler;\n" + "\n" + "rule \"test modify block\"\n" + "when\n" + " $p: Person( name == \"hungry\" )\n" + "then\n" + " modify( $p ) { setName(\"fine\") }\n" + "end\n" + "\n" + "rule \"Log\"\n" + "when\n" + " $o: Object()\n" + "then\n" + " System.out.println( $o );\n" + "end"; final KieBase kbase = loadKnowledgeBaseFromString(str); final KieSession ksession = kbase.newKieSession(); final Person p = new Person("hungry"); ksession.insert(p); ksession.fireAllRules(); ksession.dispose(); }
Example 3
Source File: NullSafeDereferencingTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testNullSafeBinding() { String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " Person( $streetName : address!.street ) \n" + "then\n" + "end"; KieBase kbase = loadKnowledgeBaseFromString(str); KieSession ksession = kbase.newKieSession(); ksession.insert(new Person("Mario", 38)); Person mark = new Person("Mark", 37); mark.setAddress(new Address("Main Street")); ksession.insert(mark); Person edson = new Person("Edson", 34); edson.setAddress(new Address(null)); ksession.insert(edson); assertEquals(2, ksession.fireAllRules()); ksession.dispose(); }
Example 4
Source File: MarshallerTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@ParameterizedMarshallerTest public void testFromAndJoinWithPartialFiring(Environment env) throws Exception { String str = "import java.util.Collection\n" + "rule R1 when\n" + " String() from [ \"x\", \"y\", \"z\" ]\n" + " Integer()\n" + "then\n" + "end\n"; KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build(); KieSession ksession = null; try { ksession = kbase.newKieSession(null, env); ksession.insert(42); assertEquals(2, ksession.fireAllRules(2)); ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true); assertEquals(1, ksession.fireAllRules()); } finally { if (ksession != null) { ksession.dispose(); } } }
Example 5
Source File: NodeHashingTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testHashingOnClassConstraint() { String drl = "import " + A.class.getCanonicalName() + "\n" + "rule R1 when\n" + " A( configClass == String.class );\n" + "then\n" + "end\n" + "\n" + "rule R2 when\n" + " A( configClass == String.class );\n" + "then\n" + "end\n" + "\n" + "rule R3 when\n" + " A( configClass == String.class );\n" + "then\n" + "end\n\n"; KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL ) .build().newKieSession(); kieSession.insert( new A( ) ); assertEquals( 3, kieSession.fireAllRules() ); }
Example 6
Source File: ComparisonOperatorTest.java From fw-spring-cloud with Apache License 2.0 | 6 votes |
/** * 测试规则的触发 */ @Test public void testRule(){ KieSession kieSession = kieBase.newKieSession(); //Fact对象,事实对象 ComparisonOperator fact = new ComparisonOperator(); fact.setNames("王五"); List<String> list = new ArrayList<>(); list.add("张三2"); list.add("李四"); fact.setList(list); kieSession.insert(fact); kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("rule_comparison_")); kieSession.dispose(); }
Example 7
Source File: NestedAccessorsTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testNestedAccessorWithInlineCast() throws Exception { final String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " Person( name == \"mark\", address#LongAddress.(country == \"uk\", suburb == \"suburb\") )\n" + "then\n" + "end\n"; final KieBase kbase = loadKnowledgeBaseFromString(str); final KieSession ksession = kbase.newKieSession(); final Person mark1 = new Person("mark"); mark1.setAddress(new LongAddress("street", "suburb", "zipCode", "uk")); ksession.insert(mark1); final Person mark2 = new Person("mark"); ksession.insert(mark2); final Person mark3 = new Person("mark"); mark3.setAddress(new Address("street", "suburb", "zipCode")); ksession.insert(mark3); assertEquals(1, ksession.fireAllRules()); ksession.dispose(); }
Example 8
Source File: MarshallerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@ParameterizedMarshallerTest public void testAgendaDoNotSerializeObject(Environment env) throws Exception { KieSession ksession = null; try { String str = "import java.util.Collection\n" + "rule R1 when\n" + " String(this == \"x\" || this == \"y\" || this == \"z\")\n" + "then\n" + "end\n"; KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build(); ksession = kbase.newKieSession(null, env); ksession.insert("x"); ksession.insert("y"); ksession.insert("z"); assertEquals(3, ksession.fireAllRules()); ReadSessionResult serialisedStatefulKnowledgeSession = SerializationHelper.getSerialisedStatefulKnowledgeSessionWithMessage( ksession, ksession.getKieBase(), true); ksession = serialisedStatefulKnowledgeSession.getSession(); ProtobufMessages.KnowledgeSession deserializedMessage = serialisedStatefulKnowledgeSession.getDeserializedMessage(); assertEquals(0, ksession.fireAllRules()); assertFalse(deserializedMessage.getRuleData().getAgenda().getMatchList().stream().anyMatch( ml -> ml.getTuple().getObjectList().size() > 0)); } finally { if (ksession != null) { ksession.dispose(); } } }
Example 9
Source File: I18nTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test @Disabled("Fails because of JBRULES-3435. But the JBRULES-2853 part works fine. Support for i18n properties must be fixed in mvel") public void readDrlInEncodingUtf8() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_I18nPerson_utf8.drl", "UTF-8", getClass() ), ResourceType.DRL ); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal( "list", list ); I18nPerson i18nPerson = new I18nPerson(); i18nPerson.setGarçon("Value 1"); i18nPerson.setÉlève("Value 2"); i18nPerson.setИмя("Value 3"); i18nPerson.set名称("Value 4"); ksession.insert(i18nPerson); ksession.fireAllRules(); assertTrue(list.contains("garçon")); assertTrue(list.contains("élève")); assertTrue(list.contains("имя")); assertTrue(list.contains("名称")); ksession.dispose(); }
Example 10
Source File: PropertyReactivityTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testWatchFieldInExternalPatternWithNoConstraints() { // DROOLS-2333 final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $p1 : Person()\n" + " $p2 : Person( name == \"Mark\", age > $p1.age ) \n" + "then\n" + " list.add(\"t0\");\n" + "end\n" + "rule Z when\n" + " $p1 : Person( name == \"Mario\" ) \n" + "then\n" + " modify($p1) { setAge(35); } \n" + "end\n"; // making the default explicit: final KieSession ksession = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl, ResourceType.DRL) .build() .newKieSession(); final List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); final Person mario = new Person("Mario", 40); final Person mark = new Person("Mark", 37); final FactHandle fh_mario = ksession.insert(mario); ksession.insert(mark); final int x = ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals("t0", list.get(0)); }
Example 11
Source File: HelloWorldExample.java From drools-examples with Apache License 2.0 | 5 votes |
public static void execute( KieContainer kc ) { // From the container, a session is created based on // its definition and configuration in the META-INF/kmodule.xml file KieSession ksession = kc.newKieSession("HelloWorldKS"); // Once the session is created, the application can interact with it // In this case it is setting a global as defined in the // org/drools/examples/helloworld/HelloWorld.drl file ksession.setGlobal( "list", new ArrayList<Object>() ); // The application can also setup listeners ksession.addEventListener( new DebugAgendaEventListener() ); ksession.addEventListener( new DebugRuleRuntimeEventListener() ); // To setup a file based audit logger, uncomment the next line // KieRuntimeLogger logger = ks.getLoggers().newFileLogger( ksession, "./helloworld" ); // To setup a ThreadedFileLogger, so that the audit view reflects events whilst debugging, // uncomment the next line // KieRuntimeLogger logger = ks.getLoggers().newThreadedFileLogger( ksession, "./helloworld", 1000 ); // The application can insert facts into the session final Message message = new Message(); message.setMessage( "Hello World" ); message.setStatus( Message.HELLO ); ksession.insert( message ); // and fire the rules ksession.fireAllRules(); // Remove comment if using logging // logger.close(); // and then dispose the session ksession.dispose(); }
Example 12
Source File: StatefulSessionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testDisconnectedFactHandle() { final KieBase kbase = getKnowledgeBase(); final KieSession ksession = createKnowledgeSession( kbase ); final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert( "hello" ); final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert( "goodbye" ); FactHandle key = DefaultFactHandle.createFromExternalFormat( helloHandle.toExternalForm() ); assertEquals( "hello", ksession.getObject( key ) ); key = DefaultFactHandle.createFromExternalFormat( goodbyeHandle.toExternalForm() ); assertEquals( "goodbye", ksession.getObject( key ) ); }
Example 13
Source File: PropertyReactivityTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testWatchFieldInExternalNotPattern() { // DROOLS-1445 final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $p1 : Person( name == \"Mario\" )\n" + " not( Person( age < $p1.age ) )\n" + "then\n" + " list.add(\"t0\");\n" + "end\n" + "rule Z when\n" + " $p1 : Person( name == \"Mario\" ) \n" + "then\n" + " modify($p1) { setAge(35); } \n" + "end\n"; // making the default explicit: final KieSession ksession = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl, ResourceType.DRL) .build() .newKieSession(); final List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); final Person mario = new Person("Mario", 40); final Person mark = new Person("Mark", 37); final FactHandle fh_mario = ksession.insert(mario); ksession.insert(mark); final int x = ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals("t0", list.get(0)); }
Example 14
Source File: OOPathReactiveTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testReactiveOnLia() { final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n"; final KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL ) .build() .newKieSession(); final List<String> list = new ArrayList<>(); ksession.setGlobal( "list", list ); final Woman alice = new Woman( "Alice", 38 ); final Man bob = new Man( "Bob", 40 ); bob.setWife( alice ); final Child charlie = new Child( "Charles", 12 ); final Child debbie = new Child( "Debbie", 10 ); alice.addChild( charlie ); alice.addChild( debbie ); charlie.addToy( new Toy( "car" ) ); charlie.addToy( new Toy( "ball" ) ); debbie.addToy( new Toy( "doll" ) ); ksession.insert( bob ); ksession.fireAllRules(); assertThat(list).containsExactlyInAnyOrder("car", "ball"); list.clear(); debbie.setAge( 11 ); ksession.fireAllRules(); assertThat(list).containsExactlyInAnyOrder("doll"); }
Example 15
Source File: MatchTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testGetObjectsExists() { // DROOLS-1474 String str = "import org.drools.compiler.Foo\n" + "import org.drools.compiler.Bar\n" + "global java.util.List list\n" + "rule R when\n" + " $b : Bar(id == \"roadster\")\n" + " exists Foo(bar == $b)\n" + "then\n" + " list.addAll(((org.drools.core.spi.Activation)kcontext.getMatch()).getObjectsDeep());\n" + "end\n"; KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(); List<Object> list = new ArrayList<>(); ksession.setGlobal("list", list); Bar roadsterType = new Bar("roadster"); ksession.insert(roadsterType); Foo bmwZ4 = new Foo("BMW Z4", roadsterType); ksession.insert(bmwZ4); Foo lotusElise = new Foo("Lotus Elise", roadsterType); ksession.insert(lotusElise); Foo mazdaMx5 = new Foo("Mazda MX-5", roadsterType); ksession.insert(mazdaMx5); Bar miniVanType = new Bar("minivan"); ksession.insert(miniVanType); Foo kiaCarnival = new Foo("Kia Carnival", miniVanType); ksession.insert(kiaCarnival); Foo renaultEspace = new Foo("Renault Espace", miniVanType); ksession.insert(renaultEspace); ksession.fireAllRules(); assertTrue(list.contains(roadsterType)); assertTrue(list.contains(bmwZ4)); assertTrue(list.contains(lotusElise)); assertTrue(list.contains(mazdaMx5)); assertFalse(list.contains(miniVanType)); assertFalse(list.contains(kiaCarnival)); assertFalse(list.contains(renaultEspace)); ksession.dispose(); }
Example 16
Source File: KieCompilationCacheTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testHelloWorldWithPackagesAnd2KieBases() throws Exception { String drl1 = "package org.pkg1\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n"; String drl2 = "package org.pkg2\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + " $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n"; KieServices ks = KieServices.Factory.get(); ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0"); KieFileSystem kfs = ks.newKieFileSystem() .generateAndWritePomXML(releaseId) .write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1) .write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2) .writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML()); ks.newKieBuilder( kfs ).buildAll(); InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule( releaseId ); byte[] jar = kieModule.getBytes(); MemoryFileSystem mfs = MemoryFileSystem.readFromJar( jar ); File file = mfs.getFile( KieBuilderImpl.getCompilationCachePath( releaseId, "KBase1") ); assertNotNull( file ); file = mfs.getFile( KieBuilderImpl.getCompilationCachePath( releaseId, "KBase2") ); assertNotNull( file ); Resource jarRes = ks.getResources().newByteArrayResource( jar ); KieModule km = ks.getRepository().addKieModule( jarRes ); KieSession ksession = ks.newKieContainer( km.getReleaseId() ).newKieSession("KSession1"); ksession.insert(new Message("Hello World")); assertEquals( 1, ksession.fireAllRules() ); ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1"); ksession.insert(new Message("Hi Universe")); assertEquals( 1, ksession.fireAllRules() ); ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1"); ksession.insert(new Message("Aloha Earth")); assertEquals( 0, ksession.fireAllRules() ); ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2"); ksession.insert(new Message("Hello World")); assertEquals( 1, ksession.fireAllRules() ); ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2"); ksession.insert(new Message("Hi Universe")); assertEquals( 0, ksession.fireAllRules() ); ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2"); ksession.insert(new Message("Aloha Earth")); assertEquals(1, ksession.fireAllRules()); }
Example 17
Source File: ProcessStateTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test @Disabled public void FIXMEtestDelayedStateConstraintPriorities1() { 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.state\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + " <header>\n" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + " </imports>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <state id=\"2\" >\n" + " <constraints>\n" + " <constraint toNodeId=\"3\" name=\"one\" priority=\"1\" >\n" + " Person( )" + " </constraint>"+ " <constraint toNodeId=\"5\" name=\"two\" priority=\"2\" >\n" + " Person( )" + " </constraint>"+ " </constraints>\n" + " </state>\n" + " <actionNode id=\"3\" name=\"ActionNode1\" >\n" + " <action type=\"expression\" dialect=\"java\" >list.add(\"1\");</action>\n" + " </actionNode>\n" + " <end id=\"4\" name=\"End\" />\n" + " <actionNode id=\"5\" name=\"ActionNode2\" >\n" + " <action type=\"expression\" dialect=\"java\" >list.add(\"2\");</action>\n" + " </actionNode>\n" + " <end id=\"6\" 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" + " <connection from=\"2\" to=\"5\" />\n" + " <connection from=\"5\" to=\"6\" />\n" + " </connections>\n" + "\n" + "</process>"); kbuilder.add( ResourceFactory.newReaderResource(source), ResourceType.DRF ); KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ProcessInstance processInstance = ksession.startProcess("org.drools.state"); assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState()); assertTrue(list.isEmpty()); Person person = new Person("John Doe", 30); ksession.insert(person); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); assertEquals(1, list.size()); assertEquals("1", list.get(0)); }
Example 18
Source File: MatchTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testGetObjectsAccumulate() { // DROOLS-1470 String str = "import org.drools.compiler.Foo\n" + "import org.drools.compiler.Bar\n" + "global java.util.List list\n" + "rule R when\n" + " $b : Bar(id == \"roadster\")\n" + " accumulate(\n" + " $f : Foo(bar == $b);\n" + " $t : count($f)\n" + " )\n" + "then\n" + " list.addAll(((org.drools.core.spi.Activation)kcontext.getMatch()).getObjectsDeep());\n" + "end\n"; KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(); List<Object> list = new ArrayList<>(); ksession.setGlobal("list", list); Bar roadsterType = new Bar("roadster"); ksession.insert(roadsterType); Foo bmwZ4 = new Foo("BMW Z4", roadsterType); ksession.insert(bmwZ4); Foo lotusElise = new Foo("Lotus Elise", roadsterType); ksession.insert(lotusElise); Foo mazdaMx5 = new Foo("Mazda MX-5", roadsterType); ksession.insert(mazdaMx5); Bar miniVanType = new Bar("minivan"); ksession.insert(miniVanType); Foo kieCarnival = new Foo("Kia Carnival", miniVanType); ksession.insert(kieCarnival); Foo renaultEspace = new Foo("Renault Espace", miniVanType); ksession.insert(renaultEspace); ksession.fireAllRules(); assertTrue(list.contains(roadsterType)); assertTrue(list.contains(bmwZ4)); assertTrue(list.contains(lotusElise)); assertTrue(list.contains(mazdaMx5)); ksession.dispose(); }
Example 19
Source File: TraitMapCoreTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testMapCoreAliasing( ) { String source = "package org.drools.core.factmodel.traits.test;\n" + "\n" + "import java.util.*;\n" + "import org.drools.core.factmodel.traits.*;\n" + "" + "global List list;\n " + "" + "declare HashMap @Traitable() end \n" + "\n" + "global List list; \n" + "\n" + "declare trait PersonMap\n" + "@propertyReactive \n" + " name : String \n" + " age : Integer @Alias( \"years\" ) \n" + " eta : Integer @Alias( \"years\" ) \n" + " height : Double @Alias( \"tall\" ) \n" + " sen : String @Alias(\"years\") \n " + "end\n" + "\n" + "rule Don \n" + "when \n" + " $m : Map()\n" + "then \n" + " don( $m, PersonMap.class );\n" + "\n" + "end\n" + "\n" + "rule Log \n" + "when \n" + " $p : PersonMap( name == \"john\", age > 10 && < 35 )\n" + "then \n" + " modify ( $p ) { \n" + " setHeight( 184.0 ), \n" + " setEta( 42 ); \n" + " }\n" + " System.out.println(\"Log: \" + $p );\n" + "end\n" + "" + "\n"; KieSession ks = loadKnowledgeBaseFromString( source ).newKieSession(); TraitFactory.setMode( VirtualPropertyMode.MAP, ks.getKieBase() ); List list = new ArrayList(); ks.setGlobal( "list", list ); Map<String,Object> map = new HashMap<String, Object>( ); map.put( "name", "john" ); map.put( "years", new Integer( 18 ) ); ks.insert( map ); ks.fireAllRules(); for ( Object o : ks.getObjects() ) { System.err.println( o ); } assertEquals( 42, map.get( "years" ) ); assertEquals( 184.0, map.get( "tall" ) ); }
Example 20
Source File: PropertySpecificTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testWatchNothing() throws Exception { String rule = "package org.drools.compiler.integrationtests\n" + "dialect \"mvel\"\n" + "declare A\n" + " s : String\n" + "end\n" + "declare B\n" + " @propertyReactive\n" + " on : boolean\n" + " s : String\n" + "end\n" + "rule R1\n" + "when\n" + " A($s : s)\n" + " $b : B(s != $s) @watch( !* )\n" + "then\n" + " modify($b) { setS($s) }\n" + "end\n" + "rule R2\n" + "when\n" + " $b : B(on == false)\n" + "then\n" + " modify($b) { setOn(true) }\n" + "end\n"; KieBase kbase = loadKnowledgeBaseFromString( rule ); KieSession ksession = kbase.newKieSession(); FactType factTypeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" ); Object factA = factTypeA.newInstance(); factTypeA.set( factA, "s", "y" ); ksession.insert(factA); FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" ); Object factB = factTypeB.newInstance(); factTypeB.set( factB, "on", false ); factTypeB.set( factB, "s", "x" ); ksession.insert(factB); int rules = ksession.fireAllRules(); assertEquals(2, rules); assertEquals(true, factTypeB.get(factB, "on")); assertEquals("y", factTypeB.get(factB, "s")); ksession.dispose(); }