Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Dynamic EMF: the feature 'xyz' is not a valid feature
Dynamic EMF: the feature 'xyz' is not a valid feature [message #430563] Wed, 27 May 2009 14:11 Go to next message
Eclipse UserFriend
Originally posted by: htran.infosys.tuwien.ac.at

This is a multi-part message in MIME format.
--------------050105010405060008080605
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Dear all,

I'm sorry to bring you back to the problem of "IllegalArgumentException:
The feature 'xyz' is not a valid feature" that have been sometimes
investigated before.

I try to make a small example concerning the problem I met.

The ecore model I used is named test.ecore.

test: EPackage (prefix=test, URI=http://test)
+-- A (EClass)
+--- b (EReference, EType=B)
+---B (EClass)


Before, I used Genmodel to generate model code in Java, then used these
Java code to create/serialize/de-serialize my model instance. This
worked perfectly, but no "schemaLocation" included in the serialization.

Therefore, I want to use EMF dynamic feature to create the model
instance without code generated by the Genmodel.

The code for dynamically creating a model instance is given below:

/*********************************************************** *****************/
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put(
"ecore", new XMIResourceFactoryImpl());
URI ecoreURI = URI.createFileURI("test.ecore");

ResourceSet resourceSet = new ResourceSetImpl();

Resource resource = resourceSet.getResource(ecoreURI, true);

EPackage mm = (EPackage) resource.getContents().get(0);

// get the meta-class A
EClass A = (EClass) mm.getEClassifier("A");

// create an instance of A
EObject a = mm.getEFactoryInstance().create(A);

// get the EStructuralFeature between A and B
Object a_b = A.eGet(A.getEStructuralFeature(0)); //(1)

boolean isSet = A.eIsSet(A.getEStructuralFeature(0)); //(2)
/*********************************************************** *****************/

Unfortunately, I confronted with the well-known exception at either (1)
or (2) (if remove (1)).

/*********************************************************** *****************/
Exception in thread "main" java.lang.IllegalArgumentException: The
feature 'b' is not a valid feature
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenGet(BasicEO bjectImpl.java:1062)
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1016)
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1004)
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:999)
at test.TestEMF.main(TestEMF.java:34)
/*********************************************************** *****************/


I guessed the exception is raised because the test.ecore package was not
properly registered. Then, I did a lot of searching around and tried a
number of similar hints/guidances for registering an Ecore package but
the problem is not solved.

Could you please give me some hints?

Thanks in advance for your time.

Regards,
Huy

PS: For your convenience, I attach the test.ecore and the Java code.

--------------050105010405060008080605
Content-Type: text/xml;
name="test.ecore"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="test.ecore"

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
nsURI="http://test" nsPrefix="test">
<eClassifiers xsi:type="ecore:EClass" name="A">
<eStructuralFeatures xsi:type="ecore:EReference" name="b" upperBound="-1" eType="#//B"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="B"/>
</ecore:EPackage>

--------------050105010405060008080605
Content-Type: text/x-java;
name="TestEMF.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="TestEMF.java"

package test;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

public class TestEMF {

public static void main(String[] args) {

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put(
"ecore", new XMIResourceFactoryImpl());

URI ecoreURI = URI.createFileURI("test.ecore");

ResourceSet resourceSet = new ResourceSetImpl();

Resource resource = resourceSet.getResource(ecoreURI, true);

EPackage mm = (EPackage) resource.getContents().get(0);

// get the meta-class A
EClass A = (EClass) mm.getEClassifier("A");

// create an instance of A
EObject a = mm.getEFactoryInstance().create(A);

// get the EStructuralFeature between A and B
Object a_b = A.eGet(A.getEStructuralFeature(0)); // IllegalArgumentException here

boolean isSet = A.eIsSet(A.getEStructuralFeature(0)); // and here

}

}

