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

  1. Open the decision engine execution class created in the Java project for rules.
  2. 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);
  3. 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.

  4. Create an Engine instance from an engine definition.
    Engine engine = definition.createEngine();

    For more information, see EngineDefinition.

  5. 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);
  6. 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&ltObject&gt; 
      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 ) { 
             ... 
      }