Obtain the type of a pattern variable?

23 views
Skip to first unread message

koeny

unread,
Jul 16, 2012, 5:20:41 AM7/16/12
to incquer...@googlegroups.com
Is there a way to obtain, through the IncQuery API, the type of a pattern variable (parameter) when that type is explicitly declared in the header of the pattern?

I have seen issue #223 about a type inference API (https://github.com/ujhelyiz/EMF-IncQuery/issues/223), but I was wondering whether this simpler case was already possible somehow?

Ujhelyi Zoltán

unread,
Jul 16, 2012, 5:35:01 AM7/16/12
to EMF-IncQuery Users on behalf of koeny
Hi,

I would like to clarify your question a bit.

If you'd like to have a type-safe matcher API, it is generated into the src-gen folder of the IncQuery project (at least it should be): for every pattern there is a Match, Matcher and MatcherFactory classes that have all type information set. If these classes are not available with the type system, that is most most likely your IncQuery project does not depend on the project that provides the EMF model objects.

However, if you want to be able to query these types using the IncQuery API, that scenario is not implemented.

I hope this is helpful,
Zoltán
-- Zoltán Ujhelyi
https://www.inf.mit.bme.hu/en/members/ujhelyiz

Fault Tolerant Systems Research Group
Budapest University of Technology and Economics
> --
> You received this message because you are subscribed to the Google Groups "EMF-IncQuery Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/incquery-users/-/cfkEmChcwM8J.
> To post to this group, send email to incquer...@googlegroups.com.
> To unsubscribe from this group, send email to incquery-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/incquery-users?hl=en.
>

István Ráth

unread,
Jul 16, 2012, 5:41:16 AM7/16/12
to EMF-IncQuery Users on behalf of Zoltán Ujhelyi
 
However, if you want to be able to query these types using the IncQuery API, that scenario is not implemented.

It *is* implemented. IncQuery parses query definitions into EMF models, and the registered pattern definitions can be accessed through the PatternRegistry class (getPatterns, getPatternByFqn etc). You will get a reference to an EMF instance model that can be traversed and the type information extracted.

regards
Istvan

István Ráth

unread,
Jul 16, 2012, 5:44:52 AM7/16/12
to EMF-IncQuery Users on behalf of Zoltán Ujhelyi
Another useful tool here is described on http://viatra.inf.mit.bme.hu/incquery/new/download#Reflectiveeditorforeiqfiles, which will allow you to inspect .eiq files as EMF instance models in a tree editor.

Istvan
--
István


koeny

unread,
Jul 16, 2012, 7:03:05 AM7/16/12
to incquer...@googlegroups.com
I'll try to clarify a bit what I want to do.

Starting from an IncQuery Pattern object, I'd like to find out the parameters (which I can do via the getParameters() call). However, the resulting Variable objects don't seem to have their type property set to anything that leads me to either the right EClass or the actual Java class. I assume that you mean to derive the type by iterating through the body constraints of the pattern (which, I assume, is what the type inference API would eventually do), but I was hoping that there was an easier way available (out of the box)... Also, for variable types that are declared in the header, no constraints that define the type are added to the pattern body...

Ujhelyi Zoltán

unread,
Jul 16, 2012, 7:25:00 AM7/16/12
to EMF-IncQuery Users on behalf of koeny
This is one of the harder to understand parts of the model. The typename attribute is never filled, and only exists for technical reasons. You have to downcast the Type object to a corresponding ClassType or ReferenceType (depending on whether the type if a EMF classifier or an EReference), and then the classname or refname attributes will point to the corresponding types.

Sorry for the confusion,
Zoltán
-- Zoltán Ujhelyi
https://www.inf.mit.bme.hu/en/members/ujhelyiz

Fault Tolerant Systems Research Group
Budapest University of Technology and Economics

> --
> You received this message because you are subscribed to the Google Groups "EMF-IncQuery Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/incquery-users/-/4JmHkI3wkGUJ.

Ujhelyi Zoltán

unread,
Jul 16, 2012, 7:40:52 AM7/16/12
to EMF-IncQuery Users on behalf of Zoltán Ujhelyi
To make my previous email more understandable, I add a simple example code from the EMF-IncQuery codebase (available in the code of EMF-IncQuery in line 225 https://github.com/ujhelyiz/EMF-IncQuery/blob/master/plugins/org.eclipse.viatra2.patternlanguage.emf/src/org/eclipse/viatra2/patternlanguage/types/EMFPatternTypeProvider.java). The goal of the code segment is to provide the Java types for the EMF type of a variable. For your question, only the following snippet is needed, where the Type object is downcasted to the corresponding subtypes, then the className and refName attributes are used to get the EMF types.

protected JvmTypeReference resolve(Type type, Variable variable) {
if (type instanceof ClassType) {
return resolve((ClassType)type, variable);
}
if (type instanceof ReferenceType) {
return resolve((ReferenceType) type, variable);
}
return null;
}

protected JvmTypeReference resolve(ClassType type, Variable variable) {
final EClassifier classifier = type.getClassname();
if (classifier != null) {
return resolve(classifier, variable);
}
return null;
}

protected JvmTypeReference resolve(ReferenceType type,
Variable variable) {
final EStructuralFeature feature = type.getRefname();
if (feature instanceof EAttribute) {
return resolve((EAttribute) feature, variable);
}
if (feature instanceof EReference) {
return resolve((EReference)feature, variable);
}
return null;
}

I hope, this way it is more easy to understand,
Zoltán
-- Zoltán Ujhelyi
https://www.inf.mit.bme.hu/en/members/ujhelyiz

Fault Tolerant Systems Research Group
Budapest University of Technology and Economics

koeny

unread,
Jul 16, 2012, 9:00:10 AM7/16/12
to incquer...@googlegroups.com
Thanks, that's exactly what I needed!
Reply all
Reply to author
Forward
0 new messages