Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions linkml_runtime/utils/yamlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import yaml
from deprecated.classic import deprecated
from jsonasobj2 import JsonObj, JsonObjTypes, as_dict, as_json, items
from rdflib import Graph, URIRef
from jsonasobj2 import JsonObj, as_json, as_dict, JsonObjTypes, items
import jsonasobj2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is unused

from rdflib import Graph, URIRef, Literal
from yaml.constructor import ConstructorError

from linkml_runtime.utils.context_utils import CONTEXTS_PARAM_TYPE, merge_contexts
Expand Down Expand Up @@ -85,6 +86,17 @@ def _default(self, obj, filtr: Callable[[dict], dict] = None):
# elif isinstance(v, EnumDefinition):
elif isinstance(v, EnumDefinitionImpl):
rval[k] = v.code
elif isinstance(v, Literal):
if v.datatype:
# use v and not not v.value to not serialize the python object (datetime, xml, html, ...)
rval[k] = {
'@value': str(v), '@type': v.datatype}
elif v.language:
# language tag format https://www.rfc-editor.org/rfc/rfc5646#section-2.1.1
rval[k] = {
'@value': v.value, '@language': v.language}
else:
rval[k] = v
else:
rval[k] = v
return rval
Expand Down
33 changes: 33 additions & 0 deletions poetry.lock

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

21 changes: 21 additions & 0 deletions pyproject.toml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these entries should be removed-- html5lib should be added to the [dependencies] section in pyproject.toml.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ enable = true
vcs = "git"
style = "pep440"

[tool.poetry.scripts]
comparefiles = "linkml_runtime.utils.comparefiles:cli"
linkml-normalize = "linkml_runtime.processing.referencevalidator:cli"

[tool.poetry.dependencies]
python = "^3.8"
click = "*"
deprecated = "*"
hbreader = "*"
json-flattener = ">=0.1.9"
jsonasobj2 = "==1.*,>=1.0.0,>=1.0.4"
jsonschema = ">=3.2.0"
prefixcommons = ">=0.1.12"
pyyaml = "*"
rdflib = ">=6.0.0"
requests = "*"
prefixmaps = ">=0.1.4"
curies = ">=0.5.4"
pydantic = ">=1.10.2, <3.0.0"
html5lib = "^1.1"

