Modules

class semdsl.dsl_engine.DSLEngine(schemaview: SchemaView | None = None, _grammar: SchemaGrammar | None = None, _lark_serialization: str | None = None, _compiled_grammar_module: module | None = None, _compiled_datamodel: module | None = None, normalizer: ReferenceValidator | None = None)[source]

Engine for generating DSLs from LinkML Schemas and for parsing serialization to these DSLs.

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema("examples/clue/model_clue.yaml")  ## Annotated LinkML schema
>>> obj = engine.parse_as_dict("< Colonel Mustard in the Kitchen with the Candlestick >")
>>> print(obj)
{'person': 'Colonel Mustard', 'location': 'Kitchen', 'weapon': 'Candlestick'}

This works as follows:

  • The LinkML schema is loaded from a YAML source

  • A Lark grammar is generated from the LinkML schema, using annotations in the schema

  • The Lark grammar is used to parse the input into a tree

  • The tree is automatically transformed into an object conformant with the schema

schemaview: SchemaView | None = None

Wrapper onto a LinkML schema

normalizer: ReferenceValidator | None = None

Uses to normalize object structures prior to object initialization

load_schema(schema: str | TextIO | SchemaDefinition) None[source]

Load a schema from a LinkML schema source.

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema("examples/clue/model_clue.yaml")
Parameters:

schema – path to a schema or a schema object

parse_as_dict(input: str, start=None, target_class=None) dict[source]

Parses input string to a raw object/dict.

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema("examples/clue/model_clue.yaml")
>>> obj = engine.parse_as_dict("< Colonel Mustard in the Kitchen with the Candlestick >")
>>> print(obj)
{'person': 'Colonel Mustard', 'location': 'Kitchen', 'weapon': 'Candlestick'}
Parameters:
  • input – input string to parse

  • start – start symbol in grammar

  • target_class – target data model class for parsing

Returns:

input parsed into a dict object conforming to the schema/data model

parse_as_object(input: str, start=None, target_class: str | Type | None = None) BaseModel[source]

Parses the input string into a pydantic BaseModel.

Note

this requires EITHER that the main linkml package is installed, OR that the module for the pydantic data model is set using compiled_datamodel

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema("examples/clue/model_clue.yaml")
>>> obj = engine.parse_as_object("< Colonel Mustard in the Kitchen with the Candlestick >")
>>> print(type(obj))
<class 'test.ClueHypothesis'>
>>> print(obj.weapon)
Candlestick
>>> print(obj.dict())
{'person': 'Colonel Mustard', 'location': 'Kitchen', 'weapon': 'Candlestick'}
Parameters:
  • input – input string to parse

  • start – start symbol in grammar

  • target_class – target data model class for parsing

Returns:

pydantic object representing the parsed input

property grammar: SchemaGrammar

Access the grammar object.

Note

for most purposes, you do not need to access the grammar object directly.

If the grammar object is not explicitly set, this will generate a grammar from the schema, using annotations if present, and auto-generating otherwise.

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema("examples/clue/model_clue.yaml")
>>> grammar = engine.grammar
>>> print(grammar.rules[0].lhs_symbol)
class_clue_hypothesis
Returns:

a SchemaGrammar object

load_grammar(grammar: str | Path | TextIO | SchemaGrammar) None[source]

Load a grammar from a DSL YAML file.

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_grammar("examples/linkml_lite/linkml_model_lite.semdsl.yaml")
>>> grammar = engine.grammar
>>> print(grammar.rules[0].lhs_symbol)
schema
>>> print(engine.lark_serialization)
from lark import Lark
...
schema : schema_id schema_name description? (comment|import|prefix|class|slot|type|enum)*
class : "class" class_name is_a? mixins? "{" description? class_uri? (comment|attribute|slot_usage|slots)* "}"
slot : "slot" slot_block
...
Parameters:

grammar – path to a YAML file containing the grammar using the semdsl metamodel

generate_grammar() None[source]

Generate a grammar from the current schema.

This will use annotations if present, and auto-generate otherwise.

Note

if the grammar has previously been set, this will overwrite it.

property lark_serialization: str

Serialize as a lark program.

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema("examples/clue/model_clue.yaml")
>>> print(engine.lark_serialization)
from lark import Lark
...
class_clue_hypothesis : "<" person "in the" location "with the" weapon ">"
person : WORD WORD
location : WORD
weapon : WORD
...
Returns:

a string containing the lark program

property compiled_grammar_module: module

Get the python module for the compiled grammar.

Note that most clients do not need to use this directly.

property compiled_datamodel: module

Get the pydantic module for the current data model.

parse_tree(input: str, start=None, target_class=None) Tree[source]

Generates Lark parse tree using grammar from input string.

Parameters:
  • input

  • start

  • target_class

Returns: