use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class NaiveBayesTest method testNaiveBayesWithGaussianDistr.
@Test
public void testNaiveBayesWithGaussianDistr() throws Exception {
KieSession kieSession = getModelSession(source2, VERBOSE);
setKSession(kieSession);
// init model
kieSession.fireAllRules();
kieSession.getEntryPoint("in_Gender").insert("male");
kieSession.getEntryPoint("in_AgeOfIndividual").insert(24.0);
kieSession.getEntryPoint("in_NoOfClaims").insert("2");
kieSession.getEntryPoint("in_AgeOfCar").insert(1.0);
kieSession.fireAllRules();
System.out.println(reportWMObjects(kieSession));
QueryResults q1 = kieSession.getQueryResults("ProbabilityOf1000", "NaiveBayesInsurance", Variable.v);
assertEquals(1, q1.size());
Object a1 = q1.iterator().next().get("$result");
assertTrue(a1 instanceof Double);
assertEquals(0.112, (Double) a1, 4);
QueryResults q2 = kieSession.getQueryResults("ChosenClass", "NaiveBayesInsurance", Variable.v);
assertEquals(1, q2.size());
Object a2 = q2.iterator().next().get("$result");
assertTrue(a2 instanceof Integer);
assertEquals(100, a2);
checkGeneratedRules();
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryBadResultTest method testAccessToNotExistingVariable.
@Test(expected = IllegalArgumentException.class)
public void testAccessToNotExistingVariable() {
final KieBase kieBase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "query.drl");
final KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Petr"));
final QueryResults results = ksession.getQueryResults("simple query with no parameters");
results.iterator().next().get("bad");
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class UnicodeTest method testQueryCallFromJava.
// test queries in Czech language
@Test
public void testQueryCallFromJava() throws InstantiationException, IllegalAccessException {
final KieServices kieServices = KieServices.Factory.get();
final Resource resource = kieServices.getResources().newClassPathResource("unicode.drl", getClass());
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
final KieSession ksession = kbase.newKieSession();
final FactType locationType = kbase.getFactType(TestConstants.PACKAGE_FUNCTIONAL, "Umístění");
// a pear is in the kitchen
final Object hruška = locationType.newInstance();
locationType.set(hruška, "věc", "hruška");
locationType.set(hruška, "místo", "kuchyně");
// a desk is in the office
final Object stůl = locationType.newInstance();
locationType.set(stůl, "věc", "stůl");
locationType.set(stůl, "místo", "kancelář");
// a flashlight is on the desk
final Object svítilna = locationType.newInstance();
locationType.set(svítilna, "věc", "svítilna");
locationType.set(svítilna, "místo", "stůl");
// an envelope is on the desk
final Object obálka = locationType.newInstance();
locationType.set(obálka, "věc", "obálka");
locationType.set(obálka, "místo", "stůl");
// a key is in the envelope
final Object klíč = locationType.newInstance();
locationType.set(klíč, "věc", "klíč");
locationType.set(klíč, "místo", "obálka");
// create working memory objects
final List<Command<?>> commands = new ArrayList<Command<?>>();
// Location instances
commands.add(kieServices.getCommands().newInsert(hruška));
commands.add(kieServices.getCommands().newInsert(stůl));
commands.add(kieServices.getCommands().newInsert(svítilna));
commands.add(kieServices.getCommands().newInsert(obálka));
commands.add(kieServices.getCommands().newInsert(klíč));
// fire all rules
final String queryAlias = "obsaženo";
commands.add(kieServices.getCommands().newQuery(queryAlias, "jeObsažen", new Object[] { Variable.v, "kancelář" }));
final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
final QueryResults qResults = (QueryResults) results.getValue(queryAlias);
final List<String> l = new ArrayList<String>();
for (QueryResultsRow r : qResults) {
l.add((String) r.get("x"));
}
// items in the office should be the following
Assertions.assertThat(l.size()).isEqualTo(4);
Assertions.assertThat(l.contains("stůl")).isTrue();
Assertions.assertThat(l.contains("svítilna")).isTrue();
Assertions.assertThat(l.contains("obálka")).isTrue();
Assertions.assertThat(l.contains("klíč")).isTrue();
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class ConcurrentBasesParallelTest method testQueries.
@Test(timeout = 20000)
public void testQueries() throws InterruptedException {
final int numberOfObjects = 100;
final TestExecutor exec = counter -> {
final String query = "import " + BeanA.class.getCanonicalName() + ";\n" + "query Query " + " bean : BeanA( seed == " + counter + " ) " + "end";
final KieBase base = getKieBase(query);
final KieSession session = base.newKieSession();
try {
final BeanA bean = new BeanA(counter);
session.insert(bean);
for (int i = 0; i < numberOfObjects; i++) {
if (i != counter) {
session.insert(new BeanA(i));
}
}
final QueryResults results = session.getQueryResults("Query");
Assertions.assertThat(results).hasSize(1);
for (final QueryResultsRow row : results) {
Assertions.assertThat(row.get("bean")).isEqualTo(bean);
}
return true;
} finally {
session.dispose();
}
};
parallelTest(NUMBER_OF_THREADS, exec);
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryCepFireUntilHaltTest method withResultTest.
@Test(timeout = 10000L)
public void withResultTest() {
secondEntryPoint.insert(new TestEvent("minusOne"));
clock.advanceTime(5, TimeUnit.SECONDS);
firstEntryPoint.insert(new TestEvent("zero"));
secondEntryPoint.insert(new TestEvent("one"));
clock.advanceTime(10, TimeUnit.SECONDS);
secondEntryPoint.insert(new TestEvent("two"));
clock.advanceTime(5, TimeUnit.SECONDS);
secondEntryPoint.insert(new TestEvent("three"));
QueryResults results = ksession.getQueryResults("ZeroToNineteenSeconds");
assertEquals(1, results.size());
}
Aggregations