Unexpected results with EObject::eGet(..) [message #1786453] |
Thu, 03 May 2018 13:55 |
Fernando Macias 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:
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
End
Any idea what may be causing this?
Cheers,
Fernando
|
|
|
|
Re: Unexpected results with EObject::eGet(..) [message #1786455 is a reply to message #1786454] |
Thu, 03 May 2018 15:19 |
Ed Merks 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/
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02671 seconds