Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [QVTo] how to do copying of contained objects?
[QVTo] how to do copying of contained objects? [message #528118] Mon, 19 April 2010 12:33
Albert Hofkamp is currently offline Albert HofkampFriend
Messages: 41
Registered: August 2009
Member
Hello,

I seem to have a problem with making unique copies of a contained object.
As a small example, I want to transform from Source to Dest:
@namespace(uri="http://example.com/source", prefix="SourcePrefix")
package Source;

class XPairs {
  val XNames[*] names;
}

class XNames {
  attr String[+] ks;
  val XValue[1] v;
}

class XValue {
  attr String[1] value;
}
Source is an XPairs object, containing XNames. Each XName is a sequence of key-strings ks, and a single contained XValue v.

The Dest language is similar, except that YName has a single string k, (and again a contained YValue object).
@namespace(uri="http://example.com/dest", prefix="DestPrefix")
package Dest;

class YPairs {
  val YName[*] names;
}

class YName {
  attr String[1] k;
  val YValue[1] v;
}

class YValue {
  attr String[1] value;
}


In the transformation, I need to make an YName for each string in ks in XNames.
Each XValue should be transformed once, followed by cloning copies for the other names, so each YValue is contained by exactly one YName.

I have tried two ways to achieve this, and failed both times.
The simplest approach is (with the deepclone() code commented out):
modeltype Source uses "http://example.com/source";
modeltype Dest uses "http://example.com/dest";

transformation contain(in s:Source, out d:Dest);

main() {
	s.rootObjects()[XPairs] -> xmap mapPairs();
}

mapping XPairs::mapPairs() : YPairs
{
	names := self.names -> xmap mapNames() -> flatten();
}

mapping XNames::mapNames() : Sequence(YName)
{
	init {
		result := self.ks -> xmap mapSingle(self.v);
	}
}

mapping String::mapSingle(xv : XValue) : YName
{
	k := self;
	v := xv.xmap mapValue(); //.deepclone().oclAsType(YValue);
}

mapping XValue::mapValue() : YValue
{
	value := self.value;
}
I specify to compute "xv.xmap mapValue()" for each YName.

As test-case, I use
<?xml version="1.0" encoding="UTF-8"?>
<SourcePrefix:XPairs xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:SourcePrefix="http://example.com/source">
  <names>
    <ks>a</ks>
    <ks>b</ks>
    <ks>c</ks>
    <v value="the_value"/>
  </names>
</SourcePrefix:XPairs>
That is, a single XNames object with names "a, b, c" and value "the_value".

In the generated Dest file, I don't get 3 YValue objects, there is only one value:
<?xml version="1.0" encoding="UTF-8"?>
<DestPrefix:YPairs xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:DestPrefix="http://example.com/dest">
  <names k="a"/>
  <names k="b"/>
  <names k="c">
    <v value="the_value"/>
  </names>
</DestPrefix:YPairs>


The second approach I tried is obtained by uncommenting the deepclone() code part in mapSingle(), such that it reads "v := xv.xmap mapValue().deepclone().oclAsType(YValue);". In that case I get:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:DestPrefix="http://example.com/dest">
  <DestPrefix:YPairs>
    <names k="a">
      <v value="the_value"/>
    </names>
    <names k="b">
      <v value="the_value"/>
    </names>
    <names k="c">
      <v value="the_value"/>
    </names>
  </DestPrefix:YPairs>
  <DestPrefix:YValue value="the_value"/>
</xmi:XMI>
The v value is attached to every name, but I get an additional '<DestPrefix:YValue value="the_value"/>' at the top level, which should not be there.

So my question is, how to do this correctly in QVTo ?

Albert

Previous Topic:ATL function writeTo(fileName : String) Problem
Next Topic:[QVTo] Import from different project: is it possible?
Goto Forum:
  


Current Time: Sat Aug 24 07:45:51 GMT 2019

Powered by FUDForum. Page generated in 0.01934 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top