Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Semantic-DSL

For rapid development of semantically-backed Domain-Specific Languages (DSLs),

Installation

pip install semdsl

Usage

To illustrate the usage of semdsl, we will create a simple LinkML schema for part of the Clue board game, in particular for representing a hypothesis about who committed the misdeed, where, and with what.

We will annotate the schema with grammar hints, that can be used to generate the grammar for the DSL.

```python
>>> schema = """
... id: https://example.org/clue
... name: clue
... imports:
...   - https://w3id.org/linkml/types
... classes:
...   ClueHypothesis:
...     attributes:
...       person:   # e.g. Colonel Mustard
...         annotations:
...           grammar.main: "WORD WORD"
...       location: # e.g. Kitchen
...         annotations:
...           grammar.main: "WORD"
...       weapon:   # e.g. Candlestick
...         annotations:
...           grammar.main: "WORD"
...     annotations:
...       grammar.main: >-
...         "<" person "in the" location "with the" weapon ">" 
... """

The idea is to be able to represent hypotheses using strings like <Colonel Mustard in the Kitchen with the Candlestick>.

We can then use the DSLEngine class to load the schema and generate a grammar:

>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema(schema)
>>> 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
...

The default is Lark syntax.

You can then use the generated grammar to parse serializations into pydantic objects that are schema conformant:

>>> obj = engine.parse_as_object('<Colonel Mustard in the Kitchen with the Candlestick>')
>>> print(obj.location)
Kitchen
>>> print(obj.json())
   {"person": "Colonel Mustard", "location": "Kitchen", "weapon": "Candlestick"}

Auto-assigning production rules

In the previous example we saw how we could annotate an existing schema with grammar rules

However, we can also generate grammar rules from the schema itself.

This is done by using the grammar.main annotation on a class, and then using the grammar.auto annotation on the attributes of that class. For example:

>>> schema = """
... id: https://example.org/clue
... name: clue
... imports:
...   - https://w3id.org/linkml/types
... classes:
...   ClueHypothesis:
...     attributes:
...       person:   # e.g. Colonel Mustard
...       location: # e.g. Kitchen
...       weapon:   # e.g. Candlestick
... """

now we will create a new engine and load the schema, and generate a de-novo "functional-style" grammar:

>>> engine = DSLEngine() ## create new DSLEngine
>>> engine.load_schema(schema)
>>> print(engine.lark_serialization)
from lark import Lark
...
class_clue_hypothesis : "ClueHypothesis(" slot_clue_hypothesis__person? slot_clue_hypothesis__location? slot_clue_hypothesis__weapon? ")"
slot_clue_hypothesis__person : "person=" TYPE_STRING
slot_clue_hypothesis__location : "location=" TYPE_STRING
slot_clue_hypothesis__weapon : "weapon=" TYPE_STRING
...

You can then use the generated grammar to parse strings into objects:

>>> obj = engine.parse_as_object('ClueHypothesis(person="Colonel Mustard" location="Kitchen" weapon="Candlestick")')
>>> print(obj.location)
Kitchen

Adding additional semantics

You can use the following metamodel element:

to assign URIs to classes and slots in your schema, which can be used in RDF serialization.

Here we extend our Clue schema, adding classes for the ranges of the slots in the main class:

>>> schema = """
... id: https://example.org/clue
... name: clue
... prefixes:
...   linkml: https://w3id.org/linkml/
...   clue: https://example.org/clue/
...   schema: http://schema.org/
...   prov: http://www.w3.org/ns/prov#
...   dbpedia: http://dbpedia.org/ontology/
... imports:
...   - linkml:types
... classes:
...   NamedThing:
...     class_uri: schema:Thing
...     attributes:
...       id:
...         identifier: true
...       range: uriorcurie
...   Person:
...     class_uri: schema:Person
...     is_a: NamedThing
...   Location:
...     class_uri: schema:Location
...     is_a: NamedThing
...   Weapon:
...     class_uri: dbpedia:Weapon
...     is_a: NamedThing
...   ClueHypothesis:
...     class_uri: prov:Action
...     tree_root: true
...     attributes:
...       person:   # e.g. Colonel Mustard
...         slot_uri: prov:wasAssociatedWith
...         range: Person
...         annotations:
...           grammar.main: TYPE_URIORCURIE
...       location: # e.g. Kitchen
...         slot_uri: prov:atLocation
...         range: Location
...         annotations:
...           grammar.main: TYPE_URIORCURIE
...       weapon:   # e.g. Candlestick
...         slot_uri: prov:used
...         range: Weapon
...         annotations:
...           grammar.main: TYPE_URIORCURIE
...     annotations:
...       grammar.main: >-
...         "<" person "in the" location "with the" weapon ">"
... """

Now parse and export to a file. This time the input string uses CURIEs to represent the different things in the Clue hypothesis.

>>> engine = DSLEngine()
>>> engine.load_schema(schema)
>>> obj = engine.parse_as_object("< clue:ColonelMustard in the clue:Kitchen with the clue:Candlestick >")
>>> import yaml
>>> with open("tests/output/clue-output.yaml", "w", encoding="utf-8") as f:
...     yaml.dump(obj.dict(), f)

From here we can use LinkML to convert to an RDF serialization:

cd clue-output.yaml
linkml-convert clue-output -s clue_model.yaml -t ttl

Results:

@prefix clue: <https://example.org/clue/> .
@prefix prov: <http://www.w3.org/ns/prov#> .

[] a prov:Action ;
    prov:atLocation clue:Kitchen ;
    prov:used clue:Candlestick ;
    prov:wasAssociatedWith clue:ColonelMustard .

Command Line Interface

semdsl --help

Limitations

Restricted to Lark grammars

Currently, semdsl only supports Lark grammars. The framework is designed to allow extensibility, e.g. to ANTLR, but this is currently unsupported.

Release files for semdsl 0.0.1rc1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semdsl 0.0.1rc1
File Size Uploaded
semdsl-0.0.1rc1.tar.gz 16.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for semdsl 0.0.1rc1
File Interpreter ABI Platform
semdsl-0.0.1rc1-py3-none-any.whl Python 3 none any Details

Total release size: 36.6 kB

Release files / semdsl-0.0.1rc1.tar.gz

Download URL semdsl-0.0.1rc1.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
632180978d2f3bdf1cfba53952dc52dba935422632b0810f510b4bc60de8fd89
BLAKE2b-256 checksum
How to use checksums
8a0aaebca01345b205692f695e51ba4beac04c2972f804101b4f5af8f3b4f5ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.1 CPython/3.9.16 Darwin/21.6.0

Release files / semdsl-0.0.1rc1-py3-none-any.whl

Download URL semdsl-0.0.1rc1-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1c9ddd482a702a7ac54adef15fcfe301825a02fd43d036aed2c96a34141c0e8
BLAKE2b-256 checksum
How to use checksums
31d0d8bbbd4beed259063c729213e02ec053313b36a124c5330941ec0b674955
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.1 CPython/3.9.16 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

0.0.1rc1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page