Java Code Examples for org.kie.internal.command.CommandFactory#newBatchExecution()

The following examples show how to use org.kie.internal.command.CommandFactory#newBatchExecution() . 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: MoreBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 7 votes vote down vote up
@Test
public void testQuery() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ksession = createKnowledgeSession(kbase);
    
    ksession.insert( new Cheese( "stinky", 5 ) );
    ksession.insert( new Cheese( "smelly", 7 ) );
    
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull(result, "Batch execution result is null!");

    Object queryResultsObject = result.getValue("numStinkyCheeses");
    assertTrue(queryResultsObject != null && queryResultsObject instanceof QueryResults, "Retrieved object is null or incorrect!");
    
    assertEquals( 1, ((QueryResults) queryResultsObject).size() );
}
 
Example 2
Source File: SimpleBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test 
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testSetGlobalCommand() throws Exception {
    
    ksession.insert(new Integer(5));
    ksession.insert(new Integer(7));
    ksession.fireAllRules();
    
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newSetGlobal( "globalCheeseCountry", "France", true ));

    Command cmds = CommandFactory.newBatchExecution( commands );

    ExecutionResults result = (ExecutionResults) ksession.execute( cmds );
    assertNotNull(result);
    Object global = result.getValue("globalCheeseCountry");
    assertNotNull(global);
    assertEquals("France", global);
}
 
Example 3
Source File: SimpleBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test 
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetGlobalCommand() throws Exception {
    
    ksession.insert(new Integer(5));
    ksession.insert(new Integer(7));
    ksession.fireAllRules();
    ksession.setGlobal("globalCheeseCountry", "France");
    
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetGlobal( "globalCheeseCountry", "cheeseCountry" ));
    Command cmds = CommandFactory.newBatchExecution( commands );

    ExecutionResults result = (ExecutionResults) ksession.execute( cmds );
    assertNotNull(result, "GetGlobalCommand result is null!");
    Object global = result.getValue("cheeseCountry");
    assertNotNull(global, "Retrieved global fact is null!");
    assertEquals("France", global, "Retrieved global is not equal to 'France'.");
}
 
Example 4
Source File: SimpleBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectCommand() throws Exception {
    
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    FactHandle handle_1 = ksession.insert( expected_1 );
    FactHandle handle_2 = ksession.insert( expected_2 );
    ksession.fireAllRules();
    
    Object fact = ksession.getObject(handle_1);
    assertNotNull(fact);
    assertEquals(expected_1, fact);

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetObject(handle_1, "out_1"));
    commands.add(CommandFactory.newGetObject(handle_2, "out_2"));
    Command cmds = CommandFactory.newBatchExecution( commands );
    
    ExecutionResults result = (ExecutionResults) ksession.execute( cmds );
    assertNotNull(result, "GetObjectCommand result is null!");

    assertEquals(expected_1, result.getValue("out_1"));
    assertEquals(expected_2, result.getValue("out_2"));
}
 
Example 5
Source File: MoreBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testFireAllRules() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/drl/test_ImportFunctions.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ksession = createKnowledgeSession(kbase);

    final Cheese cheese = new Cheese("stilton", 15);
    ksession.insert(cheese);
    List<?> list = new ArrayList();
    ksession.setGlobal("list", list);

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newFireAllRules("fired"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull(result, "Batch execution result is null!");

    Object firedObject = result.getValue("fired");
    assertTrue( firedObject != null && firedObject instanceof Integer, "Retrieved object is null or incorrect!");
    assertEquals(4, firedObject);

    list = (List<?>) ksession.getGlobal("list");
    assertEquals(4, list.size());

    assertEquals("rule1", list.get(0));
    assertEquals("rule2", list.get(1));
    assertEquals("rule3", list.get(2));
    assertEquals("rule4", list.get(3));
}
 
Example 6
Source File: SimpleBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test 
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertObjectCommand() throws Exception {
    
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(expected_1, "out_1"));
    commands.add(CommandFactory.newInsert(expected_2, "out_2"));
    Command cmds = CommandFactory.newBatchExecution( commands );
    
    ExecutionResults result = (ExecutionResults) ksession.execute( cmds );
    
    Object fact_1 = result.getValue("out_1");
    assertNotNull(fact_1);
    Object fact_2 = result.getValue("out_2");
    assertNotNull(fact_2);
    ksession.fireAllRules();

    Object [] expectedArr = {expected_1, expected_2};
    List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
    
    Collection<? extends Object> factList = ksession.getObjects();
    assertTrue(factList.size() == expectedList.size(),
                          "Expected " + expectedList.size() + " objects but retrieved " + factList.size());
    for( Object fact : factList ) {
       if( expectedList.contains(fact) ) { 
           expectedList.remove(fact);
       }
    }
    assertTrue(expectedList.isEmpty(), "Retrieved object list did not contain expected objects.");
}
 
