Skip to content

Commit

Permalink
WIP (sync between work places)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Oct 8, 2024
1 parent 92b60b0 commit 71c0099
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
16 changes: 7 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ linkml-runtime = "^1.7.2"
duckdb = "^0.10.1"
numpy = "<2.0"

Sphinx = { version = ">=4.4.0", optional = true }
sphinx-pdj-theme = { version = ">=0.2.1", optional = true }
sphinx-click = ">=3.1.0"
sphinxcontrib-mermaid = { version = ">=0.9.2", optional = true }

[tool.poetry.dev-dependencies]
pytest = ">=7.1.1"
Sphinx = ">=4.4.0"
sphinx-pdj-theme = ">=0.2.1"
sphinx-click = ">=3.1.0"
sphinxcontrib-mermaid = ">=0.9.2"
myst-parser = "*"
jupyter = ">=1.0.0"
lxml = ">=4.9.1"

#mariadb = { version = "^1.3", optional = true }

[tool.poetry.group.llm.dependencies]
llm = ">=0.12"

Expand All @@ -82,7 +85,7 @@ extract-schema = "schema_automator.utils.schema_extractor:cli"

[tool.poetry.extras]
docs = ["Sphinx", "sphinx-pdj-theme", "sphinxcontrib-mermaid"]
mariadb = ["mariadb"]
#mariadb = ["mariadb"]

[tool.codespell]
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
Expand Down
18 changes: 16 additions & 2 deletions schema_automator/importers/shacl_import_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from collections import defaultdict
import logging

from dataclasses import dataclass
from typing import Dict, List, Any

from rdflib import Graph, RDF, OWL, URIRef, RDFS, SKOS, SDO, Namespace

from funowl import Literal

from linkml.utils.schema_builder import SchemaBuilder
from linkml_runtime import SchemaView
from linkml_runtime.utils.formatutils import underscore
from linkml_runtime.utils.introspection import package_schemaview
from linkml_runtime.linkml_model import (
SchemaDefinition,
SlotDefinition,
ClassDefinition,
)
from schema_automator.importers.import_engine import ImportEngine

logger = logging.getLogger(__name__)

HTTP_SDO = Namespace("http://schema.org/")

Expand Down Expand Up @@ -80,7 +92,7 @@ def convert(
**kwargs,
) -> SchemaDefinition:
"""
Converts an OWL schema-style ontology
Converts an shacl shapes file
:param file:
:param name:
Expand Down Expand Up @@ -110,6 +122,7 @@ def convert(
if default_prefix not in schema.prefixes:
sb.add_prefix(default_prefix, model_uri, replace_if_present=True)
schema.id = schema.prefixes[default_prefix].prefix_reference

cls_slots = defaultdict(list)
props = []
for rdfs_property_metaclass in self._rdfs_metamodel_iri(
Expand Down Expand Up @@ -137,6 +150,7 @@ def convert(
slot = SlotDefinition(sn, **init_dict)
slot.slot_uri = str(p.n3(g.namespace_manager))
sb.add_slot(slot)

rdfs_classes = []
for rdfs_class_metaclass in self._rdfs_metamodel_iri(ClassDefinition.__name__):
for s in g.subjects(RDF.type, rdfs_class_metaclass):
Expand Down Expand Up @@ -201,7 +215,7 @@ def _element_from_iri(self, iri: URIRef) -> str:
r = self.reverse_metamodel_mappings.get(iri, [])
if len(r) > 0:
if len(r) > 1:
logging.debug(f"Multiple mappings for {iri}: {r}")
logger.debug(f"Multiple mappings for {iri}: {r}")
return r[0]

def _object_to_value(self, obj: Any, metaslot: SlotDefinition = None) -> Any:
Expand Down
3 changes: 0 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
import pprint

ROOT = os.path.abspath(os.path.dirname(__file__))
INPUT_DIR = os.path.join(ROOT, 'resources')
OUTPUT_DIR = os.path.join(ROOT, 'outputs')
MODEL_DIR = os.path.join(ROOT, 'test_models')


34 changes: 34 additions & 0 deletions tests/resources/shacl_simple.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# example from http://book.validatingrdf.com/bookHtml011.html#ch050SHACLExample

@prefix schema: <http://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.org/> .

ex:UserShape a sh:NodeShape;
sh:targetClass ex:User ;
sh:property [ # Blank node 1
sh:path schema:name ;
sh:minCount 1;
sh:maxCount 1;
sh:datatype xsd:string ;
] ;
sh:property [ # Blank node 2
sh:path schema:gender ;
sh:minCount 1;
sh:maxCount 1;
sh:or (
[ sh:in (schema:Male schema:Female) ]
[ sh:datatype xsd:string]
)
] ;
sh:property [ # Blank node 3
sh:path schema:birthDate ;
sh:maxCount 1;
sh:datatype xsd:date ;
] ;
sh:property [ # Blank node 4
sh:path schema:knows ;
sh:nodeKind sh:IRI ;
sh:class ex:User ;
] .
15 changes: 5 additions & 10 deletions tests/test_importers/test_shacl_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

# TODO - Write tests (this is a copy of test_rdfs_importer)

REPRO = os.path.join(INPUT_DIR, 'reproschema.ttl')
OUTSCHEMA = os.path.join(OUTPUT_DIR, 'reproschema-from-ttl.yaml')

REPRO = os.path.join(INPUT_DIR, 'shacl_simple.ttl')
OUTSCHEMA = os.path.join(OUTPUT_DIR, 'user_from_shacl_simple2.yaml')


def test_from_shacl():
"""Test Shacl conversion."""
oie = ShaclImportEngine()
sie = ShaclImportEngine()

return
schema = oie.convert(REPRO, default_prefix='reproschema', identifier='id')
schema = sie.convert(REPRO, default_prefix='usr', identifier='id')
write_schema(schema, OUTSCHEMA)
return
# roundtrip
s = YAMLGenerator(OUTSCHEMA).serialize()
print(s[0:100])
Expand All @@ -35,7 +34,3 @@ def test_from_shacl():
assert len(slots) == 1
slot = slots[0]
assert slot.name == "id"




0 comments on commit 71c0099

Please sign in to comment.