[rules-users] Rule definition introspection

Wolfgang Laun wolfgang.laun at gmail.com
Sat Feb 18 03:28:15 EST 2012


The model is always defined by the DRL syntax. There is no point in
exposing the implementation of some object hierarchy as it is produced
by the DRL compiler, tree walker, etc. There may be a limit to what
is possible to reconstruct from that internal implementation, but
surely not for the top level objects.

-W


On 17 February 2012 19:36, Mark Proctor <mproctor at codehaus.org> wrote:

> 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/20120218/eea697af/attachment.html 


More information about the rules-users mailing list