bkbit.model_editors.linkml_trimmer module

This script provides a utility for trimming a LinkML schema by retaining specified classes, slots, and enums, along with their dependencies.

It defines a YamlTrimmer class for schema manipulation and offers a command-line interface using Click for easy usage from the terminal.

Usage:

python script.py [OPTIONS] SCHEMA

Options:
--classes, -c TEXT

Comma-separated list of classes to include in the trimmed schema (required).

--slots, -s TEXT

Comma-separated list of slots to include in the trimmed schema.

--enums, -e TEXT

Comma-separated list of enums to include in the trimmed schema.

Example

python script.py schema.yaml -c Person,Organization -s name,age -e StatusEnum

The script performs the following steps: 1. Loads the specified LinkML schema. 2. Trims the schema by keeping only the specified classes, slots, and enums, along with their dependencies. 3. Serializes and prints the trimmed schema in YAML format.

Dependencies:
  • click

  • linkml-runtime

  • linkml

class bkbit.model_editors.linkml_trimmer.YamlTrimmer(schema: str | Path | SchemaDefinition)[source]

Bases: object

A utility class for trimming a LinkML schema by retaining specified classes, slots, and enums, along with their dependencies.

This class helps in generating a simplified version of a LinkML schema by removing all elements that are not reachable from the specified classes, slots, and enums to keep.

Parameters:

schema (Union[str, Path, SchemaDefinition]) – The LinkML schema to be trimmed. It can be a file path, URL, or a SchemaDefinition object.

schemaview

An object representing the loaded schema, used for manipulation and traversal.

Type:

SchemaView

trim_model(keep_classes

list[str], keep_slots: list[str] = [], keep_enums: list[str] = []): Trims the schema by keeping only the specified classes, slots, and enums, and their dependencies.

serialize()[source]

Serializes and prints the trimmed schema in YAML format.

Example

>>> yt = YamlTrimmer('path/to/schema.yaml')
>>> yt.trim_model(['Person', 'Organization'], keep_slots=['name'], keep_enums=['StatusEnum'])
>>> yt.serialize()
serialize()[source]

Serializes the schema using YAMLGenerator and prints the serialized output.

trim_model(keep_classes: list[str], keep_slots: list[str] = [], keep_enums: list[str] = [])[source]

Trims the model by removing classes, slots, and enums that are not reachable from the specified keep_classes, keep_slots, and keep_enums.

Parameters:
  • keep_classes (list[str]) – List of classes to keep.

  • keep_slots (list[str], optional) – List of slots to keep. Defaults to [].

  • keep_enums (list[str], optional) – List of enums to keep. Defaults to [].