How to use RDFDumper? #1836
Unanswered
WolfgangFahl
asked this question in
Q&A
Replies: 2 comments
-
gen-jsonld-context personinfo.yaml > personinfo.context.jsonld? as described in https://linkml.io/linkml/generators/jsonld-context.html? |
Beta Was this translation helpful? Give feedback.
0 replies
-
I am trying the tests code below with the given data and schema. I am using a bash script to generate the code and json ld context. The code may be found in https://github.com/WolfgangFahl/dcm/tree/main/dcm/linkml for
test_rdf.py '''
Created on 2024-01-20
@author: wf
'''
import os
import json
from ngwidgets.basetest import Basetest
#from dcm.dcm_core import Learner
from dcm.linkml.dcm_model import Learner
from linkml_runtime.loaders.json_loader import JSONLoader
from linkml_runtime.dumpers.rdf_dumper import RDFDumper
from rdflib import Graph
class TestTriplify(Basetest):
"""
test triplifying the dcm core classes
"""
def setUp(self, debug=False, profile=True):
Basetest.setUp(self, debug=debug, profile=profile)
# Path to the JSON file
scriptdir=os.path.dirname(__file__)
topdir=os.path.dirname(scriptdir)
self.json_file_path = os.path.join(topdir, 'dcm_examples/arch_student_123.json')
self.linkml_map_path = os.path.join(topdir, "dcm/linkml/dcm_schema.yaml")
self.json_ld_context= os.path.join(topdir, "dcm/linkml/dcm.context.jsonld")
def getLearner(self) -> Learner:
# Read the JSON file as a dict
with open(self.json_file_path, 'r') as file:
json_data = json.load(file)
# Load JSON data into a Learner instance
json_loader = JSONLoader()
json_data["@type"]="Learner"
learner = json_loader.load(json_data, Learner)
return learner
def testLearner(self):
"""
test the learner example
"""
learner=self.getLearner()
self.assertIsInstance(learner,Learner)
pass
# Serialize Learner instance to RDF
rdf_dumper=RDFDumper()
g=rdf_dumper.as_rdf_graph(learner,self.json_ld_context)
for subj, pred, obj in g:
print(f"Subject: {subj}, Predicate: {pred}, Object: {obj}")
# Your assertions to validate the RDF triples
# For example, you could assert the number of triples or specific content
assert len(g) > 0 # Example assertion: there should be at least one triple arch_student_123.json {
"learner_id": "arch_student_123",
"achievements": [{
"path": "architecture/DandP/ArchDesign",
"level": 2,
"score": 75.0,
"date_assessed_iso": "2023-06-15"
},
{
"path": "architecture/DandP/UrbanPlanning",
"level": 1,
"score": 60.0,
"date_assessed_iso": "2023-06-12"
},
{
"path": "architecture/DandP/BuildCodes",
"level": 1,
"score": 50.0,
"date_assessed_iso": "2023-06-10"
},
{
"path": "architecture/DandP/SustainDesign",
"level": 2,
"score": 80.0,
"date_assessed_iso": "2023-06-18"
},
{
"path": "architecture/TandC/StructEng",
"level": 1,
"score": 65.0,
"date_assessed_iso": "2023-06-11"
},
{
"path": "architecture/TandC/BuildTech",
"level": 2,
"score": 70.0,
"date_assessed_iso": "2023-06-14"
},
{
"path": "architecture/PMandA/ProjMan",
"level": 1,
"score": 55.0,
"date_assessed_iso": "2023-06-09"
},
{
"path": "architecture/PMandA/CostEst",
"level": 1,
"score": 50.0,
"date_assessed_iso": "2023-06-09"
},
{
"path": "architecture/PCandC/InterpSkills",
"level": 2,
"score": 85.0,
"date_assessed_iso": "2023-06-20"
},
{
"path": "architecture/PCandC/TeamCollab",
"level": 1,
"score": 70.0,
"date_assessed_iso": "2023-06-17"
}
]
} dcm_schema.yaml # Dynamic Competence Map LinkML schema
# WF 2024-01-20
#
# to install LinkML:
# pip install linkml
#
# to generate python code:
# gen-python dcm_schema.yaml > dcm_model.py
id: https://dcm.org/learner-schema
name: learner-schema
description: Schema for representing learners and their achievements.
prefixes:
linkml: https://w3id.org/linkml/
dcm: https://dcm.org/learner-schema/
imports:
- linkml:types
default_range: string
default_prefix: dcm
classes:
Learner:
description: An individual learner with a unique identifier and a list of achievements.
slots:
- learner_id
- achievements
Achievement:
description: A record of an achievement attained by a learner.
slots:
- path
- level
- score
- date_assessed_iso
slots:
learner_id:
description: Unique identifier for the learner.
range: string
achievements:
description: A list of achievements attained by the learner.
range: Achievement
multivalued: true
path:
description: The path representing the specific area of achievement.
range: string
level:
description: The level of achievement.
range: integer
score:
description: The score attained in the achievement.
range: float
date_assessed_iso:
description: The ISO format date when the achievement was assessed.
range: string
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Since I didn't find any RDFDumper documentation in https://linkml.io/linkml/data/conversion.html I looked into the source code
and did some try and error. The conversion asks for a JSON-LD context ... but doesn't explain how to get it.
Where would i find a working example or tutorial?
Beta Was this translation helpful? Give feedback.
All reactions