Editing the ruleset execution method

After creating the Java™ project for rules, a main class is created but you must edit it to define your parameters and complete it depending on your needs.

Procedure

  1. Load the ruleset.
    try {
         // --------------------------------------------------------------------
         // Retrieve the ruleset archive JAR file using the Java I/O API.
         // --------------------------------------------------------------------
       FileInputStream fileStream = 
          new FileInputStream(the_archive_rule_name);
       JarInputStream jarStream = new JarInputStream(fileStream);
         // ----------------------------------------------
         // Create the ruleset archive parsing objects.
         // ----------------------------------------------
       IlrJarArchiveLoader loader = 
          new IlrJarArchiveLoader(jarStream);
       IlrRulesetArchiveParser parser = 
          new IlrRulesetArchiveParser();
         // ----------------------------------------------
         // Create a ruleset to store the parsed rules.
         // ----------------------------------------------
       IlrRuleset ruleset = new IlrRuleset();
         // -----------------------------------------------------------------
         // Connect the Ruleset to the Ruleset Archive parser so that
         // the parsed rules are accumulated in the Ruleset as the parsing
         // is peformed.
         // -----------------------------------------------------------------
       parser.setRuleset(ruleset);
         // ---------------------------------------------
         // Launch the parsing of the Ruleset Archive:
         // ---------------------------------------------
       if (! parser.parseArchive(loader)) {
             // -----------------------------------
             // In case of errors, list the errors.
             // -----------------------------------
           String[] errors = parser.getErrors();
           for (int i = 0; i < errors.length; i++) {
                String error = errors[i];
                System.err.println("Error while parsing Ruleset Archive:" 
                                    + error);
           }
       }
       else {
           // ----------------------------------------------------------------
           // [Optional] Optimize the ruleset if in RETE and if runtime
           // resources (time) permit.
           // ----------------------------------------------------------------
           IlrRulesetOptimConfig config = new IlrRulesetOptimConfig();
           config.hasherGeneration = true;   
           
           // adapt the optimization to your needs
           config.wmModifiedByCode = false;  
           
           // adapt the optimization to your needs
           ruleset.optimize(config);
       }
    catch (Exception exception) {
    exception.printStackTrace(System.err);
    }
  2. Create an engine based on the ruleset:
    IlrContext engine = new IlrContext(ruleset);
  3. Set ruleset parameter values and insert objects in the working memory:
    engine.setParameterValue(“myParam”,myValue);
    ...
    engine.insert(myObject);
    ...
  4. Run the engine:
    engine.execute();
  5. Prepare the rule engine for another execution:
    1. Retrieve the objects after they have been modified by the rule engine.
      engine.retractAll();
    2. Clean the ruleset parameter values.
      engine.cleanRulesetVariables();
      ();
    3. Reset the ruleflow.
      engine.resetRuleflow();
  6. [Optional] Terminate the rule engine and free the memory.
    engine.end();
    Note:

    A parsed ruleset is not necessarily empty when the parsing occurs. If a ruleset already exists in the engine, a merge silently occurs to take into account the latest version of the rules, functions, tasks, and so on in the case of conflicts.