Editing the ruleset execution class
When you create a Java™ project for rules, a main class is generated from the template that you selected. You must edit this class to define your parameters and complete it for your needs.
Procedure
- Open the decision engine execution class created in the Java project for rules.
- Load the ruleset archive.
// Retrieve the decision engine ruleset archive ZIP file using the Java I/O API File file = new File(rulesetPath); // Load the archive EngineLoader loader = new EngineLoader(file); - Get an engine definition.
EngineDefinition definition = loader.load();Note: The EngineDefinition interface is the base interface to implement a decision engine. Alternatively, use the RuleEngineDefinition interface to access the rules at run time, or the RuleflowEngineDefinition interface to define a specific ruleflow, task, or rule task.You can also use an alternative class loader to load the decision engine classes. For more information, see EngineLoader.
- Create an Engine instance from an engine
definition.
Engine engine = definition.createEngine();For more information, see EngineDefinition.
- Set the ruleset parameter values and run the engine in
one of the following ways:
- Add a map of the parameters as
input:
EngineOutput execute(Map<String, Object>input) - Use the dedicated API to manage input and output
parameters:
EngineInput input = engine.createInput(); Input.setParameter("n", 10); EngineOutput output = engine.execute(input);
- Add a map of the parameters as
input:
- Add the ruleset parameters to the working memory of either
the RuleflowEngine or RuleEngine class:
- RuleflowEngine:
RuleflowEngineDefinition ruleflowED = (RuleflowEngineDefinition) definition; RuleflowEngine ruleflowEngine = ruleflowED.createEngine(); // build the working memory Collection<Object> wm = new ArrayList<Object>(); wm.add( myObject1 ); ... wm.add( myObjectN); RuleflowEngineInput input = ruleflowEngine.createRuleflowEngineInput() ; input.setWorkingMemory( wm ); // set potential parameters input.setParameter ( "myParameter", myValue ); // run the engine try { RuleflowEngineOutput output = ruleflowEngine.execute( input ); } catch ( Exception e ) { ... } - RuleEngine:
RuleEngineDefinition engineDefinition = (RuleEngineDefinition) loader.load() ; RuleEngine ruleEngine = engineDefinition.createEngine(); // build the working memory Collection<Object> wm = new ArrayList<Object> (); wm.add( myObject1 ); ... wm.add( myObjectN); RuleEngineInput input = ruleEngine.createRuleEngineInput(); input.setWorkingMemory( wm , true); // set potential parameters input.setParameter( "myParameter", myValue ); // run the engine try { RuleEngineOutput output = ruleEngine.execute( input ); } catch ( Exception e ) { ... }
- RuleflowEngine: