SchemaBuilder#

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

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

Builder class for SchemaDefinitions.

Example

>>> from linkml.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')
>>> sb.add_slot('age',description='Age of the person', range='integer')
>>> schema = sb.schema
>>> print()

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:

>>> sb = SchemaBuilder('test-schema').add_class('Person', slots=['name', 'age'])
add_class(cls: ClassDefinition | Dict | str, slots: Dict | List[str | SlotDefinition] = None, slot_usage: Dict[str, SlotDefinition] | Dict[str, Any] | List[SlotDefinition] = None, replace_if_present=False, use_attributes=False, **kwargs) SchemaBuilder[source]#

Adds a class to the schema.

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

  • slots – slot of slot names or slot objects.

  • slot_usage – slots keyed by slot name

  • replace_if_present – if True, replace existing class if present

  • kwargs – additional ClassDefinition properties

  • use_attributes – if True, add slots as attributes

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

  • permissible_values

  • replace_if_present

  • kwargs

Returns:

builder

Raises:

ValueError – if enum already exists and replace_if_present=False

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

Adds a prefix for use with CURIEs

Parameters:
  • prefix

  • url

  • replace_if_present

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