Cyclic dependency limitation

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

Cyclic dependency limitation

Geoffrey De Smet
Hi,

I am using xstream to load an xml file containing my testdata.
I edit that xml file by hand.

Now the following cyclic dependency seems to work:

<linked-list>
   <A id="myA">
     <name>myName</name>
     <b class="B" id="myB">
       <age>15</age>
       <a reference="myA"/>
     </b>
   </A>
   <B reference="myB"/>
</linked-list>

But these cyclic dependency does not work:

<linked-list>
   <A id="myA">
     <name>myName</name>
     <b reference="myB"/> <!-- turns out null but doesn't fail -->
   </A>
   <B id="myB">
     <age>15</age>
     <a reference="myA"/>
   </B>
</linked-list>

Due to the nature of my testdata, the second example is actually the way
I 'd strongly prefer to do it, even if this means sacrificing performance.
Might this be accepted as an improvement request?

--
With kind regards,
Geoffrey De Smet

Reply | Threaded
Open this post in threaded view
|

Re: Cyclic dependency limitation

Jörg Schaible-2
Geoffrey wrote:

> Hi,
>
> I am using xstream to load an xml file containing my testdata.
> I edit that xml file by hand.
>
> Now the following cyclic dependency seems to work:
>
> <linked-list>
>    <A id="myA">
>      <name>myName</name>
>      <b class="B" id="myB">
>        <age>15</age>
>        <a reference="myA"/>
>      </b>
>    </A>
>    <B reference="myB"/>
> </linked-list>
>
> But these cyclic dependency does not work:
>
> <linked-list>
>    <A id="myA">
>      <name>myName</name>
>      <b reference="myB"/> <!-- turns out null but doesn't fail -->
>    </A>
>    <B id="myB">
>      <age>15</age>
>      <a reference="myA"/>
>    </B>
> </linked-list>
>
> Due to the nature of my testdata, the second example is actually the way
> I 'd strongly prefer to do it, even if this means sacrificing performance.
> Might this be accepted as an improvement request?

The point is, that the XML parser works sequential and you cannot refer
elements, that are not yet defined. You can use a DOM-based though and
might implement your own ReferenceByIdMarshallingStrategy, that takes
advantage of the fact, that the DOM is already in memory.

- Jörg

Reply | Threaded
Open this post in threaded view
|

Re: Cyclic dependency limitation

Geoffrey De Smet
Even though the xml parser is sequential,
wouldn't it be possible to:
1) Remember pre-references in a HashMap of <reference id> to List of
<setter of instance>.
2) When an id is defined, check if it exists in the pre-references
hashmap and update every instance in the list if it does and remove it
from the hashmap.

In any case it would be nice if xstream fails fast when it can't resolve
a reference, instead of just leaving it on null, so:

3) At the end check if the hashmap is empty, fail fast with a list
otherwise.

Jörg Schaible wrote:

> Geoffrey wrote:
>
>> Hi,
>>
>> I am using xstream to load an xml file containing my testdata.
>> I edit that xml file by hand.
>>
>> Now the following cyclic dependency seems to work:
>>
>> <linked-list>
>>    <A id="myA">
>>      <name>myName</name>
>>      <b class="B" id="myB">
>>        <age>15</age>
>>        <a reference="myA"/>
>>      </b>
>>    </A>
>>    <B reference="myB"/>
>> </linked-list>
>>
>> But these cyclic dependency does not work:
>>
>> <linked-list>
>>    <A id="myA">
>>      <name>myName</name>
>>      <b reference="myB"/> <!-- turns out null but doesn't fail -->
>>    </A>
>>    <B id="myB">
>>      <age>15</age>
>>      <a reference="myA"/>
>>    </B>
>> </linked-list>
>>
>> Due to the nature of my testdata, the second example is actually the way
>> I 'd strongly prefer to do it, even if this means sacrificing performance.
>> Might this be accepted as an improvement request?
>
> The point is, that the XML parser works sequential and you cannot refer
> elements, that are not yet defined. You can use a DOM-based though and
> might implement your own ReferenceByIdMarshallingStrategy, that takes
> advantage of the fact, that the DOM is already in memory.
>
> - Jörg
>
>

--
With kind regards,
Geoffrey De Smet

Reply | Threaded
Open this post in threaded view
|

Re: Re: Cyclic dependency limitation

Justin T. Sampson-2
On 1/9/06, Geoffrey <[hidden email]> wrote:

> Even though the xml parser is sequential, wouldn't it be
> possible to:
>
> 1) Remember pre-references in a HashMap of <reference id> to
> List of <setter of instance>.
>
> 2) When an id is defined, check if it exists in the
> pre-references hashmap and update every instance in the list if
> it does and remove it from the hashmap.

You could even instantiate the object, with all fields
uninitialized, at the point of the first reference, and then fill
in its fields when the full object appears in the XML. Then you
don't have to track and later update all the pre-references. But
that would require including the "class" attribute on references
in the general case.

Cheers,
Justin