Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "CDOQuery OCL"

m (Queries)
(OCL Query Examples)
 
(2 intermediate revisions by one other user not shown)
Line 38: Line 38:
  
 
Get the number of instances of Patient <pre>Patient.allInstances()->size()</pre>
 
Get the number of instances of Patient <pre>Patient.allInstances()->size()</pre>
 +
 +
Note: The OCLQueryHandler uses lazy extents by default. That means that the OCL interpreter can already start using the extent before it's fully populated. But this means that the size of the extent may not be known at that time. If your query needs to know the size of the allInstances() set (which triggers the CDOExtentCreator) you need to tell the query handler not to use lazy extents:
 +
 +
<pre>query.setParameter("cdoLazyExtents", false);</pre>
  
 
Get all instances of Patient where <tt>nachname</tt> is exactly <tt>Descher</tt> <pre>Patient.allInstances()->select(p:Patient|p.nachname='Descher')</pre>
 
Get all instances of Patient where <tt>nachname</tt> is exactly <tt>Descher</tt> <pre>Patient.allInstances()->select(p:Patient|p.nachname='Descher')</pre>
  
 
Get all instances of Patient where <tt>nachname</tt> starts with <tt>G</tt> <pre>Patient.allInstances()->select(p:Patient|p.nachname.substring(1,1)='G')</pre>
 
Get all instances of Patient where <tt>nachname</tt> starts with <tt>G</tt> <pre>Patient.allInstances()->select(p:Patient|p.nachname.substring(1,1)='G')</pre>
 +
 +
Get the dates of all Konsultations <pre>Konsultation.allInstances().datum</pre>
 +
 +
Get the attribute numericId of all instances of Patient <pre>Patient.allInstances()->collect(numericId)</pre>

Latest revision as of 07:07, 10 January 2014

Based on CDO 4.0-SR2

For some basic references to OCL please find http://atlanmod.emn.fr/atldemo/oclturorial/ or http://cs.ulb.ac.be/public/_media/teaching/infoh302/oclnotes.pdf

Model

We have the following, deliberately trivial, model:

TrivialModel.png

We have the entities Patient and Konsultation (Consultation), which hold some generic attributes resp. features. The enumeration Geschlecht (sex) is self explaining. Each Konsultation belongsTo exactly one Patient, it is however not contained by the patient but exists as independent entity.

Queries

This section documents some queries based on the aforementioned model. It is work in progress.

Queries are carried out on the whole Repository, so it is not important to what Resource an element belongs to.

Finding object references

The following OCL query finds all Konsultation entries in the entire repository, that belong to a certain Patient p, "injected" by the context. The element provided by the context is refered as self within the query.

CDOSession session = ...
 
CDOTransaction cdoTransaction = session.openTransaction();
 
CDOQuery cqo = cdoTransaction.createQuery("ocl",
		"Konsultation.allInstances()->select(k:Konsultation|k.belongsTo=self)",
		((Patient)p).cdoID(), false);
 
List<Konsultation> lre = cqo.getResult(Konsultation.class);

The most important part in this query is the fact that we are comparing real objects, as k.belongsTo refers to an element of type Patient. For OCL CDO to evaluate this query correct, we need to pass the context objects CDOID object as otherwise we will receive an error. A direct comparison by simply passing (Patient)p as context is hence not possible.

OCL Query Examples

These query examples are pure OCL only, the need to be embedded into an OCL query!

Find all instances of Patient
Patient.allInstances()
Get the number of instances of Patient
Patient.allInstances()->size()

Note: The OCLQueryHandler uses lazy extents by default. That means that the OCL interpreter can already start using the extent before it's fully populated. But this means that the size of the extent may not be known at that time. If your query needs to know the size of the allInstances() set (which triggers the CDOExtentCreator) you need to tell the query handler not to use lazy extents:

query.setParameter("cdoLazyExtents", false);
Get all instances of Patient where nachname is exactly Descher
Patient.allInstances()->select(p:Patient|p.nachname='Descher')
Get all instances of Patient where nachname starts with G
Patient.allInstances()->select(p:Patient|p.nachname.substring(1,1)='G')
Get the dates of all Konsultations
Konsultation.allInstances().datum
Get the attribute numericId of all instances of Patient
Patient.allInstances()->collect(numericId)

Back to the top