Search in sources :

Example 6 with Abducible

use of org.drools.core.beliefsystem.abductive.Abducible in project drools by kiegroup.

the class AbductionTest method testAbductiveLogicSprinklerAndRainExample.

@Test
public void testAbductiveLogicSprinklerAndRainExample() {
    // Sprinkler & Rain, abductive version
    String droolsSource = "package org.drools.abductive.test; \n" + "" + "import " + Abducible.class.getName() + "; \n" + "import " + Defeasible.class.getName() + "; \n" + "global java.util.List list; \n" + "" + "declare Sunny id : int @key end \n" + "declare WetGrass @Abducible id : int @key end \n" + "declare Rain @Abducible id : int @key end \n" + "declare Sprinkler @Abducible id : int @key end \n" + "query wetGrass() \n" + "   @Abductive( target=WetGrass.class ) \n" + "   rain() or sprinkler() \n" + "end \n" + "query rain() \n" + "   @Abductive( target=Rain.class ) \n" + "   @Defeasible " + "end \n" + "query sprinkler() \n" + "   @Abductive( target=Sprinkler.class ) \n" + "   @Defeasible " + "end \n" + "rule SunIntegrityConstraint \n" + "@Direct \n" + "when \n" + "   Sunny()" + "then \n" + "   insertLogical( new Rain(), 'neg' ); \n" + "end \n" + "rule Facts \n" + "when \n" + "then \n" + " insert( new Sunny( 0 ) ); \n" + "end \n" + "rule Raaain \n" + "when " + "   $r : Rain( _.neg ) " + "then \n" + "   list.add( 'no_rain_check' ); \n" + "end \n" + "rule Main_1\n" + "when \n" + "   wetGrass() \n" + "   ( " + "     Sprinkler() do[sprk] \n" + "     or \n" + "     Rain() do[rain] \n" + "     or \n" + "     Rain( _.neg ) do[norn] \n" + "   ) \n" + "then \n" + "then[sprk] \n" + "   list.add( 'sprinkler' ); \n" + "   System.out.println( \"The grass is wet because the sprinkler is on \" ); \n" + "then[rain] \n" + "   list.add( 'rain' ); \n " + "   System.out.println( \"The grass is wet because it's raining! \" ); \n" + "then[norn] \n" + "   list.add( 'not rain' ); \n" + "   System.out.println( \"The grass can't be wet due to rain, it's sunny today!!! \" ); \n" + "end \n" + "";
    // ///////////////////////////////////
    KieSession session;
    try {
        System.setProperty("drools.negatable", "on");
        session = getSessionFromString(droolsSource);
    } finally {
        System.setProperty("drools.negatable", "off");
    }
    List list = new ArrayList();
    session.setGlobal("list", list);
    session.fireAllRules();
    System.out.println(list);
    assertEquals(3, list.size());
    assertTrue(list.contains("sprinkler"));
    assertTrue(list.contains("not rain"));
    assertTrue(list.contains("no_rain_check"));
    assertEquals(3, session.getObjects().size());
    int i = 0;
    Iterator it = session.getObjects().iterator();
    while (it.hasNext()) {
        i++;
        it.next();
    }
    assertEquals(3, i);
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Abducible(org.drools.core.beliefsystem.abductive.Abducible) Test(org.junit.Test)

Example 7 with Abducible

use of org.drools.core.beliefsystem.abductive.Abducible in project drools by kiegroup.

the class AbductionTest method testQueryAPIs.

@Test
public void testQueryAPIs() {
    String droolsSource = "package org.drools.abductive.test; \n" + "import " + Abducible.class.getName() + "; \n" + "" + "declare Foo " + "   @Abducible " + "   id : String " + "end " + "query foo( String $s ) " + "   @Abductive( target=Foo.class ) \n" + "end \n " + "query bar( String $s, Foo $foo ) " + "   $foo := Foo() " + "end \n " + "rule MoveAround " + "when " + "   $s : String() " + "   $f : foo( $s ; ) " + "   bar( $s, $f ; ) " + "then " + "   delete( $s ); " + "   System.out.println( 'Foo ' + $f ); " + "end " + "";
    // ///////////////////////////////////
    KieSession session = getSessionFromString(droolsSource);
    session.insert("faa");
    session.fireAllRules();
    for (Object o : session.getObjects()) {
        System.out.println(">>> " + o);
    }
    assertEquals(1, session.getObjects().size());
    Query q1 = session.getKieBase().getQuery("org.drools.abductive.test", "foo");
    Query q2 = session.getKieBase().getQuery("org.drools.abductive.test", "bar");
    assertNotNull(q1);
    assertNotNull(q2);
    QueryResults q10res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("foo", "foo"));
    QueryResults q11res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("foo", "foo", Variable.v));
    QueryResults q20res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("bar", "foo", Variable.v));
    assertEquals(1, q10res.size());
    assertEquals(1, q11res.size());
    assertEquals(1, q20res.size());
    QueryResultsRow row10 = q10res.iterator().next();
    QueryResultsRow row11 = q11res.iterator().next();
    QueryResultsRow row20 = q20res.iterator().next();
    assertEquals("foo", row10.get("$s"));
    assertEquals("foo", row11.get("$s"));
    assertEquals("foo", row20.get("$s"));
    Object foo = row20.get("$foo");
    assertSame(foo, session.getObjects().iterator().next());
    // the implicit return argument, the abduced/retrieved fact, is hidden
    assertNull(row11.get(""));
}
Also used : Query(org.kie.api.definition.rule.Query) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieSession(org.kie.api.runtime.KieSession) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Abducible(org.drools.core.beliefsystem.abductive.Abducible) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 8 with Abducible

use of org.drools.core.beliefsystem.abductive.Abducible in project drools by kiegroup.

the class AbductionTest method testAbductiveLogicNoConstructorFoundError.

@Test
public void testAbductiveLogicNoConstructorFoundError() {
    String droolsSource = "package org.drools.abductive.test; \n" + "" + "import " + Abducible.class.getName() + "; \n" + "global java.util.List list; \n" + "" + "declare Foo \n" + "   @Abducible \n" + "   id : Integer @key \n" + "end \n" + "query foo( String $x ) \n" + // Foo does not have a String constructor
    "   @Abductive( target=Foo.class ) \n" + "end \n" + "rule R1 " + "when " + "   $x : foo( \"x\" ; ) " + "then " + "   list.add( $x ); " + "end \n" + "" + "";
    // ///////////////////////////////////
    KieHelper kieHelper = new KieHelper();
    kieHelper.addContent(droolsSource, ResourceType.DRL);
    Results res = kieHelper.verify();
    assertEquals(1, res.getMessages(Message.Level.ERROR).size());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Results(org.kie.api.builder.Results) KieHelper(org.kie.internal.utils.KieHelper) Abducible(org.drools.core.beliefsystem.abductive.Abducible) Test(org.junit.Test)

Aggregations

Abducible (org.drools.core.beliefsystem.abductive.Abducible)8 Test (org.junit.Test)8 KieSession (org.kie.api.runtime.KieSession)5 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)4 QueryResults (org.kie.api.runtime.rule.QueryResults)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Results (org.kie.api.builder.Results)3 KieHelper (org.kie.internal.utils.KieHelper)3 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Query (org.kie.api.definition.rule.Query)1 FactType (org.kie.api.definition.type.FactType)1 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)1