org.kie.api.command.Command Java Examples

The following examples show how to use org.kie.api.command.Command. 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: 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 #2
Source File: FireAllRulesCommandTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void infiniteLoopTerminatesAtMaxTest() {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " update($c); \n";
    str += "end \n";

    StatelessKieSession ksession = getSession(str);

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
    farc.setOutIdentifier("num-rules-fired");
    commands.add(farc);

    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());

    assertEquals(10, fired);
}
 
Example #3
Source File: FireAllRulesCommandTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void oneRuleFiredTest() {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";

    StatelessKieSession ksession = getSession(str);

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    commands.add(CommandFactory.newFireAllRules("num-rules-fired"));

    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());

    assertEquals(1, fired);
}
 
Example #4
Source File: FireAllRulesCommandTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void zeroRulesFiredTest() {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";

    StatelessKieSession ksession = getSession(str);

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert("not cheese"));
    commands.add(CommandFactory.newFireAllRules("num-rules-fired"));

    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());

    assertEquals(0, fired);
}
 
Example #5
Source File: BatchExecutionCommandImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public BatchExecutionCommandImpl addCommand( Command cmd ) {
    if ( commands == null ) {
        commands = new ArrayList<Command>();
    }
    this.commands.add(cmd);
    return this;
}
 
Example #6
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Command newSetGlobal(String identifier, Object object, boolean out) {
    if (out) {
        return newSetGlobal(identifier, object, identifier);
    } else {
        return newSetGlobal(identifier, object);
    }
}
 
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 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 #8
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 #9
Source File: DynamicRulesChangesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public List<String> call() throws Exception {
    final List<String> events = new ArrayList<String>();

    try {
        KieSession ksession = createKnowledgeSession(kbase);
        ksession.setGlobal("events", events);

        Room room1 = new Room("Room 1");
        Fire fire1 = new Fire(room1);

        // phase 1
        List<Command> cmds = new ArrayList<Command>();
        cmds.add(CommandFactory.newInsert(room1, "room1"));
        cmds.add(CommandFactory.newInsert(fire1, "fire1"));
        cmds.add(CommandFactory.newFireAllRules());
        ksession.execute(CommandFactory.newBatchExecution(cmds));
        assertEquals(1, events.size());

        // phase 2
        cmds = new ArrayList<Command>();
        cmds.add(CommandFactory.newInsert(new Sprinkler(room1), "sprinkler1"));
        cmds.add(CommandFactory.newFireAllRules());
        ksession.execute(CommandFactory.newBatchExecution(cmds));
        assertEquals(2, events.size());

        // phase 3
        cmds = new ArrayList<Command>();
        cmds.add(CommandFactory.newDelete(ksession.getFactHandle(fire1)));
        cmds.add(CommandFactory.newFireAllRules());
        ksession.execute(CommandFactory.newBatchExecution(cmds));
    } catch (Exception e) {
        System.err.println("Exception in thread " + Thread.currentThread().getName() + ": " + e.getLocalizedMessage());
        throw e;
    }

    return events;
}
 
Example #10
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Command newStartProcess(String processId,
        Map<String, Object> parameters) {
    StartProcessCommand startProcess = new StartProcessCommand();
    startProcess.setProcessId(processId);
    startProcess.setParameters(parameters);
    return startProcess;
}
 
Example #11
Source File: EnableAuditLogCommandTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnableAuditLogCommand() throws Exception {

    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";

    KieSession kSession = new KieHelper().addContent( str, ResourceType.DRL )
                                         .build().newKieSession();

    List<Command> commands = new ArrayList<Command>();
    commands.add( CommandFactory.newEnableAuditLog( auditFileDir, auditFileName ) );
    commands.add( CommandFactory.newInsert( new Cheese() ) );
    commands.add( CommandFactory.newFireAllRules() );
    kSession.execute( CommandFactory.newBatchExecution( commands ) );
    kSession.dispose();

    File file = new File( auditFileDir + File.separator + auditFileName + ".log" );

    assertTrue( file.exists() );

}
 
Example #12
Source File: CommandFactory.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static Command newSignalEvent(String processInstanceId,
                                     String type,
                                     Object event) {
    return getCommandFactoryProvider().newSignalEvent( processInstanceId,
                                                       type,
                                                       event );
}
 
Example #13
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command newGetGlobal(String identifier) {
    return new GetGlobalCommand(identifier);
}
 
Example #14
Source File: ExecuteCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public ExecuteCommand(String identifier, Command  command) {
    this.command = command;
    this.outIdentifier = identifier;
}
 
Example #15
Source File: ExecuteCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public ExecuteCommand(Command  command) {
    this.command = command;
}
 
Example #16
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command newModify(FactHandle factHandle,
                         List<Setter> setters) {
    return new ModifyCommand(factHandle, setters);
}
 
Example #17
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command newClearRuleFlowGroup(String name) {
    return new ClearRuleFlowGroupCommand(name);
}
 
Example #18
Source File: InternalLocalRunner.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public SingleCommandExecutable( Command command ) {
    super(new BatchImpl().addCommand( command ) );
}
 
Example #19
Source File: InternalLocalRunner.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
default <T> T execute( Command<T> command, Context ctx ) {
    execute( new SingleCommandExecutable( command ), (RequestContext) ctx );
    return command instanceof BatchExecutionCommand ?
           (T) ( (RegistryContext) ctx ).lookup( ExecutionResultImpl.class ) :
           (T) ( (RequestContext) ctx ).getResult();
}
 
Example #20
Source File: InternalLocalRunner.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
default <T> T execute( Command<T> command ) {
    Context ctx = execute( new SingleCommandExecutable( command ) );
    return command instanceof BatchExecutionCommand ?
           (T) ( (RegistryContext) ctx ).lookup( ExecutionResultImpl.class ) :
           (T) ( (RequestContext) ctx ).getResult();
}
 
Example #21
Source File: CommandFactory.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public static Command<FactHandle> fromExternalFactHandleCommand(String factHandleExternalForm) {
    return getCommandFactoryProvider().fromExternalFactHandleCommand(factHandleExternalForm);
}
 
Example #22
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command<FactHandle> fromExternalFactHandleCommand(String factHandleExternalForm) {
    return fromExternalFactHandleCommand(factHandleExternalForm, false);
}
 
Example #23
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Command newGetFactHandleInEntryPoint( Object object, String entryPoint ) {
    return new GetFactHandleInEntryPointCommand( object, entryPoint );
}
 
Example #24
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Command newGetFactHandles() {
    return new GetFactHandlesCommand();
}
 
Example #25
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Command newEnableAuditLog( String filename ) {
    return new EnableAuditLogCommand( null, filename );
}
 
Example #26
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command newQuery(String identifier, String name, Object[] arguments) {
    return new QueryCommand(identifier, name, arguments);
}
 
Example #27
Source File: ExecuteCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public ExecuteCommand(String identifier, Command  command, boolean disconnected) {
    this.command = command;
    this.outIdentifier = identifier;
    this.disconnected = disconnected;
}
 
Example #28
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command newFireAllRules(String outidentifier) {
    return new FireAllRulesCommand(outidentifier);
}
 
Example #29
Source File: ExecuteCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command getCommand() {
    return this.command;
}
 
Example #30
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Command newSignalEvent(String processInstanceId,
                           String type,
                           Object event) {
    return new SignalEventCommand( processInstanceId, type, event );
}