Java Code Examples for org.kie.api.KieBase#getFactType()

The following examples show how to use org.kie.api.KieBase#getFactType() . 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: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerics() throws Exception {
    // DROOLS-4939
    String drl =
            "package org.drools.compiler\n" +
                    "import java.util.List\n" +
                    "declare Node\n" +
                    "    values: List<String>\n" +
                    "end\n" +
                    "rule R1 when\n" +
                    "   $node: Node( values.get(0).length == 4 )\n" +
                    "then\n" +
                    "   System.out.println( $node );\n" +
                    "end";

    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();

    FactType nodeType = kbase.getFactType( "org.drools.compiler", "Node" );
    Object parent = nodeType.newInstance();
    nodeType.set( parent, "values", Arrays.asList("test") );
    ksession.insert( parent );

    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );
}
 
Example 2
Source File: GeneratedBeansTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratedBeansSerializable() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_GeneratedBeansSerializable.drl");

    final FactType cheeseFact = kbase.getFactType("org.drools.generatedbeans", "Cheese");
    assertTrue(Serializable.class.isAssignableFrom(cheeseFact.getFactClass()), "Generated beans must be serializable");

    final Object cheese = cheeseFact.newInstance();
    cheeseFact.set(cheese, "type", "stilton");

    final Object cheese2 = cheeseFact.newInstance();
    cheeseFact.set(cheese2, "type", "brie");

    final KieSession ksession = createKnowledgeSession(kbase);
    final List<Number> results = new ArrayList<>();
    ksession.setGlobal("results", results);

    ksession.insert(cheese);
    ksession.insert(cheese2);
    ksession.fireAllRules();

    assertEquals(1, results.size());
    assertEquals(2, results.get(0).intValue());
}
 
Example 3
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircularDeclarationWithExtension() throws Exception {
    // DROOLS-640
    String drl = "package org.drools.compiler.test\n" +
                 "declare FactA\n" +
                 "    fieldB: FactB\n" +
                 "end\n" +
                 "declare FactB extends FactA end\n" +
                 "rule R1 when\n" +
                 "   $a : FactA( )\n" +
                 "   $b : FactB( this == $a.fieldB )\n" +
                 "then\n" +
                 "end";

    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();

    FactType aType = kbase.getFactType( "org.drools.compiler.test", "FactA" );
    FactType bType = kbase.getFactType( "org.drools.compiler.test", "FactB" );
    Object a = aType.newInstance();
    Object b = bType.newInstance();
    aType.set( a, "fieldB", b );
    ksession.insert( a );
    ksession.insert( b );

    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );
}
 
Example 4
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositionalArguments() throws InstantiationException, IllegalAccessException {
    String drl = "package org.test;\n" +
                 "global java.util.List names;\n" +
                 "declare Person\n" +
                 "    name : String\n" +
                 "    age : int\n" +
                 "end\n" +
                 "rule R when \n" +
                 "    $p : Person( \"Mark\", 37; )\n" +
                 "then\n" +
                 "    names.add( $p.getName() );\n" +
                 "end\n";

    KieBuilder kieBuilder = build(drl);

    assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
    KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase();

    FactType factType = kieBase.getFactType( "org.test", "Person" );
    Object instance = factType.newInstance();
    factType.set(instance, "name", "Mark");
    factType.set(instance, "age", 37);

    List<String> names = new ArrayList<String>();
    KieSession ksession = kieBase.newKieSession();
    ksession.setGlobal("names", names);

    ksession.insert(instance);
    ksession.fireAllRules();

    assertEquals( 1, names.size() );
    assertEquals( "Mark", names.get( 0 ) );
}
 
Example 5
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecursiveDeclaration() throws Exception {
    String rule = "package org.drools.compiler\n" +
                  "declare Node\n" +
                  "    value: String\n" +
                  "    parent: Node\n" +
                  "end\n" +
                  "rule R1 when\n" +
                  "   $parent: Node( value == \"parent\" )\n" +
                  "   $child: Node( $value : value, parent == $parent )\n" +
                  "then\n" +
                  "   System.out.println( $value );\n" +
                  "end";

    KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();

    FactType nodeType = kbase.getFactType( "org.drools.compiler", "Node" );
    Object parent = nodeType.newInstance();
    nodeType.set( parent, "value", "parent" );
    ksession.insert( parent );

    Object child = nodeType.newInstance();
    nodeType.set( child, "value", "child" );
    nodeType.set( child, "parent", parent );
    ksession.insert( child );

    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );
}
 
