use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.
the class QueryTest method testQueryWithOOPath.
@Test
public void testQueryWithOOPath() {
String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$cities : List() from accumulate (Person ( $city: /address#InternationalAddress[state == \"Safecountry\"]/city), collectList($city))\n" + "end";
KieSession ksession = getKieSession(str);
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
QueryResults results = ksession.getQueryResults("listSafeCities");
List cities = (List) results.iterator().next().get("$cities");
assertEquals(1, cities.size());
assertEquals("Milan", cities.get(0));
}
use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.
the class QueryTest method testQueryWithOOPathTransformedToFromInsideAcc.
@Test
public void testQueryWithOOPathTransformedToFromInsideAcc() {
String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$cities : List() from accumulate (" + " $p : Person() and\n" + " $a : InternationalAddress(state == \"Safecountry\") from $p.address and\n" + " $city : String() from $a.city, collectList($city))\n" + "end";
KieSession ksession = getKieSession(str);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
QueryResults results = ksession.getQueryResults("listSafeCities");
List cities = (List) results.iterator().next().get("$cities");
assertEquals(1, cities.size());
assertEquals("Milan", cities.get(0));
}
use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.
the class QueryTest method testQueryWithOOPathTransformedToFrom.
@Test
public void testQueryWithOOPathTransformedToFrom() {
String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$p : Person()\n" + "$a : InternationalAddress(state == \"Safecountry\") from $p.address\n" + "$cities : List() from accumulate ($city : String() from $a.city, collectList($city))\n" + "end";
KieSession ksession = getKieSession(str);
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
QueryResults results = ksession.getQueryResults("listSafeCities");
List cities = (List) results.iterator().next().get("$cities");
assertEquals(1, cities.size());
assertEquals("Milan", cities.get(0));
}
use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.
the class FlowTest method testQueryOOPathAccumulateTransformed.
@Test
public void testQueryOOPathAccumulateTransformed() {
final org.drools.model.QueryDef queryDef_listSafeCities = query("listSafeCities");
final Variable<org.drools.modelcompiler.oopathdtables.Person> var_$p = declarationOf(org.drools.modelcompiler.oopathdtables.Person.class, "$p");
final Variable<org.drools.modelcompiler.oopathdtables.InternationalAddress> var_$a = declarationOf(org.drools.modelcompiler.oopathdtables.InternationalAddress.class, "$a", from(var_$p, (_this) -> _this.getAddress()));
final Variable<java.util.List> var_$cities = declarationOf(java.util.List.class, "$cities");
final Variable<String> var_$city = declarationOf(String.class, "$city", from(var_$a, (_this) -> _this.getCity()));
org.drools.model.Query listSafeCities_build = queryDef_listSafeCities.build(input(var_$p), expr("$expr$2$", var_$a, (_this) -> _this.getState().equals("Safecountry")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getState(), "Safecountry").reactOn("state"), accumulate(expr(var_$city, s -> true), accFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction.class, var_$city).as(var_$cities)));
Model model = new ModelImpl().addQuery(listSafeCities_build);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
QueryResults results = ksession.getQueryResults("listSafeCities");
assertEquals("Milan", ((List) results.iterator().next().get("$cities")).iterator().next());
}
use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.
the class AccumulateTest method testAccumulateFromWithConstraint.
@Test
public void testAccumulateFromWithConstraint() {
String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "rule listSafeCities when\n" + " $a : InternationalAddress()\n" + " $cities : List(size > 0) from accumulate ($city : String() from $a.city, collectList($city))\n" + "then\n" + " insert($cities.get(0));\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.fireAllRules();
List<String> results = getObjectsIntoList(ksession, String.class);
assertEquals(1, results.size());
assertThat(results, hasItem("Milan"));
}
Aggregations