0

In an application I receive a list of strings. Then I want to use OclPs to find all objects where a specific attribute equals any of the strings in the list. E.g. if we have Person objects and receive a list of last names, find all persons whose last name appears in the list.

Although this can surely be done in MDriven's in-memory OCL engine, I can't seem to achieve this in the more limited OclPs (which translates the OCL to SQL and evaluates it as such in the database).

Attempt 1: First assign the list of names to vNames (collection of strings), then:

Person.allInstances->select(p|vNames->exists(n|n = p.LastName))

This gives error "Loop variables can only have class type, not System.String".

Attempt 2: First assign a "|" separated string of the sought names, including leading and trailing "|", to vNames, then:

Person.allInstances->select(p|vNames.SqlLike('%|' + p.LastName + '|%'))

This gives error saying strings cant be added in Firebird. But Firebird does support string concatenation using the || operator.

Trying with .Contains(...) instead of .SqlLike(...) says it's not supported in OclPs. Besides, it would find persons with a last name that is CONTAINED in any of the sought names, i.e. an incorrect search.

I'm out of ideas...

Kjell Rilbe
  • 1,331
  • 14
  • 39

3 Answers3

1

In this case when you have a long list of strings and you want a list of objects I think you best option is to use SQL with sqlpassthroughobjects:

https://wiki.mdriven.net/index.php/OCLOperators_sqlpassthroughobjects

Person.sqlpassthroughobjects('select personid from person where lastname in ('+vNames->collect(n|'\''+n+'\'')->asCommaList+')')

Hans Karlsen
  • 2,275
  • 1
  • 15
  • 15
1

oclPS only implements a quite small subset of OCL because its converting the OCL to sql.

For example, collections of "non-objects" can't be used.

Person.allInstances->select(p|vNames.SqlLike('%|' + p.LastName + '|%'))

Instead, look at sqlpassthroughobjects here https://wiki.mdriven.net/index.php/OCLOperators_sqlpassthroughobjects

You could also insert the names as objects of a class into the database and then use oclPS.

Lars
  • 191
  • 4
0

I suggest that you use a genuine OCL tool. You seem to be demonstrating that MDriven OclPs is not OCL.

Ed Willink
  • 1,205
  • 7
  • 8
  • Please reach out if you want to try MDriven to better judge its credibility as an genuine OCL tool. – Hans Karlsen Dec 16 '21 at 19:29
  • @EdWillink, That remark could possibly apply to the OclPs part, which converts OCL into SQL and executes it at the database layer. It is quite limited, but converting OCL to SQL probably isn't straightforward. MDriven does have OCL capabilities evaluated in memory that's a lot more complete. – Kjell Rilbe Dec 17 '21 at 11:16