Example 6
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfig() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" +
            "declare A\n" +
            "    i : int\n" +
            "    j : int\n" +
            "end\n" +
            "rule R1\n" +
            "when\n" +
            "    $a : A(i == 1)\n" +
            "then\n" +
            "    modify($a) { setJ(2) };\n" +
            "end\n";

    KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    config.setOption(PropertySpecificOption.ALWAYS);
    KieBase kbase = loadKnowledgeBaseFromString( config, rule );
    KieSession ksession = kbase.newKieSession();

    FactType typeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" );
    Object a = typeA.newInstance();
    typeA.set( a, "i", 1 );
    ksession.insert( a );

    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
 
Example 7
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericsMap() throws Exception {
    // DROOLS-4939
    String drl =
            "package org.drools.compiler\n" +
                    "import " + ValuesProvider.class.getCanonicalName() + "\n" +
                    "import java.util.Map\n" +
                    "declare Node extends ValuesProvider\n" +
                    "    values: Map<String, String>\n" +
                    "end\n" +
                    "rule R1 when\n" +
                    "   $node: Node( values.get(\"value\").length == 4 )\n" +
                    "then\n" +
                    "   System.out.println( $node );\n" +
                    "end";

    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();

    FactType nodeType = kbase.getFactType( "org.drools.compiler", "Node" );
    Object parent = nodeType.newInstance();
    Map<String,String> map = new HashMap<>();
    map.put("value", "test");
    nodeType.set( parent, "values", map );
    ksession.insert( parent );

    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );
}
 
Example 8
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testExplictPositionalArguments() throws InstantiationException, IllegalAccessException {
    String drl = "package org.test;\n" +
                 "global java.util.List names;\n" +
                 "declare Person\n" +
                 "    name : String @position(1)\n" +
                 "    age : int @position(0)\n" +
                 "end\n" +
                 "rule R when \n" +
                 "    $p : Person( 37, \"Mark\"; )\n" +
                 "then\n" +
                 "    names.add( $p.getName() );\n" +
                 "end\n";

    KieBuilder kieBuilder = build(drl);

    assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
    KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase();

    FactType factType = kieBase.getFactType("org.test", "Person");
    Object instance = factType.newInstance();
    factType.set(instance, "name", "Mark");
    factType.set(instance, "age", 37);

    List<String> names = new ArrayList<String>();
    KieSession ksession = kieBase.newKieSession();
    ksession.setGlobal("names", names);

    ksession.insert(instance);
    ksession.fireAllRules();

    assertEquals(1, names.size());
    assertEquals("Mark", names.get(0));
}
 
Example 9
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassReactive() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" +
            "global java.util.concurrent.atomic.AtomicInteger counter\n" +
            "declare B\n" +
            "    @classReactive\n" +
            "    on : boolean\n" +
            "    s : String\n" +
            "end\n" +
            "rule R1\n" +
            "when\n" +
            "    $b : B(s == \"test\")\n" +
            "then\n" +
            "    modify($b) { setOn(true) }\n" +
            "    if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" +
            "end\n";

    KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    config.setOption(PropertySpecificOption.ALWAYS);
    KieBase kbase = loadKnowledgeBaseFromString( config, rule );
    KieSession ksession = kbase.newKieSession();

    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal( "counter", counter );

    FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" );
    Object factB = factTypeB.newInstance();
    factTypeB.set( factB, "s", "test" );
    factTypeB.set( factB, "on", false );
    ksession.insert( factB );

    assertThatThrownBy(() -> ksession.fireAllRules())
            .isInstanceOf(RuntimeException.class)
            .hasMessageContaining("Exception executing consequence for rule \"R1\"");
}
 
