SchemaBuilder

The SchemaBuilder class in the linkml_runtime package provides a way of programmatically constructing schemas following a Builder pattern.

Deprecated since version Importing: SchemaBuilder from linkml.utils.schema_builder is deprecated. Use from linkml_runtime.utils.schema_builder import SchemaBuilder instead.

class linkml_runtime.utils.schema_builder.SchemaBuilder(name: str | None = None, id: str | None = None, schema: SchemaDefinition = None)[source]

Builder class for SchemaDefinitions.

Example

>>> from linkml_runtime.utils.schema_builder import SchemaBuilder
>>> sb = SchemaBuilder('test-schema')
>>> _ = sb.add_class('Person', slots=['name', 'age'])
>>> _ = sb.add_class('Organization', slots=['name', 'employees'])
>>> _ = sb.add_slot('name',description='Name of the person or organization', replace_if_present=True)
>>> _ = sb.add_slot('age',description='Age of the person', range='integer', replace_if_present=True)
>>> schema = sb.schema

Most builder methods accepts either a string, an instance of a metamodel element, or a dictionary. If a string is provided, then a new element is created with this as the name.

This follows the standard Builder pattern, so the results of a build operation are a builder, allowing chaining. For example:

>>> _ = SchemaBuilder('test-schema').add_class('Person', slots=['name', 'age'])
add_class(cls: ClassDefinition | dict | str, slots: dict[str, dict] | list[str | SlotDefinition] = None, slot_usage: dict[str, SlotDefinition | dict] | list[SlotDefinition] = None, replace_if_present: bool = False, use_attributes: bool = False, **kwargs) SchemaBuilder[source]

Adds a class to the schema.

Parameters:
  • cls – name, dict object, or ClassDefinition object to add

  • slots – list of slot names or slot objects, or a dict mapping slot names to dicts of slot properties. Must be a list of SlotDefinition objects if use_attributes=True

  • slot_usage – dict mapping slot names to SlotDefinition objects or dicts of slot properties, or a list of SlotDefinition objects. Ignored if use_attributes=True

  • replace_if_present – if True, replace existing class if present

  • use_attributes – Whether to specify the given slots as an inline definition of slots, attributes, in the class definition

  • kwargs – additional ClassDefinition properties

Returns:

builder

Raises:

ValueError – if class already exists and replace_if_present=False

add_defaults() SchemaBuilder[source]

Sets defaults, including:

  • default_range

  • default imports to include linkml:types

  • default prefixes

Returns:

builder

add_enum(enum_def: EnumDefinition | dict | str, permissible_values: list[str | PermissibleValue] = None, replace_if_present=False, **kwargs) SchemaBuilder[source]

Adds an enum to the schema

Parameters:
  • enum_def – The base specification of the enum to be added

  • permissible_values – Additional, or overriding, permissible values of the enum to be added

  • replace_if_present – Whether to replace the enum if it already exists in the schema by name

  • kwargs – Additional EnumDefinition properties to be set as part of the enum to be added

Returns:

builder

Raises:

ValueError – if enum already exists and replace_if_present=False

add_imports(*imports) SchemaBuilder[source]

Adds imports to the schema

Parameters:

imports – list of imports

Returns:

builder

add_prefix(prefix: str, url: str, replace_if_present=False) SchemaBuilder[source]

Adds a prefix for use with CURIEs

Parameters:
  • prefix

  • url

Returns:

builder

Raises:

ValueError – if prefix already exists and replace_if_present=False

add_slot(slot: SlotDefinition | dict | str, class_name: str = None, replace_if_present=False, **kwargs) SchemaBuilder[source]

Adds the slot to the schema.

Parameters:
  • slot – name, dict object, or SlotDefinition object to add

  • class_name – if specified, this will become a valid slot for this class

  • replace_if_present – if True, replace existing slot if present

  • kwargs – additional properties

Returns:

builder

Raises:

ValueError – if slot already exists and replace_if_present=False

add_type(type: TypeDefinition | dict | str, typeof: str = None, uri: str = None, replace_if_present=False, **kwargs) SchemaBuilder[source]

Adds the type to the schema

Parameters:
  • type

  • typeof – if specified, the parent type

  • uri – if specified, the URI or curie of the type

  • replace_if_present

  • kwargs

Returns:

builder

Raises:

ValueError – if type already exists and replace_if_present=False

as_dict() dict[source]

Returns the schema as a dictionary.

Compaction is performed to eliminate redundant keys

Returns:

dictionary representation

id: str | None = None

Initialized id for the schema.

name: str | None = None

Initialized name for the schema.

schema: SchemaDefinition = None

generated SchemaDefinition object.

set_slot(slot_name: str, **kwargs) SchemaBuilder[source]

Set details of the slot

Parameters:
  • slot_name

  • kwargs

Returns:

builder