David,

    This is an interesting use case, but I don't think you can build a complete explanation from the agenda listener.
    Let me clarify: you can do 80% of the explanation from there, based on the position of the fact handles in the tuple. The collection you get from event.getActivation().
getFactHandles() will have the facts in the exact same "offset" order you have in your rule. For simple rules, it is "straight forward":

when
    A()
    B()
    C()
then
end
 
    In the above example, A() will be offset 0, B() will be 1 and C() will be 2.

    The 20% you will not be able to explain with such solution are the cases where you have nested patterns, with the use of ORs (that create multiple logical branches), NOT/EXISTS/FORALL ( that don't generate a fact in the activating tuple), COLLECT/ACCUMULATE that will condense multiple conditions into one element, etc.

    My best advise for you is: if this is a non-critical part of your system, go ahead with the approach above, and you will be able to provide most of the explanations with a simple heuristic. But if this is critical for your use case, my suggestion would be for you to get your company to allow you to work with us for some time implementing this requirement as a proper drools feature. We can guide you through the drools internals and add the feature your company needs and your company donates the code for the project so that we can support it in the future. It is an open source project after all.
 
    Need to check with Mark what he thinks about the feature, but it sounds interesting for me.

    []s
    Edson


2009/3/9 David Boaz <davidb@dbmotion.com>

Thanks Mark,

I tried to analyze (in the debugger) the RETE network, and to my
understanding, it is not possible to map back from the RETE nodes to the
rule pattern. in the example above the two Person patterns will share the
same EntryPointNode and ObjectTypeNode, hence, there is no way to determine
what was the original pattern.

My use case, is to generate 'explanation' objects. These object will log for
each successful rule firing the identifiers of the facts that produced this
consequence. Later on, we want to enable providing explanations as: "we
reached this conclusion because field (actual=2) < 4". So, in the
explanation process we want to substitute the rule patterns with actual
facts.
When the rule patterns are of the same object-type, there is a possibility
to confuse and to assign the wrong fact to the pattern. Hence, I wanted to
determine and log what was the 'causing' pattern for each fact.

I hope that my description is clear,
BR, David



Mark Proctor wrote:
>
> Mark Proctor wrote:
>> David Boaz wrote:
>>> Hi all,
>>>
>>> Is it possible to determine the Pattern that ‘produced’ a given Fact?
>>> For
>>> example, assuming we have the following rule and event handler:
>>>
>>> rule r1
>>>     when         p1:Person(...)
>>>         p2:Person(...)
>>>     then
>>>     ...
>>> end
>>>
>>> protected class MyAgendaEventHandler extends
>>>         DefaultAgendaEventListener {
>>>
>>>     @Override
>>>     public void
>>> afterActivationFired(org.drools.event.rule.AfterActivationFiredEvent
>>> event)
>>> {
>>>         String ruleName = event.getActivation().getRule().getName();
>>>         KnowledgeRuntime knowledgeRuntime = event.getKnowledgeRuntime();
>>>         for (FactHandle factHandle :
>>> event.getActivation().getFactHandles()) {
>>>             Object fact = knowledgeRuntime.getObject(factHandle);
>>>             Pattern pattern= ?;
>>>         }
>>>     }
>>> }
>>>
>>> Is it possible to determine the Pattern that originate a Person
>>> object? If
>>> yes, how the pattern is identified?
>>>
>> no
> We'll that's not exactly true, but it's not something an end user should
> be doing. But the basic jist is the Activation wraps the AgendaItem if
> you can get access to that it has a Tuple field, this references the
> chaines of tuples that make up the row of data. you can iterate that
> chain and each one has a handle field which you can match up with the
> facthandle you are searching for. Further more each tuple references the
> join node it was added via, which also has an index number, so you can
> deduce the pattern from there. However I seriously doubt this is what
> you want, and you probably need to think harder about your use case.
>
> Mark
>>> Thanks, David
>>>
>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users@lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>

--
View this message in context: http://www.nabble.com/Events---Determining-the-Fact-Pattern-tp22397570p22413433.html
Sent from the drools - user mailing list archive at Nabble.com.


_______________________________________________



--
 Edson Tirelli
 JBoss Drools Core Development
 JBoss, a division of Red Hat @ www.jboss.com