Example 10
Source File: StrictAnnotationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testExplictPositionalArguments() throws InstantiationException, IllegalAccessException {
    String str = "package org.test;\n" +
                 "global java.util.List names;\n" +
                 "declare Person\n" +
                 "    @Position(1) name : String \n" +
                 "    @Position(0) age : int \n" +
                 "end\n" +
                 "rule R when \n" +
                 "    $p : Person( 37, \"Mark\"; )\n" +
                 "then\n" +
                 "    names.add( $p.getName() );\n" +
                 "end\n";

    KieBase kieBase = new KieHelper()
            .setKieModuleModel(KieServices.Factory.get().newKieModuleModel()
                                                  .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME,
                                                                            LanguageLevelOption.DRL6_STRICT.toString()))
            .addContent(str, ResourceType.DRL)
            .build();

    FactType factType = kieBase.getFactType("org.test", "Person");
    Object instance = factType.newInstance();
    factType.set(instance, "name", "Mark");
    factType.set(instance, "age", 37);

    List<String> names = new ArrayList<String>();
    KieSession ksession = kieBase.newKieSession();
    try {
        ksession.setGlobal("names", names);

        ksession.insert(instance);
        ksession.fireAllRules();

        assertEquals(1, names.size());
        assertEquals("Mark", names.get(0));
    } finally {
        ksession.dispose();
    }
}
 
Example 11
Source File: MVELTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testMVELClassReferences() throws InstantiationException, IllegalAccessException {
    final String str = "package org.drools.compiler\n" +
            "declare Assignment\n" +
            "    source : Class\n" +
            "    target : Class\n" +
            "end\n" +
            "rule ObjectIsAssignable1\n" +
            "when\n" +
            "    Assignment( $t: target == java.lang.Object.class || target == source )\n" +
            "then\n" +
            "end\n" +
            "rule ObjectIsAssignable2\n" +
            "when\n" +
            "    Assignment( $t: target == source || target == java.lang.Object.class )\n" +
            "then\n" +
            "end";

    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = createKnowledgeSession(kbase);

    final FactType asgType = kbase.getFactType("org.drools.compiler", "Assignment");
    final Object asg = asgType.newInstance();
    asgType.set(asg, "source", Object.class);
    asgType.set(asg, "target", Object.class);

    ksession.insert(asg);

    final int rules = ksession.fireAllRules();
    ksession.dispose();

    assertEquals(2, rules);
}
 
Example 12
Source File: ExistentialOperatorTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testForallAfterOr() throws Exception {
    // DROOLS-2710
    String str =
            "package redhat\n" +
            "declare Fact\n" +
            "    integer : int\n" +
            "    string1 : String\n" +
            "    string2 : String\n" +
            "end\n" +
            "rule \"Rule\"\n" +
            "when\n" +
            "Fact(string2 == \"Y\")\n" +
            "(\n" +
            "    exists (Fact(integer == 42)) or\n" +
            "    Fact(integer == 43)\n" +
            ")\n" +
            "forall (Fact(string1 == \"X\"))\n" +
            "then\n" +
            "end";

    KieBase kieBase = new KieHelper().addContent( str, ResourceType.DRL ).build();
    KieSession kieSession = kieBase.newKieSession();

    FactType factType = kieBase.getFactType("redhat", "Fact");

    Object fact = factType.newInstance();
    factType.set(fact, "string1", "X");
    factType.set(fact, "string2", "Y");
    factType.set(fact, "integer", 42);

    kieSession.insert(fact);

    int n = kieSession.fireAllRules();
    assertEquals(1, n);
}
 
Example 13
Source File: KieCompilationCacheTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompilationCache() throws Exception {
    String drl = "package org.drools.compiler\n" +
            "declare type X\n" +
            "    foo : String\n" +
            "end\n"+
            "rule R1 when\n" +
            "   $m : X( foo == \"Hello World\" )\n" +
            "then\n" +
            "end\n";
    
    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", drl );
    ks.newKieBuilder( kfs ).buildAll();
    
    ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
    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 );

    Resource jarRes = ks.getResources().newByteArrayResource( jar );
    KieModule km = ks.getRepository().addKieModule( jarRes );
    KieContainer kc = ks.newKieContainer( km.getReleaseId() );
    
    KieBase kbase = kc.getKieBase();
    FactType type = kbase.getFactType( "org.drools.compiler", "X" );
    FactField foo = type.getField( "foo" );
    Object x = type.newInstance();
    foo.set( x, "Hello World" );
    
    KieSession ksession = kc.newKieSession();
    ksession.insert(x);

    int count = ksession.fireAllRules();
    assertEquals( 1, count );
}
 
