Search in sources :

Example 96 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class DeleteTest method deleteFactTwiceTest.

@Test
public void deleteFactTwiceTest() {
    FactHandle george = ksession.insert(new Person("George", 19));
    QueryResults results = ksession.getQueryResults("countPerson");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(1L);
    ksession.delete(george);
    results = ksession.getQueryResults("countPerson");
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(0L);
    ksession.delete(george);
    Assertions.assertThat(results).isNotEmpty();
    Assertions.assertThat(results.iterator().next().get("$personCount")).isEqualTo(0L);
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Person(org.drools.core.test.model.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 97 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 98 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<>();
    // 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<>();
    for (final 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 99 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class PatternDSLTest method testQueryInvokingQuery2.

@Test
public void testQueryInvokingQuery2() {
    final org.drools.model.Query2Def<java.lang.String, java.lang.String> queryDef_isRelatedTo2 = query("isRelatedTo2", java.lang.String.class, "x", java.lang.String.class, "y");
    final org.drools.model.Query2Def<java.lang.String, java.lang.String> queryDef_isRelatedTo = query("isRelatedTo", java.lang.String.class, "x", java.lang.String.class, "y");
    org.drools.model.Query isRelatedTo_build = queryDef_isRelatedTo.build(queryDef_isRelatedTo2.call(true, queryDef_isRelatedTo.getArg1(), queryDef_isRelatedTo.getArg2()));
    final org.drools.model.Variable<org.drools.modelcompiler.domain.Relationship> var_$pattern_Relationship$4$ = declarationOf(org.drools.modelcompiler.domain.Relationship.class, "$pattern_Relationship$4$");
    org.drools.model.Query isRelatedTo2_build = queryDef_isRelatedTo2.build(pattern(var_$pattern_Relationship$4$).expr("$expr$63$", queryDef_isRelatedTo2.getArg1(), (_this, x) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getStart(), x), betaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getStart(), x -> x), reactOn("start")).expr("$expr$64$", queryDef_isRelatedTo2.getArg2(), (_this, y) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getEnd(), y), betaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getEnd(), y -> y), reactOn("end")));
    Model model = new ModelImpl().addQuery(isRelatedTo_build).addQuery(isRelatedTo2_build);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Relationship("A", "B"));
    ksession.insert(new Relationship("B", "C"));
    QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "B");
    assertEquals(1, results.size());
    String paramName = ((QueryImpl) ksession.getKieBase().getQuery("defaultpkg", "isRelatedTo")).getParameters()[1].getIdentifier();
    assertEquals("B", results.iterator().next().get(paramName));
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Activation(org.drools.core.spi.Activation) Index(org.drools.model.Index) Predicate1(org.drools.model.functions.Predicate1) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) ClassObjectType(org.drools.core.base.ClassObjectType) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) Pattern(org.drools.core.rule.Pattern) PatternDSL(org.drools.model.PatternDSL) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Assert.assertSame(org.junit.Assert.assertSame) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IOException(java.io.IOException) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Accumulate(org.drools.core.rule.Accumulate) DSL.supply(org.drools.model.DSL.supply) Assert.assertEquals(org.junit.Assert.assertEquals) QueryResults(org.kie.api.runtime.rule.QueryResults) Query(org.drools.model.Query) QueryImpl(org.drools.core.rule.QueryImpl) KieBase(org.kie.api.KieBase) Relationship(org.drools.modelcompiler.domain.Relationship) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Example 100 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class PatternDSLTest method testQueryInvokingQuery.

@Test
public void testQueryInvokingQuery() {
    Variable<Relationship> relV = DSL.declarationOf(Relationship.class);
    Query2Def<String, String> query1Def = query("isRelatedTo1", String.class, String.class);
    Query2Def<String, String> query2Def = query("isRelatedTo2", String.class, String.class);
    Query query2 = query2Def.build(pattern(relV).expr("exprA", query2Def.getArg1(), (r, s) -> r.getStart().equals(s)).expr("exprB", query2Def.getArg2(), (r, e) -> r.getEnd().equals(e)));
    Query query1 = query1Def.build(query2Def.call(query1Def.getArg1(), query1Def.getArg2()));
    Model model = new ModelImpl().addQuery(query2).addQuery(query1);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Relationship("A", "B"));
    ksession.insert(new Relationship("B", "C"));
    QueryResults results = ksession.getQueryResults("isRelatedTo1", "A", "B");
    assertEquals(1, results.size());
    assertEquals("B", results.iterator().next().get(query1Def.getArg2().getName()));
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Activation(org.drools.core.spi.Activation) Index(org.drools.model.Index) Predicate1(org.drools.model.functions.Predicate1) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) ClassObjectType(org.drools.core.base.ClassObjectType) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) Pattern(org.drools.core.rule.Pattern) PatternDSL(org.drools.model.PatternDSL) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Assert.assertSame(org.junit.Assert.assertSame) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IOException(java.io.IOException) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Accumulate(org.drools.core.rule.Accumulate) DSL.supply(org.drools.model.DSL.supply) Assert.assertEquals(org.junit.Assert.assertEquals) Query(org.drools.model.Query) KieBase(org.kie.api.KieBase) Relationship(org.drools.modelcompiler.domain.Relationship) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)120 Test (org.junit.Test)103 KieSession (org.kie.api.runtime.KieSession)87 ArrayList (java.util.ArrayList)45 KieBase (org.kie.api.KieBase)43 List (java.util.List)35 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)34 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)22 FactHandle (org.kie.api.runtime.rule.FactHandle)22 Person (org.drools.modelcompiler.domain.Person)21 Arrays (java.util.Arrays)12 Collection (java.util.Collection)12 Assertions (org.assertj.core.api.Assertions)11 Map (java.util.Map)10 Relationship (org.drools.modelcompiler.domain.Relationship)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 IOException (java.io.IOException)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)7