Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Unexpected results with EObject::eGet(..)
Unexpected results with EObject::eGet(..) [message #1786453] Thu, 03 May 2018 13:55 Go to next message
Fernando Macias is currently offline Fernando MaciasFriend
Messages: 20
Registered: March 2014
Junior Member
Hello,

I have been experiencing problems when trying to get the value of attributes that may be set or not, when these values are "0" for EInt, "false" for EBoolean, etc.

I load a model from its XMI file, where I have an eObject with two attributes of type EInt called "start" and "end" and with unsettable = true. The first one is not set and the second one is set to "0"; if I open the file as text, I can see that the value of "end" is explicitly set:

index.php/fa/32761/0/

When I parse the values from the file, I need to distinguish between both cases. However, the result of calling EObject::eGet(..) and EObject::eIsSet(..) is always the same, as shown below:

Start

index.php/fa/32759/0/

End

index.php/fa/32760/0/

Any idea what may be causing this?

Cheers,

Fernando
Re: Unexpected results with EObject::eGet(..) [message #1786454 is a reply to message #1786453] Thu, 03 May 2018 14:24 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

If you want 3-valued Booleans use EBooleanObject. Similary use EIntegerObject for an N+1-valued Integer. There is no support for N+1 valued ENums.

Regards

Ed Willink
Re: Unexpected results with EObject::eGet(..) [message #1786455 is a reply to message #1786454] Thu, 03 May 2018 15:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Ed, Fernando is asking about unsettable features, in particular when using them with primitive types. It seems unhelpful to suggest he use wrapper types instead, because that isn't what he asked about. Your assertion about EEnums is also incorrect:

https://wiki.eclipse.org/EMF/Recipes#Recipe:_Generating_enumeration-based_attributes_that_support_null

Fernando,

I'm quite sure this should work properly, i.e., the following does what I would expect, i.e., an attribute that's eIsSet true before saving will be eIsSet true after loading:
package test.dynamic.unsettable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
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.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

public class Test {

	public static void main(String[] args) throws Exception {
		EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
		ePackage.setNsURI("test");
		ePackage.setName("test");
		ePackage.setNsPrefix("test");

		EClass eClass = EcoreFactory.eINSTANCE.createEClass();
		eClass.setName("Test");
		ePackage.getEClassifiers().add(eClass);

		EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
		eAttribute.setName("test");
		eAttribute.setUnsettable(true);
		eAttribute.setEType(EcorePackage.Literals.EBOOLEAN);
		eClass.getEStructuralFeatures().add(eAttribute);

		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore",
				new EcoreResourceFactoryImpl());
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
		resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage);

		Resource ecoreResource = resourceSet.createResource(URI.createURI("test.ecore"));
		ecoreResource.getContents().add(ePackage);

		EObject eObject = EcoreUtil.create(eClass);
		System.out.println("isSet=" + eObject.eIsSet(eAttribute));
		eObject.eSet(eAttribute, false);
		System.out.println("isSet=" + eObject.eIsSet(eAttribute));

		Resource xmiResource = resourceSet.createResource(URI.createURI("test.xmi"));
		xmiResource.getContents().add(eObject);
		xmiResource.save(System.out, null);

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		xmiResource.save(out, null);

		Resource xmiResource2 = resourceSet.createResource(URI.createURI("test2.xmi"));
		xmiResource2.load(new ByteArrayInputStream(out.toByteArray()), null);

		EObject eObject2 = xmiResource2.getContents().get(0);
		System.out.println("isSet=" + eObject2.eIsSet(eAttribute));
	}

}

If you experience otherwise, I would need to see a test case that reproduces the problem.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Unexpected results with EObject::eGet(..) [message #1786696 is a reply to message #1786455] Wed, 09 May 2018 14:41 Go to previous messageGo to next message
Fernando Macias is currently offline Fernando MaciasFriend
Messages: 20
Registered: March 2014
Junior Member
Thank you very much to both.

After trying the toy example and comparing it with my code's behaviour, I realised it was my mistake for not handling IResources properly (I was obtaining an older version of the same model without the attributes). So once I ruled out that it was the APIs fault, I was able to look in the right place and fix it.

Best regards,

Fernando
Re: Unexpected results with EObject::eGet(..) [message #1786702 is a reply to message #1786696] Wed, 09 May 2018 15:44 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Thanks for following up with your findings.

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Customize emf generated genmodel to modify generated model code
Next Topic:Ecoreutil.copier
Goto Forum:
  


Current Time: Fri Apr 19 10:44:49 GMT 2024

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

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

Back to the top