Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Finding Match Key Value Pairs

Help
Cerin
2011-12-29
2013-04-25
  • Cerin
    Cerin
    2011-12-29

    Is there a simple way to get a dictionary for each set of key/value pairs matching a rule?

    For example, if the left hadn side of rule "A" is "(myfact ?name)" and this matches facts (myfact bob) and (myfact sue) then is there some function like GetRuleMatchDict("A") =>  ?

     
  • There is no "simple" way. In fact, the task of the RETE algorithm is to find these matches, so the actual way to find facts that match a rule and thus build the dictionary you're looking for, is to let CLIPS work for you. There are some (debugging) functions that I borrowed from CLIPS, that show partial matches (that is the facts that match a certain rule, as indexed facts) and the agenda (the rules that would be fired, and which facts would fire them). But there are some drawbacks: one is that you only get the fact indexes, as per CLIPS style - thus you'd have to build the dictionary by analyzing every single fact. Another drawback is that you only get the list of indexed facts in a "fancy" form on the standard output. That is, no structured data. As I said before, these functions are intended as debugging functions for CLIPS, and nothing more.

    >>> import clips
    >>> clips.Clear()
    >>> A = clips.BuildRule("A", "(myfact ?name)", "")
    >>> clips.Reset()
    >>> f1 = clips.Assert("(myfact bob)")
    >>> f2 = clips.Assert("(myfact sue)")
    >>> f3 = clips.Assert("(duck donald)")
    >>> clips.PrintFacts()
    f-0     (initial-fact)
    f-1     (myfact bob)
    f-2     (myfact sue)
    f-3     (duck donald)
    For a total of 4 facts.
    >>> clips.PrintAgenda()
    MAIN:
       0      A: f-2
       0      A: f-1
    For a total of 2 activations.
    >>> A.PrintMatches()
    Matches for Pattern 1
    f-1
    f-2
    Activations
    f-2
    f-1
    >>>
    

    It's also clear that such a function would be quite difficult to implement generically, because facts and rules in CLIPS might have quite complex structures, and the case shown in the question (with "key/value" pairs) can only be used with facts and rules of a particular form.

    F.