Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Ecore model namespace from EObject
Ecore model namespace from EObject [message #1304461] Sat, 19 April 2014 22:30 Go to next message
Bartosz Popiela is currently offline Bartosz PopielaFriend
Messages: 21
Registered: March 2012
Junior Member
Hi,
I have a following problem. How can I get ecore model namespace from EObject.
I have an instance of my.domain.MyElement (extends EObjectImpl) definied in "my.domain" namespace in ecore model.
my.domain.MyElement extends super.domain.MyElement. Is there an easy way to get "my.domain" namespace form my.domain.MyElement?

[Updated on: Sat, 19 April 2014 22:30]

Report message to a moderator

Re: Ecore model namespace from EObject [message #1304930 is a reply to message #1304461] Sun, 20 April 2014 04:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Bartosz,

Without the GenModel, you can't know all the details of how the model
was generated. But for a generated model you can know about the
results, i.e., eObject.eClass().getInstanceClassName() will yield the
qualified name of the interface that was generated. Using
org.eclipse.emf.ecore.plugin.EcorePlugin.getEPackageNsURIToGenModelLocationMap(boolean)
you could try to find the GenModel that was used to generate your model
(assuming that's registered in the plugin.xml and retained in the binary
bundle), and get all the details from that.

On 20/04/2014 12:30 AM, Bartosz Popiela wrote:
> Hi,
> I have following problem. How can I get ecore model namespace from
> EObject.
> I have an instance of my.domain.MyElement (extends EObjectImpl)
> definied in "my.domain" namespace in ecore model.
> my.domain.MyElement extends super.domain.MyElement. Is there an easy
> way to get "my.domain" namespace form my.domain.MyElement?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Ecore model namespace from EObject [message #1307322 is a reply to message #1304461] Mon, 21 April 2014 13:38 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
You can try these helper methods:

	public static String determineFullyQualifiedPackageName(EObject eObject) {
		return eObject == null ? null
				: determineFullyQualifiedPackageName(eObject.eClass());
	}

	public static String determineFullyQualifiedPackageName(EClass eclass) {
		List<String> packageNames = new ArrayList<String>();
		EObject context = eclass;

		while ((context = context.eContainer()) != null
				&& context instanceof EPackage) {
			packageNames.add(((ENamedElement) context).getName());
		}

		StringBuilder ret = new StringBuilder();

		for (int i = packageNames.size() - 1; i >= 0; i--) {
			ret.append(packageNames.get(i));

			if (i > 0) {
				ret.append(".");
			}
		}

		return ret.length() == 0 ? null : ret.toString();
	}



Bartosz Popiela wrote on Sun, 20 April 2014 00:30
Hi,
I have a following problem. How can I get ecore model namespace from EObject.
I have an instance of my.domain.MyElement (extends EObjectImpl) definied in "my.domain" namespace in ecore model.
my.domain.MyElement extends super.domain.MyElement. Is there an easy way to get "my.domain" namespace form my.domain.MyElement?

Re: Ecore model namespace from EObject [message #1307408 is a reply to message #1307322] Mon, 21 April 2014 14:49 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
Hm, I guess my suggestion wont work (in your case) as it does not take the generated package name prefix into account (Ed's suggestion is the proper way to go)...

Erdal Karaca wrote on Mon, 21 April 2014 15:38
You can try these helper methods:

	public static String determineFullyQualifiedPackageName(EObject eObject) {
		return eObject == null ? null
				: determineFullyQualifiedPackageName(eObject.eClass());
	}

	public static String determineFullyQualifiedPackageName(EClass eclass) {
		List<String> packageNames = new ArrayList<String>();
		EObject context = eclass;

		while ((context = context.eContainer()) != null
				&& context instanceof EPackage) {
			packageNames.add(((ENamedElement) context).getName());
		}

		StringBuilder ret = new StringBuilder();

		for (int i = packageNames.size() - 1; i >= 0; i--) {
			ret.append(packageNames.get(i));

			if (i > 0) {
				ret.append(".");
			}
		}

		return ret.length() == 0 ? null : ret.toString();
	}



Bartosz Popiela wrote on Sun, 20 April 2014 00:30
Hi,
I have a following problem. How can I get ecore model namespace from EObject.
I have an instance of my.domain.MyElement (extends EObjectImpl) definied in "my.domain" namespace in ecore model.
my.domain.MyElement extends super.domain.MyElement. Is there an easy way to get "my.domain" namespace form my.domain.MyElement?


Re: Ecore model namespace from EObject [message #1307439 is a reply to message #1307408] Mon, 21 April 2014 15:11 Go to previous messageGo to next message
Bartosz Popiela is currently offline Bartosz PopielaFriend
Messages: 21
Registered: March 2012
Junior Member
Thank you for response! Actually I solved my problem in a different way without getting the domain.
Re: Ecore model namespace from EObject [message #1308656 is a reply to message #1307439] Tue, 22 April 2014 07:28 Go to previous message
Jan Reimann is currently offline Jan ReimannFriend
Messages: 140
Registered: July 2009
Senior Member
Hi Bartosz,
it would be nice if you could share your solution.

cheers,
Jan

Bartosz Popiela wrote:
> Thank you for response! Actually I solved my problem in a different way
> without getting the domain.
Previous Topic:[Xcore] How to allow null in an EList?
Next Topic:SVG images in editor which is generated using GMF
Goto Forum:
  


Current Time: Sat Apr 27 04:44:34 GMT 2024

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

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

Back to the top