Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Xcore] instanceof / typeof / casting problem when working with dynamic Instances
[Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751230] Thu, 05 January 2017 21:05 Go to next message
Joel Greenyer is currently offline Joel GreenyerFriend
Messages: 19
Registered: October 2014
Junior Member
Hi all,
there seems to be a problem with using the instanceof / typeof as well as casts ("as") operators in Xcore when working with dynamic instances.

Consider the following example:

package zoo

import org.eclipse.emf.common.util.BasicEList
import org.eclipse.emf.common.util.EList

class Zoo{
	contains Animal[] animals
	
	refers derived volatile unsettable Reptile[] poisounousReptiles get{
		var EList<Reptile> oldReptiles = new BasicEList<Reptile>()
		for(animal:animals){
			if (animal instanceof Reptile){
				if(animal.poisounous)
					oldReptiles.add(animal)					
			}
		}
		return oldReptiles
	}

	refers derived volatile unsettable Reptile[] poisounousReptiles2 get{
		animals.filter(typeof(Reptile)).filter(r | r.poisounous).toEList
	}	
}

abstract class Animal{
	String name
}
abstract class Mammal extends Animal{}
abstract class Reptile extends Animal{
	boolean poisounous
}
class Elephant extends Mammal{}
class Snake extends Reptile{}


The uses of instanceof and typeof do not work when loading a dynamic model. When loading a model based on generated Java classes, there is no problem.

When I look at the generated Java code, the problem becomes obvious. An instanceof operation translates directly to a Java instanceof:

...
if ((animal instanceof Reptile)) {
...


When working with a dynamic model, animal will be a DynamicEObject and Reptile will still be a Java interface that is obviously not implemented by DynamicEObject, so the simple instanceof does not do it.

I could of course check that the animal.eClass.name == "Reptile", but then it turns out that casting, which on the Java side will be translated to
... ((Reptile)animal).IsPoisounous() ...

will have the same problem.

Are there any solution to this? Is this a known limitation? (I could not find any related post,..)

Thanks

Joel
Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751249 is a reply to message #1751230] Fri, 06 January 2017 10:06 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
Messages: 89
Registered: December 2015
Location: Grenoble, France
Member
Hi

How are you loading your dynamic model?

If you use the generated package, you should not have instances of DynamicEObject but rather instances of the actual generated implementations (i.e. ReptileImpl).

Are you executing an eclipse plugin or an standalone application?

I had similar problems in my standalone application, and were related to wrong manual package registration.

Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751252 is a reply to message #1751249] Fri, 06 January 2017 10:29 Go to previous messageGo to next message
Joel Greenyer is currently offline Joel GreenyerFriend
Messages: 19
Registered: October 2014
Junior Member
German,
comments below.

German Vega wrote on Fri, 06 January 2017 10:06


How are you loading your dynamic model?



see the example project that was attached to my initial post. Just open the MyZoo.xmi in the Sample Reflective Editor and see that the derived values will not be shown. In my application I'm loading the models in the same way.

If you launch another eclipse runtime workbench, so that the generated code will be loaded for the Zoo model, and you import the zoo project there as well, and then open the MyZoo model, the derived values will be shown.


German Vega wrote on Fri, 06 January 2017 10:06

If you use the generated package, you should not have instances of DynamicEObject but rather instances of the actual generated implementations (i.e. ReptileImpl).


For our application it is important that we can work with dynamic instances.

German Vega wrote on Fri, 06 January 2017 10:06

Are you executing an eclipse plugin or an standalone application?


All within Eclipse

Quote:

I had similar problems in my standalone application, and were related to wrong manual package registration.


Thanks

Joel
Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751256 is a reply to message #1751252] Fri, 06 January 2017 12:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31900
Registered: July 2009
Senior Member
Yes, I can't imagine that could possibly work with the XcoreInterpreter as it exists today. The interpreter would definitely need specializations for XCastedExpression and XInstanceOfExpression which could know the corresponding EClass, if there is one, and use org.eclipse.emf.ecore.EClassifier.isInstance(Object) for such testing in that case. Feel free to open a Bugzilla.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751258 is a reply to message #1751256] Fri, 06 January 2017 13:56 Go to previous messageGo to next message
Joel Greenyer is currently offline Joel GreenyerFriend
Messages: 19
Registered: October 2014
Junior Member
Hi Ed,
thanks for clarifying.

I filed a bug report.

-- As you pointed me to the XcoreInterpreter, I see that the problem is rather in the XbaseInterpreter, which the XcoreInterpreter extends...

regards,
Joel
Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751261 is a reply to message #1751258] Fri, 06 January 2017 14:57 Go to previous messageGo to next message
Joel Greenyer is currently offline Joel GreenyerFriend
Messages: 19
Registered: October 2014
Junior Member
I tried to override the method

_doEvaluate(XInstanceOfExpression instanceOf, ...)


from the XbaseInterpreter using the org.eclipse.emf.ecore.EClassifier.isInstance(Object) as you suggested. That seems to work. However, I cannot come up for a solution that fixes casts and typeof-checks. Maybe an XBase expert should have a look there...

see
https://bugs.eclipse.org/bugs/show_bug.cgi?id=510033
Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751263 is a reply to message #1751261] Fri, 06 January 2017 15:21 Go to previous message
Joel Greenyer is currently offline Joel GreenyerFriend
Messages: 19
Registered: October 2014
Junior Member
I managed to extend the XcoreInterpreter so that my Zoo example works. Not sure whether I forgot checks for certain corner cases.

@Ed, maybe you can check... Thanks!

See the XcoreInterpreter.java file uploaded with the bug report
https://bugs.eclipse.org/bugs/show_bug.cgi?id=510033
Previous Topic:XML Binding to EMF
Next Topic:Beginner EMF Drag and Drop Notes
Goto Forum:
  


Current Time: Sun Jun 13 14:17:48 GMT 2021

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

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

Back to the top