Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EMF-IncQuery » Substring search in atributes
Substring search in atributes [message #1042428] Tue, 16 April 2013 08:23 Go to next message
Emre T is currently offline Emre T
Messages: 119
Registered: April 2013
Senior Member
Hello there!

I was wondering if it is possible to check the attributes of the class instances in different patterns not just on equality, but also on different aspects such as whether a string value of a class attribute contains a given substring or whether an integer value of a class is greater or lesser than a given integer parameter?

The closest thing I guess, is to use check() clauses in patterns, which are based on XBase expressions. Still, I couldn't for example manage to use String.contains() in them; don't know why, but it tests the string and the substring for equality and not containment.

Should probably go deeper into the check() clauses, but I still needed to ask, if there is a more direct approach.

Thanks for your help!





Re: Substring search in atributes [message #1042642 is a reply to message #1042428] Tue, 16 April 2013 13:43 Go to previous messageGo to next message
Gabor Bergmann is currently offline Gabor Bergmann
Messages: 16
Registered: July 2009
Junior Member
Hi Emre,

check() should do exactly what you need here. And you should be able to call String.contains() in them.

Could you perhaps show us the pattern where check() does not work for you?

Cheers,
Gábor
Re: Substring search in atributes [message #1042660 is a reply to message #1042642] Tue, 16 April 2013 14:15 Go to previous messageGo to next message
Emre T is currently offline Emre T
Messages: 119
Registered: April 2013
Senior Member
Hi Gabor,

e.g. I have a class "Book" and it has a String attribute "description". In my model instance there are more than one books, and I want to query these books over their descriptions based on certain keywords, which are not hard-coded in the pattern but can be given as filter parameters. My pattern looks like this:

pattern bookByDescription(book:Book, description:EString, keyword) {
Book.description(book, description);
EString(keyword);
check((description as String).contains(keyword));
}

Let's say there are three books in my model instance:

Book1, description: "First Book of The Lord of the Rings Trilogy"
Book2, description: "Second Book of Harry Potter Series"
Book3, description: "The Lord of the Flies"

And I want all the books which have the word "Lord" in their description.

If I set no values for "book" and "description" parameters, the query returns all the books as result. But the moment I type something into the "keyword" filter parameter, no books are returned; unless I type the whole description of a Book instance, in which case only the matching book is returned. In other words, the contains() acts like equals(), if I m not mistaken.

Another thing to mention is, if I define the type of the keyword parameter in the pattern signature, as follows:

pattern bookByDescription(book:Book, description:EString, keyword:EString) {
Book.description(book, description);
check((description as String).contains(keyword));
}

I get two interesting errors, which are not appearing in the first case:

-Only simple EDataTypes are allowed in check expressions. The variable keywordhas type of null.

-Incompatible types. Expected java.lang.CharSequence but was java.lang.Object

I hope, I have given enough information. Thanks again!

Emre
Re: Substring search in atributes [message #1042663 is a reply to message #1042642] Tue, 16 April 2013 14:16 Go to previous messageGo to next message
Emre T is currently offline Emre T
Messages: 119
Registered: April 2013
Senior Member
Sorry for the unnecessary empty post.

[Updated on: Tue, 16 April 2013 14:18]

Report message to a moderator

Re: Substring search in atributes [message #1042664 is a reply to message #1042428] Tue, 16 April 2013 14:16 Go to previous messageGo to next message
Emre T is currently offline Emre T
Messages: 119
Registered: April 2013
Senior Member
Sorry for the unnecessary empty post.

[Updated on: Tue, 16 April 2013 14:18]

Report message to a moderator

Re: Substring search in atributes [message #1042732 is a reply to message #1042660] Tue, 16 April 2013 16:19 Go to previous messageGo to next message
Istvan Rath is currently offline Istvan Rath
Messages: 54
Registered: July 2009
Member
Hi Emre,

Quote:

pattern bookByDescription(book:Book, description:EString, keyword) {
Book.description(book, description);
EString(keyword);
check((description as String).contains(keyword));
}


this is better written as

pattern bookByDescription(book:Book, description:EString, keyword:EString) {
 Book.description(book, description);
 check(description.contains(keyword));
}


Note that the type cast to String is not necessary.

Additionally, the semantics of this pattern dictates that 'keyword' be an EString _that is actually present_ in your instance model as some attribute value (of type EString). Therefore, it will not work in the way you might expect: it will only return those matches where the substitution for the variable 'keyword' is present in your model in some way.

At the moment, the way of operation you would like is not supported; essentially, information is only allowed to go "into" a check() constraint from an object that is part of your instance model. (Additionally, in your interpretation, this pattern would require IncQuery to enumerate _all possible substrings_ of your EStrings, which would be a very costly operation.)
Re: Substring search in atributes [message #1042749 is a reply to message #1042732] Tue, 16 April 2013 16:45 Go to previous messageGo to next message
Istvan Rath is currently offline Istvan Rath
Messages: 54
Registered: July 2009
Member
Quote:
Additionally, the semantics of this pattern dictates that 'keyword' be an EString _that is actually present_ in your instance model as some attribute value (of type EString). Therefore, it will not work in the way you might expect: it will only return those matches where the substitution for the variable 'keyword' is present in your model in some way.

At the moment, the way of operation you would like is not supported; essentially, information is only allowed to go "into" a check() constraint from an object that is part of your instance model. (Additionally, in your interpretation, this pattern would require IncQuery to enumerate _all possible substrings_ of your EStrings, which would be a very costly operation.)


For the sake of completeness, two more remarks.

i) You can of course also use String constants inside pattern definitions, which sort of "approximates" your original use case:
pattern bookByDescription_Lord(book:Book, description:EString) {
 Book.description(book, description);
 check(description.contains("Lord"));
}


ii) You can also use IncQuery's Java API to accomplish your original goal (with some loss of incrementality, of course), e.g. as follows:


pattern bookByDescription(book:Book, description:EString) {
 Book.description(book, description);
}

BookByDescriptionMatcher m = new BookByDescriptionMatcher(...);
m.forEachMatch(new BookByDescriptionProcessor() {
 public void process(Book book, EString description) {
   // do something, e.g. collect Books by looking into their description
 }
});


For more details on our Java API, see http://wiki.eclipse.org/EMFIncQuery/UserDocumentation/API

Hope this helps,
Istvan
Re: Substring search in atributes [message #1042752 is a reply to message #1042732] Tue, 16 April 2013 16:47 Go to previous messageGo to next message
Emre T is currently offline Emre T
Messages: 119
Registered: April 2013
Senior Member
Hello Istvan,

Istvan Rath wrote on Tue, 16 April 2013 16:19
Hi Emre,

Quote:

pattern bookByDescription(book:Book, description:EString, keyword) {
Book.description(book, description);
EString(keyword);
check((description as String).contains(keyword));
}


this is better written as

pattern bookByDescription(book:Book, description:EString, keyword:EString) {
 Book.description(book, description);
 check(description.contains(keyword));
}


Note that the type cast to String is not necessary.



Yes, it indeed looks cleaner, and no errors either.

Istvan Rath wrote on Tue, 16 April 2013 16:19
Hi Emre,

Additionally, the semantics of this pattern dictates that 'keyword' be an EString _that is actually present_ in your instance model as some attribute value (of type EString). Therefore, it will not work in the way you might expect: it will only return those matches where the substitution for the variable 'keyword' is present in your model in some way.

At the moment, the way of operation you would like is not supported; essentially, information is only allowed to go "into" a check() constraint from an object that is part of your instance model. (Additionally, in your interpretation, this pattern would require IncQuery to enumerate _all possible substrings_ of your EStrings, which would be a very costly operation.)


If I am understanding this right, the reason why this function fails, is that the IncQuery sees the keyword parameter as an attribute of a class from the model, so it strictly checks for EString attributes, which matches with the given value?

Isn't there some sort of a workaround, like using helper parameters, which IncQuery doesn't interpret as class attributes from the model, so we can use them in check() clauses, to filter found results further and return them as results? Or every parameter in the pattern signature will be interpreted as attributes/classes from the model?

This would be very helpful, also for similar situations with integer attributes or dates, where we want to compare them and not just check on equality.

What I forgot to mention was, that all this functionality can be obtained by hardcoding e.g. substrings in check() clauses to use with the contains() method. But of course this limits the function a lot.

Best regards,
Emre

[Updated on: Tue, 16 April 2013 16:48]

Report message to a moderator

Re: Substring search in atributes [message #1042767 is a reply to message #1042428] Tue, 16 April 2013 17:15 Go to previous message
Istvan Rath is currently offline Istvan Rath
Messages: 54
Registered: July 2009
Member
Quote:

If I am understanding this right, the reason why this function fails, is that the IncQuery sees the keyword parameter as an attribute of a class from the model, so it strictly checks for EString attributes, which matches with the given value?


Yes. Note that is is not a failure, but this behavior is by design. As a general rule of thumb, variables in IncQuery pattern definitions must take their values from a finite and enumerable set, that is (i) objects present in the instance model within the matcher scope (resource, resourceset, containment subtree), (ii) constants inside the pattern definition, (iii) values inferred from certain language constructs (integer results of count find). This rule is necessary because incremental query evaluation requires that the query result set be computed and kept up-to-date at all times, hence all value substitution combinations must be enumerable.

Quote:

Isn't there some sort of a workaround, like using helper parameters, which IncQuery doesn't interpret as class attributes from the model, so we can use them in check() clauses, to filter found results further and return them as results? Or every parameter in the pattern signature will be interpreted as attributes/classes from the model?


Some related features for the query language are planned (https://bugs.eclipse.org/bugs/show_bug.cgi?id=399620), but for your goals, the recommended "workaround" (i.e. supported approach) is to use the Java API as I mentioned in the previous post. Note that this way, you still get the benefits of IncQuery (because it can enumerate Book-attribute value pairs quickly and keep their set corresponding to your model's contents at all times), but you have to iterate over these results for further filtering.

[Updated on: Wed, 17 April 2013 02:35]

Report message to a moderator

Previous Topic:Substring search in Atributes [Double-post]
Next Topic:Special syntax for pattern activation on nullable features?
Goto Forum:
  


Current Time: Fri Aug 29 20:25:30 EDT 2014

Powered by FUDForum. Page generated in 0.10865 seconds