multiple parent child relationships using xmlrules

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

multiple parent child relationships using xmlrules

Richard Wood
Hello

I'm defining my rules using XML and using the DigesterRuleParser.
How can I set multiple parent child relationships and vice versa if I create
more than one child object inside one pattern

example

class BBB {

void setAAA(AAA a) {...}
}

<pattern value="x">
    <object-create-rule classname="AAA" />

    <object-create-rule classname="BBB"/>
    <set-top-rule methodname="setAAA"/>
    <set-next-rule methodname="addBBB"/>
    <object-create-rule classname="BBB"/>
    <set-top-rule methodname="setAAA"/>
    <set-next-rule methodname="addBBB"/>

</pattern>

The second batch of setter rules won't work as the stack looks as follows

BBB
BBB
AAA

The xml would have to bes restructured to look something like this.

<pattern value="x">
    <object-create-rule classname="AAA" />

    <object-create-rule classname="BBB"> <- push object on stack
        <set-top-rule methodname="setAAA"/>
        <set-next-rule methodname="addBBB"/>
    </object-create-rule> <- pop object on stack
    <object-create-rule classname="BBB">
        <set-top-rule methodname="setAAA"/>
        <set-next-rule methodname="addBBB"/>
    </object-create-rule>

</pattern>

Any easy way to do this?

The method Rule.end() is not very helpful here.

Cheers
    Richard


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[digester] multiple parent child relationships using xmlrules

Simon Kitching
Hi Richard,

On Tue, 2005-06-28 at 18:45 +0200, Richard Wood wrote:

> Hello
>
> I'm defining my rules using XML and using the DigesterRuleParser.
> How can I set multiple parent child relationships and vice versa if I create
> more than one child object inside one pattern
>
> example
>
> class BBB {
>
> void setAAA(AAA a) {...}
> }
>
> <pattern value="x">
>     <object-create-rule classname="AAA" />
>
>     <object-create-rule classname="BBB"/>
>     <set-top-rule methodname="setAAA"/>
>     <set-next-rule methodname="addBBB"/>
>     <object-create-rule classname="BBB"/>
>     <set-top-rule methodname="setAAA"/>
>     <set-next-rule methodname="addBBB"/>
>
> </pattern>

In future, please put the commons component you are interested in at the
start of the email subject line. It's in your own interest as I didn't
see your email until now...

I don't understand what you are trying to achieve.

It looks like you are trying to do this:
 * when element <x> is encountered
     Create one AAA and two BBB objects and link them all together

Is this really the case? This is very unusual and it isn't any surprise
that Digester doesn't handle it out of the box.

Assuming you really do want this, the best solution is probably to write
your own custom rule class. Don't be reluctant to create a custom Rule;
it's quite ok to do so.

public class CreateOddSetOfObjectsRule extends Rule {
  public void begin(String ns, String element, Attributes attrs) {
    AAA obj1 = new AAA();
    BBB obj2 = new BBB();
    BBB obj3 = new BBB();
    obj1.addBBB(obj2);
    obj1.addBBB(obj2);
    // etc

    // Here possibly push one or more of these objects onto the
    // digester stack so other rules can manipulate them. If
    // you do this, provide an end method that pops them back
    // off again.
  }
}

Note however that digester rules typically operate on the top object on
the stack. So you may find that pushing more than one of the objects you
create onto the stack isn't useful.

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] multiple parent child relationships using xmlrules

Richard Wood
Hello Simon

Thanks for replying

The creation of new rules is not an issue but I was speculating on using
existing rules like
object-create-rule, set-next-rule and others.

Putting all rule logic into one isn't really very efficient and I would be
duplicating code...

> Note however that digester rules typically operate on the top object on
> the stack. So you may find that pushing more than one of the objects you
> create onto the stack isn't useful.

That is exactly the problem. Inside one pattern I cannot add more than one
object to the stack as popping only happens when the pattern ends. And not
the rule...

And is it really so unusual  to set two or more parent-child connections
instead of only one?

Regards
    Richard

----- Original Message -----
From: "Simon Kitching" <[hidden email]>
To: "Richard Wood" <[hidden email]>
Cc: <[hidden email]>
Sent: Wednesday, June 29, 2005 12:13 PM
Subject: [digester] multiple parent child relationships using xmlrules


