org.kie.api.command.BatchExecutionCommand Java Examples

The following examples show how to use org.kie.api.command.BatchExecutionCommand. 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: StatefulKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public <T> T execute(Command<T> command) {
    ExecutableRunner<RequestContext> runner = ExecutableRunner.create();
    RequestContext context = runner.createContext().with( this.kBase ).with( this );

    if ( !(command instanceof BatchExecutionCommand) ) {
        return runner.execute( command, context );
    }

    try {
        startBatchExecution();
        return runner.execute( command, context );
    } finally {
        endBatchExecution();
        if (kBase.flushModifications() && !stateless) {
            fireAllRules();
        }
    }
}
 
Example #2
Source File: FoodRecommendationServiceImpl.java    From drools-workshop with Apache License 2.0 6 votes vote down vote up
@Override
public Meal recommendMeal(Person person) throws BusinessException {
    StatelessKieSession kieSession = kContainer.newStatelessKieSession();

    InsertObjectCommand insertObjectCommand = new InsertObjectCommand(person);
    FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand();
    QueryCommand queryCommand = new QueryCommand("results", "getMeal", (Object[]) null);
    List<GenericCommand<?>> commands = new ArrayList<GenericCommand<?>>();
    commands.add(insertObjectCommand);
    commands.add(fireAllRulesCommand);
    commands.add(queryCommand);
    BatchExecutionCommand batch = new BatchExecutionCommandImpl(commands);

    ExecutionResults results = kieSession.execute(batch);
    QueryResults queryResults = (QueryResults) results.getValue("results");
    Iterator<QueryResultsRow> iterator = queryResults.iterator();
    while (iterator.hasNext()) {
        Meal meal = (Meal) iterator.next().get("$m");
        if (meal != null) {
            System.out.println("Meal : " + meal);
            return meal;
        }
    }

    return null;
}
 
Example #3
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public <T> T execute(Command<T> command) {
    StatefulKnowledgeSession ksession = newWorkingMemory();

    RegistryContext context = new ContextImpl().register( KieSession.class, ksession );

    try {
        if ( command instanceof BatchExecutionCommand ) {
            ((RegistryContext) context).register( ExecutionResultImpl.class, new ExecutionResultImpl() );
        }

        ((StatefulKnowledgeSessionImpl) ksession).startBatchExecution();

        Object o = ((ExecutableCommand) command).execute( context );
        // did the user take control of fireAllRules, if not we will auto execute
        boolean autoFireAllRules = true;
        if ( command instanceof FireAllRulesCommand ) {
            autoFireAllRules = false;
        } else if ( command instanceof BatchExecutionCommandImpl ) {
            for ( Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands() ) {
                if ( nestedCmd instanceof FireAllRulesCommand ) {
                    autoFireAllRules = false;
                    break;
                }
            }
        }
        if ( autoFireAllRules ) {
            ksession.fireAllRules();
        }
        if ( command instanceof BatchExecutionCommand ) {
            return (T) ((RegistryContext) context).lookup( ExecutionResultImpl.class );
        } else {
            return (T) o;
        }
    } finally {
        ((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
        dispose(ksession);
    }
}
 
Example #4
Source File: LibraryClient.java    From rhpam-7-openshift-image with Apache License 2.0 5 votes vote down vote up
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 #5
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 #6
Source File: CommandFactory.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @return
 */
public static BatchExecutionCommand newBatchExecution(List< ? extends Command> commands, String lookup ) {
    return getCommandFactoryProvider().newBatchExecution( commands,
                                                          lookup);
}
 
Example #7
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 #8
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 #9
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public BatchExecutionCommand newBatchExecution(List<? extends Command> commands) {
    return newBatchExecution( commands, null );
}
 
Example #10
Source File: CommandFactoryServiceImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public BatchExecutionCommand newBatchExecution(List<? extends Command> commands, String lookup) {
    return new BatchExecutionCommandImpl( commands, lookup );
}
 
Example #11
Source File: CommandFactory.java    From kogito-runtimes with Apache License 2.0 2 votes vote down vote up
/**
 * This is a special composite command and will execute all the supplied commands in turn.
 * @param commands
 * @return
 */
public static BatchExecutionCommand newBatchExecution(List< ? extends Command> commands) {
    return getCommandFactoryProvider().newBatchExecution( commands, null );
}