Decision Management (ODM,ADS)

 View Only
Expand all | Collapse all

Missing Decision Engine API to replace legacy Classic Rule Engine API

  • 1.  Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed September 28, 2022 05:21 AM

    Hi experts, in the context of a migration from Classic Rule Engine to Decision Engine, the b2x code of the legacy service was using engine API to manipulate the values of intermediate variables in a variable set.

    IlrPackage myPackage = ruleset.getPackage(rulePackage);
    IlrRulesetParameter[] variables = myPackage.getVariables();
    ... 
    context.setParameterValue(rulePackage+"."+variables[i].getName(), someValue);
    

    These classes including IlrContext are deprecated in Decision Engine and replaced by com.ibm.rules.engine.runtime.EgineDefinition.
    However, this only allows access to input/output parameters, but not to intermediate variables in a variable set.

    Is there a replacement API in Decision Engine API to manipulate a variable set in a specific package?

    Thanks.



    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------


  • 2.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed September 28, 2022 09:58 AM
    Hi,

    It is in fact easier to use variables with decision engine in ARL, they are directly accessible:

    `pckmypack.a sub pack`.myvar = 12 ;

    Note the pck prefix for variables not in the top package and the back quote to use business package names.

    More here: https://www.ibm.com/docs/en/odm/8.10?topic=dbxm-accessing-decision-engine-related-artifacts-from-arl-mapping-code


    ------------------------------
    Alain Robert
    ------------------------------



  • 3.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed September 28, 2022 11:59 AM

    Hi Alain, unfortunately, is not that easy, I cannot know in advance neither the name nor the type of the variables in the var set. This is a complicated and non-typical ODM use case. In fact, it is a very smart idea of this customer that I've never seen before, but it has the inconvenience to be based in deprecated API of CRE that I don't think will exists any more in DE as this one follows a code-generation deployment style.

    Following you'r suggestion, what I would really need in b2x code is something like this:

    // I can get the name in runtime but I don't know the name in design time
    String varName = ... whatever code here ...
    
    `pckmypack.a sub pack`.eval(varName) = 12;
    

    Note that with CRC this was possible using the API

    myPackage.getVariables();


    Any idea for this?

    PD: I cannot disclose here, but, If you want, I drop you an e-mail explaining the use case in few words, just in case you have one of your smart ideas for an alternative walk-around.





    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 4.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed September 28, 2022 12:14 PM
    Or maybe it does exists something like
    String varName = ... some code here ...
    setParameterValue("pckmypack.a sub pack",varName,12);
    ​


    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 5.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed September 28, 2022 12:19 PM

    I have a Toolkit workspace which uses a single string field to store a dynamic list of values. It's essentially a comma separated pair list of key/value pairs stored in a string. I have getter and setter methods in the virtual BOM so they can be used easily in the rules. I use the dynamic string as an input/output parameter to the rules so you can pass in initial values to the rules and return the new results.

    Would that work for you (I'm happy to share the project, you may even have it already ;-) or does your solution rely on them being actual variables?



    ------------------------------
    Andy Macdonald
    ------------------------------



  • 6.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed September 28, 2022 01:14 PM
    Thanks Adny, but It has to be actual variables in specific varset.

    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 7.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Thu September 29, 2022 04:23 AM

    Hi, I really need a solution to set the value of variables (other than parameters) in DE without knowing the name of the variable at design time.
    The generated class $EngineData only provides access to parameters, furthermore it only provides support for getting the value:

       $EngineData.this.get(varName);

    But the counterpart method is missing:

      $EngineData.this.set(varName,value);

    This is very limited compared with the capabilities that CRE use to have.
    Any idea or not documented API for that?



    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 8.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Thu September 29, 2022 05:06 AM

    I seemed to recall when ARL was first created that there was a way to update the ruleset variables. So I checked my "ODM the Knowledge" ppt where I keep all such hard learned info/tips and it had the following:

    put("myVar", value);

    I checked it and it works in a virtual function B2X so there is probably an equivalent API method for it somewhere.



    ------------------------------
    Andy Macdonald
    ------------------------------



  • 9.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Thu September 29, 2022 09:00 AM
    Thanks Andy for your responsiveness.

    I already checked that, but only works for in/out params, as soon as "myVar" is not mapped to a param you get an Exception:

    java.lang.IllegalArgumentException: myVar
                com.ibm.rules.engine.runtime.impl.AbstractEngineData.wrongRulesetParameter(AbstractEngineData.java:85)
                 ....


    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 10.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Mon October 03, 2022 05:45 AM
    You may use put(String,Object). It works for all variables. For a variable within a package, you have to pass the full name, something like "pckmypack.a sub pack​.myVar"

    Alternatively you may use Java reflection.
    There is a generated class that contains all ruleset variables. You can access the current instance in B2X using $EngineData.this.
    It should be possible to access its Java class using $EngineData.this.class.
    All fields that represent variables or packages are public.

    You may have a look at the generated engine data class to see what the fields look like, if you have any doubts.


    ------------------------------
    Jean Louis Ardoint
    ------------------------------



  • 11.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Mon October 03, 2022 07:17 AM
    Hi Jean Louis, this was already tested. It does not work for me.
    I really don't know what I'm doing wrong.
    See the screen shoot, there is a variable defined in "vars" package.
    b2x code line highlighted in green works, the line highlighted in red produces the exception that you can see in the console bellow.


    The class that throws the exception is AbstractEngineData.


    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 12.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Mon October 03, 2022 08:27 AM
    Hi, I introspect the generated code and this is what I found:

    • the EngineDataClass contains a field for every package containing a varset with type of a generated class that follows the name of your package: com.ibm.rules.generated.pck.vars._PckClass pckvars;
    • this generated class has fields for the variables defined in the package, so you can access like this: $EngineData.this.pckvars.suggestedCategory="VIP";
    • but this generated class is missing set() / get() methods.
    • the set() / get() methods of the cotaining generated EngineDataClass only provides access to variables in the top level package.
    (see bellow an excerpt of the introscpection)

    So I think there is something missing in the generated code. All the variables should be accesible the same way. Makes no sense.

    public class com.ibm.rules.generated.EngineDataClass{
      public com.ibm.rules.engine.runtime.EngineSignature __signature;
      public java.lang.String name;
      public java.lang.Integer age;
      public java.lang.String category;
      public java.lang.String status;
      public com.ibm.rules.generated.pck.vars._PckClass pckvars;
      public com.ibm.rules.engine.runtime.RunningEngine engine;
      ...
      public void update();
      public void put();
      public java.lang.Object get();
      ...
    }
    public class com.ibm.rules.generated.pck.vars._PckClass{
      public com.ibm.rules.generated.EngineDataClass context;
      public java.lang.String suggestedCategory;
      public java.lang.Class getClass();
      public void wait();
      public int hashCode();
      public boolean equals();
      public com.ibm.rules.engine.runtime.RunningEngine getEngine();
      public void notifyAll();
      public void reset();
      public java.lang.String toString();
      public void notify();
    }​


    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 13.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Tue October 04, 2022 04:20 AM

    Hi,

    I still believe you may use put(String,Object) for all variables. For a variable within a package, you have to pass the full name, something like "mypack.a sub pack​.myVar" (note that there is no "pck" prefix in this case). Sorry for the confusion.

    You still want to do something with all variables, and there is no public documented API to retrieve the list of variable names that you could pass to get/put methods. You may access by reflection the getAllVars() method that returns a List<String> containing all variable names. This is a bit fragile, as undocumented, yet it seems better that trying to find the variable names by reflection.

    I hope this helps.



    ------------------------------
    Jean Louis Ardoint
    ------------------------------



  • 14.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API
    Best Answer

    Posted Tue October 04, 2022 04:33 PM
    Hi Eduardo,

    Can you check the below sample B2X code I tried to achieve what you are trying to implement? Please feel free to comment if I missed any of your requirements.

    note("********* <Start> Params **********");
    java.util.Map<java.lang.String, java.lang.Object> params = $EngineData.this.getAll();
    note("Map Keys: ");
    for (String keys : params.keySet())
    {
    note(keys);
    }
    note("\n Map Keys & Values: ");
    for (java.util.Map.Entry<java.lang.String, java.lang.Object> entry : params.entrySet()) {
    note(entry.getKey() + ":" + entry.getValue().toString());
    }
    note("********* <End> Params ********** \n");
    note("\n ********* <Start> Vars **********");
    java.util.Collection<java.lang.String> varsBefore = $EngineData.this.getAllVars();
    note(java.util.Arrays.toString(varsBefore.toArray()));
    for (String sB: varsBefore) {
    System.out.println(get(sB));
    if ("number".equals(sB)) {
    //$EngineData.this.number=new java.lang.Integer(50);
    put(sB, new java.lang.Integer(50));
    }

    if ("vars.name".equals(sB)) {
    //put("mypack.a sub pack​.myVar", value); - note that there is no "pck" prefix in this case
    put(sB, "Eduardo");
    }
    }
    note("======================================");
    if (com.ibm.rules.engine.runtime.RunningEngineWithWorkingMemory.this instanceof com.ibm.rules.engine.runtime.RunningEngineWithWorkingMemory) {
    com.ibm.rules.engine.runtime.RunningEngineWithWorkingMemory ruleEngine = (com.ibm.rules.engine.runtime.RunningEngineWithWorkingMemory)com.ibm.rules.engine.runtime.RunningEngineWithWorkingMemory.this;
    ruleEngine.updateData();
    ruleEngine.updateGenerators();
    }
    java.util.Collection<java.lang.String> varsAfter = $EngineData.this.getAllVars();
    System.out.println(java.util.Arrays.toString(varsAfter.toArray()));
    for (String sA: varsAfter) {
    System.out.println(get(sA));
    }
    note("********* <End> Vars ********** \n");

    ------------------------------
    Charan Paladugu
    ------------------------------



  • 15.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Wed October 05, 2022 03:05 AM
    HI Charan, Jean Louis, this is exactly what I need. Thanks you very much for your support!!

    As a suggestion would be nice having product documentation telling about this way to access variables in a package with EngindeData.set().
    Specially because sometimes you prefix the package name with "pck" and sometime you don't, and is really tricky to guess.

    Regards.

    ------------------------------
    Eduardo Izquierdo Lázaro
    Automation Architect
    DECIDE
    Madrid
    609893677
    ------------------------------



  • 16.  RE: Missing Decision Engine API to replace legacy Classic Rule Engine API

    Posted Tue October 04, 2022 04:38 PM


    ------------------------------
    Charan Paladugu
    ------------------------------