SchemaView

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

See [SchemaView notebooks](https://github.com/linkml/linkml-runtime/tree/main/notebooks)

class linkml_runtime.utils.schemaview.SchemaView(schema: Union[str, SchemaDefinition], importmap: Optional[Mapping[str, str]] = 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_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 slots 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: 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: Union[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: Union[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: Union[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: Union[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: Union[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: Union[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: Union[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: Union[EnumDefinitionName, str], imports=True, mixins=True, is_a=True) List[EnumDefinitionName][source]
Parameters
  • enum_name – child enum name

  • imports – include import closure

  • mixins – include mixins (default is True)

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_class(class_name: Union[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_element(element: Union[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: Union[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: Union[ClassDefinitionName, str], use_key=False, imports=True) Optional[SlotDefinition][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: Union[ClassDefinitionName, str], imports=True) Optional[SlotDefinition][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: Union[SlotDefinitionName, str], imports=True, attributes=False, strict=False) SlotDefinition[source]
Parameters
  • slot_name – name of the slot to be retrieved

  • imports – include import closure

Returns

slot definition

get_subset(subset_name: Union[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: Union[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: Union[ClassDefinitionName, str], imports=True) Optional[SlotDefinition][source]
Parameters
  • cn – class name

  • imports

Returns

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

get_uri(element: Union[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

imports_closure(traverse=True, inject_metadata=True) List[SchemaDefinitionName][source]

Return all imports

Parameters

traverse – 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: Union[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: Union[EnumDefinitionName, str] = None) EnumDefinition[source]
Parameters

enum_name

Returns

induced_slot(slot_name: Union[SlotDefinitionName, str], class_name: Union[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: Union[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: Union[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: Union[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

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) None[source]

merges another schema into this one :param schema: schema to be merged

slot_ancestors(slot_name: Union[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: Union[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: Union[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: Union[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[EnumDefinitionName][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: Union[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