🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators classfromstring
This page was created by Hans.karlsen on 2024-09-24. Last edited by Wikiadmin on 2026-07-29.

You can use ClassFromString in OCL when a string contains a model class name and you need to work with that class as an OclType, for example to inspect its tagged values.

Purpose

ClassFromString converts a string representation of a model type into an actionable model type (OclType). It is the inverse of AsString when you are working with model metadata.

Use it when the class name is available as text but the expression must access information on the class itself. A common use is filtering classes by a naming convention or by a tagged value stored on the class.

Syntax

string.ClassFromString

The expression before .ClassFromString must identify a model type as a string. The result can then be used where an OCL expression expects a model type.

Filter classes by tagged value

The following expression starts with all subclasses of Customer. For each entry, it converts the class string to a model type and reads that type's status tagged value.

Customer.allSubClasses->select(x|x.classfromstring.TaggedValue('status')<>'Obsolete')

This selects the entries whose underlying class does not have Obsolete as the value of its status tag. The select operation returns the matching entries from Customer.allSubClasses; the conversion is used in the predicate to inspect each class's metadata.

How to use it

  1. Identify the expression that supplies a class name as a string. For example, x in a collection of subclass strings.
  2. Apply ClassFromString to convert that string to an OclType.
  3. Access metadata or other type-level information on the resulting OclType.
  4. Use the result in a predicate when filtering a collection.

For example, the predicate below converts x before reading its status tag:

x.classfromstring.TaggedValue('status')<>'Obsolete'

When to use ClassFromString

Situation Use ClassFromString? Example
You have a class represented as text and need its tagged value. Yes. x.classfromstring.TaggedValue('status')
You have an OclType and need to store or compare its text representation. Use AsString instead. Convert a type to its string form before handling it as text.
You need to work with ordinary string content, such as its character count. No. Use length for the number of characters.

See also