Sounds good. Thanks Mark. I probably won't find time until I finish writing the book. It should be no problem after I finish.
I'm going to get the basics done, then I'm really hoping that you or paul or someone else will pick up the rest and polish it off (like your suggestion below) and do javadocs - as I need to get on with core work - I'm just trying to get the ball rolling.
I'll try and have something committed today, that you people can roll with.
Mark
Michal Bali wrote:Hi,
I think the xml looks good.Spring's PropertyEditors - http://static.springframework.org/spring/docs/2.5.x/reference/validation.html#beans-beans-conversion could be used to even further simplify it. Using convention over configuration to guess the resource type from file's extension. The configuration will then become:
<bean id="rb1" class="org.drools.ioc.spring.KnowledgeBaseFactoryBean">
<property name="resources">
<list>
<value>file:src/main/java/org/drools/ioc/spring/testSpring.drl</value></list>
</property></bean>
I am not that familiar with PropertyEditors to know if this is possible to do or not. I am just throwing it out here, maybe somebody else will know better. It is not that important just nice to have..
Best Regards,Michal
On Tue, Nov 25, 2008 at 6:07 AM, Mark Proctor <mproctor@codehaus.org> wrote:
This is now what the spring XML looks like:
<bean id="rb1" class="org.drools.ioc.spring.KnowledgeBaseFactoryBean">
<property name="resources">
<list>
<bean class="org.drools.ioc.spring.KnowledgeResourceFactoryBean"><property name="source" value="file:src/main/java/org/drools/ioc/spring/testSpring.drl" />
<property name="knowledgeType" value="DRL" />
</bean>
<bean class="org.drools.ioc.spring.KnowledgeResourceFactoryBean"><property name="source" value="file:src/main/java/org/drools/ioc/spring/IntegrationExampleTest.xls" />
<property name="knowledgeType" value="DTABLE" />
<property name="knowledgeResourceConfiguration">
<bean class="org.drools.ioc.spring.DTableResourceConfigurationFactoryBean">
<property name="inputType" value="XLS" /></bean>
</bean>
</property>
</bean>
</list>
</property>
How is that looking? Most people happy with that, it's very generic. I've dropped the agent side for the moment, just focusing on scripting of a KnowledgeBase via spring. It now more closely follows the KnowedgeBuilder api. The above spring configuration will return a rulebase of two resources, on is a simple DRL, the other is a decisiontable of type XLS.
I'll extend this further to make sure that ALL knowledge types can be built for a knowledge base via the spring api. I'll also make sure it can take a KnowledgeBaseConfiguration and KnowledgeBuilderConfiguration. Then I'll also make sure there is a spring configuration to return a stateful or stateless session given a knowlebasefactorybean ref. Finally I might add some basic agent facilities.
I think this should give spring users everything they need, i.e. the capability to build knowledge bases and wire sessions and knowledge bases to other beans - all via xml.
Mark
Mark Proctor wrote:I'm updating drools-api as we speak, based on the spring Resource idea. the following Factory will provide a set of ready to go Resources, that should satisfy most people's needs. So drools-api will be spimplied to
kbuilder.addResource( Resource, KnowledgeType )
kbuilder.addResource( Resource, KnowledgeType, ResourceConfiguration )
And the Resources available, and ofcourse people can implement the interface themselves is:
public class ResourceFactory { public static Resource newURLResource(URL url) { return null; } public static Resource newURLResource(URI uri) { return null; } public static Resource newURLResource(String url) { return null; } public static Resource newFileResource(File file) { return null; } public static Resource newFileResource(String file) { return null; } public static Resource newByteArrayResource(byte[] bytes) { return null; } public static Resource newInputStreamResource(InputStream stream) { return null; } public static Resource newReaderResource(Reader reader) { return null; } public static Resource newReaderResource(Reader reader, String encoding) { return null; } public static Resource newClassPathResource(String path) { return null; } public static Resource newClassPathResource(String path, ClassLoader classLoader) { return null; } } It can take a Reader, which underneath will open an InputStream, it'll use default encoding unless the encoding is specified as otherwise in the alternative param. So typically it'll be kbuilder.addResource( ResourceFactory.newClassPathResource( "some path" ), KnowledgeType.DRL ); don't forget you can import static methods, so it can actually be: kbuilder.addResource( newClassPathResource( "some path" ), KnowledgeType.DRL ); Mark
Greg Barton wrote:Well, if you look at String.getBytes() it uses the default encoding, then ISO-8859-1 if that fails. (Default is UTF-8 unless otherwise specified in the "file.encoding" property.) As long as that's documented I guess it's not a problem, unless someone wants to specify their own charset for a particular file. Java 1.6 has String.getBytes(Charset) and that could allow someone to use an alternate charset. I figure for the vast majority of cases it won't be a problem. --- On Sun, 11/23/08, Mark Proctor <mproctor@codehaus.org> wrote:From: Mark Proctor <mproctor@codehaus.org> Subject: Re: [rules-dev] Spring support for drools-api To: "Rules Dev List" <rules-dev@lists.jboss.org> Date: Sunday, November 23, 2008, 9:09 PM I'm thinking of adopting the Spring approach to Resources for drools-api, obviously not tieing drools-api to spring - just copying the concept. Resource however doesn't seem to work with Readers. Readers in general are only needed for in memory generation , using StringReader, otherwise file/url/classpath resources suffice. So it seems to do the Spring way you would do a ByteArrayResource( "this is my drl file".getBytes() ). I'm wondering what people think of that, and what about encoding issues? Compared to the way at the moment that we just take a Reader, and that's it. Mark Mark Proctor wrote:now my spring skills are improving, I'm looking toimprove the xml andleverage spring Resource framework - so that we canget completescripting of the entire KnowlegeBuilder api. I'vecome up with the twoxmls so far: <property name="drls"> <list><value>file:src/main/java/org/drools/ioc/spring/testSpring.drl</value></list> </property> <property name="dtables"> <list> <beanclass="org.drools.ioc.spring.DtableResourceFactoryBean"><propertyname="resource"value="file:src/main/java/org/drools/ioc/spring/dt.xls" /><propertyname="inputType" value="XLS" /></bean></list> </property> This one has a property per knowledge type, theadvantage is onknowledge types which are just string, we can use asimple <value>.Although knowlege tyep that require additionalinformation, like theDT input type, will need a bean. <property name="resources"> <list> <beanclass="org.drools.ioc.spring.KnowledgeResourceBeanFactory"><propertyname="knowledgeType" value="DRL" /><propertyname="resource"value="file:src/main/java/org/drools/ioc/spring/testSpring.drl"/> </bean> <beanclass="org.drools.ioc.spring.KnowledgeResourceBeanFactory"><propertyname="knowledgeType" value="DTABLE" /><propertyname="resource"value="file:src/main/java/org/drools/ioc/spring/dt.xls" /><propertyname="resourceConfiguration"><beanclass="org.drools.ioc.spring.DtableResourceFactoryBean"><propertyname="inputType" value="XLS" /></bean></property></bean></list></property> This approach more closely resembles the kbuilder api.We have simpleresources list. Everything is a bean, so it's veryflexible, but welose the shortcut approach of the first one where wecan just give alist of strings. As each one must be a bean, so thatit can containatleast the knowledge type, as well as the resourcestring.My preference currently is for the second one, as Idon't tink it'stoo verbose, and it provides better consistency thanthe first.Mark Geoffrey De Smet wrote:It doesn't indeed sound overkill to me tocreate a FactoryBean forthe Builder. Though I would probably reuse the Builder insidetheKnowlegdeBaseFactory to build the Knowlegde base. The real issue is concurrency. Spring promotes the idea of stateless beans whichdo have globalvariables, but those global variables are eithersynchronisedthread-safe or also follow the stateless beanpattern.This in fact makes the stateless beans threadsafe, without any needfor synchronization/locking. So the question is: is your KnowlegdeBasethread-safe?Thread safe objects are usually put into globalvariables,while not thread unsafe objects are always putinto local variables.class A { private B b; // thread-safe by synchronization(JDBCConnection, ...)private C c; // thread-safe by the statelesspattern:// both b and c are set during initialization(constr. or setter),before A is exposed to other threads public void metho(D d) { // d is not thread-safe E e = ...; // e is not thread-safe } } In drools 4. B is the RuleBase, while E is theworking memory instance.With kind regards, Geoffrey De Smet Mark Proctor schreef:Geoffrey De Smet wrote:With kind regards, Geoffrey De Smet Mark Proctor schreef:I'd also maybe considerdropping the Spring prefix, as it's in apackage called spring? =>org.drools.spring.KnowledgeAgentFactoryBeanMy worry here is it might be aduplicate of the same name used inthe future - although it'll be ina different package namespace,it's still not good practice.Anyone else have any opinions on this?Good point, prefixing it with Springcan't hurt :)I'm just wondering aboutKnowledgeBuilderFactory andKnowledgeBaseFactory. While it makes sense toput theKnowledgeAgentFactory into spring, does itmake sense for thoseother factories? What are they gonna do otherthan returnKnowledgeBuilder or a KnowledgeBase, onlypossible advantage is thatit would allow spring to inject theKnowledgeBuilderConfigurationand KnowledgeBaseConfiguration. And ideas?http://anonsvn.labs.jboss.com/labs/jbossrules/trunk/drools-api/src/main/java/org/drools/builder/KnowledgeBuilderFactory.javahttp://anonsvn.labs.jboss.com/labs/jbossrules/trunk/drools-api/src/main/java/org/drools/builder/KnowledgeBuilder.javahttp://anonsvn.labs.jboss.com/labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeBaseFactory.javahttp://anonsvn.labs.jboss.com/labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeBase.javaThis is typically how those two factories areused:KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.addResource( new URL("file://myrules.drl" ),KnowledgeType.DRL);KnowledgeBase kbase =KnowledgeBaseFactory.newKnowledgeBase();If we use spring to automate the adding ofresources, that's prettymuch what the agent is doing anyway, sowouldn't that be pointless?Mark_______________________________________________rules-dev mailing list rules-dev@lists.jboss.orghttps://lists.jboss.org/mailman/listinfo/rules-dev_______________________________________________rules-dev mailing list rules-dev@lists.jboss.orghttps://lists.jboss.org/mailman/listinfo/rules-dev_______________________________________________ rules-dev mailing list rules-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-dev_______________________________________________ rules-dev mailing list rules-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-dev_______________________________________________ rules-dev mailing list rules-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-dev_______________________________________________ rules-dev mailing list rules-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-dev
_______________________________________________ rules-dev mailing list rules-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-dev
_______________________________________________
rules-dev mailing list
rules-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-dev
_______________________________________________ rules-dev mailing list rules-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-dev
_______________________________________________
rules-dev mailing list
rules-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-dev