[rules-users] Hold the Beans!

Mark Proctor mproctor at codehaus.org
Sat Sep 11 03:35:40 EDT 2010


  On 11/09/2010 07:17, Greg Barton wrote:
> As opposed to data wrapped in a Fact?  I suppose you could use 
> java.util.Map instead of Fact and write rules around that.  Or you 
> could use a strongly typed POJO, which drools is optimized for.  For 
> translating the xml use something like JAXB or XStream. (I'm 
> preferring XStream these days.)
>
Edson and I have been discussing how to have better "out of the box" xml 
support that doesn't need transformation. It's a lot of work, but an 
interesting project if anyone is interested.

The first aspect is to allow the type and the property accessors to be 
dynamically defined
declare Element
     @type( nodeName )
     @property( get( $1 ) )
end

Where $1 is the interpolation point for the passed property name. That 
would then allow access to Dom Elements as though they were natural 
facts. Same can be done for Maps, or similar constructs
declare Map
     @type( get( "type" )  //the type is a key in the Map
     @propert( get( $1 ) )
end

declare MyFact
     @type( type ) //The type is field
     @property( getProperty( $1 ) ) // all properties are via this getter
end

If you look at the expiremental FactTemplates you can see the guts of 
how fields are accessed and figure out how to plumb this all up. Then 
there is the RHS, which is a big more difficult, if you want the 
properties supported as named fields, and not emulated getters.

The other aspect to this would be support for a STAX parser. Here as it 
does a single parse of the XML it'll emit events which represent the DOM 
element, as long as each event is emitted references the Parent and Rete 
only keeps hold events which are likely to match let the rest be GC'd. 
Then we have a powerful STAX single pass based parser with advanced 
reasoning, ideal for countent routers.

Any takers? :)

