SchemaView#

The SchemaView class in the linkml-runtime provides a method for dynamically introspecting and manipulating schemas.

See SchemaView notebooks

class linkml_runtime.utils.schemaview.SchemaView(schema: str | Path | SchemaDefinition, importmap: Dict[str, str] | None = None, merge_imports: bool = False, base_dir: str | None = None)[source]#

A SchemaView provides a virtual schema layered on top of a schema plus its import closure

Most operations are parameterized by imports. If this is set to True (default), then the full import closure is considered when answering

This class utilizes caching for efficient lookup operations.

TODO: decide how to use this in conjunction with the existing schemaloader, which injects into the schema rather than providing dynamic methods.

See:
add_class(cls: ClassDefinition) None[source]#
Parameters:

cls – class to be added

Returns:

add_enum(enum: EnumDefinition) None[source]#
Parameters:

enum – enum to be added

Returns:

add_slot(slot: SlotDefinition) None[source]#
Parameters:

slot – slot to be added

Returns:

add_subset(subset: SubsetDefinition) None[source]#
Parameters:

subset – subset to be added

Returns:

add_type(type: TypeDefinition) None[source]#
Parameters:

type – type to be added

Returns:

all_aliases() List[str][source]#

Get the aliases

Returns:

list of aliases

all_class(imports=True) Dict[ClassDefinitionName, ClassDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all classes in schema view

all_classes(ordered_by=OrderedBy.PRESERVE, imports=True) Dict[ClassDefinitionName, ClassDefinition][source]#
Parameters:
  • ordered_by – an enumerated parameter that returns all the classes in the order specified.

  • imports – include imports closure

Returns:

all classes in schema view

all_element(imports=True) Dict[ElementName, Element][source]#
Parameters:

imports – include imports closure

Returns:

all elements in schema view

all_elements(imports=True) Dict[ElementName, Element][source]#
Parameters:

imports – include imports closure

Returns:

all elements in schema view

all_enum(imports=True) Dict[EnumDefinitionName, EnumDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all enums in schema view

all_enums(imports=True) Dict[EnumDefinitionName, EnumDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all enums in schema view

all_schema(imports: bool = True) List[SchemaDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all schemas

all_slot(**kwargs) Dict[SlotDefinitionName, SlotDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all slots in schema view

all_slots(ordered_by=OrderedBy.PRESERVE, imports=True, attributes=True) Dict[SlotDefinitionName, SlotDefinition][source]#
Parameters:
  • ordered_by – an enumerated parameter that returns all the slots in the order specified.

  • imports – include imports closure

  • attributes – include attributes as slots or not, default is to include.

Returns:

all slots in schema view

all_subset(imports=True) Dict[SubsetDefinitionName, SubsetDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all subsets in schema view

all_subsets(imports=True) Dict[SubsetDefinitionName, SubsetDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all subsets in schema view

all_type(imports=True) Dict[TypeDefinitionName, TypeDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all types in schema view

all_types(imports=True) Dict[TypeDefinitionName, TypeDefinition][source]#
Parameters:

imports – include imports closure

Returns:

all types in schema view

annotation_dict(element_name: ElementName, imports=True) Dict[URIorCURIE, Any][source]#

Return a dictionary where keys are annotation tags and values are annotation values for any given element.

Note this will not include higher-order annotations

See also: https://github.com/linkml/linkml/issues/296

Parameters:
  • element_name

  • imports

Returns:

annotation dictionary

class_ancestors(class_name: ClassDefinitionName | str, imports=True, mixins=True, reflexive=True, is_a=True, depth_first=True) List[ClassDefinitionName][source]#

Closure of class_parents method

Parameters:
  • class_name – query class

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of ancestors

  • depth_first

Returns:

ancestor class names

class_children(class_name: ClassDefinitionName | str, imports=True, mixins=True, is_a=True) List[ClassDefinitionName][source]#
Parameters:
  • class_name – parent class name

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

Returns:

all direct child class names (is_a and mixins)

class_descendants(class_name: ClassDefinitionName | str, imports=True, mixins=True, reflexive=True, is_a=True) List[ClassDefinitionName][source]#

Closure of class_children method

Parameters:
  • class_name – query class

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of descendants

Returns:

descendants class names

class_induced_slots(class_name: ClassDefinitionName | str = None, imports=True) List[SlotDefinition][source]#

All slots that are asserted or inferred for a class, with their inferred semantics

Parameters:
  • class_name

  • imports

Returns:

inferred slot definition

class_leaves(imports=True, mixins=True, is_a=True) List[ClassDefinitionName][source]#

All classes that have no children :param imports: :param mixins: :param is_a: include is_a parents (default is True) :return:

class_name_mappings() Dict[str, ClassDefinition][source]#

Mapping between processed safe class names (following naming conventions) and classes.

For example, a class may have name ‘named thing’, the code-safe version is NamedThing

Returns:

mapping from safe names to class

class_parents(class_name: ClassDefinitionName | str, imports=True, mixins=True, is_a=True) List[ClassDefinitionName][source]#
Parameters:
  • class_name – child class name

  • imports – include import closure

  • mixins – include mixins (default is True)

Returns:

all direct parent class names (is_a and mixins)

class_roots(imports=True, mixins=True, is_a=True) List[ClassDefinitionName][source]#

All classes that have no parents :param imports: :param mixins: :param is_a: include is_a parents (default is True) :return:

class_slots(class_name: ClassDefinitionName | str, imports=True, direct=False, attributes=True) List[SlotDefinitionName][source]#
Parameters:
  • class_name

  • imports – include imports closure

  • direct – only returns slots directly associated with a class (default is False)

  • attributes – include attribute declarations as well as slots (default is True)

Returns:

all slot names applicable for a class

delete_class(class_name: ClassDefinitionName, delete_references=True) None[source]#
Parameters:

class_name – class to be deleted

Returns:

delete_enum(enum_name: EnumDefinitionName) None[source]#
Parameters:

enum_name – enum to be deleted

Returns:

delete_slot(slot_name: SlotDefinitionName) None[source]#
Parameters:

slot_name – slot to be deleted

Returns:

delete_subset(subset_name: SubsetDefinitionName) None[source]#
Parameters:

subset_name – subset to be deleted

Returns:

delete_type(type_name: TypeDefinitionName) None[source]#
Parameters:

type_name – type to be deleted

Returns:

enum_ancestors(enum_name: EnumDefinitionName | str, imports=True, mixins=True, reflexive=True, is_a=True, depth_first=True) List[EnumDefinitionName][source]#

Closure of enum_parents method

Parameters:
  • enum_name – query enum

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of ancestors

  • depth_first

Returns:

ancestor enum names

enum_parents(enum_name: EnumDefinitionName | str, imports=False, mixins=False, is_a=True) List[EnumDefinitionName][source]#
Parameters:
  • enum_name – child enum name

  • imports – include import closure (False)

  • mixins – include mixins (default is False)

Returns:

all direct parent enum names (is_a and mixins)

expand_curie(uri: str) str[source]#

Expands a URI or CURIE to a full URI :param uri: :return: URI as a string

get_children(name: str, mixin: bool = True) List[str][source]#

get the children of an element (any class, slot, enum, type) :param name: name of the parent element :param mixin: include mixins :return: list of child element

get_class(class_name: ClassDefinitionName | str, imports=True, strict=False) ClassDefinition[source]#
Parameters:
  • class_name – name of the class to be retrieved

  • imports – include import closure

Returns:

class definition

get_classes_by_slot(slot: SlotDefinition, include_induced: bool = False) List[ClassDefinitionName][source]#

Get all classes that use a given slot, either as a direct or induced slot.

Parameters:
  • slot – slot in consideration

  • include_induced – supplement all direct slots with induced slots, defaults to False

Returns:

list of slots, either direct, or both direct and induced

get_classes_modifying_slot(slot: SlotDefinition) List[ClassDefinition][source]#

Get all ClassDefinitions that modify a given slot.

Parameters:

slot_name – slot in consideration

Returns:

list of ClassDefinitions modifying the slot of interest

get_element(element: ElementName | Element, imports=True) Element[source]#

Fetch an element by name

Parameters:
  • element – query element

  • imports – include imports closure

Returns:

get_elements_applicable_by_identifier(identifier: str) List[str][source]#

Get a model element by identifier. The model element corresponding to the given identifier as available via the id_prefixes mapped to that element.

Parameters:

identifier

Returns:

Optional[str]

get_elements_applicable_by_prefix(prefix: str) List[str][source]#

Get a model element by prefix. The model element corresponding to the given prefix as available via the id_prefixes mapped to that element.

Parameters:

prefix – the prefix of a CURIE

Returns:

Optional[str]

get_enum(enum_name: EnumDefinitionName | str, imports=True, strict=False) EnumDefinition[source]#
Parameters:
  • enum_name – name of the enum to be retrieved

  • imports – include import closure

Returns:

enum definition

get_identifier_slot(cn: ClassDefinitionName | str, use_key=False, imports=True) SlotDefinition | None[source]#

Find the slot that is the identifier for the given class

Parameters:
  • cn – class name

  • imports

Returns:

name of slot that acts as identifier

get_key_slot(cn: ClassDefinitionName | str, imports=True) SlotDefinition | None[source]#

Find the slot that is the key for the given class

Parameters:
  • cn – class name

  • imports

Returns:

name of slot that acts as key

get_mapping_index(imports=True, expand=False) Dict[URIorCURIE, List[Tuple[str, Element]]][source]#

Returns an index of all elements keyed by the mapping value. The index values are tuples of mapping type and element

Parameters:
  • imports

  • expand – if true the index will be keyed by expanded URIs, not CURIEs

Returns:

index

get_mappings(element_name: ElementName = None, imports=True, expand=False) Dict[str, List[URIorCURIE]][source]#

Get all mappings for a given element

Parameters:
  • element_name – the query element

  • imports – include imports closure

  • expand – expand CURIEs to URIs

Returns:

index keyed by mapping type

get_slot(slot_name: SlotDefinitionName | str, imports=True, attributes=True, strict=False) SlotDefinition[source]#
Parameters:
  • slot_name – name of the slot to be retrieved

  • imports – include import closure

  • attributes – include attributes

  • strict – raise ValueError is not found

Returns:

slot definition

get_slots_by_enum(enum_name: EnumDefinitionName | str = None) List[SlotDefinition][source]#

Get all slots that use a given enum: schema defined, attribute, or slot_usage.

Parameters:

enum_name – enum in consideration

Returns:

list of slots, either schem or both class attribute defined

get_subset(subset_name: SubsetDefinitionName | str, imports=True, strict=False) SubsetDefinition[source]#
Parameters:
  • subset_name – name of the subsey to be retrieved

  • imports – include import closure

Returns:

subset definition

get_type(type_name: TypeDefinitionName | str, imports=True, strict=False) TypeDefinition[source]#
Parameters:
  • type_name – name of the type to be retrieved

  • imports – include import closure

Returns:

type definition

get_type_designator_slot(cn: ClassDefinitionName | str, imports=True) SlotDefinition | None[source]#
Parameters:
  • cn – class name

  • imports

Returns:

name of slot that acts as type designator for the given class

get_uri(element: ElementName | Element, imports=True, expand=False, native=False) str[source]#

Return the CURIE or URI for a schema element. If the schema defines a specific URI, this is used, otherwise this is constructed from the default prefix combined with the element name

Parameters:
  • element_name – name of schema element

  • imports – include imports closure

  • native – return the native CURIE or URI rather than what is declared in the uri slot

  • expand – expand the CURIE to a URI; defaults to False

Returns:

URI or CURIE as a string

importmap: Mapping[str, str] | None = None#

Optional mapping between schema names and local paths/URLs

imports_closure(imports: bool = True, traverse: bool | None = None, inject_metadata=True) List[SchemaDefinitionName][source]#

Return all imports

Objects in imported classes override one another in a “python-like” order - from the point of view of the importing schema, imports will override one another from first to last, recursively for each layer of imports.

An import tree like:

- main
  - s1
    - s1_1
    - s1_2
        - s1_2_1
        - s1_2_2
  - s2
    - s2_1
    - s2_2

will override objects with the same name, in order:

['s1_1', 's1_2_1', 's1_2_2', 's1_2', 's1', 's2_1', 's2_2', 's2']
Parameters:
  • imports – bool (default: True ) include imported schemas, recursively

  • traverse – bool, optional (default: True ) (Deprecated, use imports ). if true, traverse recursively

Returns:

all schema names in the transitive reflexive imports closure

in_schema(element_name: ElementName) SchemaDefinitionName[source]#
Parameters:

element_name

Returns:

name of schema in which element is defined

induced_class(class_name: ClassDefinitionName | str = None) ClassDefinition[source]#

Generate an induced class

  • the class will have no slots

  • the class will have one attribute per class_induced_slots

Induced slots are represented as attributes as these are fully locally owner by the class :param class_name: base class name :param imports: :return: induced class

induced_enum(enum_name: EnumDefinitionName | str = None) EnumDefinition[source]#
Parameters:

enum_name

Returns:

induced_slot(slot_name: SlotDefinitionName | str, class_name: ClassDefinitionName | str = None, imports=True, mangle_name=False) SlotDefinition[source]#

Given a slot, in the context of a particular class, yield a dynamic SlotDefinition that has all properties materialized.

This makes use of schema slots, such as attributes, slot_usage. It also uses ancestor relationships to infer missing values, for inheritable slots

Parameters:
  • slot_name – slot to be queries

  • class_name – class used as context

  • imports – include imports closure

Returns:

dynamic slot constructed by inference

induced_type(type_name: TypeDefinitionName | str = None) TypeDefinition[source]#
Parameters:

type_name

Returns:

inverse(slot_name: SlotDefinition)[source]#

Determines whether the given name is a relationship, and if that relationship has an inverse, returns the inverse.

Parameters:

slot_name – The name or alias of an element in the model

Returns:

inverse_name

is_inlined(slot: SlotDefinition, imports=True) bool[source]#

True if slot is inferred or asserted inline

Parameters:
  • slot

  • imports

Returns:

is_mixin(element_name: ElementName | Element)[source]#

Determines whether the given name is the name of a mixin in the model. An element is a mixin if one of its properties is “is_mixin:true”

Parameters:

element_name – The name or alias of an element in the model

Returns:

boolean

is_multivalued(slot_name: SlotDefinition) bool[source]#

returns True if slot is multivalued, else returns False :param slot_name: slot to test for multivalued :return boolean:

is_relationship(class_name: ClassDefinitionName | str = None, imports=True) bool[source]#

Tests if a class represents a relationship or reified statement

Parameters:
  • class_name

  • imports

Returns:

true if the class represents a relationship

is_slot_percent_encoded(slot: SlotDefinitionName) bool[source]#

True if slot or its range is has a percent_encoded annotation.

This is true for type fields that are the range of identifier columns, where the identifier is not guaranteed to be a valid URI or CURIE

Parameters:

slot

Returns:

load_import(imp: str, from_schema: SchemaDefinition | None = None)[source]#

Handles import directives.

The value of the import can be:

  • a URL (specified as either a full URL or a CURIE)

  • a local file path

The import should leave off the .yaml suffix.

If the import is a URL then the import is fetched over the network UNLESS this is a metamodel import, in which case it is fetched from within the linkml_runtime package, where the yaml is distributed. This ensures that the version of the metamodel imported matches the version of the linkml_runtime package.

In future, this mechanism may be extended to arbitrary modules, such that we avoid network dependence at runtime in general.

For local paths, the import is resolved relative to the directory containing the source file, or the URL of the source file, if it is a URL.

Parameters:
  • imp

  • from_schema

Returns:

materialize_derived_schema() SchemaDefinition[source]#

Materialize a schema view into a schema definition

materialize_patterns() None[source]#

Materialize schema by expanding structured patterns into regular expressions based on composite patterns provided in the settings dictionary.

merge_imports()[source]#

Merges the full imports closure

Returns:

merge_schema(schema: SchemaDefinition, clobber=False) None[source]#

merges another schema into this one.

If the other schema has an element with the same name as an element in this schema, then this element is NOT copied.

Parameters:
  • schema – schema to be merged

  • clobber – if True, then overwrite existing elements

ordered(elements: Dict[ElementNameType, ElementType], ordered_by: OrderedBy | None = None) Dict[ElementNameType, ElementType][source]#

Order a dictionary of elements with some ordering method in OrderedBy

permissible_value_ancestors(permissible_value_text: str, enum_name: EnumDefinitionName | str, reflexive=True, depth_first=True) List[str][source]#

Closure of permissible_value_parents method :enum

permissible_value_descendants(permissible_value_text: str, enum_name: EnumDefinitionName | str, reflexive=True, depth_first=True) List[str][source]#

Closure of permissible_value_children method :enum

permissible_value_parent(permissible_value: str, enum_name: EnumDefinitionName | str) str | PermissibleValueText | None | ValueError[source]#
Parameters:
  • enum_name – child enum name

  • permissible_value – permissible value

Returns:

all direct parent enum names (is_a)

slot_ancestors(slot_name: SlotDefinitionName | str, imports=True, mixins=True, reflexive=True, is_a=True) List[SlotDefinitionName][source]#

Closure of slot_parents method

Parameters:
  • slot_name – query slot

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of ancestors

Returns:

ancestor slot names

slot_applicable_range_elements(slot: SlotDefinition) List[ClassDefinitionName][source]#

Returns all applicable metamodel elements for a slot range (metamodel class names returned: class_definition, enum_definition, type_definition)

Typically any given slot has exactly one range, and one metamodel element type, but a proposed feature in LinkML 1.2 is range expressions, where ranges can be defined as unions

Additionally, if linkml:Any is a class_uri then this maps to the any element

Parameters:

slot

Returns:

list of element types

slot_children(slot_name: SlotDefinitionName | str, imports=True, mixins=True, is_a=True) List[SlotDefinitionName][source]#
Parameters:
  • slot_name – parent slot name

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

Returns:

all direct child slot names (is_a and mixins)

slot_descendants(slot_name: SlotDefinitionName | str, imports=True, mixins=True, reflexive=True, is_a=True) List[SlotDefinitionName][source]#

Closure of slot_children method

Parameters:
  • slot_name – query slot

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of descendants

Returns:

descendants slot names

slot_is_true_for_metadata_property(slot_name: SlotDefinition, metadata_property: str) bool[source]#

Returns true if the value of the provided “metadata_property” is True. For example, sv.slot_is_true_for_metadata_property(‘id’,’identifier’) will return True if the slot id has the identifier property set to True.

Parameters:
  • slot_name – slot to test for multivalued

  • metadata_property – controlled vocabulary for boolean attribtues

Returns:

boolean

slot_leaves(imports=True, mixins=True) List[SlotDefinitionName][source]#

All slotes that have no children :param imports: :param mixins: :return:

slot_name_mappings() Dict[str, SlotDefinition][source]#

Mapping between processed safe slot names (following naming conventions) and slots.

For example, a slot may have name ‘lives at’, the code-safe version is lives_at

Returns:

mapping from safe names to slot

slot_parents(slot_name: SlotDefinitionName | str, imports=True, mixins=True, is_a=True) List[SlotDefinitionName][source]#
Parameters:
  • slot_name – child slot name

  • imports – include import closure

  • mixins – include mixins (default is True)

Returns:

all direct parent slot names (is_a and mixins)

slot_range_as_union(slot: SlotDefinition) List[ElementName][source]#

Returns all applicable ranges for a slot

Typically any given slot has exactly one range, and one metamodel element type, but a proposed feature in LinkML 1.2 is range expressions, where ranges can be defined as unions

Parameters:

slot

Returns:

list of ranges

slot_roots(imports=True, mixins=True) List[SlotDefinitionName][source]#

All slotes that have no parents :param imports: :param mixins: :return:

type_ancestors(type_name: types, imports=True, reflexive=True, depth_first=True) List[TypeDefinitionName][source]#

All ancestors of a type via typeof

Parameters:
  • type_name – query type

  • imports – include import closure

  • reflexive – include self in set of ancestors

  • depth_first

Returns:

ancestor class names

type_parents(type_name: TypeDefinitionName | str, imports=True) List[TypeDefinitionName][source]#
Parameters:
  • type_name – child type name

  • imports – include import closure

Returns:

all direct parent enum names (is_a and mixins)

usage_index() Dict[ElementName, List[SchemaUsage]][source]#

Fetch an index that shows the ways in which each element is used

Returns:

dictionary of SchemaUsages keyed by used elements