Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Efficient check on EDataType(Trying to find out which EDataType you are dealing with)
Efficient check on EDataType [message #1740847] Thu, 18 August 2016 23:28 Go to next message
Johannes Gross is currently offline Johannes GrossFriend
Messages: 28
Registered: July 2009
Junior Member
I am parsing some JSON and putting it in a EMF based model, and for literals i have to distinguish to set them.
Currently i am doing:
if (type.getName().equals("Real")) {
        newobject.eSet(sf, Double.valueOf((String) ((JSONObject) je).get(property)));
} else if (type.getName().equals("Integer")) {
	newobject.eSet(sf, Integer.valueOf((String) ((JSONObject) je).get(property)));
} else {
	newobject.eSet(sf, ((JSONObject) je).get(property));
}

But i dont like it because string comparison is expensive and it should be an instanceof or equals object check. I could not get it to work however. EcorePackage.Literals.EString is just an int but not the type so i can't compare. As well as (type instanceof String) or EString didnt work for me. Any hints what the right approach is?

Thanks,
Joe

[Updated on: Wed, 24 August 2016 17:53]

Report message to a moderator

Re: Efficiant check on EDataType [message #1740872 is a reply to message #1740847] Fri, 19 August 2016 09:15 Go to previous messageGo to next message
Aurélien Mora is currently offline Aurélien MoraFriend
Messages: 38
Registered: July 2014
Member
Hello Joe,

I'm not sure to fully understand, what is your "type" variable ? an EMF object ? Anyway, if you want a quick comparison with no String, you probably should use an EEnum. You'll be able to use it like a java enum :

EType type = myObject.getType();
switch(type)
{
case INTEGER:
	break;
case REAL:
	break;
default:
	break;
}

[Updated on: Fri, 19 August 2016 09:56]

Report message to a moderator

Re: Efficiant check on EDataType [message #1740882 is a reply to message #1740872] Fri, 19 August 2016 10:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31900
Registered: July 2009
Senior Member
It seems NNTP is broken so reposting this on the web forum.
Johannes,

Comments below.


On 19.08.2016 01:28, Johannes Mising name wrote:
> I am parsing some JSON and putting it in a EMF based model, and for literals i have to distinguish to set them.
> Currently i am doing:
>
> if (type.getName().equals("Real")) {
So type here an an EDataType presumably.
> newobject.eSet(sf, Double.valueOf((String) ((JSONObject) je).get(property)));
> } else if (type.getName().equals("Integer")) {
> newobject.eSet(sf, Integer.valueOf((String) ((JSONObject) je).get(property)));
> } else {
> newobject.eSet(sf, ((JSONObject) je).get(property));
> }
>
> But i dont like it because string comparison is expensive and it should be an instanceof or equals object check.
Well for Ecore you can assume identity. So testing type == XyzPackage.Literals.ABC is fine and of course highly efficient.
> I could not get it to work however. EcorePackage.Literals.EString is just an int but not the type so i can't compare.
No, your confusing EcorePackage.ESTRING, which is an int with EcorePackage.Literals.ESTRING, which is an EDataType.
> As well as (type instanceof String) or EString didnt work for me. Any hints what the right approach is?
I think better would be to use EcoreUtil.createFromString(type, (String) ((JSONObject) je).get(property))) in all cases; the factory knows how to convert a literal to a value of the data type. Of course I don't understand if it will always be a String, i.e., without your model, I don't know which cases the final else covers.
> Thanks,
> Joe



Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Efficiant check on EDataType [message #1741088 is a reply to message #1740882] Mon, 22 August 2016 22:21 Go to previous messageGo to next message
Johannes Gross is currently offline Johannes GrossFriend
Messages: 28
Registered: July 2009
Junior Member
Hi Ed, Aurelien,

thanks for your input! This resolved the issue.
I forgot to mention that type is:

EDataType type = ((EAttribute) sf).getEAttributeType();

where sf is my StructuralFeature that i am currently working with.
So Ed, yes the EcoreUtil works fine and does what it should, however I still would like to understand why i can't:

EDataType myType = EcorePackage.Literals.ESTRING;
if (type instanceof myType) {

}
This gives me a compilation error: myType can't be resolved to a type.
Really weird. If i put the EcorePackage.Literals.ESTRING directly in the check, its the same error. This is only our of interest, so if you have an idea what the reason could be, let me know.

Thanks,
Joe
Re: Efficiant check on EDataType [message #1741110 is a reply to message #1741088] Tue, 23 August 2016 07:42 Go to previous messageGo to next message
Aurélien Mora is currently offline Aurélien MoraFriend
Messages: 38
Registered: July 2014
Member
Hello,

Actually, EcorePackage.Literals.ESTRING is not a type, it is an instance of EDataType, so you can't make an instanceof with that. If you need to make an instanceof, just use the interface EString.

But, in your example, both type and myType are not type of EStrings, they are EDataTypes. So I think the only comparison you can do is :

EDataType type = ((EAttribute) sf).getEAttributeType();
EDataType myType = EcorePackage.Literals.ESTRING;
if (type == myType) {
}

[Updated on: Tue, 23 August 2016 07:53]

Report message to a moderator

Re: Efficiant check on EDataType [message #1741204 is a reply to message #1741110] Tue, 23 August 2016 16:19 Go to previous message
Johannes Gross is currently offline Johannes GrossFriend
Messages: 28
Registered: July 2009
Junior Member
Got it, thanks Aurelien!
Re: Efficiant check on EDataType [message #1741988 is a reply to message #1740847] Fri, 19 August 2016 05:18 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 31900
Registered: July 2009
Senior Member
Johannes,

Comments below.


On 19.08.2016 01:28, Johannes Mising name wrote:
> I am parsing some JSON and putting it in a EMF based model, and for
> literals i have to distinguish to set them.
> Currently i am doing:
>
> if (type.getName().equals("Real")) {
So type here an an EDataType presumably.
> newobject.eSet(sf, Double.valueOf((String) ((JSONObject)
> je).get(property)));
> } else if
> (type.getName().equals("Integer")) {
> newobject.eSet(sf, Integer.valueOf((String) ((JSONObject)
> je).get(property)));
> } else {
> newobject.eSet(sf, ((JSONObject) je).get(property));
> }
>
> But i dont like it because string comparison is expensive and it
> should be an instanceof or equals object check.
Well for Ecore you can assume identity. So testing type ==
XyzPackage.Literals.ABC is fine and of course highly efficient.
> I could not get it to work however. EcorePackage.Literals.EString is
> just an int but not the type so i can't compare.
No, your confusing EcorePackage.ESTRING, which is an int with
EcorePackage.Literals.ESTRING, which is an EDataType.
> As well as (type instanceof String) or EString didnt work for me. Any
> hints what the right approach is?
I think better would be to use EcoreUtil.createFromString(type, (String)
((JSONObject) je).get(property))) in all cases; the factory knows how to
convert a literal to a value of the data type. Of course I don't
understand if it will always be a String, i.e., without your model, I
don't know which cases the final else covers.
> Thanks,
> Joe


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:check whether the model is loaded properly or not
Next Topic:Memory leak with EditingDomainActionBarContributor?
Goto Forum:
  


Current Time: Sun Jun 13 14:33:45 GMT 2021

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

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

Back to the top