Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Extend a Model via ExtendedMetaData Interface
Extend a Model via ExtendedMetaData Interface [message #1083047] Fri, 09 August 2013 11:49 Go to next message
Max Power is currently offline Max Power
Messages: 5
Registered: August 2013
Junior Member
Hello,

I have a little problem to merge two EMF models.

Situation:
I have two Models generated from a XSD Schema. "Model B" extends "Model A" via the ExtendendMetaData interface. I created the Object "Method" from "Model B" and would like to include it in the "addData" Object of "Model A".

Code snippes from the XSD "Model A":
<xsd:complexType name="addData">
	<xsd:annotation>
		<xsd:documentation>Application specific data defined in external schemata </xsd:documentation>
	</xsd:annotation>
	<xsd:sequence>
		<xsd:element name="data" minOccurs="0" maxOccurs="unbounded">
			<xsd:complexType>
				<xsd:sequence>
					[b]<xsd:any namespace="##any"[/b] processContents="lax"/>
				</xsd:sequence>
				<xsd:attribute name="name" type="xsd:anyURI" use="required">
					<xsd:annotation>
						<xsd:documentation>Uniquely identifies the additional data element.</xsd:documentation>
					</xsd:annotation>
				</xsd:attribute>
				<xsd:attribute name="handleUnknown" use="required">
					<xsd:annotation>
							<xsd:documentation>Recommended processor handling for unknown data elements.
Specifies if the processor should try to preserve the additional data element, dismiss the element (e.g. because the data is invalid if not updated correctly) or use the processors default behaviour for unknown data.</xsd:documentation>
						</xsd:annotation>
						<xsd:simpleType>
							<xsd:restriction base="xsd:NMTOKEN">
								<xsd:enumeration value="preserve"/>
								<xsd:enumeration value="discard"/>
								<xsd:enumeration value="implementation"/>
							</xsd:restriction>
						</xsd:simpleType>
					</xsd:attribute>
				</xsd:complexType>
			</xsd:element>
		</xsd:sequence>
	</xsd:complexType>



My Code looks like this:
// Create DataType1 Object from ModelA
DataType1 dataType = ModelAFactory.createDataType1();
dataType.setName("myLINK");
dataType.setHandleUnknown(HandleUnknownType.IMPLEMENTATION);
	
// Create MethodType Object from ModelB	
MethodType methodType = ModelBFactory.createMethodType();
methodType.setName("Iam a method");

// Bring it together
ExtendedMetaData extendedMetaData = ExtendedMetaData.INSTANCE;
Entry entry = FeatureMapUtil.createEntry(extendedMetaData.demandFeature("", "", true), methodType);

dataType.getAny().add(entry);

// Save Model as XML
URI fileUri = URI.createFileURI(new File(fbGen.getOutputpath() + "//Test.xml").getPath());
Resource res = resourceSet.createResource(fileUri);

// Add UTF-8 encoding option
Map<String, Object> options = new HashMap<String, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
options.put(XMLResource.OPTION_DECLARE_XML, Boolean.TRUE);
options.put(XMLResource.OPTION_EXTENDED_META_DATA, metadata );
       
// Save the content
res.getContents().add(fbGen.getPlcOpen());
res.save(options); 


The result is:
          <data handleUnknown="implementation" name="myLINK">
            <_1:Method name="Iam a method">
              <interface>
                <localVars>
                  <variable name="Name">
                    <type>
                      <sINT/>
                    </type>
                    <initialValue/>
                    <documentation>
                      <xhtml>Mycomment</xhtml>
                    </documentation>
                  </variable>
                </localVars>
              </interface>
            </_1:Method>
          </data>


But it should looks like this:
          <data name="myLINK" handleUnknown="implementation">
            <Method name="Iam a method">
              <interface>
                <localVars>
                  <variable name="Name">
                    <type>
                      <sINT/>
                    </type>
                    <initialValue/>
                    <documentation>
                      <xhtml>Mycomment</xhtml>
                    </documentation>
                  </variable>
                </localVars>
              </interface>
            </Method>
          </data>


Why is the prefix "_1" set before the "Method"? What can I do to generate the XML file correctly?

Greetz
Re: Extend a Model via ExtendedMetaData Interface [message #1083196 is a reply to message #1083047] Fri, 09 August 2013 16:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26051
Registered: July 2009
Senior Member
Max,

Comments below.

On 09/08/2013 3:41 PM, Max Power wrote:
> Hello,
>
> I have a little problem to merge two EMF models.
> Situation:
> I have two Models generated from a XSD Schema. "Model B" extends
> "Model A" via the ExtendendMetaData interface. I created the Object
> "Method" from "Model B" and would like to include it in the "addData"
> Object of "Model A".
>
> Code snippes from the XSD "Model A":
>
> <xsd:complexType name="addData">
> <xsd:annotation>
> <xsd:documentation>Application specific data defined in
> external schemata </xsd:documentation>
> </xsd:annotation>
> <xsd:sequence>
> <xsd:element name="data" minOccurs="0" maxOccurs="unbounded">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:any namespace="##any"
> processContents="lax"/>
> </xsd:sequence>
> <xsd:attribute name="name" type="xsd:anyURI"
> use="required">
> <xsd:annotation>
> <xsd:documentation>Uniquely identifies the
> additional data element.</xsd:documentation>
> </xsd:annotation>
> </xsd:attribute>
> <xsd:attribute name="handleUnknown" use="required">
> <xsd:annotation>
> <xsd:documentation>Recommended processor
> handling for unknown data elements.
> Specifies if the processor should try to preserve the additional data
> element, dismiss the element (e.g. because the data is invalid if not
> updated correctly) or use the processors default behaviour for unknown
> data.</xsd:documentation>
> </xsd:annotation>
> <xsd:simpleType>
> <xsd:restriction base="xsd:NMTOKEN">
> <xsd:enumeration value="preserve"/>
> <xsd:enumeration value="discard"/>
> <xsd:enumeration value="implementation"/>
> </xsd:restriction>
> </xsd:simpleType>
> </xsd:attribute>
> </xsd:complexType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
>
>
>
> My Code looks like this:
>
> // Create DataType1 Object from ModelA
> DataType1 dataType = ModelAFactory.createDataType1();
> dataType.setName("myLINK");
> dataType.setHandleUnknown(HandleUnknownType.IMPLEMENTATION);
>
> // Create MethodType Object from ModelB
> MethodType methodType = ModelBFactory.createMethodType();
> methodType.setName("Iam a method");
>
> // Bring it together
> ExtendedMetaData extendedMetaData = ExtendedMetaData.INSTANCE;
> Entry entry =
> FeatureMapUtil.createEntry(extendedMetaData.demandFeature("", "",
> true), methodType);
Hmmm. Why doesn't the feature have a name? I.e., shouldn't it be
extendedMetaData.demandFeature("", "Method", true)?
>
> dataType.getAny().add(entry);
>
> // Save Model as XML
> URI fileUri = URI.createFileURI(new File(fbGen.getOutputpath() +
> "//Test.xml").getPath());
> Resource res = resourceSet.createResource(fileUri);
>
> // Add UTF-8 encoding option
> Map<String, Object> options = new HashMap<String, Object>();
> options.put(XMLResource.OPTION_ENCODING, "UTF-8");
> options.put(XMLResource.OPTION_DECLARE_XML, Boolean.TRUE);
> options.put(XMLResource.OPTION_EXTENDED_META_DATA, metadata );
> // Save the content
> res.getContents().add(fbGen.getPlcOpen());
> res.save(options);
>
> The result is:
>
> <data handleUnknown="implementation" name="myLINK">
> <_1:Method name="Iam a method">
> <interface>
> <localVars>
> <variable name="Name">
> <type>
> <sINT/>
> </type>
> <initialValue/>
> <documentation>
> <xhtml>Mycomment</xhtml>
> </documentation>
> </variable>
> </localVars>
> </interface>
> </_1:Method>
> </data>
>
>
> But it should looks like this:
>
> <data name="myLINK" handleUnknown="implementation">
> <Method name="Iam a method">
> <interface>
> <localVars>
> <variable name="Name">
> <type>
> <sINT/>
> </type>
> <initialValue/>
> <documentation>
> <xhtml>Mycomment</xhtml>
> </documentation>
> </variable>
> </localVars>
> </interface>
> </Method>
> </data>
>
>
> Why is the prefix "_1" set before the "Method"?
Is there an xmlns declaration from _1?
> What can I do to generate the XML file correctly?
>
> Greetz
Re: Extend a Model via ExtendedMetaData Interface [message #1083242 is a reply to message #1083196] Fri, 09 August 2013 17:37 Go to previous messageGo to next message
Max Power is currently offline Max Power
Messages: 5
Registered: August 2013
Junior Member
Hi,
Quote:
Hmmm. Why doesn't the feature have a name? I.e., shouldn't it be
extendedMetaData.demandFeature("", "Method", true)?

I have tried everything, with a name, without a name, but it had not effect on the result.

Quote:
Is there an xmlns declaration from _1?

There is no xmlns declaration. It looks like an automatically generated prefix.

I'm not a big EMF expert, but maybe it has something to do with the open/save options?
Re: Extend a Model via ExtendedMetaData Interface [message #1083281 is a reply to message #1083242] Fri, 09 August 2013 18:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26051
Registered: July 2009
Senior Member
Max,

That's very odd. If
http://www.theserverside.com/news/1364302/Binding-XML-to-Java doesn't
solve the problem (e.g., use null for the first argument rather than "",
and use "Method" for the second argument) can you zip up the projects
(or some other reproducible test case) and attach them so I can have a
closer look?


On 09/08/2013 7:37 PM, Max Power wrote:
> Hi,
> Quote:
>> Hmmm. Why doesn't the feature have a name? I.e., shouldn't it be
>> extendedMetaData.demandFeature("", "Method", true)?
>
> I have tried everything, with a name, without a name, but it had not
> effect on the result.
>
> Quote:
>> Is there an xmlns declaration from _1?
>
> There is no xmlns declaration. It looks like an automatically
> generated prefix.
> I'm not a big EMF expert, but maybe it has something to do with the
> open/save options?
>
Re: Extend a Model via ExtendedMetaData Interface [message #1085165 is a reply to message #1083281] Mon, 12 August 2013 14:41 Go to previous messageGo to next message
Max Power is currently offline Max Power
Messages: 5
Registered: August 2013
Junior Member
Hi Ed,

your link doesn't solve the problem.
Maybe you can take a look on my code.


Re: Extend a Model via ExtendedMetaData Interface [message #1087388 is a reply to message #1085165] Thu, 15 August 2013 15:13 Go to previous messageGo to next message
Max Power is currently offline Max Power
Messages: 5
Registered: August 2013
Junior Member
Has nobody an idea or a tip how I can solve the problem?
Re: Extend a Model via ExtendedMetaData Interface [message #1087423 is a reply to message #1087388] Thu, 15 August 2013 16:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26051
Registered: July 2009
Senior Member
Max,

Can you share a test case that I can modify to make it work correctly
(or to track down any associated bugs)? If you can't attach via the
newsgroup, open a bugzilla and attach it there.


On 15/08/2013 5:13 PM, Max Power wrote:
> Has nobody an idea or a tip how I can solve the problem?
Re: Extend a Model via ExtendedMetaData Interface [message #1090587 is a reply to message #1087423] Tue, 20 August 2013 11:02 Go to previous messageGo to next message
Max Power is currently offline Max Power
Messages: 5
Registered: August 2013
Junior Member
Hello,

the attachment from my previous post includes contains a test case. If you execute the main class in the PLCOpen package the file model.xml is automatically generated.

The final xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="plcopen.org/xml/tc6_0201">
  <types>
    <pous>
      <pou name="Pou Name">
        <addData>
          <data handleUnknown="implementation" name="3s-software.com/plcopenxml/pouinheritance">
            <_1:Inheritance>
              <Extends>MyType</Extends>
            </_1:Inheritance>
          </data>
        </addData>
      </pou>
    </pous>
  </types>
</project>


The Problem is the "_1:" prefix.

This is the important code snippet:
        /*
         * Create content
         */
	ProjectType projectType = factory.createProjectType();
	TypesType typesType = factory.createTypesType();
	PousType pousType = factory.createPousType();
	AddData addData = factory.createAddData();
	PouType1 pouType1 = factory.createPouType1();
		
	pouType1.setName("Pou Name");
		
	// Add model extension code
	DataType1 dataType = factory.createDataType1();
	dataType.setName("3s-software.com/plcopenxml/pouinheritance");
	dataType.setHandleUnknown(HandleUnknownType.IMPLEMENTATION);

	InheritanceType inhertitanceType = codesysFactory.createInheritanceType();
	inhertitanceType.setExtends("MyType");

        // Here is the problem!!! 
	ExtendedMetaData extendedMetaData = ExtendedMetaData.INSTANCE;
	Entry entry = FeatureMapUtil.createEntry(extendedMetaData.demandFeature(null, "", true), inhertitanceType);
	dataType.getAny().add(entry);		
		
	addData.getData().add(dataType);
	pouType1.setAddData(addData);
	pousType.getPou().add(pouType1);
	typesType.setPous(pousType);
	projectType.setTypes(typesType);

	documentRoot.setProject(projectType);


I hope it is now clear!

Re: Extend a Model via ExtendedMetaData Interface [message #1091529 is a reply to message #1090587] Wed, 21 August 2013 16:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26051
Registered: July 2009
Senior Member
Max,

I'm not seeing attachments in Thunderbird. Maybe I see them in the web
forum; I'll check tomorrow.


On 20/08/2013 1:02 PM, Max Power wrote:
> Hello,
>
> the attachment from my previous post includes contains a test case. If
> you execute the main class in the PLCOpen package the file model.xml
> is automatically generated.
> The final xml file looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <project xmlns="plcopen.org/xml/tc6_0201">
> <types>
> <pous>
> <pou name="Pou Name">
> <addData>
> <data handleUnknown="implementation"
> name="3s-software.com/plcopenxml/pouinheritance">
> <_1:Inheritance>
> <Extends>MyType</Extends>
> </_1:Inheritance>
> </data>
> </addData>
> </pou>
> </pous>
> </types>
> </project>
>
>
> The Problem is the "_1:" prefix.
> This is the important code snippet:
>
> /*
> * Create content
> */
> ProjectType projectType = factory.createProjectType();
> TypesType typesType = factory.createTypesType();
> PousType pousType = factory.createPousType();
> AddData addData = factory.createAddData();
> PouType1 pouType1 = factory.createPouType1();
>
> pouType1.setName("Pou Name");
>
> // Add model extension code
> DataType1 dataType = factory.createDataType1();
> dataType.setName("3s-software.com/plcopenxml/pouinheritance");
> dataType.setHandleUnknown(HandleUnknownType.IMPLEMENTATION);
>
> InheritanceType inhertitanceType =
> codesysFactory.createInheritanceType();
> inhertitanceType.setExtends("MyType");
>
> // Here is the problem!!! ExtendedMetaData extendedMetaData
> = ExtendedMetaData.INSTANCE;
> Entry entry =
> FeatureMapUtil.createEntry(extendedMetaData.demandFeature(null, "",
> true), inhertitanceType);
> dataType.getAny().add(entry);
>
> addData.getData().add(dataType);
> pouType1.setAddData(addData);
> pousType.getPou().add(pouType1);
> typesType.setPous(pousType);
> projectType.setTypes(typesType);
>
> documentRoot.setProject(projectType);
>
>
> I hope it is now clear!
>
>
Re: Extend a Model via ExtendedMetaData Interface [message #1105718 is a reply to message #1091529] Tue, 10 September 2013 07:40 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26051
Registered: July 2009
Senior Member
Max,

Sorry for taking so long to follow up...

The problem here is that "Inheritance" is an anonymous complex type, so
it's not possible to serialize an instance of such a thing using an
xsi:type because that requires a named type. The serializer recognizes
this situation and determines the name of the enclosing element for that
complex type and uses that instead. But that element isn't a global
element declaration which further confuses the situation.

I'm not sure what to suggest, but the thing you're trying to
achieve---serialize an instance of Inheritance (an anonymous type) under
a wildcard---isn't achievable/expressible with the schemas you've defined.



On 21/08/2013 6:19 PM, Ed Merks wrote:
> Max,
>
> I'm not seeing attachments in Thunderbird. Maybe I see them in the
> web forum; I'll check tomorrow.
>
>
> On 20/08/2013 1:02 PM, Max Power wrote:
>> Hello,
>>
>> the attachment from my previous post includes contains a test case.
>> If you execute the main class in the PLCOpen package the file
>> model.xml is automatically generated.
>> The final xml file looks like this:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <project xmlns="plcopen.org/xml/tc6_0201">
>> <types>
>> <pous>
>> <pou name="Pou Name">
>> <addData>
>> <data handleUnknown="implementation"
>> name="3s-software.com/plcopenxml/pouinheritance">
>> <_1:Inheritance>
>> <Extends>MyType</Extends>
>> </_1:Inheritance>
>> </data>
>> </addData>
>> </pou>
>> </pous>
>> </types>
>> </project>
>>
>>
>> The Problem is the "_1:" prefix.
>> This is the important code snippet:
>>
>> /*
>> * Create content
>> */
>> ProjectType projectType = factory.createProjectType();
>> TypesType typesType = factory.createTypesType();
>> PousType pousType = factory.createPousType();
>> AddData addData = factory.createAddData();
>> PouType1 pouType1 = factory.createPouType1();
>>
>> pouType1.setName("Pou Name");
>>
>> // Add model extension code
>> DataType1 dataType = factory.createDataType1();
>> dataType.setName("3s-software.com/plcopenxml/pouinheritance");
>> dataType.setHandleUnknown(HandleUnknownType.IMPLEMENTATION);
>>
>> InheritanceType inhertitanceType =
>> codesysFactory.createInheritanceType();
>> inhertitanceType.setExtends("MyType");
>>
>> // Here is the problem!!! ExtendedMetaData
>> extendedMetaData = ExtendedMetaData.INSTANCE;
>> Entry entry =
>> FeatureMapUtil.createEntry(extendedMetaData.demandFeature(null, "",
>> true), inhertitanceType);
>> dataType.getAny().add(entry);
>>
>> addData.getData().add(dataType);
>> pouType1.setAddData(addData);
>> pousType.getPou().add(pouType1);
>> typesType.setPous(pousType);
>> projectType.setTypes(typesType);
>>
>> documentRoot.setProject(projectType);
>>
>>
>> I hope it is now clear!
>>
>>
>
Previous Topic:Parsing of ecore models
Next Topic:Differentiate between default value of the class and a loaded object
Goto Forum:
  


Current Time: Fri Sep 19 08:02:18 GMT 2014

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

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