[rules-users] Rule definition introspection

GPatel at tsys.com GPatel at tsys.com
Fri Feb 17 14:18:52 EST 2012


Thanks Mark. Quite well understood. Thanks Wolfgang for the code snippet, 
much appreciated.

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



From:   Mark Proctor <mproctor at codehaus.org>
To:     rules-users at lists.jboss.org
Date:   02/17/2012 11:37 AM
Subject:        Re: [rules-users] Rule definition introspection
Sent by:        rules-users-bounces at lists.jboss.org



On 17/02/2012 13:54, Wolfgang Laun wrote:
> Below is some code that indicates the general direction. Since it uses
> the unstable API, it may not work as well any more, it was good in 5.1.1
> and it still works using 5.3.0 with straightforward patterns.
>
> Mark does not agree with me that it is a legitimate wish of Drools
> application development to inspect binary packages, e.g., for
> documentation purposes, and he refuses to have any of this in
> the stable API.  Constant dripping wears away the stone. ;-)
I've discussed with edson we will add top level attributes back in, and 
just deal with deprecating the methods when the time comes. So that that 
you can list the packages, get the rules, functions and type 
declarations for those packages.

We will not be exposing internal rule constructs like patterns, 
constraints etc as public supported apis. Although others are welcome to 
document and use then, on the caveat that they understand they may 
change on any release. It's not that I don't think it's a legitimate 
request, I think it's perfectly valid. It's simply that we cannot be 
tied down to an internal model, we need to be free to change that. 
However users who code against those internal models will cry havoc if 
we change them, if the think they are public apis.

Mark
>
> -W
>
>
> On 17/02/2012, Mark Proctor<mproctor at codehaus.org>  wrote:
>> On 17/02/2012 05:37, GPatel at tsys.com wrote:
>>> I need it at rule definition time, or when the rule is saved. Could
>>> you point me to a starting place in the internal api ?
>> take a look in drools-compiler and org.drools.lang.RuleParserTest.
>>
>> Mark
>>> Thanks
>>> G. Patel
>>>
>
> package whatever;
>
> import java.io.PrintStream;
> import java.util.Formatter;
> import java.util.HashMap;
> import java.util.HashSet;
> import java.util.List;
> import java.util.Map;
> import java.util.Set;
>
> import org.drools.KnowledgeBase;
> import org.drools.base.ClassObjectType;
> import org.drools.definition.KnowledgePackage;
> import org.drools.definitions.impl.KnowledgePackageImp;
> import org.drools.rule.Accumulate;
> import org.drools.rule.Collect;
> import org.drools.rule.EntryPoint;
> import org.drools.rule.EvalCondition;
> import org.drools.rule.Forall;
> import org.drools.rule.From;
> import org.drools.rule.GroupElement;
> import org.drools.rule.Pattern;
> import org.drools.rule.Rule;
> import org.drools.spi.ObjectType;
>
> public class RulesByClasses {
>    private Formatter fmt;
>    private String ruleName;
>    private String packageName;
>    private Map<String,Set<String>>  class2rules;
>
>    public RulesByClasses( PrintStream printStream ){
>      fmt = new Formatter( printStream );
>    }
>
>    public RulesByClasses(){
>      this( System.out );
>    }
>
>    private void register( String className ){
>      Set<String>  ruleSet = class2rules.get( className );
>      if( ruleSet == null ){
>        ruleSet = new HashSet<String>();
>        class2rules.put( className, ruleSet );
>      }
>      ruleSet.add( "\"" + ruleName + "\" in " + packageName );
>    }
>
>    private void inspectCollect( Collect collect ){
>      inspectPattern( collect.getSourcePattern() );
>      inspectNestedElements( collect.getNestedElements() );
>    }
>
>    private void inspectAccumulate( Accumulate accumulate ){
>      inspectNestedElements( accumulate.getNestedElements() );
>    }
>
>    private void inspectFrom( From from ){
>      inspectNestedElements( from.getNestedElements() );
>    }
>
>    private void inspectNestedElements( List<?>  nested ){
>      for( Object o: nested ){
>        if( o instanceof Pattern ){
>          inspectPattern( (Pattern)o );
>        } else if( o instanceof GroupElement ){
>          inspectGroupElement( (GroupElement)o );
>        } else if( o instanceof Forall ){
>          inspectForall( (Forall)o );
>        } else if( o instanceof Collect ){
>          inspectCollect( (Collect)o );
>        } else if( o instanceof Accumulate ){
>          inspectAccumulate( (Accumulate)o );
>        } else if( o instanceof From ){
>          inspectFrom( (From)o );
>        } else if( o instanceof EntryPoint ){
>          //...
>        } else {
>          throw new IllegalStateException( "unexpected CE " + 
o.getClass() );
>        }
>      }
>    }
>
>    private void inspectPattern( Pattern pattern ){
>      ObjectType objectType = pattern.getObjectType();
>      if( objectType instanceof ClassObjectType ){
>        ClassObjectType classObjectType = (ClassObjectType)objectType;
>        register( classObjectType.getClassName() );
>      }
>      inspectNestedElements( pattern.getNestedElements() );
>    }
>
>    private void inspectForall( Forall forall ){
>      inspectNestedElements( forall.getNestedElements() );
>    }
>
>    private void inspectGroupElement( GroupElement groupElement ){
>      for( Object o: groupElement.getChildren() ){
>        if( o instanceof Pattern ){
>          inspectPattern( (Pattern)o );
>        } else if( o instanceof GroupElement ){
>          inspectGroupElement( (GroupElement)o );
>        } else if( o instanceof Forall ){
>          inspectForall( (Forall)o );
>        } else if( o instanceof EvalCondition ){
>          // ...
>        } else {
>          throw new IllegalStateException( "unexpected CE " + 
o.getClass() );
>        }
>      }
>    }
>
>    private void inspect( KnowledgePackage kPackage ){
>      packageName = kPackage.getName();
>      for( org.drools.definition.rule.Rule ruleCopy: kPackage.getRules() 
){
>        ruleName = ruleCopy.getName();
>        Rule rule = (Rule)((KnowledgePackageImp)kPackage).getRule( 
ruleName );
>        inspectGroupElement( rule.getLhs() );
>      }
>    }
>
>    private void emit(){
>      for( Map.Entry<String,Set<String>>  entry: class2rules.entrySet() 
){
>        fmt.format( "Class %s%n", entry.getKey() );
>        for( String ruleNamePack: entry.getValue() ){
>          fmt.format( "  %s%n", ruleNamePack );
>        }
>      }
>    }
>
>    public void print( KnowledgePackage kPackage ){
>      class2rules = new HashMap<String,Set<String>>();
>      inspect( kPackage );
>      emit();
>    }
>
>    public void print( KnowledgeBase kBase ){
>      class2rules = new HashMap<String,Set<String>>();
>      for( KnowledgePackage kPackage: kBase.getKnowledgePackages() ){
>        inspect( kPackage );
>      }
>      emit();
>    }
> }
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users

_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120217/60c96fc1/attachment.html 


More information about the rules-users mailing list