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());
}
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);
}
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());
}
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"));
}
}
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);
}
Aggregations