Search in sources :

Example 56 with QueryResults

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();
}
Also used : KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 57 with QueryResults

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");
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 58 with QueryResults

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();
}
Also used : ExecutionResults(org.kie.api.runtime.ExecutionResults) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) QueryResults(org.kie.api.runtime.rule.QueryResults) FactType(org.kie.api.definition.type.FactType) Command(org.kie.api.command.Command) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 59 with QueryResults

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);
}
Also used : Arrays(java.util.Arrays) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) BeanA(org.drools.compiler.integrationtests.facts.BeanA) BeanB(org.drools.compiler.integrationtests.facts.BeanB) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) BeanA(org.drools.compiler.integrationtests.facts.BeanA) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 60 with QueryResults

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());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)79 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)58 ArrayList (java.util.ArrayList)30 KieBase (org.kie.api.KieBase)25 List (java.util.List)24 Person (org.drools.modelcompiler.domain.Person)19 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 FactHandle (org.kie.api.runtime.rule.FactHandle)15 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)10 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 Collection (java.util.Collection)8 Map (java.util.Map)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 InternationalAddress (org.drools.modelcompiler.oopathdtables.InternationalAddress)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)6 Global (org.drools.model.Global)5