Skip to main content



      Home
Home » Modeling » EMF » Ecore model namespace from EObject
Ecore model namespace from EObject [message #1304461] Sat, 19 April 2014 18:30 Go to next message
Eclipse UserFriend
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 18:30] by Moderator

Re: Ecore model namespace from EObject [message #1304930 is a reply to message #1304461] Sun, 20 April 2014 00:52 Go to previous messageGo to next message
Eclipse UserFriend
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?
Re: Ecore model namespace from EObject [message #1307322 is a reply to message #1304461] Mon, 21 April 2014 09:38 Go to previous messageGo to next message
Eclipse UserFriend
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 10:49 Go to previous messageGo to next message
Eclipse UserFriend
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 11:11 Go to previous messageGo to next message
Eclipse UserFriend
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 03:28 Go to previous message
Eclipse UserFriend
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: Thu Jul 10 02:56:12 EDT 2025

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

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

Back to the top