RecordResource.kt 7.29 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
import org.apache.jena.rdf.model.Resource
26
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
27
28
29
30
31
32
33
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
34
import org.memobase.rdf.SKOS
Jonas Waeber's avatar
Jonas Waeber committed
35
36

abstract class RecordResource(institutionId: String) : IResource {
37
    private val log = LogManager.getLogger("ResourceBuilder")
Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
41
42
43
44
45
46

    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)
Jonas Waeber's avatar
Jonas Waeber committed
47
        model.setNsPrefix("dct", NS.dcterms)
Jonas Waeber's avatar
Jonas Waeber committed
48
49
50
51
52
53
54
55
        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
56
    override fun addLiteral(property: String, value: Literal) {
Jonas Waeber's avatar
Jonas Waeber committed
57
58
59
        resource.addLiteral(KEYS.keysToPropertyMap[property], value)
    }

60
61
62
63
64
65
66
67
68
69
    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
70
71
    }

72
    private fun addRicoConcept(rdfType: Resource, ricoType: String, valueProperty: Property, value: List<Literal>, resourceProperty: Property) {
Jonas Waeber's avatar
Jonas Waeber committed
73
        val blank = model.createResource()
74
75
76
77
78
79
        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
80
81
    }

82
83
84
85
86
87
88
89
90
    override fun addRicoCarrierType(names: List<Literal>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.CarrierType)
        names.forEach {
            blank.addProperty(RICO.name, it)
        }
        resource.addProperty(RICO.hasCarrierType, blank)
    }

Jonas Waeber's avatar
Jonas Waeber committed
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
    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)
    }

Jonas Waeber's avatar
Jonas Waeber committed
122
    override fun addCreationRelation(relationType: String, relationName: List<Literal>, agentClass: String, properties: List<Pair<String, Literal>>) {
Jonas Waeber's avatar
Jonas Waeber committed
123
        val relation = model.createResource()
Jonas Waeber's avatar
Jonas Waeber committed
124
125
        relation.addProperty(RICO.type, KEYS.relationTypeMap[relationType])
        if (relationName.isEmpty()) {
Jonas Waeber's avatar
Jonas Waeber committed
126
127
                // NOTE: If this constant is changed it needs to be changed in the normalization
                // ExtractCreationRelationNameTransform as well.
Jonas Waeber's avatar
Jonas Waeber committed
128
129
130
131
132
133
               relation.addProperty(RICO.name, "DUMMY-VALUE")
        } else {
            relationName.forEach {
                relation.addProperty(RICO.name, it)
            }
        }
Jonas Waeber's avatar
Jonas Waeber committed
134
135
136
        relation.addProperty(RICO.creationRelationHasSource, resource)
        relation.addProperty(RDF.type, RICO.CreationRelation)
        val agent = model.createResource()
Jonas Waeber's avatar
Jonas Waeber committed
137
        agent.addProperty(RDF.type, KEYS.agentTypeMap[agentClass])
138
        agent.addProperty(RICO.agentIsTargetOfCreationRelation, relation)
Jonas Waeber's avatar
Jonas Waeber committed
139
140
141
142
143
144
145
        for (property in properties) {
            agent.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        relation.addProperty(RICO.creationRelationHasTarget, agent)
        resource.addProperty(RICO.recordResourceOrInstantiationIsSourceOfCreationRelation, relation)
    }

146
147
148
149
150
151
152
153
154
    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)
    }

Jonas Waeber's avatar
Jonas Waeber committed
155
    override fun addRule(type: String, value: List<Pair<String, List<Literal>>>) {
156
157
158
159
160
        if (value.isNotEmpty()) {
            val blank = model.createResource()
            blank.addProperty(RDF.type, RICO.Rule)
            blank.addProperty(RICO.type, literal(type))
            value.forEach {
Jonas Waeber's avatar
Jonas Waeber committed
161
162
163
                it.second.forEach { literal ->
                    blank.addProperty(KEYS.keysToPropertyMap[it.first], literal)
                }
164
165
166
167
168
            }
            blank.addProperty(RICO.regulates, resource)
            resource.addProperty(RICO.regulatedBy, blank)
        } else {
            log.error("Add no rule for type $type, because there were no content properties mapped.")
169
170
171
172
173
        }
    }

    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
174
175
176


    protected fun uri(ns: String, name: String): String = ns + name
Jonas Waeber's avatar
Jonas Waeber committed
177
    private fun institutionUri(id: String): Resource = model.createResource(uri(NS.mbcb, id))
Jonas Waeber's avatar
Jonas Waeber committed
178
}