Aggregation of Strings of different Objects [message #1413098] |
Wed, 27 August 2014 15:46  |
Eclipse User |
|
|
|
Hi,
I want to calculate the metric "Relevant Lines of Code" on the JaMoPP model.
This means all lines of a Compilation Unit, that contains at least one non commented character of A-Z.
The query formulated in Epsilon Object Language is as follows:
operation calculateRloc () : Integer {
return daten!CompilationUnit.all().
select(x | x.isSource()).
collect(x | x.rloc()).
sum().
asInteger();
}
operation CompilationUnit rloc() : Integer {
var layouts = self.getChildrenByEType(AttributeLayoutInformation.createInstance().type());
layouts.addAll(self.getChildrenByEType(KeywordLayoutInformation.createInstance().type()));
layouts.addAll(self.getChildrenByEType(ReferenceLayoutInformation.createInstance().type()));
return layouts.sortBy(x | x.getStartOffset()).
collect(x | x.layoutAsText()).
concat().
split("\n").
select(x | x.matches("(?s).*[a-zA-Z].*")).
select(x | x <> "<EOF>").
size();
}
operation LayoutInformation layoutAsText () : String {
return self.getHiddenTokenText().
toCharSequence().
select(x | x="\n" or x="\t" or x=" " or x="\r").
concat().
concat(self.getVisibleTokenText());
}
@cached
operation Commentable isSource() : Boolean {
if((self.eResource()).isUndefined()){
return false;
}
return self.eResource().getURI().toString().startsWith('file:/');
}
First, all source Compilation Units are collected.
For each Compilation Unit, all contained LayoutInformation Objects are gathered.
Then they are sorted by the attribute getStartOffset.
The string representation is concatened etc...
My question is if this is possible in Incquery.
Is there any way to sort a collection and concatenate the strings?
So far I understand Incquery it is not possible.
Thanks for any hint
Greetings, Mario
|
|
|
Re: Aggregation of Strings of different Objects [message #1413117 is a reply to message #1413098] |
Wed, 27 August 2014 17:09   |
Eclipse User |
|
|
|
Hi Mario,
you are right, what you are looking for is not really possible in the graph pattern formalism of EMF-IncQuery. The results of a pattern (that is represented as a set) cannot be ordered directly (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=398768 for some related ideas); however, nothing stops you from loading the results programatically, then ordering the results using a Java comparator (or if you are using Java 8, Xtend or Guava, you can also use similar sort functions to the Epsilon code).
Concatenating string values from model attributes is possible with the eval constraint (https://wiki.eclipse.org/EMFIncQuery/UserDocumentation/QueryLanguage#Advanced_Pattern_Constraints), where you can use Java-like expressions to do the concatenation. However, you cannot combine the results of multiple matches, as that would greatly extend the capabilities of our language.
----
However, for such tasks as your I would suggest a combined Java/IncQuery approach. Define queries that are used to collect the model elements required for the aggregation, but do not manage the aggregation itself. Then do the aggregation using the results of these elementary patterns in Java code. The result could be similar to the Epsilon code shown above; the imperative code (that is most of the code above) should be written in Java, while the corresponding model element tuples can be assembled using EMF-IncQuery patterns.
I hope, the idea is clear enough. If not, feel free to ask for clarification.
Cheers,
Zoltán
|
|
|
|
Powered by
FUDForum. Page generated in 0.05009 seconds