Example 14
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 15
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPropertySpecificJavaBean() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" +
            "import " + PropertySpecificTest.C.class.getCanonicalName() + "\n" +
            "declare A\n" +
            "    s : String\n" +
            "end\n" +
            "rule R1\n" +
            "when\n" +
            "    A($s : s)\n" +
            "    $c : C(s != $s)\n" +
            "then\n" +
            "    modify($c) { setS($s) }\n" +
            "end\n" +
            "rule R2\n" +
            "when\n" +
            "    $c : C(on == false)\n" +
            "then\n" +
            "    modify($c) { turnOn() }\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 );

    C c = new C();
    c.setOn(false);
    c.setS("x");
    ksession.insert( c );

    int rules = ksession.fireAllRules();
    assertEquals(2, rules);

    assertEquals(true, c.isOn());
    assertEquals("y", c.getS());
    ksession.dispose();
}
 
Example 16
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPropertySpecificSimplified() 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( ! s, on )\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();
}
 
Example 17
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSharedWatchAnnotation() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" +
            "declare A\n" +
            "    @propertyReactive\n" +
            "    a : int\n" +
            "    b : int\n" +
            "    s : String\n" +
            "    i : int\n" +
            "end\n" +
            "declare B\n" +
            "    @propertyReactive\n" +
            "    s : String\n" +
            "    i : int\n" +
            "end\n" +
            "rule R1\n" +
            "when\n" +
            "    $a : A(a == 0) @watch( i )\n" +
            "    $b : B(i == $a.i) @watch( s )\n" +
            "then\n" +
            "    modify($a) { setS(\"end\") }\n" +
            "end\n" +
            "rule R2\n" +
            "when\n" +
            "    $a : A(a == 0) @watch( b )\n" +
            "then\n" +
            "    modify($a) { setI(1) }\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, "a", 0 );
    factTypeA.set( factA, "b", 0 );
    factTypeA.set( factA, "i", 0 );
    factTypeA.set( factA, "s", "start" );
    ksession.insert( factA );

    FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" );
    Object factB = factTypeB.newInstance();
    factTypeB.set( factB, "i", 1 );
    factTypeB.set( factB, "s", "start" );
    ksession.insert( factB );

    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
    assertEquals("end", factTypeA.get(factA, "s"));
}
 
Example 18
Source File: GeneratedBeansTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeneratedBeans1() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_GeneratedBeans.drl");

    final FactType cheeseFact = kbase.getFactType("org.drools.generatedbeans", "Cheese");
    final Object cheese = cheeseFact.newInstance();
    cheeseFact.set(cheese, "type", "stilton");
    assertEquals("stilton", cheeseFact.get(cheese, "type"));

    final FactType personType = kbase.getFactType("org.drools.generatedbeans", "Person");
    final Object ps = personType.newInstance();
    personType.set(ps, "age", 42);
    final Map<String, Object> personMap = personType.getAsMap(ps);
    assertEquals(42, personMap.get("age"));

    personMap.put("age", 43);
    personType.setFromMap(ps, personMap);
    assertEquals(43, personType.get(ps, "age"));
    assertEquals("stilton", cheeseFact.getField("type").get(cheese));

    final KieSession ksession = createKnowledgeSession(kbase);
    final Object cg = cheeseFact.newInstance();
    ksession.setGlobal("cg", cg);
    final List<Object> result = new ArrayList<Object>();
    ksession.setGlobal("list", result);

    ksession.insert(cheese);
    ksession.fireAllRules();
    assertEquals(1, result.size());
    assertEquals(5, result.get(0));

    final FactType personFact = kbase.getFactType("org.drools.generatedbeans", "Person");
    final Object person = personFact.newInstance();
    personFact.getField("likes").set(person, cheese);
    personFact.getField("age").set(person, 7);

    ksession.insert(person);
    ksession.fireAllRules();
    assertEquals(2, result.size());
    assertEquals(person, result.get(1));
}
 
