Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EMF-IncQuery » Finding all elements of an association in a subtree(Can you tell me how to do this?)
Finding all elements of an association in a subtree [message #1007892] Fri, 08 February 2013 01:00 Go to next message
Remy Honig is currently offline Remy Honig
Messages: 4
Registered: March 2012
Junior Member
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 14:52 Go to previous messageGo to next message
Zoltan Ujhelyi is currently offline Zoltan Ujhelyi
Messages: 221
Registered: July 2009
Senior Member
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 15:53 Go to previous messageGo to next message
Remy Honig is currently offline Remy Honig
Messages: 4
Registered: March 2012
Junior Member
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 07:43 Go to previous message
Abel Hegedus is currently offline Abel Hegedus
Messages: 98
Registered: July 2009
Member
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.
Previous Topic:Unable to install EMF IncQuery
Next Topic:Warning about cartesian product seems unnecessary in some cases
Goto Forum:
  


Current Time: Wed Oct 22 21:56:44 GMT 2014

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

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