Skip to content

Commit

Permalink
Merge pull request #467 from UUDigitalHumanitieslab/feature/reo-migra…
Browse files Browse the repository at this point in the history
…tion

Feature/reo migration
  • Loading branch information
JeltevanBoheemen authored May 31, 2021
2 parents 13c9b2a + 680d633 commit a149e79
Show file tree
Hide file tree
Showing 20 changed files with 626 additions and 51 deletions.
114 changes: 114 additions & 0 deletions backend/ontology/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import pytest
from items import namespace as item
from rdf.ns import DCTERMS, OA, RDF, RDFS, SCHEMA, SKOS, XSD
from rdf.utils import graph_from_triples
from rdflib import BNode, Literal
from staff import namespace as staff
from vocab import namespace as vocab

from . import namespace as READIT

BLESSINGTON = BNode()
READ_FRENCH_POEMS = BNode()


@pytest.fixture
def linked_item_triples():
return (
graph_from_triples((
(item.subject1, RDF.type, READIT.reader),
(item.subject1, READIT.had_response, item.subject2),
(item.subject1, READIT.had_response, item.subject3),
(item.subject1, READIT.had_response, item.subject5),

(item.subject2, RDF.type, READIT.reading_response),
(item.subject2, RDFS.label, Literal("remember")),
(item.subject2, DCTERMS.type, Literal("example data")),

(item.subject3, RDF.type, READIT.reading_response),
(item.subject3, SKOS.prefLabel, Literal("Comment on book")),
(item.subject3, DCTERMS.creator, staff.dhdevelopers),
(item.subject3, READIT.involved, item.subject4),

(item.subject4, RDF.type, READIT.content),
(item.subject4, SKOS.prefLabel, Literal("Title of book")),
(item.subject4, DCTERMS.creator, staff.dhdevelopers),

(item.subject5, RDF.type, READIT.reading_response),
(item.subject5, RDFS.label, Literal("thought about")),
)),
graph_from_triples((
(item.subject1, RDF.type, READIT.reader),
(item.subject1, READIT.had_response, item.subject5),

(item.subject2, RDF.type, READIT.reading_response),
(item.subject2, RDFS.label, Literal("remember")),
(item.subject2, DCTERMS.type, Literal("example data")),

(item.subject3, RDF.type, READIT.reading_response),
(item.subject3, SKOS.prefLabel, Literal("Comment on book")),
(item.subject3, DCTERMS.creator, staff.dhdevelopers),

(item.subject4, RDF.type, READIT.content),
(item.subject4, SKOS.prefLabel, Literal("Title of book")),
(item.subject4, DCTERMS.creator, staff.dhdevelopers),

(item.subject5, RDF.type, READIT.reading_response),
(item.subject5, RDFS.label, Literal("thought about")),
))
)


@pytest.fixture
def color_triples():
return (
graph_from_triples(
((READIT.EXAMPLE, RDF.type, RDFS.Class),
(READIT.EXAMPLE_CHILD, RDF.type, RDFS.Class),
(READIT.EXAMPLE_CHILD, RDFS.subClassOf, READIT.EXAMPLE))
),
graph_from_triples(
((READIT.EXAMPLE, RDF.type, RDFS.Class),
(READIT.EXAMPLE, SCHEMA.color, Literal("#009e74")),
(READIT.EXAMPLE_CHILD, RDF.type, RDFS.Class),
(READIT.EXAMPLE_CHILD, RDFS.subClassOf, READIT.EXAMPLE),
(READIT.EXAMPLE_CHILD, SCHEMA.color, Literal("#009e74"))
)
)
)


@pytest.fixture
def object_triples():
return (
graph_from_triples((
(BLESSINGTON, RDF.type, READIT.reader),
(BLESSINGTON, READIT.carries_out, READ_FRENCH_POEMS),
(READ_FRENCH_POEMS, RDF.type, READIT.reading_session)
)),
graph_from_triples((
(BLESSINGTON, RDF.type, READIT.person),
(BLESSINGTON, READIT.carries_out, READ_FRENCH_POEMS),
(READ_FRENCH_POEMS, RDF.type, READIT.reading_session)
))
)