--------------050105010405060008080605--
Re: Dynamic EMF: the feature 'xyz' is not a valid feature [message #430564 is a reply to message #430563] Wed, 27 May 2009 14:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Huy,

Comments below.

Huy Tran wrote:
> Dear all,
>
> I'm sorry to bring you back to the problem of
> "IllegalArgumentException: The feature 'xyz' is not a valid feature"
> that have been sometimes investigated before.
>
> I try to make a small example concerning the problem I met.
>
> The ecore model I used is named test.ecore.
>
> test: EPackage (prefix=test, URI=http://test)
> +-- A (EClass)
> +--- b (EReference, EType=B)
> +---B (EClass)
>
>
> Before, I used Genmodel to generate model code in Java, then used
> these Java code to create/serialize/de-serialize my model instance.
> This worked perfectly, but no "schemaLocation" included in the
> serialization.
It's not difficult to have serialization produce one if you like.
You'd use OPTION_SCHEMA_LOCATION to serialize and you'd specialize the
XyzPackageImpl to override createResource to use the schemaLocation
rather than the nsURI for the package's resource.
>
> Therefore, I want to use EMF dynamic feature to create the model
> instance without code generated by the Genmodel.
>
> The code for dynamically creating a model instance is given below:
>
> /*********************************************************** *****************/
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put(
> "ecore", new XMIResourceFactoryImpl());
> URI ecoreURI = URI.createFileURI("test.ecore");
>
> ResourceSet resourceSet = new ResourceSetImpl();
>
> Resource resource = resourceSet.getResource(ecoreURI, true);
>
> EPackage mm = (EPackage) resource.getContents().get(0);
>
> // get the meta-class A
> EClass A = (EClass) mm.getEClassifier("A");
>
> // create an instance of A
> EObject a = mm.getEFactoryInstance().create(A);
>
> // get the EStructuralFeature between A and B
> Object a_b = A.eGet(A.getEStructuralFeature(0)); //(1)
Shouldn't this be "a" not "A"?
>
> boolean isSet = A.eIsSet(A.getEStructuralFeature(0)); //(2)
> /*********************************************************** *****************/
>
>
> Unfortunately, I confronted with the well-known exception at either
> (1) or (2) (if remove (1)).
>
> /*********************************************************** *****************/
>
> Exception in thread "main" java.lang.IllegalArgumentException: The
> feature 'b' is not a valid feature
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenGet(BasicEO bjectImpl.java:1062)
>
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1016)
>
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1004)
>
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:999)
>
> at test.TestEMF.main(TestEMF.java:34)
> /*********************************************************** *****************/
>
>
>
> I guessed the exception is raised because the test.ecore package was
> not properly registered.
No, it's because you're trying to look up a feature of A in an EClass.
> Then, I did a lot of searching around and tried a number of similar
> hints/guidances for registering an Ecore package but the problem is
> not solved.
>
> Could you please give me some hints?
>
> Thanks in advance for your time.
>
> Regards,
> Huy
>
> PS: For your convenience, I attach the test.ecore and the Java code.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic EMF: the feature 'xyz' is not a valid feature [message #430567 is a reply to message #430564] Wed, 27 May 2009 14:54 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: htran.infosys.tuwien.ac.at

Hi Ed,

Thanks for your very quick and precise response.

I quote the relevant code for readability:

EObject a = mm.getEFactoryInstance().create(A);
Object a_b = A.eGet(A.getEStructuralFeature(0));

You're right that I should look up a feature of "a", not "A" here.
That's my craziness ;(

Unfortunately, "a" is an EObject (more precisely, "a" is a
DynamicEObjectImpl instance). Is there any method to look up its
features like "getEStructuralFeature()" of an EClass. For instance, how
can I exactly look up the feature "b" of "a" in this case?

Regards,
Huy
Re: Dynamic EMF: the feature 'xyz' is not a valid feature [message #430569 is a reply to message #430567] Wed, 27 May 2009 15:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Huy,

Comments below.

Huy Tran wrote:
> Hi Ed,
>
> Thanks for your very quick and precise response.
>
> I quote the relevant code for readability:
>
> EObject a = mm.getEFactoryInstance().create(A);
> Object a_b = A.eGet(A.getEStructuralFeature(0));
>
> You're right that I should look up a feature of "a", not "A" here.
> That's my craziness ;(
>
> Unfortunately, "a" is an EObject (more precisely, "a" is a
> DynamicEObjectImpl instance). Is there any method to look up its
> features like "getEStructuralFeature()" of an EClass.
a.eClass().getEAllStructuralFeatures().
> For instance, how can I exactly look up the feature "b" of "a" in this
> case?
a.eGet(a.eClass().getEStructuralFeature("b"))
>
> Regards,
> Huy


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic EMF: the feature 'xyz' is not a valid feature [message #430570 is a reply to message #430569] Wed, 27 May 2009 15:09 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: htran.infosys.tuwien.ac.at

Hi Ed,

Many thanks for your helps. I totally forgot the magic eClass() method.

Regards,
Huy

Ed Merks wrote:
> Huy,
>
> Comments below.
>
> Huy Tran wrote:
>> Hi Ed,
>>
>> Thanks for your very quick and precise response.
>>
>> I quote the relevant code for readability:
>>
>> EObject a = mm.getEFactoryInstance().create(A);
>> Object a_b = A.eGet(A.getEStructuralFeature(0));
>>
>> You're right that I should look up a feature of "a", not "A" here.
>> That's my craziness ;(
>>
>> Unfortunately, "a" is an EObject (more precisely, "a" is a
>> DynamicEObjectImpl instance). Is there any method to look up its
>> features like "getEStructuralFeature()" of an EClass.
> a.eClass().getEAllStructuralFeatures().
>> For instance, how can I exactly look up the feature "b" of "a" in this
>> case?
> a.eGet(a.eClass().getEStructuralFeature("b"))
>>
>> Regards,
>> Huy
How to handle schemaLocation in case of subpackages [message #430578 is a reply to message #430564] Wed, 27 May 2009 17:29 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: htran.infosys.tuwien.ac.at

Hi Ed,

Concerning you recommendation to modify XyzPackageImpl to override
createResource(), I successfully tried this option before. However, I
got stuck in case the ecore file contains many subpackages. For
instance, in the test.ecore:

Test (EPackage, nsURI: http://test)
|
+----eSubpackage---Sp1 (EPackage, nsURI: http://test/sp1)
| +---A (EClass)
|
+----eSubpackage---Sp2 (EPackage, nsURI: http://test/sp1)
+---B (EClass)

I used Genmodel to generate the Java code, then, overrode the
createResource() of TestPackageImpl, Sp1PackageImpl, Sp2PackageImpl,
etc., as following:

@Override
protected Resource createResource(String uri) {
return
super.createResource("platform:/resource/test-projtect/src/test.ecore ");
}

This approach worked well and correctly generated the schemaLocation
when I serialized an instance of the EPackage Test (i.e., the root) and
opened it using the "EMF Generic Editor" or the "Sample Reflective Ecore
Model Editor". The corresponding generated schemaLocation is:

xsi:schemaLocation="http://test/
platform:/resource/test-project/src/test/mm.ecore"

However, as I serialized an instance of any subpackage, let say, Sp1,
the schemaLocation is different at the namespace URI, but the location
is, of course, test.ecore.

xsi:schemaLocation="http://test/sp1
platform:/resource/test-project/src/test/mm.ecore"

The above editors could not open the instance of Sp1 because they could
not find class A within the Sp1 subpackage.

============================================================ ================
org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'A' not found.
(platform:/resource/test-project/src/test/test.sp1.xmi, 5, 23)
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.validateCreateObje ctFromFactory(XMLHandler.java:2229)
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.validateCreateObje ctFromFactory(XMLHandler.java:2220)
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1318)
......
......
============================================================ ================


Is this the problem of the editors or I did something wrong with the
schemaLocation?

Thanks in advance.

Regards,
Huy
Re: How to handle schemaLocation in case of subpackages [message #430579 is a reply to message #430578] Wed, 27 May 2009 17:47 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Huy,

Comments below.

Huy Tran wrote:
> Hi Ed,
>
> Concerning you recommendation to modify XyzPackageImpl to override
> createResource(), I successfully tried this option before. However, I
> got stuck in case the ecore file contains many subpackages. For
> instance, in the test.ecore:
>
> Test (EPackage, nsURI: http://test)
> |
> +----eSubpackage---Sp1 (EPackage, nsURI: http://test/sp1)
> | +---A (EClass)
> |
> +----eSubpackage---Sp2 (EPackage, nsURI: http://test/sp1)
> +---B (EClass)
>
> I used Genmodel to generate the Java code, then, overrode the
> createResource() of TestPackageImpl, Sp1PackageImpl, Sp2PackageImpl,
> etc., as following:
>
> @Override
> protected Resource createResource(String uri) {
> return
> super.createResource("platform:/resource/test-projtect/src/test.ecore ");
> }
Shouldn't they have different locations? If they all point at the same
location, there can't be three different packages...
>
> This approach worked well and correctly generated the schemaLocation
> when I serialized an instance of the EPackage Test (i.e., the root)
> and opened it using the "EMF Generic Editor" or the "Sample Reflective
> Ecore Model Editor". The corresponding generated schemaLocation is:
>
> xsi:schemaLocation="http://test/
> platform:/resource/test-project/src/test/mm.ecore"
>
> However, as I serialized an instance of any subpackage, let say, Sp1,
> the schemaLocation is different at the namespace URI, but the location
> is, of course, test.ecore.
>
> xsi:schemaLocation="http://test/sp1
> platform:/resource/test-project/src/test/mm.ecore"
>
> The above editors could not open the instance of Sp1 because they
> could not find class A within the Sp1 subpackage.
>
> ============================================================ ================
>
> org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'A' not found.
> (platform:/resource/test-project/src/test/test.sp1.xmi, 5, 23)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.validateCreateObje ctFromFactory(XMLHandler.java:2229)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.validateCreateObje ctFromFactory(XMLHandler.java:2220)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1318)
>
> .....
> .....
> ============================================================ ================
>
>
>
> Is this the problem of the editors or I did something wrong with the
> schemaLocation?
I very much dislike nested packages and wish I'd never allowed them into
the model. They're a perfect example of syntactic sugar that turns out
to be syntactic rat poison.
>
> Thanks in advance.
>
> Regards,
> Huy


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:loading XML without namepace declaration
Next Topic:Serialization Order
Goto Forum:
  


Current Time: Sat Apr 27 00:12:57 GMT 2024

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

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

Back to the top