Example 7
Source File: SimpleBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test 
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertElementsCommand() throws Exception {
    
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    Object [] expectedArr = {expected_1, expected_2};
    Collection<Object> factCollection = Arrays.asList(expectedArr);
    
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsertElements(factCollection, "out_list", true, null));
    Command cmds = CommandFactory.newBatchExecution( commands );
    
    ExecutionResults result = (ExecutionResults) ksession.execute( cmds );
    
    Collection<? extends Object> outList = (Collection<? extends Object>) result.getValue("out_list");
    assertNotNull(outList);
    ksession.fireAllRules();

    List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
    
    Collection<? extends Object> factList = ksession.getObjects();
    assertTrue(factList.size() == expectedList.size(),
                          "Expected " + expectedList.size() + " objects but retrieved " + factList.size());
    for( Object fact : factList ) {
       if( expectedList.contains(fact) ) { 
           expectedList.remove(fact);
       }
    }
    assertTrue(expectedList.isEmpty(), "Retrieved object list did not contain expected objects.");
}
 
Example 8
Source File: SimpleBatchExecutionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test 
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectsCommand() throws Exception {
    
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    FactHandle handle_1 = ksession.insert( expected_1 );
    FactHandle handle_2 = ksession.insert( expected_2 );
    ksession.fireAllRules();
    
    Object object = ksession.getObject(handle_1);
    assertNotNull(object);
    assertEquals(expected_1, object);
    object = ksession.getObject(handle_2);
    assertNotNull(object);
    assertEquals(expected_2, object);

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetObjects("out_list"));
    Command cmds = CommandFactory.newBatchExecution( commands );
    
    ExecutionResults result = (ExecutionResults) ksession.execute( cmds );
    assertNotNull(result, "GetObjectsCommand result is null!");

    List<Object> objectList = (List) result.getValue("out_list");
    boolean b = objectList != null && ! objectList.isEmpty();
    assertTrue(b, "Retrieved object list is null or empty!");

    Collection<? extends Object> factList = ksession.getObjects();
    Object [] expectedArr = {expected_1, expected_2};
    List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
    assertTrue(factList.size() == expectedList.size(),
                          "Expected " + expectedList.size() + " objects but retrieved " + factList.size());
    for( Object fact : factList ) {
       if( expectedList.contains(fact) ) { 
           expectedList.remove(fact);
       }
    }
    assertTrue(expectedList.isEmpty(), "Retrieved object list did not contain expected objects.");
}
 
Example 9
Source File: BusinessRulesBean.java    From iot-ocp with Apache License 2.0 5 votes vote down vote up
public Measure processRules(@Body Measure measure) {
	
	KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(
			kieHost, kieUser,
			kiePassword);
	
	Set<Class<?>> jaxBClasses = new HashSet<Class<?>>();
	jaxBClasses.add(Measure.class);
	
	config.addJaxbClasses(jaxBClasses);
	config.setMarshallingFormat(MarshallingFormat.JAXB);
	RuleServicesClient client = KieServicesFactory.newKieServicesClient(config)
			.getServicesClient(RuleServicesClient.class);

       List<Command<?>> cmds = new ArrayList<Command<?>>();
	KieCommands commands = KieServices.Factory.get().getCommands();
	cmds.add(commands.newInsert(measure));
	
    GetObjectsCommand getObjectsCommand = new GetObjectsCommand();
    getObjectsCommand.setOutIdentifier("objects");

	
	cmds.add(commands.newFireAllRules());
	cmds.add(getObjectsCommand);
	BatchExecutionCommand myCommands = CommandFactory.newBatchExecution(cmds,
			"DecisionTableKS");
	ServiceResponse<ExecutionResults> response = client.executeCommandsWithResults("iot-ocp-businessrules-service", myCommands);
			
	List responseList = (List) response.getResult().getValue("objects");
	
	Measure responseMeasure = (Measure) responseList.get(0);
	
	return responseMeasure;

}
 
Example 10
Source File: Utils.java    From servicemix with Apache License 2.0 5 votes vote down vote up
public void insertAndFireAll(Exchange exchange) {
    final Message in = exchange.getIn();
    final Object body = in.getBody();

    final List<Command<?>> commands = new ArrayList<Command<?>>(2);
    commands.add(CommandFactory.newInsert(body));
    commands.add(CommandFactory.newFireAllRules());

    Command<?> batch = CommandFactory.newBatchExecution(commands);
    in.setBody(batch);
}
 
Example 11
Source File: Utils.java    From servicemix with Apache License 2.0 5 votes vote down vote up
/**
 * Create commands for Drools engine.
 *
 * @param body {@link org.apache.camel.Exchange}
 */
public Command<ExecutionResults> insertAndFireAll(final Customer body) {

    Command<?> insert = CommandFactory.newInsert(body, "customer");

    @SuppressWarnings("unchecked")
    Command<ExecutionResults> batch = CommandFactory
            .newBatchExecution(Arrays.asList(insert));

    return batch;
}