Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Get a defrule name

Help
vranoch
2011-06-08
2012-11-23
  • vranoch
    vranoch
    2011-06-08

    Hello, is there a chance to get a name of a defrule within its body?
    (defrule XY
    =>
       (printout t "NAME: " (get-defrule-name ) crlf)
    )

    thanks Vranoch

     
  • Gary Riley
    Gary Riley
    2011-06-13

    No, you would have to hardcode the rule name.

     
  • vranoch
    vranoch
    2011-06-13

    Yes, that is how I have it done now but it is a bit ugly ;-). And isn't there any way to get this information using a user defined function? Isn't there any "pointer" to the actual rule body, etc.?

    Thanks Vranoch

     
  • Gary Riley
    Gary Riley
    2011-06-18

    Create a user-defined function and include engine.h in your source file. You can then use the macro

    EngineData(theEnv)->ExecutingRule

    to reference the executing rule. The argument to the macro is a pointer to the environment, which if you write an environment aware user function will be passed into it.

     
  • vranoch
    vranoch
    2011-06-23

    Thanks Gary, that is exactly what I searched. For others:

    userfunctions.h:
    long int xActualRule(void *theEnv, DATA_OBJECT_PTR returnValuePtr);

    userfunctions.c:
    long int xActualRule(void *theEnv,
    DATA_OBJECT_PTR returnValuePtr)
    {  

    if (EnvArgCountCheck(theEnv,"kmx-actual-rule",EXACTLY,0) == -1)
    { return(-1L); }

    SetpType(returnValuePtr,STRING);
    if (NULL != EngineData(theEnv)->ExecutingRule)
       SetpValue(returnValuePtr, EnvAddSymbol (theEnv, EnvGetDefruleName(theEnv, &(EngineData(theEnv)->ExecutingRule->header))));
    else
       SetpValue(returnValuePtr, "");
    return 0L;
    }

    void InitializeUserFunction(void *theEnv)
    {
    EnvDefineFunction2(theEnv,"x-actual-rule", 'k', PTIEF xActualRule, "xActualRule", "00s");
    }

    test.clp:
    (defrule XY ""
    =>
       (printout t "RULE: >" (x-actual-rule ) "<" crlf)
    )

     
  • pron
    pron
    2011-07-13

    Thanks for the code!!

    It works great!