Java Code Examples for org.kie.api.runtime.rule.QueryResultsRow#get()
The following examples show how to use
org.kie.api.runtime.rule.QueryResultsRow#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testQueryWithCollect() throws Exception { KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_Query.drl")); KieSession session = createKieSession( kbase ); session.fireAllRules(); QueryResults results = getQueryResults( session, "collect objects" ); assertEquals( 1, results.size() ); final QueryResultsRow row = results.iterator().next(); final List list = (List) row.get( "$list" ); assertEquals( 2, list.size() ); }
Example 2
Source File: ClinicalRulesJUnitTest.java From drools-workshop with Apache License 2.0 | 6 votes |
@Test public void testPatientWithBloodPressureObservations() { printGeneratedDRL( ClinicalRulesJUnitTest.class.getResourceAsStream( "/rules/clinical-decision-table.xls" ), System.out ); Assert.assertNotNull( kBase ); KieSession kSession = kBase.newKieSession(); kSession.insert( generatePatients( 1 ).get( 0 ) ); List<Observation> observations = generateObservations( "headache", 4 ); for ( Observation observation : observations ) { kSession.insert( observation ); } Assert.assertEquals( 2, kSession.fireAllRules() ); QueryResults queryResults = kSession.getQueryResults( "getAllRecommendations", ( Object ) null ); for ( QueryResultsRow row : queryResults ) { Recommendation r = ( Recommendation ) row.get( "$r" ); System.out.println( " >>> Result : " + r ); } kSession.dispose(); }
Example 3
Source File: ConcurrentBasesParallelTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@ParameterizedConcurrentBasesParallelTest public void testQueries2(Parameters params) throws InterruptedException { final int numberOfObjects = 100; final String queryTemplate = "import " + BeanA.class.getCanonicalName() + ";\n" + "query Query " + " bean : BeanA( seed == ${seed} ) " + "end"; final TestExecutor exec = counter -> { final int seed = counter % 2; final String seedString = "" + seed; final String queryDrl = queryTemplate.replace("${seed}", seedString); final KieBase base = getKieBase(params, queryDrl); final KieSession session = base.newKieSession(); try { for (int i = 0; i < numberOfObjects; i++) { session.insert(new BeanA(seed)); } final QueryResults results = session.getQueryResults("Query"); assertThat(results).hasSize(numberOfObjects); for (final QueryResultsRow row : results) { final BeanA bean = (BeanA) row.get("bean"); assertThat(bean.getSeed()).isEqualTo(seed); } return true; } finally { session.dispose(); } }; parallelTest(NUMBER_OF_THREADS, exec); }
Example 4
Source File: BasicUpdateTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private <T> void verifyFactsWithQuery(final Class<T> expectedClassOfFacts, final String queryToGetFacts, final T... factsToVerify) { final QueryResults results = ksession.getQueryResults(queryToGetFacts); assertThat(results).isNotEmpty(); final QueryResultsRow resultsRow = results.iterator().next(); assertThat(resultsRow.get("$" + queryToGetFacts)).isInstanceOf(List.class); final List<Object> objects = (List<Object>) resultsRow.get("$" + queryToGetFacts); assertThat(objects).hasSize(factsToVerify.length); assertThat(objects).hasOnlyElementsOfType(expectedClassOfFacts); assertThat(objects).containsAll(Arrays.asList(factsToVerify)); }
Example 5
Source File: BasicUpdateTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private void verifyWithQueryNoPersonsPresentInFacts() { QueryResults results = ksession.getQueryResults("persons"); assertThat(results).isNotEmpty(); results = ksession.getQueryResults("persons"); assertThat(results).isNotEmpty(); final QueryResultsRow resultsRow = results.iterator().next(); assertThat(resultsRow.get("$persons")).isInstanceOf(List.class); final List<Object> persons = (List<Object>) resultsRow.get("$persons"); assertThat(persons).isEmpty(); }
Example 6
Source File: LibraryClient.java From rhpam-7-openshift-image with Apache License 2.0 | 5 votes |
Suggestion getSuggestion(String keyword) { SuggestionRequest suggestionRequest = new SuggestionRequest(); suggestionRequest.setKeyword(keyword); suggestionRequest.setKeyword("Zombie"); List<Command<?>> cmds = new ArrayList<Command<?>>(); cmds.add(commands.newInsert(suggestionRequest)); cmds.add(commands.newFireAllRules()); cmds.add(commands.newQuery("suggestion", "get suggestion")); BatchExecutionCommand batch = commands.newBatchExecution(cmds, "LibraryRuleSession"); ExecutionResults execResults; if (appcfg.getKieSession() != null) { execResults = appcfg.getKieSession().execute(batch); } else { ServiceResponse<ExecutionResults> serviceResponse = appcfg.getRuleServicesClient().executeCommandsWithResults("rhpam-kieserver-library", batch); //logger.info(String.valueOf(serviceResponse)); execResults = serviceResponse.getResult(); } QueryResults queryResults = (QueryResults) execResults.getValue("suggestion"); if (queryResults != null) { for (QueryResultsRow queryResult : queryResults) { SuggestionResponse suggestionResponse = (SuggestionResponse) queryResult.get("suggestionResponse"); if (suggestionResponse != null) { return suggestionResponse.getSuggestion(); } } } return null; }
Example 7
Source File: DeliveryServiceImpl.java From drools-workshop with Apache License 2.0 | 5 votes |
@Override public Delivery getDelivery(String deliveryId) throws BusinessException { KieSession kieSession = onGoingDeliveries.get(deliveryId); QueryResults queryResults = kieSession.getQueryResults("getDelivery", new Object[]{}); for (QueryResultsRow qrr : queryResults) { return (Delivery) qrr.get("$d"); } return null; }
Example 8
Source File: AbductionTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testQueryAPIs() { String droolsSource = "package org.drools.abductive.test; \n" + "import " + Abducible.class.getName() + "; \n" + "" + "declare Foo " + " @Abducible " + " id : String " + "end " + "query foo( String $s ) " + " @Abductive( target=Foo.class ) \n" + "end \n "+ "query bar( String $s, Foo $foo ) " + " $foo := Foo() " + "end \n "+ "rule MoveAround " + "when " + " $s : String() " + " $f : foo( $s ; ) " + " bar( $s, $f ; ) " + "then " + " delete( $s ); " + " System.out.println( 'Foo ' + $f ); " + "end " + ""; ///////////////////////////////////// KieSession session = getSessionFromString( droolsSource ); session.insert( "faa" ); session.fireAllRules(); for ( Object o : session.getObjects() ) { System.out.println( ">>> " + o ); } assertEquals( 1, session.getObjects().size() ); Query q1 = session.getKieBase().getQuery( "org.drools.abductive.test", "foo" ); Query q2 = session.getKieBase().getQuery( "org.drools.abductive.test", "bar" ); assertNotNull( q1 ); assertNotNull( q2 ); QueryResults q10res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults( "foo", "foo", null )); QueryResults q11res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults( "foo", "foo", Variable.v )); QueryResults q20res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults( "bar", "foo", Variable.v )); assertEquals( 1, q10res.size() ); assertEquals( 1, q11res.size() ); assertEquals( 1, q20res.size() ); QueryResultsRow row10 = q10res.iterator().next(); QueryResultsRow row11 = q11res.iterator().next(); QueryResultsRow row20 = q20res.iterator().next(); assertEquals( "foo", row10.get( "$s" ) ); assertEquals( "foo", row11.get( "$s" ) ); assertEquals( "foo", row20.get( "$s" ) ); Object foo = row20.get( "$foo" ); assertSame( foo, session.getObjects().iterator().next() ); // the implicit return argument, the abduced/retrieved fact, is hidden assertNull( row11.get( "" ) ); }
Example 9
Source File: QueryTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
private static QueryResults getQueryResults(KieSession session, String queryName, Object... arguments ) throws Exception { QueryResultsImpl results = (QueryResultsImpl) session.getQueryResults( queryName, arguments ); FlatQueryResults flatResults = new FlatQueryResults(results); assertEquals(results.size(), flatResults.size(), "Query results size"); assertEquals(results.getIdentifiers().length, flatResults.getIdentifiers().length, "Query results identifiers"); Set<String> resultIds = new TreeSet<String>(Arrays.asList(results.getIdentifiers())); Set<String> flatIds = new TreeSet<String>(Arrays.asList(flatResults.getIdentifiers())); assertArrayEquals(resultIds.toArray(), flatIds.toArray(), "Flat query results identifiers"); FlatQueryResults copyFlatResults = roundTrip(flatResults); String [] identifiers = results.getIdentifiers(); Iterator<QueryResultsRow> copyFlatIter = copyFlatResults.iterator(); for( int i = 0; i < results.size(); ++i ) { QueryResultsRow row = results.get(i); assertTrue(copyFlatIter.hasNext(), "Round-tripped flat query results contain less rows than original query results"); QueryResultsRow copyRow = copyFlatIter.next(); for( String id : identifiers ) { Object obj = row.get(id); if( obj != null ) { Object copyObj = copyRow.get(id); assertTrue(obj != null && obj.equals(copyObj), "Flat query result [" + i + "] does not contain result: '" + id + "': " + obj + "/" + copyObj); } FactHandle fh = row.getFactHandle(id); FactHandle copyFh = copyRow.getFactHandle(id); if( fh != null ) { assertNotNull(copyFh, "Flat query result [" + i + "] does not contain facthandle: '" + ((InternalFactHandle) fh).getId() + "'"); String fhStr = fh.toExternalForm(); fhStr = fhStr.substring(0, fhStr.lastIndexOf(":")); String copyFhStr = copyFh.toExternalForm(); copyFhStr = copyFhStr.substring(0, copyFhStr.lastIndexOf(":")); assertEquals(fhStr, copyFhStr, "Unequal fact handles for fact handle '" + ((InternalFactHandle) fh).getId() + "':"); } } } // check identifiers Set<String> copyFlatIds = new TreeSet<String>(Arrays.asList(copyFlatResults.getIdentifiers())); assertArrayEquals(flatIds.toArray(), copyFlatIds.toArray(), "Flat query results identifiers"); return copyFlatResults; }