Search in sources :

Example 1 with Abducible

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

the class AbductionTest method testAbductiveLogicWithNonExistingArgsMapping.

@Test
public void testAbductiveLogicWithNonExistingArgsMapping() {
    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 : String @key \n" + "   name : String @key \n" + "end \n" + "query foo( String $name ) \n" + "   @Abductive( target=Foo.class, args={ $missing, $name } ) \n" + "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)

Example 2 with Abducible

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

the class AbductionTest method testAbductiveLogicUnlinking.

@Test
public void testAbductiveLogicUnlinking() {
    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( Integer $i ) \n" + "   @Abductive( target=Foo.class ) \n" + "end \n" + "rule R1 " + "when " + "   foo( 42 ; ) " + "   Foo( 42 ; ) " + "then " + "   list.add( 1 ); " + "end \n" + "" + "rule R2 " + "when " + "   foo( 24 ; ) " + "   String() " + "   Foo( 24 ; ) " + "then " + "   list.add( 2 ); " + "end \n" + "" + "";
    // ///////////////////////////////////
    KieSession session = getSessionFromString(droolsSource);
    List list = new ArrayList();
    session.setGlobal("list", list);
    session.fireAllRules();
    session.insert("test");
    session.fireAllRules();
    for (Object o : session.getObjects()) {
        System.out.println(">> " + o);
    }
    System.err.println(list);
    assertEquals(Arrays.asList(1, 2), list);
}
Also used : ArrayList(java.util.ArrayList) 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 3 with Abducible

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

the class AbductionTest method testAbductiveLogicWithWrongTypeArgsMapping.

@Test
public void testAbductiveLogicWithWrongTypeArgsMapping() {
    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 : String @key \n" + "   name : String @key \n" + "end \n" + "query foo( String $name, int $x ) \n" + "   @Abductive( target=Foo.class, args={ $x, $name } ) \n" + "end \n" + "rule R0 " + "when " + "   $f := foo( \"name_test\", 99 ; ) " + "then " + "   list.add( $f ); " + "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)

Example 4 with Abducible

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

the class AbductionTest method testAbducedReturnBinding.

@Test
public void testAbducedReturnBinding() {
    String droolsSource = "package org.drools.abductive.test; \n" + "" + "import " + Abducible.class.getName() + "; \n" + "global java.util.Map map; \n" + "" + "declare Foo \n" + "   @Abducible \n" + "   id : Integer @key \n" + "end \n" + "query foo( Integer $i ) \n" + "   @Abductive( target=Foo.class ) \n" + "   $i := Integer() " + "end \n" + "rule R1 " + "when " + "   $x : foo( $v ; ) " + "then " + "   map.put( $v, $x ); " + "end \n" + "";
    // ///////////////////////////////////
    KieSession session = getSessionFromString(droolsSource);
    Map map = new HashMap();
    session.setGlobal("map", map);
    session.insert(3);
    session.insert(42);
    session.insert(11);
    session.fireAllRules();
    System.out.println(map);
    assertTrue(map.keySet().containsAll(Arrays.asList(3, 42, 11)));
    FactType foo = session.getKieBase().getFactType("org.drools.abductive.test", "Foo");
    for (Object k : map.keySet()) {
        Object val = map.get(k);
        assertSame(foo.getFactClass(), val.getClass());
        assertEquals(k, foo.get(val, "id"));
    }
}
Also used : HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) HashMap(java.util.HashMap) Map(java.util.Map) Abducible(org.drools.core.beliefsystem.abductive.Abducible) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 5 with Abducible

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

the class AbductionTest method testAbductiveLogicWithConstructorArgs.

@Test
public void testAbductiveLogicWithConstructorArgs() {
    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" + "   name : String @key \n" + "   value : double \n" + "   flag : boolean \n" + "end \n" + "query foo() \n" + "   @Abductive( target=Foo.class ) \n" + "end \n" + "query foo2( Integer $i, String $name ) \n" + "   @Abductive( target=Foo.class ) \n" + "   $i := Integer() from new Integer( 4 ) \n" + "   $name := String() " + "end \n" + "query foo3( Integer $i, String $name, double $val, boolean $bool ) \n" + "   @Abductive( target=Foo.class ) \n" + "end \n" + "rule Init " + "   salience 9999 " + "when " + "then " + "   System.out.println( 'Foo zero is in' ); \n" + "   insert( new Foo() ); \n" + "end " + "rule R1 " + "when " + "   $fx : foo() " + "then " + "   list.add( 1 ); " + "end \n" + "" + "rule R2 " + "when " + "   foo2( 4, $n ; ) " + "then " + "   list.add( 2 ); " + "end \n" + "" + "rule R3 " + "when " + "   foo3( 42, \"test2\", $dbl, $bool ; ) " + "then " + "   list.add( 3 ); " + "end \n" + "" + "";
    // ///////////////////////////////////
    KieSession session = getSessionFromString(droolsSource);
    List list = new ArrayList();
    session.setGlobal("list", list);
    session.insert("john");
    session.fireAllRules();
    for (Object o : session.getObjects()) {
        System.out.println(">> " + o);
    }
    System.err.println(list);
    assertEquals(Arrays.asList(1, 2, 3), list);
}
Also used : ArrayList(java.util.ArrayList) 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)

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