Mark
>
> --- On *Fri, 9/10/10, Donald Winston /<satchwinston at yahoo.com>/* wrote:
>
>
>     From: Donald Winston <satchwinston at yahoo.com>
>     Subject: Re: [rules-users] Hold the Beans!
>     To: "Rules Users List" <rules-users at lists.jboss.org>
>     Date: Friday, September 10, 2010, 9:06 PM
>
>     So unlike Jess, Drools can or cannot use asserted facts from a
>     data structure and can only use data wrapped in a bean?
>
>     /* Assert ordered or unordered facts from the session's document.
>     A deftemplate with matching
>     * id and slots for each "fact" element should already exist for
>     unordered facts.
>     */
>     public void assertDocument(Document document, Rete engine) throws
>     JessException {
>     Iterator facts =
>     (document.getRootElement().getChildren("fact")).iterator();
>     while (facts.hasNext() == true) {
>     Element factElement = (Element)facts.next();
>     String id = factElement.getAttributeValue("id");
>     Fact fact = new Fact(id, engine);
>     Iterator slots = (factElement.getChildren("slot")).iterator();
>     while (slots.hasNext() == true) {
>     Element slotElement = (Element)slots.next();
>     String name = slotElement.getAttributeValue("name");
>     String type = slotElement.getAttributeValue("type");
>     if (slotElement.getChildren("value").size() == 1) {
>     String value = slotElement.getChild("value").getText();
>     fact.setSlotValue(name, getValue(type, value));
>     }
>     else {
>     ValueVector valueVector = new ValueVector();
>     Iterator values = (slotElement.getChildren("value")).iterator();
>     while (values.hasNext() == true) {
>     String value = ((Element)values.next()).getText();
>     valueVector.add(getValue(type, value));
>     }
>     fact.setSlotValue(name, new Value(valueVector, RU.LIST));
>     }
>     } //while slots
>     engine.assertFact(fact);
>     } //while facts
>     }
>
>
>     /*
>     * Output the fact base to the response output stream.
>     * Intended for debugging. Override to do your own processing.
>     */
>     public String processResults(HttpServletRequest request,
>     HttpServletResponse response,
>     Rete engine) throws IOException {
>     PrintWriter writer = response.getWriter();
>     writer.println("<html><head><title>Jess Results</title></head>");
>     writer.println("<body><h1>PrettyPrint listFacts()</h1>");
>     writer.println("<pre>");
>     Iterator facts = engine.listFacts();
>     while (facts.hasNext() == true) {
>     Fact fact = (Fact)facts.next();
>     writer.println(new PrettyPrinter(fact));
>     }
>     writer.println("</pre>");
>     writer.println("</body></html>");
>     return null;
>     }
>
>
>     private Value getValue(String type, String value) throws
>     JessException {
>     if ("STRING".equals(type) == true)
>     return new Value(value, RU.STRING);
>     else if ("INTEGER".equals(type) == true)
>     return new Value(Integer.parseInt(value), RU.INTEGER);
>     else if ("FLOAT".equals(type) == true)
>     return new Value(Float.parseFloat(value), RU.FLOAT);
>     else if ("SYMBOL".equals(type) == true)
>     return new Value(value, RU.SYMBOL);
>     else if ("LONG".equals(type) == true)
>     return new Value(Long.parseLong(value), RU.LONG);
>     else
>     return new Value(value, RU.ANY);
>     }
>     On Sep 10, 2010, at 11:18 AM, Wolfgang Laun wrote:
>
>>     So everthing is String or list of String? (What if your data
>>     contains numbers, where you'd like to use > in patterns?)
>>
>>     Is there a stable relationship between key/slot and its type,
>>     i.e., scalar String or list of String?
>>
>>     You realize that a record of untyped fields (slots) is
>>     fundamentally different from an approach where data is bound to
>>     be stored in (necessarily) strongly typed fields of an object.
>>     There's no difficulty with writing similar code for storing
>>     values from a Map into an object of some class, but class fields
>>     must have fixed types.
>>
>>     -W
>>
>>
>>     2010/9/10 Donald Winston <satchwinston at yahoo.com
>>     </mc/compose?to=satchwinston at yahoo.com>>
>>
>>         This is what I'm doing:
>>
>>         /* Assert unordered facts from the request parameter map. A
>>         deftemplate with matching id and
>>         * slots should already exist.
>>         */
>>         public void assertParameterMap(String id, Map map, Rete
>>         engine) throws JessException {
>>         Fact fact = new Fact(id, engine);
>>         Iterator keys = map.keySet().iterator();
>>         while (keys.hasNext() == true) {
>>         String key = (String)keys.next();
>>         if (ID.equals(key) == true) continue;
>>         String[] paramValues = (String[])map.get(key);
>>         if (paramValues.length > 1) {
>>         ValueVector values = new ValueVector();
>>         for(String value : paramValues)
>>         values.add(new Value(value, RU.STRING));
>>         fact.setSlotValue(key, new Value(values, RU.LIST));
>>         }
>>         else
>>         fact.setSlotValue(key, new Value(paramValues[0], RU.STRING));
>>         }
>>         engine.assertFact(fact);
>>         }
>>
>>         I'm working on something similar for a jdom document object.
>>         I'm not using POJOs. I'm using a data structure. (technically
>>         they're objects because java is object oriented, but they're
>>         not "problem domain objects").
>>
>>         On Sep 10, 2010, at 10:12 AM, Wolfgang Laun wrote:
>>
>>>         If you insert POJOs as facts in Jess, you'll have to write a
>>>            (deftemplace X (declare (from-class X)))
>>>         and the fields available for pattern matching in rules rely
>>>         on the JavaBeans convention.
>>>
>>>         I have (quite successfully) used POJOs resulting from
>>>         unmarshalling an XML document (via JAXB) as facts, both in
>>>         Drools and in Jess; most certainly without writing any
>>>         "copycat" fact classes and tedious transformations.
>>>
>>>         As for globals: They play the same role in Drools as in
>>>         Jess; in neither system are they part of the working memory.
>>>
>>>         I don't know what you could mean by a "standard fact class".
>>>
>>>         As for iterating over all fact objects in Drools' WM, Drools
>>>         provides getObjects() in WorkingMemory; or you could set up
>>>         a query and run this.
>>>
>>>         -W
>>>
>>>
>>>         On 10 September 2010 14:54, Donald Winston
>>>         <satchwinston at yahoo.com
>>>         </mc/compose?to=satchwinston at yahoo.com>> wrote:
>>>
>>>             I'm reviewing JBoss Rules (Drools) for an application
>>>             I'm starting to build. It appears that the only way to
>>>             assert facts is to use the insert(Object) method where
>>>             the object is a bean using the proper naming conventions
>>>             for it's properties. There also appears to be a way to
>>>             use arbitrary objects using globals but do these end up
>>>             in the fact base? It's disturbing to me that I have to
>>>             create a bunch of classes whose sole purpose in life is
>>>             to support the rule base. This is similar to using java
>>>             server pages and having to create a bunch of classes
>>>             just to support each page. That's why I don't use java
>>>             server pages and use xsl transformations instead. I want
>>>             to use my xml jdom document to represent my data and not
>>>             have to create a bunch of beans. I can't seem to find
>>>             anything in the api where I can assert facts without
>>>             creating my own custom classes. There's no standard Fact
>>>             class?
>>>
>>>             I've been also experimenting with Jess and it provides
>>>             an easy way for me to do this. I just iterate through my
>>>             jdom document and create Fact objects and assert them. I
>>>             can then execute the rules and then iterate through the
>>>             updated fact base using engine.listFacts() and update my
>>>             jdom document. It couldn't be easier or more natural. Is
>>>             there an analogous way to do this in Drools?
>>>
>>>
>>>             Thank you very much.
>>>             _______________________________________________
>>>             rules-users mailing list
>>>             rules-users at lists.jboss.org
>>>             </mc/compose?to=rules-users at lists.jboss.org>
>>>             https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>>         _______________________________________________
>>>         rules-users mailing list
>>>         rules-users at lists.jboss.org
>>>         </mc/compose?to=rules-users at lists.jboss.org>
>>>         https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>         _______________________________________________
>>         rules-users mailing list
>>         rules-users at lists.jboss.org
>>         </mc/compose?to=rules-users at lists.jboss.org>
>>         https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>     _______________________________________________
>>     rules-users mailing list
>>     rules-users at lists.jboss.org
>>     </mc/compose?to=rules-users at lists.jboss.org>
>>     https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>     -----Inline Attachment Follows-----
>
>     _______________________________________________
>     rules-users mailing list
>     rules-users at lists.jboss.org
>     </mc/compose?to=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/20100911/6bc431c0/attachment.html 


More information about the rules-users mailing list