> Hi Richard,
>
> On Tue, 2005-06-28 at 18:45 +0200, Richard Wood wrote:
>> Hello
>>
>> I'm defining my rules using XML and using the DigesterRuleParser.
>> How can I set multiple parent child relationships and vice versa if I
>> create
>> more than one child object inside one pattern
>>
>> example
>>
>> class BBB {
>>
>> void setAAA(AAA a) {...}
>> }
>>
>> <pattern value="x">
>>     <object-create-rule classname="AAA" />
>>
>>     <object-create-rule classname="BBB"/>
>>     <set-top-rule methodname="setAAA"/>
>>     <set-next-rule methodname="addBBB"/>
>>     <object-create-rule classname="BBB"/>
>>     <set-top-rule methodname="setAAA"/>
>>     <set-next-rule methodname="addBBB"/>
>>
>> </pattern>
>
> In future, please put the commons component you are interested in at the
> start of the email subject line. It's in your own interest as I didn't
> see your email until now...
>
> I don't understand what you are trying to achieve.
>
> It looks like you are trying to do this:
> * when element <x> is encountered
>     Create one AAA and two BBB objects and link them all together
>
> Is this really the case? This is very unusual and it isn't any surprise
> that Digester doesn't handle it out of the box.
>
> Assuming you really do want this, the best solution is probably to write
> your own custom rule class. Don't be reluctant to create a custom Rule;
> it's quite ok to do so.
>
> public class CreateOddSetOfObjectsRule extends Rule {
>  public void begin(String ns, String element, Attributes attrs) {
>    AAA obj1 = new AAA();
>    BBB obj2 = new BBB();
>    BBB obj3 = new BBB();
>    obj1.addBBB(obj2);
>    obj1.addBBB(obj2);
>    // etc
>
>    // Here possibly push one or more of these objects onto the
>    // digester stack so other rules can manipulate them. If
>    // you do this, provide an end method that pops them back
>    // off again.
>  }
> }
>
> Note however that digester rules typically operate on the top object on
> the stack. So you may find that pushing more than one of the objects you
> create onto the stack isn't useful.
>
> Regards,
>
> Simon
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] multiple parent child relationships using xmlrules

Simon Kitching
On Wed, 2005-06-29 at 19:55 +0200, Richard Wood wrote:

> Hello Simon
>
> Thanks for replying
>
> The creation of new rules is not an issue but I was speculating on using
> existing rules like
> object-create-rule, set-next-rule and others.
>
> Putting all rule logic into one isn't really very efficient and I would be
> duplicating code...

I don't see digester as a tool with the ability to be extended via
custom rules. I see it as an extensible core that happens to be bundled
with some convenient rules. There's nothing that says that the bundled
rules are better than your own. In fact the bundled rules need to use
lots of reflection etc. because they can't make any assumptions about
the classes they are manipulating. Custom rules can be far simpler and
more efficient as they can deal with concrete classes rather than work
only via reflection.

But which approach you use is your call.
 
>
> > Note however that digester rules typically operate on the top object on
> > the stack. So you may find that pushing more than one of the objects you
> > create onto the stack isn't useful.
>
> That is exactly the problem. Inside one pattern I cannot add more than one
> object to the stack as popping only happens when the pattern ends. And not
> the rule...
>

Yep.

> And is it really so unusual  to set two or more parent-child connections
> instead of only one?

Having a parent object with multiple child objects is not unusual.

But having multiple child objects created as a result of encountering a
single xml element is. I've been using digester for years and not needed
to do this. Nor have I seen anyone else on the user list trying to do
this. And I can't think of a reason why this would be useful.

If you absolutely do need to do this, then I suggest that Digester is
not the right tool for your job.

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] multiple parent child relationships using xmlrules

Simon Kitching
On Thu, 2005-06-30 at 14:43 +1200, Simon Kitching wrote:

> >
> > > Note however that digester rules typically operate on the top object on
> > > the stack. So you may find that pushing more than one of the objects you
> > > create onto the stack isn't useful.
> >
> > That is exactly the problem. Inside one pattern I cannot add more than one
> > object to the stack as popping only happens when the pattern ends. And not
> > the rule...
> >
>
> Yep.

Umm..I meant Nope.

An ObjectCreateRule pushes an object when begin is executed and pops the
corresponding object when end is executed.

A pattern is nothing but a way to select a set of Rule objects.

Your issue is that you have multiple ObjectCreateRule instances
associated with a single pattern. All their begin methods fire when the
<x> element is matched, and all their end methods fire when </x> is
encountered. In between, there are multiple objects on the stack. This
behaviour is fundamental to Digester.

Note that I have a long-standing dislike of the xmlrules module, and
this is a clear example of why. If you were using the java API directly
then the way Rule objects and patterns work would be much more obvious.

I suggest you look at the example code in the src/examples directory of
the source download to learn how digester's API works. This may then
shed some light on why your xmlrules definitions (ie rule definitions
specified in an xml file) work the way they do.

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]