Finding all elements of an association in a subtree [message #1007892] |
Thu, 07 February 2013 20:00  |
Eclipse User |
|
|
|
I am trying write a query in EMF-IncQuery that returns every note of every category in a subtree of categories. In my Category entity i have a feature named "allNotes" that must return those notes.
So Category2.allNotes should return Note1 and Note2.
And Category2a.allNotes should return Note2.
Category1
\--Category2
\--Category2a
\--Note2
\--Category2b
\--Note1
\--Category3
This is how my model look like:
class Notebook
{
contains Note[] notes opposite notebook
contains Category[] categories opposite notebook
}
class Note
{
String text
container Notebook notebook opposite notes
refers Category[] categories opposite notes
}
class Category
{
refers Note[] notes opposite categories
refers Note[] allNotes
contains Category[] children opposite parent
container Notebook notebook opposite categories
container Category parent opposite children
}
The query below only return the Notes inside one Category.
@DerivedFeature
pattern allNotes(C: Category, N: Note) {
Category.notes(C, N);
}
What should my query look like to do what I want?
|
|
|
Re: Finding all elements of an association in a subtree [message #1007993 is a reply to message #1007892] |
Fri, 08 February 2013 09:52   |
Eclipse User |
|
|
|
Hi,
currently there is no operator for traversing the containment hierarchy generically, however, you could use some helper patterns to get what you want. I suggest something as follows:
/*
* Subcategory relation
*/
pattern subCategory(c : Category, s : Category) {
Category.children(c, s);
}
/*
* List of all categories contained in the selected Category 'c'. The result also includes 'c'.
pattern allSubCategories(c : Category, s : Category) {
c == s;
} or {
//This is required as find subCategory+ does no return direct relations; and there is no find subCategory* support in the language
find subCategory(c, s);
} or {
find subCategory+(c, s);
}
@DerivedFeature
pattern allNotes(c: Category, n: Note) {
find allSubCategories(c, s);
Category.notes(s, n);
}
If you would have multiple paths to check in the containment hierarchy, your helper patterns should look for those cases as well.
I hope, this is helps.
Cheers,
Zoltán
|
|
|
Re: Finding all elements of an association in a subtree [message #1008042 is a reply to message #1007993] |
Fri, 08 February 2013 10:53   |
Eclipse User |
|
|
|
Hi Zoltán,
I also found an old github ticket (github.com/ujhelyiz/EMF-IncQuery/issues/350) of your project which I found helpful as well.
My patterns now look like this as you suggested, but I did not add ...or {find subCategories(This, Child);}... and still I get the result I was expecting. Not sure why that is.
private pattern subCategories(This: Category, Child: Category) {
Category.children(This, Child);
}
private pattern thisAndSubCategories(This: Category, Child: Category) {
find subCategories+(This, Child);
} or {
This == Child;
}
@DerivedFeature
pattern allNotes(This: Category, Notes: Note) {
find thisAndSubCategories(This, Child);
Category.notes(Child, Notes);
}
Thank your for your help, I greatly appreciate it.
|
|
|
Re: Finding all elements of an association in a subtree [message #1008160 is a reply to message #1008042] |
Mon, 11 February 2013 02:43  |
Eclipse User |
|
|
|
Just a quick clarification:
The pattern you wrote is correct and it gives the expected results as required.
Contrary to what Zoltán said, the + operator does include direct references, what it does not include is the "source" element, which is why the "{c == s}" pattern body is required.
|
|
|
Powered by
FUDForum. Page generated in 0.27687 seconds