Example 19
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private void testBetaWith2RTNSinksExec(boolean addInfiniteLoopWatch) throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" +
            "global java.util.concurrent.atomic.AtomicInteger counter\n" +
            "declare A\n" +
            "   @propertyReactive\n" +
            "   x : int\n" +
            "end\n" +
            "declare B\n" +
            "   @propertyReactive\n" +
            "end\n" +
            "declare C\n" +
            "   @propertyReactive\n" +
            "   y : int\n" +
            "end\n" +
            "rule R1 when\n" +
            "   A ( x == 1 )\n" +
            "   B ( )\n" +
            (addInfiniteLoopWatch ? "   $c : C ( ) @watch(y)\n" : "   $c : C ( )\n") +
            "then " +
            "   modify( $c ) { setY( 2 ) };\n" +
            "   if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" +
            "end;\n" +
            "rule R2 when\n" +
            "   A ( x == 1 )\n" +
            "   B ( )\n" +
            "   C ( ) @watch(y)\n" +
            "then end;\n" +
            "rule InitA when\n" +
            "   $a : A ( x == 0 )\n" +
            "then\n" +
            "   modify( $a ) { setX( 1 ) };\n" +
            "end;\t\n" +
            "rule InitC salience 1 when\n" +
            "   $c : C ( y == 0 )\n" +
            "then\n" +
            "   modify( $c ) { setY( 1 ) };\n" +
            "end;\n";

    KieBase kbase = loadKnowledgeBaseFromString( rule );
    KieSession ksession = kbase.newKieSession();

    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal( "counter", counter );

    FactType factTypeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" );
    Object factA = factTypeA.newInstance();
    factTypeA.set( factA, "x", 0 );
    ksession.insert(factA);

    FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" );
    Object factB = factTypeB.newInstance();
    ksession.insert(factB);

    FactType factTypeC = kbase.getFactType( "org.drools.compiler.integrationtests", "C" );
    Object factC = factTypeC.newInstance();
    factTypeC.set( factC, "y", 0 );
    ksession.insert(factC);

    try {
        ksession.fireAllRules();
    } finally {
        assertEquals(2, factTypeC.get(factC, "y"));
        ksession.dispose();
    }
}
 
Example 20
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private void testBetaWith2BetaSinksExec(boolean addInfiniteLoopWatch) throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" +
            "global java.util.concurrent.atomic.AtomicInteger counter\n" +
            "declare A\n" +
            "   @propertyReactive\n" +
            "   x : int\n" +
            "end\n" +
            "declare B\n" +
            "   @propertyReactive\n" +
            "end\n" +
            "declare C\n" +
            "   @propertyReactive\n" +
            "   y : int\n" +
            "end\n" +
            "declare D\n" +
            "   @propertyReactive\n" +
            "end\n" +
            "rule R1 when\n" +
            "   A ( x == 1 )\n" +
            (addInfiniteLoopWatch ? "   $c : C ( ) @watch(y)\n" : "   $c : C ( )\n") +
            "   B ( )\n" +
            "then " +
            "   modify( $c ) { setY( 2 ) };\n" +
            "   if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" +
            "end;\n" +
            "rule R2 when\n" +
            "   A ( x == 1 )\n" +
            "   C ( ) @watch(y)\n" +
            "   D ( )\n" +
            "then end;\n" +
            "rule InitA when\n" +
            "   $a : A ( x == 0 )\n" +
            "then\n" +
            "   modify( $a ) { setX( 1 ) };\n" +
            "end;\t\n" +
            "rule InitC salience 1 when\n" +
            "   $c : C ( y == 0 )\n" +
            "then\n" +
            "   modify( $c ) { setY( 1 ) };\n" +
            "end;\n";

    KieBase kbase = loadKnowledgeBaseFromString( rule );
    KieSession ksession = kbase.newKieSession();

    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal( "counter", counter );

    FactType factTypeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" );
    Object factA = factTypeA.newInstance();
    factTypeA.set( factA, "x", 0 );
    ksession.insert(factA);

    FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" );
    Object factB = factTypeB.newInstance();
    ksession.insert(factB);

    FactType factTypeC = kbase.getFactType( "org.drools.compiler.integrationtests", "C" );
    Object factC = factTypeC.newInstance();
    factTypeC.set( factC, "y", 0 );
    ksession.insert(factC);

    FactType factTypeD = kbase.getFactType( "org.drools.compiler.integrationtests", "D" );
    Object factD = factTypeD.newInstance();
    ksession.insert(factD);

    try {
        ksession.fireAllRules();
    } finally {
        assertEquals(2, factTypeC.get(factC, "y"));
        ksession.dispose();
    }
}