RecordResource.kt 6.07 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * mapper-service
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.memobase.builder

import org.apache.jena.rdf.model.Literal
import org.apache.jena.rdf.model.Model
import org.apache.jena.rdf.model.ModelFactory
24
import org.apache.jena.rdf.model.Property
Jonas Waeber's avatar
Jonas Waeber committed
25
26
27
28
29
30
31
32
import org.apache.jena.rdf.model.Resource
import org.memobase.mapping.KEYS
import org.memobase.rdf.DC
import org.memobase.rdf.EBUCORE
import org.memobase.rdf.NS
import org.memobase.rdf.RDA
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
Jonas Waeber's avatar
Jonas Waeber committed
33
import org.memobase.rdf.SKOS
Jonas Waeber's avatar
Jonas Waeber committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

abstract class RecordResource(institutionId: String) : IResource {

    val model: Model = ModelFactory.createDefaultModel()

    abstract val resource: Resource
    protected val institutionUri: Resource = institutionUri(institutionId)

    init {
        model.setNsPrefix("rico", NS.rico)
        model.setNsPrefix("rdf", NS.rdf)
        model.setNsPrefix("dct", NS.dct)
        model.setNsPrefix("ebucore", NS.ebucore)
        model.setNsPrefix("skos", NS.skos)
        model.setNsPrefix("rdau", NS.rdau)
    }

    protected fun addRdfType(type: Resource) {
        resource.addProperty(RDF.type, type)
    }
Jonas Waeber's avatar
Jonas Waeber committed
54
    override fun addLiteral(property: String, value: Literal) {
Jonas Waeber's avatar
Jonas Waeber committed
55
56
57
        resource.addLiteral(KEYS.keysToPropertyMap[property], value)
    }

58
59
60
61
62
63
64
65
66
67
    override fun addRicoConcept(rdfType: String, ricoType: String, value: List<Literal>) {
        when (rdfType) {
            KEYS.titles ->
                addRicoConcept(RICO.Title, ricoType, RICO.title, value, RICO.hasTitle)
            KEYS.identifiers ->
                addRicoConcept(RICO.Identifier, ricoType, RICO.identifier, value, RICO.identifiedBy)
            KEYS.languages ->
                addRicoConcept(RICO.Language, ricoType, RICO.name, value, RICO.hasLanguage)
            else -> throw Exception("Unknown rdf:type alias for rico:Concept $rdfType.")
        }
Jonas Waeber's avatar
Jonas Waeber committed
68
69
    }

70
    private fun addRicoConcept(rdfType: Resource, ricoType: String, valueProperty: Property, value: List<Literal>, resourceProperty: Property) {
Jonas Waeber's avatar
Jonas Waeber committed
71
        val blank = model.createResource()
72
73
74
75
76
77
        blank.addProperty(RDF.type, rdfType)
        blank.addProperty(RICO.type, ricoType)
        value.forEach {
            blank.addProperty(valueProperty, it)
        }
        resource.addProperty(resourceProperty, blank)
Jonas Waeber's avatar
Jonas Waeber committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    }

    override fun addSkosConcept(type: String, properties: List<Pair<String, Literal>>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, SKOS.Concept)
        for (property in properties) {
            blank.addProperty(SKOS.get(property.first), property.second)
        }
        when (type) {
            KEYS.genre -> resource.addProperty(EBUCORE.hasGenre, blank)
            KEYS.subject -> resource.addProperty(RICO.hasSubject, blank)
        }
    }

    override fun addPlace(type: String, properties: List<Pair<String, Literal>>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.Place)
        for (property in properties) {
            blank.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        when (type) {
            KEYS.placeOfCapture -> resource.addProperty(RDA.hasPlaceOfCapture, blank)
            KEYS.relatedPlaces -> resource.addProperty(DC.spatial, blank)
        }
    }

    override fun addDate(property: String, value: String) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.DateSet)
        blank.addProperty(RICO.expressedDate, literal(value))
        resource.addProperty(KEYS.keysToPropertyMap[property], blank)
    }

    override fun addCreationRelation(type: String, agentType: String, properties: List<Pair<String, Literal>>) {
        val relation = model.createResource()
        relation.addProperty(RICO.type, type)
        relation.addProperty(RICO.creationRelationHasSource, resource)
        relation.addProperty(RDF.type, RICO.CreationRelation)
        val agent = model.createResource()
        agent.addProperty(RDF.type, KEYS.agentTypeMap[agentType])
        for (property in properties) {
            agent.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        relation.addProperty(RICO.creationRelationHasTarget, agent)
        resource.addProperty(RICO.recordResourceOrInstantiationIsSourceOfCreationRelation, relation)
    }

125
126
127
128
129
130
131
132
133
    override fun addAgent(relation: String, agentType: String, properties: List<Pair<String, Literal>>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, KEYS.agentTypeMap[agentType])
        for (property in properties) {
            blank.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        resource.addProperty(KEYS.agentPropertiesMap[relation], blank)
    }

134
135
136
137
138
139
140
    override fun addRule(type: String, value: List<Literal>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.Rule)
        blank.addProperty(RICO.type, literal(type))
        value.forEach {
            blank.addProperty(RICO.title, it)
        }
141
142
        blank.addProperty(RICO.regulates, resource)
        resource.addProperty(RICO.regulatedBy, blank)
143
144
145
146
    }

    override fun langLiteral(text: String, language: String): Literal = model.createLiteral(text.trim(), language)
    override fun literal(text: String): Literal = model.createLiteral(text.trim())
Jonas Waeber's avatar
Jonas Waeber committed
147
148
149
150
151


    protected fun uri(ns: String, name: String): String = ns + name
    private fun institutionUri(id: String): Resource = model.createResource(uri(NS.memint, id))
}