<font size=2 face="sans-serif">Thanks Mark. Quite well understood. Thanks
Wolfgang for the code snippet, much appreciated.</font>
<br>
<br><font size=2 face="sans-serif">The reason I need to know which facts
the rule are using at runtime is to create an audit log and capture the
values the facts had at the time of rule execution, so that an audit log
can be created that showed exactly why certain rules fired and why some
did not</font>
<br>
<br>
<br>
<br><font size=1 color=#5f5f5f face="sans-serif">From: &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=1 face="sans-serif">Mark Proctor &lt;mproctor@codehaus.org&gt;</font>
<br><font size=1 color=#5f5f5f face="sans-serif">To: &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=1 face="sans-serif">rules-users@lists.jboss.org</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Date: &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=1 face="sans-serif">02/17/2012 11:37 AM</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Subject: &nbsp; &nbsp;
&nbsp; &nbsp;</font><font size=1 face="sans-serif">Re: [rules-users]
Rule definition introspection</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Sent by: &nbsp; &nbsp;
&nbsp; &nbsp;</font><font size=1 face="sans-serif">rules-users-bounces@lists.jboss.org</font>
<br>
<hr noshade>
<br>
<br>
<br><tt><font size=2>On 17/02/2012 13:54, Wolfgang Laun wrote:<br>
&gt; Below is some code that indicates the general direction. Since it
uses<br>
&gt; the unstable API, it may not work as well any more, it was good in
5.1.1<br>
&gt; and it still works using 5.3.0 with straightforward patterns.<br>
&gt;<br>
&gt; Mark does not agree with me that it is a legitimate wish of Drools<br>
&gt; application development to inspect binary packages, e.g., for<br>
&gt; documentation purposes, and he refuses to have any of this in<br>
&gt; the stable API. &nbsp;Constant dripping wears away the stone. ;-)<br>
I've discussed with edson we will add top level attributes back in, and
<br>
just deal with deprecating the methods when the time comes. So that that
<br>
you can list the packages, get the rules, functions and type <br>
declarations for those packages.<br>
<br>
We will not be exposing internal rule constructs like patterns, <br>
constraints etc as public supported apis. Although others are welcome to
<br>
document and use then, on the caveat that they understand they may <br>
change on any release. It's not that I don't think it's a legitimate <br>
request, I think it's perfectly valid. It's simply that we cannot be &nbsp;<br>
tied down to an internal model, we need to be free to change that. <br>
However users who code against those internal models will cry havoc if
<br>
we change them, if the think they are public apis.<br>
<br>
Mark<br>
&gt;<br>
&gt; -W<br>
&gt;<br>
&gt;<br>
&gt; On 17/02/2012, Mark Proctor&lt;mproctor@codehaus.org&gt; &nbsp;wrote:<br>
&gt;&gt; On 17/02/2012 05:37, GPatel@tsys.com wrote:<br>
&gt;&gt;&gt; I need it at rule definition time, or when the rule is saved.
Could<br>
&gt;&gt;&gt; you point me to a starting place in the internal api ?<br>
&gt;&gt; take a look in drools-compiler and org.drools.lang.RuleParserTest.<br>
&gt;&gt;<br>
&gt;&gt; Mark<br>
&gt;&gt;&gt; Thanks<br>
&gt;&gt;&gt; G. Patel<br>
&gt;&gt;&gt;<br>
&gt;<br>
&gt; package whatever;<br>
&gt;<br>
&gt; import java.io.PrintStream;<br>
&gt; import java.util.Formatter;<br>
&gt; import java.util.HashMap;<br>
&gt; import java.util.HashSet;<br>
&gt; import java.util.List;<br>
&gt; import java.util.Map;<br>
&gt; import java.util.Set;<br>
&gt;<br>
&gt; import org.drools.KnowledgeBase;<br>
&gt; import org.drools.base.ClassObjectType;<br>
&gt; import org.drools.definition.KnowledgePackage;<br>
&gt; import org.drools.definitions.impl.KnowledgePackageImp;<br>
&gt; import org.drools.rule.Accumulate;<br>
&gt; import org.drools.rule.Collect;<br>
&gt; import org.drools.rule.EntryPoint;<br>
&gt; import org.drools.rule.EvalCondition;<br>
&gt; import org.drools.rule.Forall;<br>
&gt; import org.drools.rule.From;<br>
&gt; import org.drools.rule.GroupElement;<br>
&gt; import org.drools.rule.Pattern;<br>
&gt; import org.drools.rule.Rule;<br>
&gt; import org.drools.spi.ObjectType;<br>
&gt;<br>
&gt; public class RulesByClasses {<br>
&gt; &nbsp; &nbsp;private Formatter fmt;<br>
&gt; &nbsp; &nbsp;private String ruleName;<br>
&gt; &nbsp; &nbsp;private String packageName;<br>
&gt; &nbsp; &nbsp;private Map&lt;String,Set&lt;String&gt;&gt; &nbsp;class2rules;<br>
&gt;<br>
&gt; &nbsp; &nbsp;public RulesByClasses( PrintStream printStream ){<br>
&gt; &nbsp; &nbsp; &nbsp;fmt = new Formatter( printStream );<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;public RulesByClasses(){<br>
&gt; &nbsp; &nbsp; &nbsp;this( System.out );<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void register( String className ){<br>
&gt; &nbsp; &nbsp; &nbsp;Set&lt;String&gt; &nbsp;ruleSet = class2rules.get(
className );<br>
&gt; &nbsp; &nbsp; &nbsp;if( ruleSet == null ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;ruleSet = new HashSet&lt;String&gt;();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;class2rules.put( className, ruleSet );<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp; &nbsp;ruleSet.add( &quot;\&quot;&quot; + ruleName +
&quot;\&quot; in &quot; + packageName );<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectCollect( Collect collect ){<br>
&gt; &nbsp; &nbsp; &nbsp;inspectPattern( collect.getSourcePattern() );<br>
&gt; &nbsp; &nbsp; &nbsp;inspectNestedElements( collect.getNestedElements()
);<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectAccumulate( Accumulate accumulate
){<br>
&gt; &nbsp; &nbsp; &nbsp;inspectNestedElements( accumulate.getNestedElements()
);<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectFrom( From from ){<br>
&gt; &nbsp; &nbsp; &nbsp;inspectNestedElements( from.getNestedElements()
);<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectNestedElements( List&lt;?&gt; &nbsp;nested
){<br>
&gt; &nbsp; &nbsp; &nbsp;for( Object o: nested ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;if( o instanceof Pattern ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectPattern( (Pattern)o );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof GroupElement ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectGroupElement( (GroupElement)o
);<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof Forall ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectForall( (Forall)o );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof Collect ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectCollect( (Collect)o );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof Accumulate ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectAccumulate( (Accumulate)o
);<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof From ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectFrom( (From)o );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof EntryPoint ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//...<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new IllegalStateException(
&quot;unexpected CE &quot; + o.getClass() );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectPattern( Pattern pattern ){<br>
&gt; &nbsp; &nbsp; &nbsp;ObjectType objectType = pattern.getObjectType();<br>
&gt; &nbsp; &nbsp; &nbsp;if( objectType instanceof ClassObjectType ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;ClassObjectType classObjectType = (ClassObjectType)objectType;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;register( classObjectType.getClassName()
);<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp; &nbsp;inspectNestedElements( pattern.getNestedElements()
);<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectForall( Forall forall ){<br>
&gt; &nbsp; &nbsp; &nbsp;inspectNestedElements( forall.getNestedElements()
);<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspectGroupElement( GroupElement groupElement
){<br>
&gt; &nbsp; &nbsp; &nbsp;for( Object o: groupElement.getChildren() ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;if( o instanceof Pattern ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectPattern( (Pattern)o );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof GroupElement ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectGroupElement( (GroupElement)o
);<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof Forall ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inspectForall( (Forall)o );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else if( o instanceof EvalCondition ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ...<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new IllegalStateException(
&quot;unexpected CE &quot; + o.getClass() );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void inspect( KnowledgePackage kPackage ){<br>
&gt; &nbsp; &nbsp; &nbsp;packageName = kPackage.getName();<br>
&gt; &nbsp; &nbsp; &nbsp;for( org.drools.definition.rule.Rule ruleCopy:
kPackage.getRules() ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;ruleName = ruleCopy.getName();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;Rule rule = (Rule)((KnowledgePackageImp)kPackage).getRule(
ruleName );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;inspectGroupElement( rule.getLhs() );<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;private void emit(){<br>
&gt; &nbsp; &nbsp; &nbsp;for( Map.Entry&lt;String,Set&lt;String&gt;&gt;
&nbsp;entry: class2rules.entrySet() ){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;fmt.format( &quot;Class %s%n&quot;, entry.getKey()
);<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;for( String ruleNamePack: entry.getValue()
){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmt.format( &quot; &nbsp;%s%n&quot;,
ruleNamePack );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;public void print( KnowledgePackage kPackage ){<br>
&gt; &nbsp; &nbsp; &nbsp;class2rules = new HashMap&lt;String,Set&lt;String&gt;&gt;();<br>
&gt; &nbsp; &nbsp; &nbsp;inspect( kPackage );<br>
&gt; &nbsp; &nbsp; &nbsp;emit();<br>
&gt; &nbsp; &nbsp;}<br>
&gt;<br>
&gt; &nbsp; &nbsp;public void print( KnowledgeBase kBase ){<br>
&gt; &nbsp; &nbsp; &nbsp;class2rules = new HashMap&lt;String,Set&lt;String&gt;&gt;();<br>
&gt; &nbsp; &nbsp; &nbsp;for( KnowledgePackage kPackage: kBase.getKnowledgePackages()
){<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;inspect( kPackage );<br>
&gt; &nbsp; &nbsp; &nbsp;}<br>
&gt; &nbsp; &nbsp; &nbsp;emit();<br>
&gt; &nbsp; &nbsp;}<br>
&gt; }<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; rules-users@lists.jboss.org<br>
&gt; </font></tt><a href="https://lists.jboss.org/mailman/listinfo/rules-users"><tt><font size=2>https://lists.jboss.org/mailman/listinfo/rules-users</font></tt></a><tt><font size=2><br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
rules-users@lists.jboss.org<br>
</font></tt><a href="https://lists.jboss.org/mailman/listinfo/rules-users"><tt><font size=2>https://lists.jboss.org/mailman/listinfo/rules-users</font></tt></a><tt><font size=2><br>
</font></tt>
<br>