drepr.models.parsers.v3.sm_parser#

Classes

SMParseContext(nodes, ...)

SMParser(default_resource_id, resources, ...)

This SemanticModel parser supports various ways to define a semantic model.

class SMParseContext(nodes: dict[str, drepr.models.sm.ClassNode | drepr.models.sm.DataNode | drepr.models.sm.LiteralNode] = <factory>, edges: dict[int, drepr.models.sm.Edge] = <factory>, prefixes: dict[str, str] = <factory>)[source]#

Bases: object

Parameters:
nodes: dict[str, drepr.models.sm.ClassNode | drepr.models.sm.DataNode | drepr.models.sm.LiteralNode]#
edges: dict[int, drepr.models.sm.Edge]#
prefixes: dict[str, str]#
class SMParser(default_resource_id: str, resources: list[drepr.models.resource.Resource], attrs: ParsedAttrs, path_parser: PathParser, attr_parser: AttrParser)[source]#

Bases: object

This SemanticModel parser supports various ways to define a semantic model.

``` semantic_model:

<class_id>:

[id]: <class_id> [uri]: <uri> [properties]: <properties> [links]: <links> [static_properties]: <static_properties> [inverse_static_properties]: <inverse_static_properties> [subject]: <attr>

prefixes:

<prefix>: <absolute uri>

```

where:

  • <class_id>: (optional) is the id of the class. It can be specified via the key or as in a separate field named <id>

  • <uri> (optional): if not provided, we extract the URI from the class_id and the class id

must have the following format: <prefix>:<name>:<alphanumeric & _> where <prefix>:<name> is used to create the URI. * <properties>: (optional)

  1. If it is a list, each item can be either:
    1. [<predicate>, <attr>]

    2. [<predicate>, <attr>, <data_type>]

    3. [<predicate>, <attr>, <is_required>]

    4. [<predicate>, <attr>, <data_type>, <is_required>]

  2. If it is a tuple, each item is (<predicate>, value) where value can be either:
    1. { attr: <attr>, [data_type: <data_type=none>], [is_required: <is_required=false>] }

    2. <attr>

  • <predicate> is either relative or absolute URI.

  • <data_type> is either relative or absolute URI specifying the data type of the property.

  • <is_required> is a boolean value specifying whether the property is required or not.

  • <attr> is the attribute of the property. It can be either a string for the attribute id or a dictionary

    to construct the attribute on fly. The dictionary will comply with the schema of the attribute.

  • <static_properties>: (optional)
    1. If it is a list, each item can be either:
      1. [<predicate>, <value>]

      2. [<predicate>, <value>, <data_type>]

    2. If it is a dictionary, each item is (<predicate>, item-value) where item-value can be either:
      1. { value: <value>, data_type: <data_type> }

      2. <value>

    • <predicate> is either relative or absolute URI.

    • <data_type> is either relative or absolute URI specifying the data type of the property.

    • <value> is the value of the property. It must be either a string, integer, float, or boolean.

  • <inverse_static_properties>: (optional)
    1. If it is a list, each item can be either:
      1. [<predicate>, <value>]

    2. If it is a tuple, each item is (<predicate>, item-value) where item-value must be a string, integer, float, or boolean.

  • <links>: (optional)
    1. If it is a list, each item can be either:

      a. [<predicate>, <target>] a. [<predicate>, <target>, <is_required>]

    2. If it is a tuple, each item is (<predicate>, item-value) where item-value can be either:
      1. { target: <target>, is_required: <is_required> }

      2. <target>

    3. If it is a dict, then it is:
      1. { prop: <predicate>, target: <target>, [is_required: <is_required=false>] }

    • <predicate> is either relative or absolute URI.

    • <is_required> is a boolean value specifying whether the link is required or not.

    • <target> is the target of the link. It can be either a string for the class id or a dictionary

      to construct the class on fly. The dictionary will comply with the schema of the class.

  • <subject> (optional): is the attribute that can be used as subject attribute of the class. It can be either a string

    for the attribute id or a dictionary to construct the attribute on fly. The dictionary will comply with the schema of the attribute.

