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
- 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); } - Create an engine based on the ruleset:
IlrContext engine = new IlrContext(ruleset); - Set ruleset parameter values and insert objects in the
working memory:
engine.setParameterValue(“myParam”,myValue); ... engine.insert(myObject); ... - Run the engine:
engine.execute(); - Prepare the rule engine for another execution:
- Retrieve the objects after they have been modified by
the rule engine.
engine.retractAll(); - Clean the ruleset parameter values.
engine.cleanRulesetVariables(); (); - Reset the ruleflow.
engine.resetRuleflow();
- Retrieve the objects after they have been modified by
the rule engine.
- [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.