@pytest.fixture
def annotation_triples():
return (
graph_from_triples((
(item.subject1, RDF.type, OA.Annotation),
(item.subject1, OA.hasBody, READIT.reader),
(item.subject2, RDF.type, OA.Annotation),
(item.subject2, OA.hasBody, READIT.otherClass)
)),
graph_from_triples((
(item.subject1, RDF.type, OA.Annotation),
(item.subject1, OA.hasBody, READIT.reader),
(item.subject1, vocab.needsVerification,
Literal("true", datatype=XSD.boolean)),
(item.subject2, RDF.type, OA.Annotation),
(item.subject2, OA.hasBody, READIT.otherClass)
))
)
11 changes: 8 additions & 3 deletions backend/ontology/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
# External file or URL from which we're loading the RDF-encoded ontology.
# This is hardcoded, i.e., not a setting, because migrations depend
# on the exact contents.
SOURCE = op.join(settings.BASE_DIR, 'ontology', 'mock-ontology.jsonld')
OLD_SOURCE = op.join(settings.BASE_DIR, 'ontology', 'mock-ontology.jsonld')
SOURCE = 'https://raw.githubusercontent.com/eureadit/reading-experience-ontology/master/REO_2.4.1.owl'

# Format must be a string that rdflib recognizes.
SOURCE_FORMAT = 'json-ld'
# SOURCE_FORMAT = 'json-ld'
SOURCE_FORMAT = 'xml'

# Namespace prefix that is used in the source. If different from
# ONTOLOGY_NS, this needs to be replaced.
SOURCE_PREFIX = 'http://readit.example/ontology/'
OLD_SOURCE_PREFIX = 'http://readit.example/ontology/'
SOURCE_PREFIX = 'http://dataforhistory.org/read-it-ongoing/'
SOURCE_PROPERTY_PREFIX = SOURCE_PREFIX + 'property/'
SOURCE_CLASS_PREFIX = SOURCE_PREFIX + 'class/'
27 changes: 22 additions & 5 deletions backend/ontology/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
This module represents what we believe *should* be in .graph.graph().
"""

from rdflib import Graph, URIRef, Literal
import re

import requests
from rdf.ns import *
from rdflib import Graph, Literal, URIRef

from . import namespace as my
from .constants import SOURCE, SOURCE_FORMAT, SOURCE_PREFIX, ONTOLOGY_NS
from .constants import (OLD_SOURCE, OLD_SOURCE_PREFIX, ONTOLOGY_NS, SOURCE,
SOURCE_CLASS_PREFIX, SOURCE_FORMAT, SOURCE_PREFIX,
SOURCE_PROPERTY_PREFIX)

REPARSE_FORMAT = 'n3'

Expand Down Expand Up @@ -39,7 +43,20 @@ def canonical_graph():
URIRefs.
"""
g = Graph()
g.parse(SOURCE, format=SOURCE_FORMAT)
if SOURCE_PREFIX != ONTOLOGY_NS:
return replace_prefix(g, SOURCE_PREFIX, ONTOLOGY_NS)
g.parse(OLD_SOURCE, format='json-ld')
if OLD_SOURCE_PREFIX != ONTOLOGY_NS:
return replace_prefix(g, OLD_SOURCE_PREFIX, ONTOLOGY_NS)
return g


def reo_graph():
source = requests.get(SOURCE).text
to_replace = (SOURCE_PROPERTY_PREFIX, SOURCE_CLASS_PREFIX,
SOURCE_PREFIX)
replaced_source = re.sub((r'|').join(to_replace), ONTOLOGY_NS, source)

g = Graph()
g.parse(data=replaced_source, format=SOURCE_FORMAT)
return g


Loading

0 comments on commit a149e79

Please sign in to comment.