Parameters:
CLS_KEYS = {'id', 'inverse_static_properties', 'links', 'properties', 'static_properties', 'subject', 'uri'}#
REG_SM_CLASS = re.compile('^((.+):[a-zA-Z0-9_]+)$')#
REG_SM_DNODE = re.compile('^((?:(?!--).)+:\\d+)--((?:(?!\\^\\^).)+)(?:\\^\\^(.+))?$')#
REG_SM_LNODE = re.compile('^((?:(?!--).)+:\\d+)--((?:(?!--).)+)--((?:(?!\\^\\^).)+)(?:\\^\\^(.+))?$')#
REG_SM_REL = re.compile('^((?:(?!--).)+:\\d+)--((?:(?!--).)+)--((?:(?!--).)+:\\d+)$')#
parse(sm: dict) SemanticModel[source]#
Parameters:

sm (dict) –

Return type:

SemanticModel

parse_class(context: SMParseContext, class_id: str, class_conf: dict)[source]#
Parameters:
parse_static_property(context: SMParseContext, class_id: str, prop: tuple | list, trace: str)[source]#

Parse a static property of a class. Can parse the following format:

  1. If prop is a list, it can be either:
    1. [<predicate>, <value>]

    2. [<predicate>, <value>, <data_type>]

  2. If prop is a tuple, it must be a tuple of (key, val), where val can be either:
    1. { value: <value>, data_type: <data_type> }

    2. <value>

  • <predicate> is either relative or absolute URI.

  • <data_type> is either relative or absolute URI specifying the data type of the property.

  • <value> is the value of the property. It must be either a string, integer, float, or boolean.

Parameters:
parse_property(context: SMParseContext, class_id: str, prop: tuple | list, trace: str)[source]#

Parse a property of a class. Can parse the following format:

  1. If prop is a list, it can be either:
    1. [<predicate>, <attr>]

    2. [<predicate>, <attr>, <data_type>]

    3. [<predicate>, <attr>, <is_required>]

    4. [<predicate>, <attr>, <data_type>, <is_required>]

  2. If prop is a tuple, it must be a tuple of (key, value), where value can be either:
    1. { attr: <attr>, [data_type: <data_type=none>], [is_required: <is_required=false>] }

    2. <attr>

  • <predicate> is either relative or absolute URI.

  • <data_type> is either relative or absolute URI specifying the data type of the property.

  • <is_required> is a boolean value specifying whether the property is required or not.

  • <attr> is the attribute of the property. It can be either a string for the attribute id or a dictionary

    to construct the attribute on fly. The dictionary will comply with the schema of the attribute.

Parameters:
parse_inverse_static_property(context: SMParseContext, class_id: str, prop: tuple | list, trace: str)[source]#

Parse an inverse static property of a class. Can parse the following format:

  1. If prop is a list, it can be either:
    1. [<predicate>, <value>]

  2. If prop is a tuple, it must be a tuple of (key, value).

  • <predicate> is either relative or absolute URI.

  • <value> is the value of the property. It must be either a string, integer, float, or boolean.

Parameters:

Parse a link of a class. Can parse the following format:

  1. If link is a list, it can be either:

    a. [<predicate>, <target>] a. [<predicate>, <target>, <is_required>]

  2. If link is a tuple, it must be a tuple of (key, value), where value can be either:
    1. { target: <target>, is_required: <is_required> }

    2. <target>

  • <predicate> is either relative or absolute URI.

  • <is_required> is a boolean value specifying whether the link is required or not.

  • <target> is the target of the link. It can be either a string for the class id or a dictionary

    to construct the class on fly. The dictionary will comply with the schema of the class.

Parameters:
create_attr_if_needed(attr: Any, trace: str)[source]#
Parameters:
  • attr (Any) –

  • trace (str) –