🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCL-PS
This page was created by Hans.karlsen on 2018-07-22. Last edited by Wikiadmin on 2026-07-29.

You can use OCL-PS when you need an OCL query to filter large persisted data sets in the database instead of loading and evaluating many objects in memory.

OCL and OCL-PS

OCL (Object Constraint Language) is MDriven's declarative query language. Normal OCL evaluates against objects in memory. When an expression navigates to data that has not yet been loaded, MDriven can fetch that data as needed.

OCL-PS is the database-query subset of OCL. PS means Persistent Storage. MDriven translates supported OCL-PS expressions to SQL and executes that SQL in the persistent storage. The database returns object identities; MDriven then uses its normal fetch operations to resolve those identities to objects.

For example, if you need to find one object among millions, or restrict a search before loading its matching objects, use OCL-PS rather than navigating a large collection with normal in-memory OCL.

Use case Choose Why
Validate or derive a value from objects that are already in memory Normal OCL The expression evaluates on the loaded object graph.
Find matching objects from a large database-backed set OCL-PS MDriven translates the supported expression to SQL so the database performs the filtering.
Change model data EAL OCL and OCL-PS are query languages without side effects. EAL uses OCL-like syntax for actions.

When to use OCL-PS

Use OCL-PS when the amount of data considered by an expression is large, such as hundreds or thousands of objects, or when the database can perform the operation more appropriately than an in-memory object traversal.

Typical uses include:

  • Search expressions and nestings in ViewModels.
  • Fetching a limited result set directly from persistent storage with PSEval.
  • Returning database-computed scalar values or tuples through the PSEval-related operators.

For example, a ViewModel search that must identify matching records across a large table should filter in the database first. After MDriven receives the identities of the matches, you can use normal OCL on the corresponding loaded objects.

Important limitations

OCL-PS is a subset of OCL. Write the database-filtering part of your expression using only functionality that OCL-PS supports.

  • OCL-PS has no side effects.
  • You cannot call your own methods, including methods marked IsQuery.
  • collect, groupby, and other operators that return tuples are not supported in OCL-PS.
  • The primary purpose is to return a set of object identities selected by criteria such as select or difference.

Do not assume that every valid normal OCL expression can be translated to SQL. Keep the persistent-storage query focused on selecting the required objects, then continue with normal OCL after those objects are loaded.

Use OCL-PS from OCL or EAL

Use PSEval when you need to invoke an OCL-PS expression from an OCL or EAL expression. Its template is:

SomeClass.PSEval(<ps-expression>, maxfetch, offset, <dependon>)

For example, the following query selects active Uppdrag objects, orders them by Startdatum descending, and requests up to 1000 results starting at offset 0:

Uppdrag.PSEval(self.Uppdrag->select(u|u.Aktivt)->orderDescending(u|u.Startdatum), 1000, 0, self.Uppdrag)

Be aware of the following PSEval behavior:

  • offset is zero-based and should normally be 0 unless you are implementing pagination.
  • PSEval does not subscribe to database result sets. Changes to data in the result set are not fetched automatically.
  • Supply a dependon expression when a change should rerun the query. For example, this can be a timer or another relevant expression.
  • Ensure that the SQL server has appropriate indexes and performance configuration for the translated query.

Validate expressions

MDriven dynamically type-checks OCL, EAL, and OCL-PS when the model is loaded or saved, and when you run a model check manually. Running a model check also cross-references the model so you can inspect where model elements are used.

Further guidance

Read Documentation:OCLps for the canonical OCL-PS description and supported usage context. For direct database access from expressions, including PSEval-related operators, use Documentation:OCLOperators PSEval. The older PSExpression_ ViewModel-column approach is deprecated; see Documentation:PSExpression , or how to do things in the DB from MDriven for background and migration context.

See also