AW: AW: AW: [stringtemplate-interest] Problems with List and Hashes
revisited REAL MAIL
Dreyer Ulf (CR/APA3)
Ulf.Dreyer at de.bosch.com
Tue May 30 07:12:48 PDT 2006
Sorry about the last one - this is the complete Mail
Hello Kunle,
thank you very much for this extensive answer!
I cut most of the citation but there where very clear
hints I needed ;)
(1) I think I've solved the problem to my satisfaction.
But only time and more coding will show ;)
Anything with children (in my tree) is put into a
list (of one element). So a template application
data:template() "unwraps" the list and leaves the
hash(treenode) untouched.
This is the behaviour I find natural because
I can't find an application for enumerating a hash.
I have cleared up a few points from the last mail
if you are interested.
(2) I have still more questions :(
Maybe the clarification below are needed.
And maybe this are side effects of my strange data-structure.
a)
Am I right in thinking that templates without formal arguments
SomeTemplate() ::= << Output=$it.something$ >>
can only be invoked as $SomeData:SomeTemplate()$
never as $SomeTemplate(SomeData)$
(I get: "template anotherOutput must have exactly one formal
arg in template context [..Context..]")
b)
Should this template work? (It always crashes on me because
of stack-overflow)
RecursiveOut(Daten) ::= << $Daten:RecursiveOut();separator=","$ >>
As both classes in my data-structure (AttributeHash and AttributeList)
are IEnumerable it should strip away one layer at a time, shouldn't it?
------------------------------------------------------------------
Clarification for last mail's mess ;)
------------------------------------------------------------------
This is a little back to front:
I) Your suggestion (and the strange similarities of my solution to it)
[...]
> Since they [my datastructure AttributeList and/or AttributeHash]
> are indexed by (and accessed by) a string key
> (not uniquely I now understand),
I am not sure if you mean what I think:
Within a Hash there are (naturally) no two keys alike.
If I try to add an existant key its value becomes a list of the old
value(s) and the new entry. If that is what you mean by "not uniquely"
we are on the same page ;)
>why not just use an IDictionary?.
That's what I DO! (Even prior to the last mail)
> The separate entries for keys like "DataA" can then held in a list.
> If preserving insertion order is important, you can develop a
> specialized IDictionary. ST#'s HashList may provide pointers there.
Here our understanding differs a little. I hope I can clarify it
with the old xml-file:
------------------------------------------------------------------
II) Data-structure again
> > Sample xml-file:
> >
> > <?xml version="1.0"?>
> > <root>
> > <DataBlock1>
> > <DataA>1_DataA</DataA>
> > <DataB>1_DataB</DataB>
> > <DataC>
> > <SubC1>1_SubC1Content1</SubC1>
> > <SubC1>1_SubC1Content2</SubC1>
> > <SubC2>1_SubC2Content</SubC2>
> > </DataC>
> > </DataBlock1>
> > <DataBlock1>
> > <DataA>2_DataA</DataA>
> > <DataB>2_DataB</DataB>
> > <DataC>
> > <SubC1>2_SubC1Content1</SubC1>
> > <SubC1>2_SubC1Content2</SubC1>
> > <SubC2>2_SubC2Content</SubC2>
> > </DataC>
> > </DataBlock1>
> > <DataBlock2>
> > <ItemB>ItemBContent</ItemB>
> > </DataBlock2>
> > </root>
>
> So you have a toplevel Hashtable that contains ("key,
> [elem1,...elemN]"
> means entry has key "key" and is a list of elem1...elemN):
> DataBlock1, [DataA, DataB, DataC]
> DataBlock2, ItemB
> where ItemB is not a list right?
Close but not quite (I think)
First: the "Is ItemB a list (of one element) or not?"-question
changes answers very often during my experiments.
CURRENTLY: If (ItemB is a String) there is NO list
If (ItemB is a Hash) there is a list of one element
One try to make it simple to understand my weird logic:
Anything named in the xml gets an entry into a hash (AttributeHash).
IF there are two (or more) equally named xml-nodes (on the same level)
they are put into a list which is entered into the hash.
With the Statement directly above (Currently...) this means
Every named xmlNode which is NOT a simple string is put into a List
which is put into a Hash.
The above XML yields the following structure (not complete)
{} = empty hash, {(),()} Hash with two elements
("Key",ANYTHING) = key-value pair within a hash
[] = empty list
Top Level Hash (nameless) =
{ ("DataBlock1",[ NamelessHash{ ("DataA", String:1_DataA )
("DataB", String:1_DataB )
("DataC", NamelessHash*SNIP
too long* )},
NamelessHash{ ("DataA", String:2_DataA )
("DataB", String:2_DataB )
("DataC", NamelessHash*SNIP
too long* )}
])
("DataBlock2",[ NamelessHash{("ItemB",String:ItemBContent)} ])
}
> I presume that the extra DataBlock1 entry is a typo
> right?. If not, then
> DataBlock1 is as below:
> DataBlock1, [DataA, DataB, DataC, DataA, DataB, DataC]
No the extra DataBlock1 is for real -
thats where Attributes MAY be multi-valued without me knowing
in advance ;)
And it SHOULD look (simplified not entirely in above syntax)
{ ("DataBlock1", [{DataA, DataB, DataC}, {DataA, DataB, DataC}])
("DataBlock2", *SNIP*}
One unexpected behaviour I mentioned in earlier mails:
Normally one could only access something like
MyDataInstance["DataBlock1"][1]["DataA"] (which would be "1_DataA")
because MyDataInstance["DataBlock1"] is of Type List.
My list supports another lookup so that the following is possible:
MyDataInstance["DataBlock1"]["DataA"] (which would return a List
containing all elements named "DataA" in all elements of the list.
["1_DataA","2_DataA"]
------------------------------------------------------------------
III) a question of syntax:
> If you don't want an enumerable attribute to be treated as enumerable,
> define a formal parameter and use the alternate syntax:
> subtemplate(<parameterName>=<DataStruct>)
> IDictionary objects are enumerable so that is the expected
> behaviour. You
> should pass your hashtable as a formal parameter if you don't want it
> enumerated by the template application logic.
I can't do that because I don't necessarily know in advance if an
attribute will be single or multi-valued.
Therefore it has to be the Data:Template() Syntax.
I solved this (for the time being) by putting any AttributeHash
(even single ones) into an AttributeList.
I hope this won't have any ugly side-effects
> Good luck.
Thanks for your time. Thanks a LOT.
Ulf
More information about the stringtemplate-interest
mailing list