Yes you are right, it is redundant to have it both in the compile API and
the DRL directly, at least it appears that way.
What is in the DRL is really just a "key" to the compiler to find the
expander - in the case of the "default" expander it is in theory redundant.
Partly it is to allow future expanders to be implemented that do different
stuff. An expander just implements a certain simple interface, and is passed
to the parser as a map of {"foo.dsl", YourExpander}.
However, the *main* benefit of having it in the DRL is that the IDE can pick
it up, and provide content assistance based on it. So given that, its
probably slightly redundant to have it in the API. But *then* the question
becomes how to locate the DSL file when parsing the DRL. The equivalent in
java is to have a classpath, so we would need to have the concept of a
rulepath (!) which is starting to get confusing.
But I would be happy to hear any other ideas to improve it and make it
easier to use.
Michael.
On 4/1/06, Paul Smith <[EMAIL PROTECTED]> wrote:
>
> Hi All,
>
> I like the look of Drools 3 very much and especially the use of expanders.
> I
> have one slight query though. In the examples you give as follows:
>
> package test
>
> #must be in the following order.
> import org.drools.Person
> import org.drools.Cheese
>
> #refer to test_expander.dsl
> expander test_expander.dsl
>
> global java.util.List messages;
>
>
>
>
> rule "my rule"
> when
> #Person(name=="Bob", likes=="stilton")
> #Cheese(type=="stilton")
> There is a person with the name of Bob who likes stilton
> There is some stilton cheese available
>
> then
> Add the message "We have a winner"
> #messages.add("We have a winner");
> end
>
> Which is invoked using the following code:
>
> public void testWithExpanderDSL() throws Exception {
> PackageBuilder builder = new PackageBuilder();
> Reader source = new
> InputStreamReader(getClass().getResourceAsStream(
> "rule_with_expander_dsl.drl" ));
> Reader dsl = new
> InputStreamReader(getClass().getResourceAsStream( "test_expander.dsl"
> ));
> builder.addPackageFromDrl( source, dsl );
>
> //the compiled package
> Package pkg = builder.getPackage();
> assertTrue( pkg.isValid() );
> assertEquals(null, pkg.getErrorSummary());
> //Check errors
> String err = builder.printErrors();
> assertEquals("", err);
>
> assertEquals(0, builder.getErrors().length);
>
> RuleBase ruleBase = getRuleBase();
> ruleBase.addPackage( pkg );
>
> WorkingMemory wm = ruleBase.newWorkingMemory();
> wm.assertObject( new Person("Bob", "stilton") );
> wm.assertObject( new Cheese("stilton", 42) );
>
>
> List messages = new ArrayList();
> wm.setGlobal( "messages", messages );
> wm.fireAllRules();
>
> //should have fired
> assertEquals(1, messages.size());
>
> }
>
> Why is it necessary to specify the expander dsl file in both the .drl file
> and as a parameter to the PackageBuilder.addPackageFromDrl method. Surely
> if
> you are specifying the expander dsl in the drl file you shouldn't need to
> pass it in as a parameter to the PackageBuilder method?
>
> Nice work though guys. It's looking pretty damn good.
>
>