Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Retrieving eObject From Ecore File(EObject from ECore File)
Retrieving eObject From Ecore File [message #634704] Fri, 22 October 2010 19:08 Go to next message
Animesh Kumar is currently offline Animesh KumarFriend
Messages: 79
Registered: September 2010
Location: Bangalore
Member
Hi,

I was trying to get EObjects from an Ecore file but could not succeed as i was getting a class cast exception.
I used the following code:-

String pathName = textForModelSelection.getText();// Full path of  a .ecore file
Resource fileResource;
ResourceSet rSet = new ResourceSetImpl();
if (rSet != null) {
	uri = URI.createFileURI(pathName);
	fileResource = rSet.getResource(uri, true);
	EPackage ecorepack = (EPackage) fileResource.getContents().get(0);
	int i = 0;
	List<EClass> classes = new ArrayList<EClass>();
	EClassifier classifier = ecorepack.getEClassifiers().get(i);
			while (classifier != null) {
				if (classifier instanceof EClass) {
					EClass modelClass = (EClass) ecorepack.getEClassifiers().get(i);
					classes.add(modelClass);
				}
				classifier = ecorepack.getEClassifiers().get(i++);
			}

		}


I got the following Exception:

java.lang.ClassCastException: org.eclipse.emf.ecore.impl.EEnumImpl cannot be cast to org.eclipse.emf.ecore.EClass

Please suggest me how else can i retrieve EObjects or EClass from .ecore file.

Also i have one more constraint and that is that i want to add only those classes who don't have any supertypes( classes that were not extended by any other class).


Regards,
Animesh

[Updated on: Fri, 22 October 2010 19:10]

Report message to a moderator

Re: Retrievinf eObject From Ecore File [message #634714 is a reply to message #634704] Fri, 22 October 2010 19:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Animesh,

Comments below.

Animesh Kumar wrote:
> Hi,
>
> I was trying to get EObjects from an Ecore file but could not succeed
> as i was getting a class cast exception.
Have you been consulting with your best friend, the debugger?
> I used the following code:-
>
>
> String pathName = textForModelSelection.getText();// Full path of a
> .ecore file
> Resource fileResource;
> ResourceSet rSet = new ResourceSetImpl();
> if (rSet != null) {
> uri = URI.createFileURI(pathName);
> fileResource = rSet.getResource(uri, true);
> EPackage ecorepack = (EPackage) fileResource.getContents().get(0);
> int i = 0;
> List<EClass> classes = new ArrayList<EClass>();
> EClassifier classifier = ecorepack.getEClassifiers().get(i);
> while (classifier != null) {
> if (classifier instanceof EClass) {
> EClass modelClass = (EClass)
> ecorepack.getEClassifiers().get(i);
> classes.add(modelClass);
> }
> classifier = ecorepack.getEClassifiers().get(i++);
Did you intend to do ++i?
> }
>
> }
>
>
> I got the following Exception:
>
> java.lang.ClassCastException: org.eclipse.emf.ecore.impl.EEnumImpl
> cannot be cast to org.eclipse.emf.ecore.EClass
>
> Please suggest me how else can i retrieve EObjects or EClass from
> .ecore file.
>
> Also i have one more constraint and that is that i want to add only
> those classes who don't have any supertypes, classes that were not
> extended by any other class.
You'll want to check if getESuperTypes is empty.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Retrievinf eObject From Ecore File [message #634830 is a reply to message #634714] Sun, 24 October 2010 06:47 Go to previous messageGo to next message
Animesh Kumar is currently offline Animesh KumarFriend
Messages: 79
Registered: September 2010
Location: Bangalore
Member
Thanks for the reply. That was a silly mistake of mine. But about the second question i had asked:-

I want list of only those classes which are not inherited by any other class.
For ex: I am working on the Extended Library Model.... In that model there are numerous classes like Library, Book, CirculatingItem, Person, Employee, Borrower etc. But I want to retrieve only classes like Library, Book, Employee and Borrower and don't want classes like CircultatingItem and Person. How can i do this???


Regards,
Animesh
Re: Retrievinf eObject From Ecore File [message #634855 is a reply to message #634830] Sun, 24 October 2010 16:24 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Animesh,

Comments below.

Animesh Kumar wrote:
> Thanks for the reply. That was a silly mistake of mine. But about the
> second question i had asked:-
>
> I want list of only those classes which are not inherited by any other
> class.
Oh, leaf classes, so to speak.
> For ex: I am working on the Extended Library Model.... In that model
> there are numerous classes like Library, Book, CirculatingItem,
> Person, Employee, Borrower etc. But I want to retrieve only classes
> like Library, Book, Employee and Borrower and don't want classes like
> CircultatingItem and Person. How can i do this???
First consider your comment about "inherited by any other class".
You'll need to define what that means. I.e., what are all classes?
Once you bound that space, you can create a set of all classes, iterate
over them, and remove from that set, the getEAllSuperTypes of each and
every class in the set. The ones left in the set at the end, will be
the ones that don't appear in any EClass' eAllSuperTypes.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Saving two eResource
Next Topic:Query2 query to find all the references of a eobject
Goto Forum:
  


Current Time: Wed Apr 24 20:27:44 GMT 2024

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

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

Back to the top