Framework
OCL
A pure Swift implementation of the OMG Object Constraint Language (OCL) for model querying and constraint specification.
Overview
OCL provides a complete implementation of the Object Constraint Language (https://www.omg.org/spec/OCL/) standard in pure Swift. Use it to query models, specify constraints, and perform operations on collections, strings, and numeric values in a declarative, side-effect-free manner.
The framework follows the OMG OCL 2.4 specification (https://www.omg.org/spec/OCL/2.4/PDF), providing familiar OCL operations for developers experienced with Eclipse OCL (https://projects.eclipse.org/projects/modeling.mdt.ocl) or other OCL implementations whilst offering a modern, type-safe Swift API.
Key Features
-
Standard Library: Complete OCL standard library operations
-
Collection Operations: Query and transform collections with select, reject, collect, forAll, exists, and iterate
-
String Manipulation: Rich string operations including concat, substring, indexOf, and pattern matching
-
Numeric Operations: Mathematical functions including abs, floor, ceiling, round, and power
-
Type Operations: Runtime type checking and casting with oclIsTypeOf, oclIsKindOf, and oclAsType
-
Side-effect Free: All operations are pure and do not modify the underlying model
Quick Example
import OCL
// Collection operations
let numbers = [1, 2, 3, 4, 5]
let evenNumbers = try OCL.select(numbers) { $0 % 2 == 0 }
// Result: [2, 4]
// String operations
let text = "Hello, World!"
let upper = try OCL.toUpperCase(text)
// Result: "HELLO, WORLD!"
// Numeric operations
let value = -42.7
let absolute = try OCL.abs(value)
// Result: 42.7
Topics
Essentials
Learn how to use OCL operations in your Swift projects.
Learn the fundamental concepts of the Object Constraint Language.
Standard Library
Enumeration of OCL unary methods (methods that take only a receiver, no arguments).
Enumeration of OCL binary methods (methods that take a receiver and one argument).
Enumeration of OCL ternary methods (methods that take a receiver and two arguments).
Error Handling
Errors that can occur during OCL operation evaluation.
Functions
Returns the absolute value of a number.
Adds two values together.
Performs logical AND operation on two boolean values.
Converts a collection to a bag (allowing duplicates).
Converts a collection to an ordered set.
Converts a collection to a sequence.
Converts a collection to a set by removing duplicates.
Returns the character at a specific position in a string.
Returns the ceiling (smallest integer not smaller than the input) of a real value.
Transforms each element of a collection using the given transform function.
Concatenates two strings.
Checks if a string contains a specified substring.
Returns the difference between two collections.
Divides the left value by the right value.
Checks if a string ends with a specified suffix.
Tests whether two values are equal.
Tests whether a collection does not contain a specific element.
Returns a new collection with all occurrences of an element removed.
Tests whether at least one element in the collection satisfies the given predicate.
Returns the first element of a collection.
Flattens a nested collection structure.
Returns the largest integer less than or equal to the value (OCL Real.floor()).
Tests whether all elements in the collection satisfy the given predicate.
Tests whether the left value is greater than the right value.
Tests whether the left value is greater than or equal to the right value.
Performs logical implication operation on two boolean values.
Tests whether a collection contains a specific element.
Returns a new collection with an element added.
Finds the index of the first occurrence of a substring within a string.
Returns the intersection of two collections.
Invokes a binary method on a receiver value with one argument.
Invokes a ternary method on a receiver value with two arguments.
Invokes a unary method on a receiver value.
Tests whether a collection or string is empty.
Accumulates elements of a collection using the given accumulator function.
Returns the last element of a collection.
Tests whether the left value is less than the right value.
Tests whether the left value is less than or equal to the right value.
Returns the maximum of two numeric values (OCL Integer/Real.max()).
Returns the minimum of two numeric values (OCL Integer/Real.min()).
Computes the remainder of dividing the left value by the right value.
Multiplies two values together.
Performs numeric negation on a numeric value.
Performs logical NOT operation on a boolean value.
Tests whether a collection or string is not empty.
Tests whether two values are not equal.
Casts an object to the specified type.
Checks if an object is of the specified type or a subtype.
Checks if an object is exactly of the specified type.
Checks if an object is of a specific ECore type by name.
Gets the type name of an ECore value as a string.
Tests whether exactly one element in the collection satisfies the given predicate.
Performs logical OR operation on two boolean values.
Raises a numeric value to the power of another numeric value.
Rejects all elements from a collection that satisfy the given predicate.
Replaces all occurrences of a substring with another string.
Returns the rounded value to the nearest integer (OCL Real.round()).
Selects all elements from a collection that satisfy the given predicate.
Returns the number of elements in a collection or characters in a string.
Splits a string into an array of substrings using a delimiter.
Checks if a string starts with a specified prefix.
Extracts a substring from a string.
Subtracts the right value from the left value.
Converts a string to lowercase.
Converts a string to uppercase.
Trims whitespace from both ends of a string.
Returns the union of two collections.