[tool.poetry.group.dev.dependencies]
coverage = "^6.2"
requests-cache = "^1.2.0"
Expand Down
200 changes: 200 additions & 0 deletions tests/test_issues/test_linkml_runtime_issue_2475.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the strategy here to define a schema inline-- if this test were merged, the definition would potentially have to be kept up to date with changes in behavior of linkml runtime. (There's already one here, as dataclasses_init_fn_with_kwargs has been deprecated).

There's already a Person schema defined in tests. Can that be reused here to define a test case? (See these lines).

It might be good to add a new test case to that file instead of adding a new file here. (Although others may have a different opinion). There's another test there that is doing something very similar to what this test is doing:

# see https://github.com/linkml/linkml/issues/429
SCHEMA_429 = INPUT_PATH / "personinfo_test_issue_429.yaml"
DATA_429 = INPUT_PATH / "example_personinfo_test_issue_429_data.yaml"
OUT_429 = OUTPUT_PATH / "example_personinfo_test_issue_429_data.ttl"

...

@pytest.fixture
def issue_429_graph():
    """Create RDF graph for issue 429 testing."""
    view = SchemaView(str(SCHEMA_429))
    container = yaml_loader.load(str(DATA_429), target_class=Container_429)
    rdflib_dumper.dump(container, schemaview=view, to_file=str(OUT_429))
    g = Graph()
    g.parse(str(OUT_429), format="ttl")
    return g

Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import dataclasses
from dataclasses import dataclass
import json
from linkml_runtime import SchemaView
from linkml_runtime.dumpers import rdflib_dumper
from linkml_runtime.utils.metamodelcore import XSDDateTime
from linkml_runtime.utils.yamlutils import YAMLRoot, as_rdf
from linkml_runtime.utils.slot import Slot
from linkml_runtime.utils.dataclass_extensions_376 import dataclasses_init_fn_with_kwargs
from linkml_runtime.utils.curienamespace import CurieNamespace
from rdflib import Graph, Literal, URIRef
from typing import (
Any,
ClassVar,
Dict,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since support for Python 3.8 was dropped earlier this year, these imports should be removed, and plain old dict and list should be used.

List,
Optional,
Union
)
import unittest


schema_content = """
id: Literal2JsonLD
name: Literal2JsonLD
prefixes:
ex: http://example.org/
linkml: https://w3id.org/linkml/
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs: http://www.w3.org/2000/01/rdf-schema#
vcard: http://www.w3.org/2006/vcard/ns#
default_prefix: ex
default_range: string
imports:
- linkml:types
classes:
Person:
class_uri: vcard:Individual
slots:
- comment
- nickname
slots:
comment:
slot_uri: rdfs:comment
nickname:
slot_uri: vcard:nickname
"""


#
# below was generated with gen-python using the above schema
#

# Auto generated from Literal2JsonLD.yaml by pythongen.py version: 0.0.1
# Generation date: 2025-01-01T22:37:07
# Schema: Literal2JsonLD
#
# id: Literal2JsonLD
# description:
# license: https://creativecommons.org/publicdomain/zero/1.0/


metamodel_version = "1.7.0"
version = None

# Overwrite dataclasses _init_fn to add **kwargs in __init__
dataclasses._init_fn = dataclasses_init_fn_with_kwargs

# Namespaces
EX = CurieNamespace('ex', 'http://example.org/')
LINKML = CurieNamespace('linkml', 'https://w3id.org/linkml/')
RDF = CurieNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
RDFS = CurieNamespace('rdfs', 'http://www.w3.org/2000/01/rdf-schema#')
VCARD = CurieNamespace('vcard', 'http://www.w3.org/2006/vcard/ns#')
DEFAULT_ = EX


# Types

# Class references


@dataclass(repr=False)
class Person(YAMLRoot):
_inherited_slots: ClassVar[List[str]] = []

class_class_uri: ClassVar[URIRef] = VCARD["Individual"]
class_class_curie: ClassVar[str] = "vcard:Individual"
class_name: ClassVar[str] = "Person"
class_model_uri: ClassVar[URIRef] = EX.Person

comment: Optional[str] = None
nickname: Optional[str] = None

def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
if self.comment is not None and not isinstance(self.comment, str):
self.comment = str(self.comment)

if self.nickname is not None and not isinstance(self.nickname, str):
self.nickname = str(self.nickname)

super().__post_init__(**kwargs)


# Enumerations


# Slots
class slots:
pass


slots.comment = Slot(uri=RDFS.comment, name="comment", curie=RDFS.curie('comment'),
model_uri=EX.comment, domain=None, range=Optional[str])

slots.nickname = Slot(uri=VCARD.nickname, name="nickname", curie=VCARD.curie('nickname'),
model_uri=EX.nickname, domain=None, range=Optional[str])

#
# below was generated with gen-jsonld-context using the above schema
#

context = {
"comments": {
"description": "Auto generated by LinkML jsonld context generator",
"generation_date": "2025-01-01T23:08:58",
"source": "Literal2JsonLD.yaml"
},
"@context": {
"xsd": "http://www.w3.org/2001/XMLSchema#",
"ex": "http://example.org/",
"linkml": "https://w3id.org/linkml/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"vcard": "http://www.w3.org/2006/vcard/ns#",
"@vocab": "http://example.org/",
"comment": {
"@id": "rdfs:comment"
},
"nickname": {
"@id": "vcard:nickname"
},
"Person": {
"@id": "vcard:Individual"
}
}
}


#
# the tests start here
#


class TestConvertingRDFLibLiteral(unittest.TestCase):
"""
See https://github.com/linkml/linkml/issues/2475
"""

def setUp(self) -> None:
self.schemaview = SchemaView(schema_content)
self.person = Person(
nickname=Literal("Bob", lang="en"),
comment=Literal("Bob is <b>strong</b>", datatype=RDF.HTML))

def _test_jsonld_has_tags(self, person: dict):
self.assertIn('@type', person)
self.assertIn(
'http://www.w3.org/2006/vcard/ns#Individual', person['@type'])
self.assertIn('http://www.w3.org/2000/01/rdf-schema#comment', person)
self.assertIn({'@type': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML',
'@value': 'Bob is <b>strong</b>'}, person['http://www.w3.org/2000/01/rdf-schema#comment'])
self.assertIn('http://www.w3.org/2006/vcard/ns#nickname', person)
self.assertIn({'@language': 'en', '@value': 'Bob'},
person['http://www.w3.org/2006/vcard/ns#nickname'])

def test_rdflib_dumper_with_schemaview(self):
g = Graph()
g += rdflib_dumper.as_rdf_graph(element=self.person,
schemaview=self.schemaview)
self.assertEqual(len(g), 3)
jsonld = json.loads(g.serialize(format="json-ld"))
print(jsonld)
self.assertEqual(len(jsonld), 1)
person = jsonld[0]
self._test_jsonld_has_tags(person)

def test_rdflib_dumper_with_context(self):
g = Graph()
g += as_rdf(self.person, contexts=context)
self.assertEqual(len(g), 3)
jsonld = json.loads(g.serialize(format="json-ld"))
print(jsonld)
self.assertEqual(len(jsonld), 1)
person = jsonld[0]
self._test_jsonld_has_tags(person)


if __name__ == "__main__":
unittest.main()