@oml/owl 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +46 -0
  2. package/out/index.d.ts +10 -0
  3. package/out/index.js +12 -0
  4. package/out/index.js.map +1 -0
  5. package/out/owl/owl-abox.d.ts +79 -0
  6. package/out/owl/owl-abox.js +765 -0
  7. package/out/owl/owl-abox.js.map +1 -0
  8. package/out/owl/owl-imports.d.ts +9 -0
  9. package/out/owl/owl-imports.js +102 -0
  10. package/out/owl/owl-imports.js.map +1 -0
  11. package/out/owl/owl-interfaces.d.ts +121 -0
  12. package/out/owl/owl-interfaces.js +3 -0
  13. package/out/owl/owl-interfaces.js.map +1 -0
  14. package/out/owl/owl-mapper.d.ts +80 -0
  15. package/out/owl/owl-mapper.js +1217 -0
  16. package/out/owl/owl-mapper.js.map +1 -0
  17. package/out/owl/owl-service.d.ts +65 -0
  18. package/out/owl/owl-service.js +552 -0
  19. package/out/owl/owl-service.js.map +1 -0
  20. package/out/owl/owl-shacl.d.ts +28 -0
  21. package/out/owl/owl-shacl.js +337 -0
  22. package/out/owl/owl-shacl.js.map +1 -0
  23. package/out/owl/owl-sparql.d.ts +71 -0
  24. package/out/owl/owl-sparql.js +260 -0
  25. package/out/owl/owl-sparql.js.map +1 -0
  26. package/out/owl/owl-store.d.ts +32 -0
  27. package/out/owl/owl-store.js +142 -0
  28. package/out/owl/owl-store.js.map +1 -0
  29. package/out/owl/owl-tbox.d.ts +98 -0
  30. package/out/owl/owl-tbox.js +575 -0
  31. package/out/owl/owl-tbox.js.map +1 -0
  32. package/out/owl-module.d.ts +15 -0
  33. package/out/owl-module.js +22 -0
  34. package/out/owl-module.js.map +1 -0
  35. package/package.json +52 -0
  36. package/src/index.ts +12 -0
  37. package/src/owl/owl-abox.ts +930 -0
  38. package/src/owl/owl-imports.ts +108 -0
  39. package/src/owl/owl-interfaces.ts +145 -0
  40. package/src/owl/owl-mapper.ts +1510 -0
  41. package/src/owl/owl-service.ts +642 -0
  42. package/src/owl/owl-shacl.ts +400 -0
  43. package/src/owl/owl-sparql.ts +317 -0
  44. package/src/owl/owl-store.ts +173 -0
  45. package/src/owl/owl-tbox.ts +727 -0
  46. package/src/owl-module.ts +52 -0
@@ -0,0 +1,1217 @@
1
+ // Copyright (c) 2026 Modelware. All rights reserved.
2
+ import { DataFactory } from 'n3';
3
+ import { isAnnotationProperty, isAnonymousConceptInstance, isAnonymousRelationInstance, isAspect, isBooleanLiteral, isBuiltInPredicate, isBuiltIn, isConcept, isConceptInstance, isDecimalLiteral, isDifferentFromPredicate, isDescription, isDescriptionBundle, isDoubleLiteral, isIntegerLiteral, isOntology, isPropertyPredicate, isPropertyCardinalityRestrictionAxiom, isPropertyRangeRestrictionAxiom, isPropertySelfRestrictionAxiom, isPropertyValueRestrictionAxiom, isQuotedLiteral, isRelation, isRelationEntityPredicate, isRelationEntity, isRelationInstance, isReverseRelation, isRule, isSameAsPredicate, isScalar, isScalarProperty, isTypePredicate, isUnreifiedRelation, isVocabulary, isVocabularyBundle, } from '@oml/language';
4
+ export const OWL_ONTOLOGY_IRI = 'http://opencaesar.io/oml';
5
+ export const OWL_ONTOLOGY_NAMESPACE = 'http://opencaesar.io/oml#';
6
+ export const OWL_ONTOLOGY_PREFIX = 'oml';
7
+ const RDF = {
8
+ type: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
9
+ first: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first',
10
+ rest: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest',
11
+ nil: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',
12
+ langRange: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langRange',
13
+ langString: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString',
14
+ };
15
+ const RDFS = {
16
+ Datatype: 'http://www.w3.org/2000/01/rdf-schema#Datatype',
17
+ label: 'http://www.w3.org/2000/01/rdf-schema#label',
18
+ subClassOf: 'http://www.w3.org/2000/01/rdf-schema#subClassOf',
19
+ subPropertyOf: 'http://www.w3.org/2000/01/rdf-schema#subPropertyOf',
20
+ domain: 'http://www.w3.org/2000/01/rdf-schema#domain',
21
+ range: 'http://www.w3.org/2000/01/rdf-schema#range',
22
+ };
23
+ const OWL = {
24
+ Ontology: 'http://www.w3.org/2002/07/owl#Ontology',
25
+ Class: 'http://www.w3.org/2002/07/owl#Class',
26
+ ObjectProperty: 'http://www.w3.org/2002/07/owl#ObjectProperty',
27
+ DatatypeProperty: 'http://www.w3.org/2002/07/owl#DatatypeProperty',
28
+ AnnotationProperty: 'http://www.w3.org/2002/07/owl#AnnotationProperty',
29
+ Datatype: 'http://www.w3.org/2002/07/owl#Datatype',
30
+ FunctionalProperty: 'http://www.w3.org/2002/07/owl#FunctionalProperty',
31
+ InverseFunctionalProperty: 'http://www.w3.org/2002/07/owl#InverseFunctionalProperty',
32
+ SymmetricProperty: 'http://www.w3.org/2002/07/owl#SymmetricProperty',
33
+ AsymmetricProperty: 'http://www.w3.org/2002/07/owl#AsymmetricProperty',
34
+ ReflexiveProperty: 'http://www.w3.org/2002/07/owl#ReflexiveProperty',
35
+ IrreflexiveProperty: 'http://www.w3.org/2002/07/owl#IrreflexiveProperty',
36
+ TransitiveProperty: 'http://www.w3.org/2002/07/owl#TransitiveProperty',
37
+ Restriction: 'http://www.w3.org/2002/07/owl#Restriction',
38
+ onProperty: 'http://www.w3.org/2002/07/owl#onProperty',
39
+ onClass: 'http://www.w3.org/2002/07/owl#onClass',
40
+ onDataRange: 'http://www.w3.org/2002/07/owl#onDataRange',
41
+ onDatatype: 'http://www.w3.org/2002/07/owl#onDatatype',
42
+ withRestrictions: 'http://www.w3.org/2002/07/owl#withRestrictions',
43
+ someValuesFrom: 'http://www.w3.org/2002/07/owl#someValuesFrom',
44
+ allValuesFrom: 'http://www.w3.org/2002/07/owl#allValuesFrom',
45
+ minCardinality: 'http://www.w3.org/2002/07/owl#minCardinality',
46
+ maxCardinality: 'http://www.w3.org/2002/07/owl#maxCardinality',
47
+ cardinality: 'http://www.w3.org/2002/07/owl#cardinality',
48
+ hasValue: 'http://www.w3.org/2002/07/owl#hasValue',
49
+ hasSelf: 'http://www.w3.org/2002/07/owl#hasSelf',
50
+ hasKey: 'http://www.w3.org/2002/07/owl#hasKey',
51
+ inverseOf: 'http://www.w3.org/2002/07/owl#inverseOf',
52
+ equivalentClass: 'http://www.w3.org/2002/07/owl#equivalentClass',
53
+ equivalentProperty: 'http://www.w3.org/2002/07/owl#equivalentProperty',
54
+ subPropertyOf: 'http://www.w3.org/2002/07/owl#subPropertyOf',
55
+ imports: 'http://www.w3.org/2002/07/owl#imports',
56
+ NamedIndividual: 'http://www.w3.org/2002/07/owl#NamedIndividual',
57
+ oneOf: 'http://www.w3.org/2002/07/owl#oneOf',
58
+ };
59
+ const XSD = {
60
+ boolean: 'http://www.w3.org/2001/XMLSchema#boolean',
61
+ decimal: 'http://www.w3.org/2001/XMLSchema#decimal',
62
+ double: 'http://www.w3.org/2001/XMLSchema#double',
63
+ integer: 'http://www.w3.org/2001/XMLSchema#integer',
64
+ string: 'http://www.w3.org/2001/XMLSchema#string',
65
+ length: 'http://www.w3.org/2001/XMLSchema#length',
66
+ minLength: 'http://www.w3.org/2001/XMLSchema#minLength',
67
+ maxLength: 'http://www.w3.org/2001/XMLSchema#maxLength',
68
+ minInclusive: 'http://www.w3.org/2001/XMLSchema#minInclusive',
69
+ maxInclusive: 'http://www.w3.org/2001/XMLSchema#maxInclusive',
70
+ minExclusive: 'http://www.w3.org/2001/XMLSchema#minExclusive',
71
+ maxExclusive: 'http://www.w3.org/2001/XMLSchema#maxExclusive',
72
+ pattern: 'http://www.w3.org/2001/XMLSchema#pattern',
73
+ };
74
+ const OML = {
75
+ ns: OWL_ONTOLOGY_NAMESPACE,
76
+ type: `${OWL_ONTOLOGY_NAMESPACE}type`,
77
+ name: `${OWL_ONTOLOGY_NAMESPACE}name`,
78
+ namespace: `${OWL_ONTOLOGY_NAMESPACE}namespace`,
79
+ prefix: `${OWL_ONTOLOGY_NAMESPACE}prefix`,
80
+ Aspect: `${OWL_ONTOLOGY_NAMESPACE}Aspect`,
81
+ Concept: `${OWL_ONTOLOGY_NAMESPACE}Concept`,
82
+ RelationEntity: `${OWL_ONTOLOGY_NAMESPACE}RelationEntity`,
83
+ RelationInstance: `${OWL_ONTOLOGY_NAMESPACE}RelationInstance`,
84
+ ConceptInstance: `${OWL_ONTOLOGY_NAMESPACE}ConceptInstance`,
85
+ Scalar: `${OWL_ONTOLOGY_NAMESPACE}Scalar`,
86
+ ForwardRelation: `${OWL_ONTOLOGY_NAMESPACE}ForwardRelation`,
87
+ UnreifiedRelation: `${OWL_ONTOLOGY_NAMESPACE}UnreifiedRelation`,
88
+ ReverseRelation: `${OWL_ONTOLOGY_NAMESPACE}ReverseRelation`,
89
+ relationEntity: `${OWL_ONTOLOGY_NAMESPACE}relationEntity`,
90
+ relationBase: `${OWL_ONTOLOGY_NAMESPACE}relationBase`,
91
+ hasSource: `${OWL_ONTOLOGY_NAMESPACE}hasSource`,
92
+ hasTarget: `${OWL_ONTOLOGY_NAMESPACE}hasTarget`,
93
+ Rule: `${OWL_ONTOLOGY_NAMESPACE}Rule`,
94
+ BuiltIn: `${OWL_ONTOLOGY_NAMESPACE}BuiltIn`,
95
+ Vocabulary: `${OWL_ONTOLOGY_NAMESPACE}Vocabulary`,
96
+ VocabularyBundle: `${OWL_ONTOLOGY_NAMESPACE}VocabularyBundle`,
97
+ Description: `${OWL_ONTOLOGY_NAMESPACE}Description`,
98
+ DescriptionBundle: `${OWL_ONTOLOGY_NAMESPACE}DescriptionBundle`,
99
+ };
100
+ const OML_TERM_TYPES = {
101
+ annotation: OWL.AnnotationProperty,
102
+ object: OWL.ObjectProperty,
103
+ datatype: OWL.DatatypeProperty,
104
+ class: OWL.Class,
105
+ };
106
+ const OML_TERMS = [
107
+ { iri: OML.type, rdfType: OML_TERM_TYPES.annotation },
108
+ { iri: OML.name, rdfType: OML_TERM_TYPES.annotation },
109
+ { iri: OML.namespace, rdfType: OML_TERM_TYPES.annotation },
110
+ { iri: OML.prefix, rdfType: OML_TERM_TYPES.annotation },
111
+ { iri: OML.relationEntity, rdfType: OML_TERM_TYPES.object },
112
+ { iri: OML.relationBase, rdfType: OML_TERM_TYPES.object },
113
+ { iri: OML.hasSource, rdfType: OML_TERM_TYPES.object },
114
+ { iri: OML.hasTarget, rdfType: OML_TERM_TYPES.object },
115
+ { iri: OML.Aspect, rdfType: OML_TERM_TYPES.class },
116
+ { iri: OML.Concept, rdfType: OML_TERM_TYPES.class },
117
+ { iri: OML.RelationEntity, rdfType: OML_TERM_TYPES.class },
118
+ { iri: OML.RelationInstance, rdfType: OML_TERM_TYPES.class },
119
+ { iri: OML.ConceptInstance, rdfType: OML_TERM_TYPES.class },
120
+ { iri: OML.Scalar, rdfType: OML_TERM_TYPES.class },
121
+ { iri: OML.ForwardRelation, rdfType: OML_TERM_TYPES.class },
122
+ { iri: OML.UnreifiedRelation, rdfType: OML_TERM_TYPES.class },
123
+ { iri: OML.ReverseRelation, rdfType: OML_TERM_TYPES.class },
124
+ { iri: OML.Rule, rdfType: OML_TERM_TYPES.class },
125
+ { iri: OML.BuiltIn, rdfType: OML_TERM_TYPES.class },
126
+ { iri: OML.Vocabulary, rdfType: OML_TERM_TYPES.class },
127
+ { iri: OML.VocabularyBundle, rdfType: OML_TERM_TYPES.class },
128
+ { iri: OML.Description, rdfType: OML_TERM_TYPES.class },
129
+ { iri: OML.DescriptionBundle, rdfType: OML_TERM_TYPES.class },
130
+ ];
131
+ const SWRL = {
132
+ Imp: 'http://www.w3.org/2003/11/swrl#Imp',
133
+ AtomList: 'http://www.w3.org/2003/11/swrl#AtomList',
134
+ body: 'http://www.w3.org/2003/11/swrl#body',
135
+ head: 'http://www.w3.org/2003/11/swrl#head',
136
+ ClassAtom: 'http://www.w3.org/2003/11/swrl#ClassAtom',
137
+ DataRangeAtom: 'http://www.w3.org/2003/11/swrl#DataRangeAtom',
138
+ IndividualPropertyAtom: 'http://www.w3.org/2003/11/swrl#IndividualPropertyAtom',
139
+ DatavaluedPropertyAtom: 'http://www.w3.org/2003/11/swrl#DatavaluedPropertyAtom',
140
+ SameIndividualAtom: 'http://www.w3.org/2003/11/swrl#SameIndividualAtom',
141
+ DifferentIndividualsAtom: 'http://www.w3.org/2003/11/swrl#DifferentIndividualsAtom',
142
+ BuiltinAtom: 'http://www.w3.org/2003/11/swrl#BuiltinAtom',
143
+ Variable: 'http://www.w3.org/2003/11/swrl#Variable',
144
+ classPredicate: 'http://www.w3.org/2003/11/swrl#classPredicate',
145
+ dataRange: 'http://www.w3.org/2003/11/swrl#dataRange',
146
+ propertyPredicate: 'http://www.w3.org/2003/11/swrl#propertyPredicate',
147
+ argument1: 'http://www.w3.org/2003/11/swrl#argument1',
148
+ argument2: 'http://www.w3.org/2003/11/swrl#argument2',
149
+ builtin: 'http://www.w3.org/2003/11/swrl#builtin',
150
+ arguments: 'http://www.w3.org/2003/11/swrl#arguments',
151
+ };
152
+ const BUILT_IN_ONTOLOGIES = new Set([
153
+ 'http://www.w3.org/2001/XMLSchema',
154
+ 'http://www.w3.org/1999/02/22-rdf-syntax-ns',
155
+ 'http://www.w3.org/2000/01/rdf-schema',
156
+ 'http://www.w3.org/2002/07/owl',
157
+ 'http://www.w3.org/2003/11/swrl',
158
+ 'http://www.w3.org/2003/11/swrlb',
159
+ ]);
160
+ const { namedNode, literal, quad } = DataFactory;
161
+ function fnv1a(input) {
162
+ let hash = 2166136261;
163
+ for (let i = 0; i < input.length; i += 1) {
164
+ hash ^= input.charCodeAt(i);
165
+ hash = (hash * 16777619) >>> 0;
166
+ }
167
+ return hash.toString(16);
168
+ }
169
+ function skolemize(namespace, ...inputs) {
170
+ if (inputs.length === 0) {
171
+ throw new Error('skolemize requires at least one input.');
172
+ }
173
+ const [kind, ...semanticInputs] = inputs;
174
+ const identity = semanticInputs.length > 0 ? semanticInputs.join('|') : kind;
175
+ const hash = fnv1a(identity);
176
+ return namedNode(`${namespace}${kind}-${hash}`);
177
+ }
178
+ /**
179
+ * Maps an OML ontology AST into asserted RDF triples.
180
+ * This mirrors the high-level structure of the Java Oml2Owl mapping while focusing on asserted triples.
181
+ */
182
+ export class Oml2OwlMapper {
183
+ constructor(options = {
184
+ emitSwrlRules: true,
185
+ emitRelationEntitySwrlRules: true,
186
+ }) {
187
+ this.options = options;
188
+ }
189
+ toQuads(ontology) {
190
+ if (!isOntology(ontology)) {
191
+ return [];
192
+ }
193
+ const ctx = this.createContext(ontology);
194
+ const triples = [];
195
+ const ontologyNode = namedNode(ctx.ontologyIri);
196
+ // Ontology header
197
+ triples.push(this.quadWithGraph(ontologyNode, namedNode(RDF.type), namedNode(OWL.Ontology)));
198
+ triples.push(this.quadWithGraph(ontologyNode, namedNode(OML.type), namedNode(this.ontologyTypeIri(ontology))));
199
+ triples.push(this.quadWithGraph(ontologyNode, namedNode(OML.namespace), namedNode(ctx.namespace)));
200
+ triples.push(this.quadWithGraph(ontologyNode, namedNode(OML.prefix), literal(ctx.prefix)));
201
+ this.mapAnnotations(ontologyNode, ontology.ownedAnnotations, ctx, triples);
202
+ // Imports
203
+ ontology.ownedImports?.forEach((imp) => {
204
+ const importedIri = this.importedOntologyIri(imp, ctx);
205
+ if (importedIri && !BUILT_IN_ONTOLOGIES.has(importedIri)) {
206
+ triples.push(this.quadWithGraph(ontologyNode, namedNode(OWL.imports), namedNode(importedIri)));
207
+ }
208
+ });
209
+ if (ctx.ontologyIri !== OWL_ONTOLOGY_IRI) {
210
+ triples.push(this.quadWithGraph(ontologyNode, namedNode(OWL.imports), namedNode(OWL_ONTOLOGY_IRI)));
211
+ }
212
+ if (isVocabulary(ontology)) {
213
+ ontology.ownedStatements?.forEach((stmt) => {
214
+ this.mapVocabularyStatement(stmt, ctx, triples);
215
+ });
216
+ }
217
+ else if (isDescription(ontology)) {
218
+ ontology.ownedStatements?.forEach((stmt) => {
219
+ this.mapDescriptionStatement(stmt, ctx, triples);
220
+ });
221
+ }
222
+ return this.deduplicateQuads(triples);
223
+ }
224
+ mapVocabularyStatement(stmt, ctx, triples) {
225
+ if (isAspect(stmt)) {
226
+ this.mapAspect(stmt, ctx, triples);
227
+ }
228
+ else if (isConcept(stmt)) {
229
+ this.mapConcept(stmt, ctx, triples);
230
+ }
231
+ else if (isRelationEntity(stmt)) {
232
+ this.mapRelationEntity(stmt, ctx, triples);
233
+ }
234
+ else if (isScalar(stmt)) {
235
+ this.mapScalar(stmt, ctx, triples);
236
+ }
237
+ else if (isAnnotationProperty(stmt)) {
238
+ this.mapAnnotationProperty(stmt, ctx, triples);
239
+ }
240
+ else if (isScalarProperty(stmt)) {
241
+ this.mapScalarProperty(stmt, ctx, triples);
242
+ }
243
+ else if (isUnreifiedRelation(stmt)) {
244
+ this.mapUnreifiedRelation(stmt, ctx, triples);
245
+ }
246
+ else if (isRelation(stmt) && isReverseRelation(stmt)) {
247
+ this.mapReverseRelation(stmt, ctx, triples);
248
+ }
249
+ else if (isRule(stmt)) {
250
+ this.mapRule(stmt, ctx, triples);
251
+ }
252
+ else if (isBuiltIn(stmt)) {
253
+ this.mapBuiltIn(stmt, ctx, triples);
254
+ }
255
+ }
256
+ mapDescriptionStatement(stmt, ctx, triples) {
257
+ if (isConceptInstance(stmt)) {
258
+ this.mapConceptInstance(stmt, ctx, triples);
259
+ }
260
+ else if (isRelationInstance(stmt)) {
261
+ this.mapRelationInstance(stmt, ctx, triples);
262
+ }
263
+ }
264
+ mapAspect(aspect, ctx, triples) {
265
+ const iri = this.elementIri(aspect, ctx);
266
+ if (!iri)
267
+ return;
268
+ const node = namedNode(iri);
269
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.Class)));
270
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.Aspect)));
271
+ this.mapAnnotations(node, aspect.ownedAnnotations, ctx, triples);
272
+ this.applySpecializations(aspect.ownedSpecializations, node, ctx, triples);
273
+ this.applyEquivalences(aspect.ownedEquivalences, node, ctx, triples);
274
+ this.applyKeys(aspect.ownedKeys, node, ctx, triples);
275
+ this.applyPropertyRestrictions(aspect.ownedPropertyRestrictions, node, ctx, triples);
276
+ }
277
+ mapConcept(concept, ctx, triples) {
278
+ const iri = this.elementIri(concept, ctx);
279
+ if (!iri)
280
+ return;
281
+ const node = namedNode(iri);
282
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.Class)));
283
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.Concept)));
284
+ this.mapAnnotations(node, concept.ownedAnnotations, ctx, triples);
285
+ this.applySpecializations(concept.ownedSpecializations, node, ctx, triples);
286
+ this.applyEquivalences(concept.ownedEquivalences, node, ctx, triples);
287
+ this.applyKeys(concept.ownedKeys, node, ctx, triples);
288
+ this.applyPropertyRestrictions(concept.ownedPropertyRestrictions, node, ctx, triples);
289
+ this.applyInstanceEnumeration(concept.ownedEnumeration, node, ctx, triples);
290
+ }
291
+ mapRelationEntity(entity, ctx, triples) {
292
+ const classIri = this.elementIri(entity, ctx);
293
+ if (!classIri)
294
+ return;
295
+ const classNode = namedNode(classIri);
296
+ const forwardIri = this.getForwardIri(entity, ctx);
297
+ const forwardNode = namedNode(forwardIri);
298
+ triples.push(this.quadWithGraph(classNode, namedNode(RDF.type), namedNode(OWL.Class)));
299
+ triples.push(this.quadWithGraph(classNode, namedNode(OML.type), namedNode(OML.RelationEntity)));
300
+ this.mapAnnotations(classNode, entity.ownedAnnotations, ctx, triples);
301
+ // Forward relation object property
302
+ triples.push(this.quadWithGraph(forwardNode, namedNode(RDF.type), namedNode(OWL.ObjectProperty)));
303
+ triples.push(this.quadWithGraph(forwardNode, namedNode(OML.type), namedNode(OML.ForwardRelation)));
304
+ triples.push(this.quadWithGraph(forwardNode, namedNode(OML.relationEntity), classNode));
305
+ this.mapAnnotations(forwardNode, entity.forwardRelation?.ownedAnnotations, ctx, triples);
306
+ this.applyDomainRange(forwardNode, entity.sources, entity.targets, ctx, triples);
307
+ this.applyObjectPropertyCharacteristics(forwardNode, entity, triples);
308
+ if (entity.reverseRelation) {
309
+ this.mapReverseRelation(entity.reverseRelation, ctx, triples, entity);
310
+ }
311
+ this.applySpecializations(entity.ownedSpecializations, classNode, ctx, triples);
312
+ this.applyEquivalences(entity.ownedEquivalences, classNode, ctx, triples);
313
+ this.applyKeys(entity.ownedKeys, classNode, ctx, triples);
314
+ this.applyPropertyRestrictions(entity.ownedPropertyRestrictions, classNode, ctx, triples);
315
+ if (this.options.emitSwrlRules !== false || this.options.emitRelationEntitySwrlRules === true) {
316
+ this.mapRelationEntityForwardRule(classIri, forwardIri, ctx, triples);
317
+ }
318
+ }
319
+ mapUnreifiedRelation(relation, ctx, triples) {
320
+ const forwardIri = this.elementIri(relation, ctx);
321
+ if (!forwardIri)
322
+ return;
323
+ const forwardNode = namedNode(forwardIri);
324
+ triples.push(this.quadWithGraph(forwardNode, namedNode(RDF.type), namedNode(OWL.ObjectProperty)));
325
+ triples.push(this.quadWithGraph(forwardNode, namedNode(OML.type), namedNode(OML.UnreifiedRelation)));
326
+ this.mapAnnotations(forwardNode, relation.ownedAnnotations, ctx, triples);
327
+ this.applyDomainRange(forwardNode, relation.sources, relation.targets, ctx, triples);
328
+ this.applyObjectPropertyCharacteristics(forwardNode, relation, triples);
329
+ if (relation.reverseRelation) {
330
+ this.mapReverseRelation(relation.reverseRelation, ctx, triples, relation);
331
+ }
332
+ this.applySpecializations(relation.ownedSpecializations, forwardNode, ctx, triples, true);
333
+ this.applyPropertyEquivalences(relation.ownedEquivalences, forwardNode, ctx, triples);
334
+ }
335
+ mapReverseRelation(reverse, ctx, triples, relationBase) {
336
+ const base = relationBase ?? reverse.$container;
337
+ const reverseIri = this.elementIri(reverse, ctx);
338
+ const forwardIri = this.getForwardIri(base, ctx);
339
+ const baseIri = this.elementIri(base, ctx);
340
+ if (!reverseIri || !baseIri)
341
+ return;
342
+ const reverseNode = namedNode(reverseIri);
343
+ triples.push(this.quadWithGraph(reverseNode, namedNode(RDF.type), namedNode(OWL.ObjectProperty)));
344
+ triples.push(this.quadWithGraph(reverseNode, namedNode(OML.type), namedNode(OML.ReverseRelation)));
345
+ triples.push(this.quadWithGraph(reverseNode, namedNode(OML.relationBase), namedNode(baseIri)));
346
+ triples.push(this.quadWithGraph(reverseNode, namedNode(OWL.inverseOf), namedNode(forwardIri)));
347
+ this.mapAnnotations(reverseNode, reverse.ownedAnnotations, ctx, triples);
348
+ }
349
+ mapScalar(scalar, ctx, triples) {
350
+ const iri = this.elementIri(scalar, ctx);
351
+ if (!iri)
352
+ return;
353
+ const node = namedNode(iri);
354
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(RDFS.Datatype)));
355
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.Scalar)));
356
+ this.mapAnnotations(node, scalar.ownedAnnotations, ctx, triples);
357
+ this.applySpecializations(scalar.ownedSpecializations, node, ctx, triples);
358
+ this.applyScalarEquivalences(scalar.ownedEquivalences, node, ctx, triples);
359
+ this.applyScalarEnumeration(scalar.ownedEnumeration, node, ctx, triples);
360
+ }
361
+ mapAnnotationProperty(property, ctx, triples) {
362
+ const iri = this.elementIri(property, ctx);
363
+ if (!iri)
364
+ return;
365
+ const node = namedNode(iri);
366
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.AnnotationProperty)));
367
+ this.mapAnnotations(node, property.ownedAnnotations, ctx, triples);
368
+ this.applySpecializations(property.ownedSpecializations, node, ctx, triples, true);
369
+ this.applyPropertyEquivalences(property.ownedEquivalences, node, ctx, triples);
370
+ }
371
+ mapScalarProperty(property, ctx, triples) {
372
+ const iri = this.elementIri(property, ctx);
373
+ if (!iri)
374
+ return;
375
+ const node = namedNode(iri);
376
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.DatatypeProperty)));
377
+ this.mapAnnotations(node, property.ownedAnnotations, ctx, triples);
378
+ this.applyDomains(node, property.domains, ctx, triples);
379
+ this.applyRanges(node, property.ranges, ctx, triples);
380
+ if (property.functional) {
381
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.FunctionalProperty)));
382
+ }
383
+ this.applySpecializations(property.ownedSpecializations, node, ctx, triples, true);
384
+ this.applyPropertyEquivalences(property.ownedEquivalences, node, ctx, triples);
385
+ }
386
+ mapConceptInstance(instance, ctx, triples) {
387
+ const iri = this.elementIri(instance, ctx);
388
+ if (!iri)
389
+ return;
390
+ const node = namedNode(iri);
391
+ const isDefinition = !instance.ref;
392
+ instance.ownedTypes?.forEach((typeAssertion) => this.mapTypeAssertion(node, typeAssertion, ctx, triples));
393
+ if (isDefinition) {
394
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.ConceptInstance)));
395
+ }
396
+ this.mapAnnotations(node, instance.ownedAnnotations, ctx, triples);
397
+ instance.ownedPropertyValues?.forEach((pv) => this.mapPropertyValueAssertion(node, pv, ctx, triples));
398
+ }
399
+ mapRelationInstance(instance, ctx, triples) {
400
+ const iri = this.elementIri(instance, ctx);
401
+ if (!iri)
402
+ return;
403
+ const node = namedNode(iri);
404
+ const isDefinition = !instance.ref;
405
+ instance.ownedTypes?.forEach((typeAssertion) => this.mapTypeAssertion(node, typeAssertion, ctx, triples));
406
+ if (isDefinition) {
407
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.RelationInstance)));
408
+ }
409
+ this.mapAnnotations(node, instance.ownedAnnotations, ctx, triples);
410
+ instance.sources?.forEach((src) => {
411
+ const srcIri = this.resolveIri(src, ctx);
412
+ if (srcIri) {
413
+ triples.push(this.quadWithGraph(node, namedNode(OML.hasSource), namedNode(srcIri)));
414
+ }
415
+ });
416
+ instance.targets?.forEach((tgt) => {
417
+ const tgtIri = this.resolveIri(tgt, ctx);
418
+ if (tgtIri) {
419
+ triples.push(this.quadWithGraph(node, namedNode(OML.hasTarget), namedNode(tgtIri)));
420
+ }
421
+ });
422
+ instance.ownedPropertyValues?.forEach((pv) => this.mapPropertyValueAssertion(node, pv, ctx, triples));
423
+ }
424
+ mapRule(rule, ctx, triples) {
425
+ if (this.options.emitSwrlRules === false)
426
+ return;
427
+ if (rule.ref)
428
+ return;
429
+ const iri = this.elementIri(rule, ctx);
430
+ if (!iri)
431
+ return;
432
+ const node = namedNode(iri);
433
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(SWRL.Imp)));
434
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), literal(OML.Rule)));
435
+ triples.push(this.quadWithGraph(node, namedNode(SWRL.body), this.createRdfList(ctx, this.mapRulePredicates(rule.antecedent, ctx, triples, iri, 'body'), triples, `rule-body|${iri}`)));
436
+ triples.push(this.quadWithGraph(node, namedNode(SWRL.head), this.createRdfList(ctx, this.mapRulePredicates(rule.consequent, ctx, triples, iri, 'head'), triples, `rule-head|${iri}`)));
437
+ this.mapAnnotations(node, rule.ownedAnnotations, ctx, triples);
438
+ }
439
+ mapBuiltIn(builtIn, ctx, triples) {
440
+ const iri = this.elementIri(builtIn, ctx);
441
+ if (!iri)
442
+ return;
443
+ const node = namedNode(iri);
444
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OML.BuiltIn)));
445
+ this.mapAnnotations(node, builtIn.ownedAnnotations, ctx, triples);
446
+ }
447
+ mapRelationEntityForwardRule(relationEntityIri, forwardIri, ctx, triples) {
448
+ const relationVar = this.toSwrlVariable('r', triples);
449
+ const sourceVar = this.toSwrlVariable('s', triples);
450
+ const targetVar = this.toSwrlVariable('t', triples);
451
+ const classAtom = skolemize(ctx.namespace, 'rule-atom', relationEntityIri, 'relationEntity', 'class');
452
+ triples.push(this.quadWithGraph(classAtom, namedNode(RDF.type), namedNode(SWRL.ClassAtom)));
453
+ triples.push(this.quadWithGraph(classAtom, namedNode(SWRL.classPredicate), namedNode(relationEntityIri)));
454
+ triples.push(this.quadWithGraph(classAtom, namedNode(SWRL.argument1), relationVar));
455
+ const sourceAtom = skolemize(ctx.namespace, 'rule-atom', relationEntityIri, 'relationEntity', 'source');
456
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(RDF.type), namedNode(SWRL.IndividualPropertyAtom)));
457
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(SWRL.propertyPredicate), namedNode(OML.hasSource)));
458
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(SWRL.argument1), relationVar));
459
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(SWRL.argument2), sourceVar));
460
+ const targetAtom = skolemize(ctx.namespace, 'rule-atom', relationEntityIri, 'relationEntity', 'target');
461
+ triples.push(this.quadWithGraph(targetAtom, namedNode(RDF.type), namedNode(SWRL.IndividualPropertyAtom)));
462
+ triples.push(this.quadWithGraph(targetAtom, namedNode(SWRL.propertyPredicate), namedNode(OML.hasTarget)));
463
+ triples.push(this.quadWithGraph(targetAtom, namedNode(SWRL.argument1), relationVar));
464
+ triples.push(this.quadWithGraph(targetAtom, namedNode(SWRL.argument2), targetVar));
465
+ const headAtom = skolemize(ctx.namespace, 'rule-atom', relationEntityIri, 'relationEntity', 'forward');
466
+ triples.push(this.quadWithGraph(headAtom, namedNode(RDF.type), namedNode(SWRL.IndividualPropertyAtom)));
467
+ triples.push(this.quadWithGraph(headAtom, namedNode(SWRL.propertyPredicate), namedNode(forwardIri)));
468
+ triples.push(this.quadWithGraph(headAtom, namedNode(SWRL.argument1), sourceVar));
469
+ triples.push(this.quadWithGraph(headAtom, namedNode(SWRL.argument2), targetVar));
470
+ const body = this.createSwrlAtomList(ctx, [classAtom, sourceAtom, targetAtom], triples, `relation-entity-rule-body|${relationEntityIri}`);
471
+ const head = this.createSwrlAtomList(ctx, [headAtom], triples, `relation-entity-rule-head|${relationEntityIri}`);
472
+ const ruleNode = namedNode(`${forwardIri}__derivation`);
473
+ triples.push(this.quadWithGraph(ruleNode, namedNode(RDF.type), namedNode(SWRL.Imp)));
474
+ triples.push(this.quadWithGraph(ruleNode, namedNode(SWRL.body), body));
475
+ triples.push(this.quadWithGraph(ruleNode, namedNode(SWRL.head), head));
476
+ }
477
+ mapRulePredicates(predicates, ctx, triples, ruleIri, side) {
478
+ return predicates.flatMap((predicate, index) => this.mapRulePredicate(predicate, ctx, triples, ruleIri, side, index));
479
+ }
480
+ mapRulePredicate(predicate, ctx, triples, ruleIri, side, index) {
481
+ const atomSeed = `${ruleIri}|${side}|${index}`;
482
+ if (isTypePredicate(predicate)) {
483
+ const typeIri = this.resolveIri(predicate.type, ctx);
484
+ if (!typeIri)
485
+ return [];
486
+ if (predicate.type.ref && isScalar(predicate.type.ref)) {
487
+ const argument = this.toSwrlDArgument(predicate.argument, ctx, triples);
488
+ if (!argument)
489
+ return [];
490
+ const atom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'type', 'dataRange', typeIri, this.termKey(argument));
491
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.DataRangeAtom)));
492
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.dataRange), namedNode(typeIri)));
493
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument1), argument));
494
+ return [atom];
495
+ }
496
+ const argument = this.toSwrlIArgument(predicate.argument, ctx, triples);
497
+ if (!argument)
498
+ return [];
499
+ const atom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'type', 'class', typeIri, this.termKey(argument));
500
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.ClassAtom)));
501
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.classPredicate), namedNode(typeIri)));
502
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument1), argument));
503
+ return [atom];
504
+ }
505
+ if (isRelationEntityPredicate(predicate)) {
506
+ const typeIri = this.resolveIri(predicate.type, ctx);
507
+ const argument = this.toSwrlIArgument(predicate.argument, ctx, triples);
508
+ const argument1 = this.toSwrlIArgument(predicate.argument1, ctx, triples);
509
+ const argument2 = this.toSwrlIArgument(predicate.argument2, ctx, triples);
510
+ if (!typeIri || !argument || !argument1 || !argument2)
511
+ return [];
512
+ const typeAtom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'relationEntity', 'type', typeIri, this.termKey(argument));
513
+ triples.push(this.quadWithGraph(typeAtom, namedNode(RDF.type), namedNode(SWRL.ClassAtom)));
514
+ triples.push(this.quadWithGraph(typeAtom, namedNode(SWRL.classPredicate), namedNode(typeIri)));
515
+ triples.push(this.quadWithGraph(typeAtom, namedNode(SWRL.argument1), argument));
516
+ const sourceAtom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'relationEntity', 'source', this.termKey(argument), this.termKey(argument1));
517
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(RDF.type), namedNode(SWRL.IndividualPropertyAtom)));
518
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(SWRL.propertyPredicate), namedNode(OML.hasSource)));
519
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(SWRL.argument1), argument));
520
+ triples.push(this.quadWithGraph(sourceAtom, namedNode(SWRL.argument2), argument1));
521
+ const targetAtom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'relationEntity', 'target', this.termKey(argument), this.termKey(argument2));
522
+ triples.push(this.quadWithGraph(targetAtom, namedNode(RDF.type), namedNode(SWRL.IndividualPropertyAtom)));
523
+ triples.push(this.quadWithGraph(targetAtom, namedNode(SWRL.propertyPredicate), namedNode(OML.hasTarget)));
524
+ triples.push(this.quadWithGraph(targetAtom, namedNode(SWRL.argument1), argument));
525
+ triples.push(this.quadWithGraph(targetAtom, namedNode(SWRL.argument2), argument2));
526
+ return [typeAtom, sourceAtom, targetAtom];
527
+ }
528
+ if (isPropertyPredicate(predicate)) {
529
+ const propertyIri = this.resolveIri(predicate.property, ctx);
530
+ const argument1 = this.toSwrlIArgument(predicate.argument1, ctx, triples);
531
+ if (!propertyIri || !argument1)
532
+ return [];
533
+ const isDataPredicate = (predicate.property.ref && isScalarProperty(predicate.property.ref)) ||
534
+ (predicate.property.ref && isAnnotationProperty(predicate.property.ref) && Boolean(predicate.argument2.literal));
535
+ const atom = isDataPredicate
536
+ ? skolemize(ctx.namespace, 'rule-atom', atomSeed, 'property', 'data', propertyIri, this.termKey(argument1))
537
+ : skolemize(ctx.namespace, 'rule-atom', atomSeed, 'property', 'object', propertyIri, this.termKey(argument1));
538
+ if (isDataPredicate) {
539
+ const argument2 = this.toSwrlDArgument(predicate.argument2, ctx, triples);
540
+ if (!argument2)
541
+ return [];
542
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.DatavaluedPropertyAtom)));
543
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.propertyPredicate), namedNode(propertyIri)));
544
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument1), argument1));
545
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument2), argument2));
546
+ }
547
+ else {
548
+ const argument2 = this.toSwrlIArgument(predicate.argument2, ctx, triples);
549
+ if (!argument2)
550
+ return [];
551
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.IndividualPropertyAtom)));
552
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.propertyPredicate), namedNode(propertyIri)));
553
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument1), argument1));
554
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument2), argument2));
555
+ }
556
+ return [atom];
557
+ }
558
+ if (isSameAsPredicate(predicate)) {
559
+ const argument1 = this.toSwrlIArgument(predicate.argument1, ctx, triples);
560
+ const argument2 = this.toSwrlIArgument(predicate.argument2, ctx, triples);
561
+ if (!argument1 || !argument2)
562
+ return [];
563
+ const atom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'sameAs', this.termKey(argument1), this.termKey(argument2));
564
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.SameIndividualAtom)));
565
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument1), argument1));
566
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument2), argument2));
567
+ return [atom];
568
+ }
569
+ if (isDifferentFromPredicate(predicate)) {
570
+ const argument1 = this.toSwrlIArgument(predicate.argument1, ctx, triples);
571
+ const argument2 = this.toSwrlIArgument(predicate.argument2, ctx, triples);
572
+ if (!argument1 || !argument2)
573
+ return [];
574
+ const atom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'differentFrom', this.termKey(argument1), this.termKey(argument2));
575
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.DifferentIndividualsAtom)));
576
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument1), argument1));
577
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.argument2), argument2));
578
+ return [atom];
579
+ }
580
+ if (isBuiltInPredicate(predicate)) {
581
+ const builtInIri = this.resolveIri(predicate.builtIn, ctx);
582
+ if (!builtInIri)
583
+ return [];
584
+ const argumentsList = predicate.arguments
585
+ .map((argument) => this.toSwrlDArgument(argument, ctx, triples))
586
+ .filter((argument) => Boolean(argument));
587
+ const atom = skolemize(ctx.namespace, 'rule-atom', atomSeed, 'builtin', builtInIri, ...argumentsList.map((argument) => this.termKey(argument)));
588
+ triples.push(this.quadWithGraph(atom, namedNode(RDF.type), namedNode(SWRL.BuiltinAtom)));
589
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.builtin), namedNode(builtInIri)));
590
+ triples.push(this.quadWithGraph(atom, namedNode(SWRL.arguments), this.createRdfList(ctx, argumentsList, triples, `rule-builtin-args|${atomSeed}|${builtInIri}`)));
591
+ return [atom];
592
+ }
593
+ return [];
594
+ }
595
+ toSwrlIArgument(argument, ctx, triples) {
596
+ if (argument.variable) {
597
+ return this.toSwrlVariable(argument.variable, triples);
598
+ }
599
+ const instanceIri = this.resolveIri(argument.instance, ctx);
600
+ if (!instanceIri)
601
+ return undefined;
602
+ return namedNode(instanceIri);
603
+ }
604
+ toSwrlDArgument(argument, ctx, triples) {
605
+ if (argument.variable) {
606
+ return this.toSwrlVariable(argument.variable, triples);
607
+ }
608
+ if (argument.literal) {
609
+ return this.toLiteral(argument.literal, ctx);
610
+ }
611
+ return undefined;
612
+ }
613
+ toSwrlVariable(variable, triples) {
614
+ const node = namedNode(`urn:swrl:var#${variable}`);
615
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(SWRL.Variable)));
616
+ return node;
617
+ }
618
+ mapTypeAssertion(subject, assertion, ctx, triples) {
619
+ const typeIri = this.resolveIri(assertion.type, ctx);
620
+ if (typeIri) {
621
+ triples.push(this.quadWithGraph(subject, namedNode(RDF.type), namedNode(typeIri)));
622
+ }
623
+ }
624
+ mapPropertyValueAssertion(subject, assertion, ctx, triples) {
625
+ const propertyIri = this.resolveIri(assertion.property, ctx);
626
+ if (!propertyIri)
627
+ return;
628
+ const propertyNode = namedNode(propertyIri);
629
+ assertion.literalValues?.forEach((lit) => {
630
+ const litNode = this.toLiteral(lit, ctx);
631
+ if (litNode) {
632
+ triples.push(this.quadWithGraph(subject, propertyNode, litNode));
633
+ }
634
+ });
635
+ assertion.referencedValues?.forEach((ref) => {
636
+ const objIri = this.resolveIri(ref, ctx);
637
+ if (objIri) {
638
+ triples.push(this.quadWithGraph(subject, propertyNode, namedNode(objIri)));
639
+ }
640
+ });
641
+ assertion.containedValues?.forEach((contained, index) => {
642
+ const valueNode = skolemize(ctx.namespace, 'anon', this.termKey(subject), propertyIri, String(index));
643
+ triples.push(this.quadWithGraph(subject, propertyNode, valueNode));
644
+ this.mapAnonymousInstance(contained, valueNode, ctx, triples, subject);
645
+ });
646
+ }
647
+ mapAnnotations(subject, annotations, ctx, triples) {
648
+ annotations?.forEach((annotation) => {
649
+ const propertyIri = this.resolveIri(annotation.property, ctx);
650
+ if (!propertyIri)
651
+ return;
652
+ const propertyNode = namedNode(propertyIri);
653
+ annotation.literalValues?.forEach((lit) => {
654
+ const litNode = this.toLiteral(lit, ctx);
655
+ if (litNode) {
656
+ triples.push(this.quadWithGraph(subject, propertyNode, litNode));
657
+ }
658
+ });
659
+ annotation.referencedValues?.forEach((ref) => {
660
+ const objIri = this.resolveIri(ref, ctx);
661
+ if (objIri) {
662
+ triples.push(this.quadWithGraph(subject, propertyNode, namedNode(objIri)));
663
+ }
664
+ });
665
+ });
666
+ }
667
+ mapAnonymousInstance(instance, node, ctx, triples, source) {
668
+ if (isAnonymousConceptInstance(instance)) {
669
+ if (instance.type) {
670
+ const typeIri = this.resolveIri(instance.type, ctx);
671
+ if (typeIri) {
672
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(typeIri)));
673
+ }
674
+ }
675
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.ConceptInstance)));
676
+ instance.ownedPropertyValues?.forEach((pv, index) => this.mapPropertyValueAssertion(node, pv, ctx, triples));
677
+ }
678
+ else if (isAnonymousRelationInstance(instance)) {
679
+ if (source) {
680
+ triples.push(this.quadWithGraph(node, namedNode(OML.hasSource), source));
681
+ }
682
+ const targetIri = this.resolveIri(instance.target, ctx);
683
+ if (targetIri) {
684
+ triples.push(this.quadWithGraph(node, namedNode(OML.hasTarget), namedNode(targetIri)));
685
+ }
686
+ triples.push(this.quadWithGraph(node, namedNode(OML.type), namedNode(OML.RelationInstance)));
687
+ instance.ownedPropertyValues?.forEach((pv, index) => this.mapPropertyValueAssertion(node, pv, ctx, triples));
688
+ }
689
+ }
690
+ applySpecializations(axioms, subNode, ctx, triples, isProperty = false) {
691
+ axioms?.forEach((ax) => {
692
+ const superIri = this.resolveIri(ax.superTerm, ctx);
693
+ if (superIri) {
694
+ triples.push(this.quadWithGraph(subNode, namedNode(isProperty ? RDFS.subPropertyOf : RDFS.subClassOf), namedNode(superIri)));
695
+ }
696
+ });
697
+ }
698
+ applyEquivalences(axioms, classNode, ctx, triples) {
699
+ axioms?.forEach((ax) => {
700
+ ax.superTerms?.forEach((sup) => {
701
+ const superIri = this.resolveIri(sup, ctx);
702
+ if (superIri) {
703
+ triples.push(this.quadWithGraph(classNode, namedNode(OWL.equivalentClass), namedNode(superIri)));
704
+ }
705
+ });
706
+ this.applyPropertyRestrictions(ax.ownedPropertyRestrictions, classNode, ctx, triples);
707
+ });
708
+ }
709
+ applyKeys(axioms, classNode, ctx, triples) {
710
+ axioms?.forEach((ax) => {
711
+ const propertyNodes = ax.properties
712
+ .map((property) => this.resolveIri(property, ctx))
713
+ .filter((iri) => Boolean(iri))
714
+ .map((iri) => namedNode(iri));
715
+ if (!propertyNodes.length)
716
+ return;
717
+ const listHead = this.createRdfList(ctx, propertyNodes, triples, `keys|${classNode.value}|${propertyNodes.map((node) => node.value).join('|')}`);
718
+ triples.push(this.quadWithGraph(classNode, namedNode(OWL.hasKey), listHead));
719
+ });
720
+ }
721
+ applyScalarEquivalences(axioms, scalarNode, ctx, triples) {
722
+ axioms?.forEach((ax) => {
723
+ ax.superTerms?.forEach((sup) => {
724
+ const superIri = this.resolveIri(sup, ctx);
725
+ if (superIri) {
726
+ triples.push(this.quadWithGraph(scalarNode, namedNode(OWL.equivalentClass), namedNode(superIri)));
727
+ }
728
+ });
729
+ this.createScalarFacetRestrictionNodes(ax, ctx, triples).forEach((restrictionNode) => {
730
+ triples.push(this.quadWithGraph(scalarNode, namedNode(OWL.equivalentClass), restrictionNode));
731
+ });
732
+ });
733
+ }
734
+ applyInstanceEnumeration(enumeration, classNode, ctx, triples) {
735
+ const instances = enumeration?.instances ?? [];
736
+ if (!instances.length)
737
+ return;
738
+ const instanceNodes = instances
739
+ .map((instance) => this.resolveIri(instance, ctx))
740
+ .filter((iri) => Boolean(iri))
741
+ .map((iri) => namedNode(iri));
742
+ if (!instanceNodes.length)
743
+ return;
744
+ const listHead = this.createRdfList(ctx, instanceNodes, triples, `instance-enum|${classNode.value}|${instanceNodes.map((node) => node.value).join('|')}`);
745
+ triples.push(this.quadWithGraph(classNode, namedNode(OWL.oneOf), listHead));
746
+ }
747
+ applyScalarEnumeration(enumeration, scalarNode, ctx, triples) {
748
+ const literals = enumeration?.literals ?? [];
749
+ if (!literals.length)
750
+ return;
751
+ const literalNodes = literals
752
+ .map((lit) => this.toLiteral(lit, ctx))
753
+ .filter((node) => Boolean(node));
754
+ if (!literalNodes.length)
755
+ return;
756
+ const literalSignature = literalNodes.map((node) => this.termKey(node)).join('|');
757
+ const listHead = this.createRdfList(ctx, literalNodes, triples, `scalar-enum|${scalarNode.value}|${literalSignature}`);
758
+ const dataRangeNode = skolemize(ctx.namespace, 'datatype-enum', scalarNode.value, literalSignature);
759
+ triples.push(this.quadWithGraph(dataRangeNode, namedNode(RDF.type), namedNode(RDFS.Datatype)));
760
+ triples.push(this.quadWithGraph(dataRangeNode, namedNode(OWL.oneOf), listHead));
761
+ triples.push(this.quadWithGraph(scalarNode, namedNode(OWL.equivalentClass), dataRangeNode));
762
+ }
763
+ applyPropertyEquivalences(axioms, propertyNode, ctx, triples) {
764
+ axioms?.forEach((ax) => {
765
+ ax.superTerms?.forEach((sup) => {
766
+ const superIri = this.resolveIri(sup, ctx);
767
+ if (superIri) {
768
+ triples.push(this.quadWithGraph(propertyNode, namedNode(OWL.equivalentProperty), namedNode(superIri)));
769
+ }
770
+ });
771
+ });
772
+ }
773
+ applyPropertyRestrictions(axioms, classNode, ctx, triples) {
774
+ axioms?.forEach((ax) => {
775
+ const restrictionNode = this.mapRestriction(ax, ctx, triples);
776
+ if (restrictionNode) {
777
+ triples.push(this.quadWithGraph(classNode, namedNode(RDFS.subClassOf), restrictionNode));
778
+ }
779
+ });
780
+ }
781
+ createScalarFacetRestrictionNodes(axiom, ctx, triples) {
782
+ const facets = [];
783
+ axiom.length.forEach((value) => {
784
+ facets.push({ iri: XSD.length, value: literal(String(value), namedNode(XSD.integer)) });
785
+ });
786
+ axiom.minLength.forEach((value) => {
787
+ facets.push({ iri: XSD.minLength, value: literal(String(value), namedNode(XSD.integer)) });
788
+ });
789
+ axiom.maxLength.forEach((value) => {
790
+ facets.push({ iri: XSD.maxLength, value: literal(String(value), namedNode(XSD.integer)) });
791
+ });
792
+ axiom.pattern.forEach((value) => {
793
+ facets.push({ iri: XSD.pattern, value: literal(value) });
794
+ });
795
+ axiom.language.forEach((value) => {
796
+ facets.push({ iri: RDF.langRange, value: literal(value) });
797
+ });
798
+ axiom.minInclusive.forEach((value) => {
799
+ const node = this.toLiteral(value, ctx);
800
+ if (node)
801
+ facets.push({ iri: XSD.minInclusive, value: node });
802
+ });
803
+ axiom.minExclusive.forEach((value) => {
804
+ const node = this.toLiteral(value, ctx);
805
+ if (node)
806
+ facets.push({ iri: XSD.minExclusive, value: node });
807
+ });
808
+ axiom.maxInclusive.forEach((value) => {
809
+ const node = this.toLiteral(value, ctx);
810
+ if (node)
811
+ facets.push({ iri: XSD.maxInclusive, value: node });
812
+ });
813
+ axiom.maxExclusive.forEach((value) => {
814
+ const node = this.toLiteral(value, ctx);
815
+ if (node)
816
+ facets.push({ iri: XSD.maxExclusive, value: node });
817
+ });
818
+ if (!facets.length)
819
+ return [];
820
+ const datatypes = axiom.superTerms
821
+ .map((sup) => this.resolveIri(sup, ctx))
822
+ .filter((iri) => Boolean(iri));
823
+ if (!datatypes.length) {
824
+ if (axiom.language.length > 0) {
825
+ datatypes.push(RDF.langString);
826
+ }
827
+ else if (axiom.length.length || axiom.minLength.length || axiom.maxLength.length || axiom.pattern.length) {
828
+ datatypes.push(XSD.string);
829
+ }
830
+ else {
831
+ const lexicalFacet = facets.find((facet) => facet.value.termType === 'Literal');
832
+ if (lexicalFacet && lexicalFacet.value.termType === 'Literal') {
833
+ datatypes.push(lexicalFacet.value.datatype.value);
834
+ }
835
+ }
836
+ }
837
+ return datatypes.map((datatypeIri) => {
838
+ const facetSignature = facets.map((facet) => `${facet.iri}=${this.termKey(facet.value)}`).join('|');
839
+ const restrictionNode = skolemize(ctx.namespace, 'restriction', 'datatype', datatypeIri, facetSignature);
840
+ triples.push(this.quadWithGraph(restrictionNode, namedNode(OWL.onDatatype), namedNode(datatypeIri)));
841
+ const restrictionItems = facets.map((facet, index) => {
842
+ const facetNode = skolemize(ctx.namespace, 'restriction-facet', datatypeIri, String(index), facet.iri, this.termKey(facet.value));
843
+ triples.push(this.quadWithGraph(facetNode, namedNode(facet.iri), facet.value));
844
+ return facetNode;
845
+ });
846
+ const listHead = this.createRdfList(ctx, restrictionItems, triples, `restriction-facets|${datatypeIri}|${facetSignature}`);
847
+ triples.push(this.quadWithGraph(restrictionNode, namedNode(OWL.withRestrictions), listHead));
848
+ return restrictionNode;
849
+ });
850
+ }
851
+ mapRestriction(axiom, ctx, triples) {
852
+ if (isPropertyRangeRestrictionAxiom(axiom)) {
853
+ return this.mapRangeRestriction(axiom, ctx, triples);
854
+ }
855
+ if (isPropertyCardinalityRestrictionAxiom(axiom)) {
856
+ return this.mapCardinalityRestriction(axiom, ctx, triples);
857
+ }
858
+ if (isPropertyValueRestrictionAxiom(axiom)) {
859
+ return this.mapValueRestriction(axiom, ctx, triples);
860
+ }
861
+ if (isPropertySelfRestrictionAxiom(axiom)) {
862
+ return this.mapSelfRestriction(axiom, ctx, triples);
863
+ }
864
+ return undefined;
865
+ }
866
+ mapRangeRestriction(axiom, ctx, triples) {
867
+ const propertyIri = this.resolveIri(axiom.property, ctx);
868
+ const rangeIri = this.resolveIri(axiom.range, ctx);
869
+ if (!propertyIri || !rangeIri)
870
+ return undefined;
871
+ const node = skolemize(ctx.namespace, 'restriction', propertyIri, axiom.kind, rangeIri, 'range');
872
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.Restriction)));
873
+ triples.push(this.quadWithGraph(node, namedNode(OWL.onProperty), namedNode(propertyIri)));
874
+ const predicate = axiom.kind === 'all' ? OWL.allValuesFrom : OWL.someValuesFrom;
875
+ triples.push(this.quadWithGraph(node, namedNode(predicate), namedNode(rangeIri)));
876
+ return node;
877
+ }
878
+ mapCardinalityRestriction(axiom, ctx, triples) {
879
+ const propertyIri = this.resolveIri(axiom.property, ctx);
880
+ if (!propertyIri)
881
+ return undefined;
882
+ const resolvedProperty = axiom.property?.ref;
883
+ const rangeIri = this.resolveIri(axiom.range, ctx) ?? '';
884
+ const node = skolemize(ctx.namespace, 'restriction', propertyIri, axiom.kind, String(axiom.cardinality), rangeIri);
885
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.Restriction)));
886
+ triples.push(this.quadWithGraph(node, namedNode(OWL.onProperty), namedNode(propertyIri)));
887
+ if (rangeIri) {
888
+ const on = resolvedProperty && isScalarProperty(resolvedProperty) ? OWL.onDataRange : OWL.onClass;
889
+ triples.push(this.quadWithGraph(node, namedNode(on), namedNode(rangeIri)));
890
+ }
891
+ const cardPredicate = axiom.kind === 'min'
892
+ ? OWL.minCardinality
893
+ : axiom.kind === 'max'
894
+ ? OWL.maxCardinality
895
+ : OWL.cardinality;
896
+ triples.push(this.quadWithGraph(node, namedNode(cardPredicate), literal(String(axiom.cardinality), namedNode(XSD.integer))));
897
+ return node;
898
+ }
899
+ mapValueRestriction(axiom, ctx, triples) {
900
+ const propertyIri = this.resolveIri(axiom.property, ctx);
901
+ if (!propertyIri)
902
+ return undefined;
903
+ const valueSeed = axiom.literalValue
904
+ ? this.literalKey(axiom.literalValue, ctx)
905
+ : axiom.referencedValue
906
+ ? this.resolveIri(axiom.referencedValue, ctx) ?? ''
907
+ : 'contained';
908
+ const node = skolemize(ctx.namespace, 'restriction', propertyIri, 'hasValue', valueSeed);
909
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.Restriction)));
910
+ triples.push(this.quadWithGraph(node, namedNode(OWL.onProperty), namedNode(propertyIri)));
911
+ if (axiom.literalValue) {
912
+ const litNode = this.toLiteral(axiom.literalValue, ctx);
913
+ if (litNode) {
914
+ triples.push(this.quadWithGraph(node, namedNode(OWL.hasValue), litNode));
915
+ }
916
+ }
917
+ else if (axiom.referencedValue) {
918
+ const refIri = this.resolveIri(axiom.referencedValue, ctx);
919
+ if (refIri) {
920
+ triples.push(this.quadWithGraph(node, namedNode(OWL.hasValue), namedNode(refIri)));
921
+ }
922
+ }
923
+ else if (axiom.containedValue) {
924
+ const valueNode = skolemize(ctx.namespace, 'anon', propertyIri, 'hasValue');
925
+ triples.push(this.quadWithGraph(node, namedNode(OWL.hasValue), valueNode));
926
+ this.mapAnonymousInstance(axiom.containedValue, valueNode, ctx, triples);
927
+ }
928
+ return node;
929
+ }
930
+ mapSelfRestriction(axiom, ctx, triples) {
931
+ const propertyIri = this.resolveIri(axiom.property, ctx);
932
+ if (!propertyIri)
933
+ return undefined;
934
+ const node = skolemize(ctx.namespace, 'restriction', propertyIri, 'hasSelf');
935
+ triples.push(this.quadWithGraph(node, namedNode(RDF.type), namedNode(OWL.Restriction)));
936
+ triples.push(this.quadWithGraph(node, namedNode(OWL.onProperty), namedNode(propertyIri)));
937
+ triples.push(this.quadWithGraph(node, namedNode(OWL.hasSelf), literal('true', namedNode(XSD.boolean))));
938
+ return node;
939
+ }
940
+ applyDomainRange(propertyNode, domains, ranges, ctx, triples) {
941
+ this.applyDomains(propertyNode, domains, ctx, triples);
942
+ this.applyRanges(propertyNode, ranges, ctx, triples);
943
+ }
944
+ applyDomains(propertyNode, domains, ctx, triples) {
945
+ domains?.forEach((d) => {
946
+ const iri = this.resolveIri(d, ctx);
947
+ if (iri) {
948
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDFS.domain), namedNode(iri)));
949
+ }
950
+ });
951
+ }
952
+ applyRanges(propertyNode, ranges, ctx, triples) {
953
+ ranges?.forEach((r) => {
954
+ const iri = this.resolveIri(r, ctx);
955
+ if (iri) {
956
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDFS.range), namedNode(iri)));
957
+ }
958
+ });
959
+ }
960
+ applyObjectPropertyCharacteristics(propertyNode, relation, triples) {
961
+ if (relation.functional) {
962
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.FunctionalProperty)));
963
+ }
964
+ if (relation.inverseFunctional) {
965
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.InverseFunctionalProperty)));
966
+ }
967
+ if (relation.symmetric) {
968
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.SymmetricProperty)));
969
+ }
970
+ if (relation.asymmetric) {
971
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.AsymmetricProperty)));
972
+ }
973
+ if (relation.reflexive) {
974
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.ReflexiveProperty)));
975
+ }
976
+ if (relation.irreflexive) {
977
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.IrreflexiveProperty)));
978
+ }
979
+ if (relation.transitive) {
980
+ triples.push(this.quadWithGraph(propertyNode, namedNode(RDF.type), namedNode(OWL.TransitiveProperty)));
981
+ }
982
+ }
983
+ quadWithGraph(subject, predicate, object) {
984
+ return quad(subject, predicate, object);
985
+ }
986
+ createRdfList(ctx, items, triples, listSeed) {
987
+ if (!items.length) {
988
+ return namedNode(RDF.nil);
989
+ }
990
+ const base = `${listSeed}|${items.map((item) => this.termKey(item)).join('|')}`;
991
+ const head = skolemize(ctx.namespace, 'list', base, '0');
992
+ let current = head;
993
+ items.forEach((item, index) => {
994
+ triples.push(this.quadWithGraph(current, namedNode(RDF.first), item));
995
+ if (index === items.length - 1) {
996
+ triples.push(this.quadWithGraph(current, namedNode(RDF.rest), namedNode(RDF.nil)));
997
+ }
998
+ else {
999
+ const next = skolemize(ctx.namespace, 'list', base, String(index + 1));
1000
+ triples.push(this.quadWithGraph(current, namedNode(RDF.rest), next));
1001
+ current = next;
1002
+ }
1003
+ });
1004
+ return head;
1005
+ }
1006
+ createSwrlAtomList(ctx, items, triples, listSeed) {
1007
+ const head = this.createRdfList(ctx, items, triples, listSeed);
1008
+ if (head.value === RDF.nil) {
1009
+ return head;
1010
+ }
1011
+ let current = head;
1012
+ while (current.value !== RDF.nil) {
1013
+ triples.push(this.quadWithGraph(current, namedNode(RDF.type), namedNode(SWRL.AtomList)));
1014
+ const next = triples.find((quad) => quad.subject.termType === 'NamedNode'
1015
+ && quad.subject.value === current.value
1016
+ && quad.predicate.value === RDF.rest
1017
+ && quad.object.termType === 'NamedNode')?.object;
1018
+ if (!next || next.termType !== 'NamedNode') {
1019
+ break;
1020
+ }
1021
+ current = next;
1022
+ }
1023
+ return head;
1024
+ }
1025
+ createContext(ontology) {
1026
+ const namespace = this.normalizeNamespace(ontology.namespace ?? '');
1027
+ const prefix = ontology.prefix ?? '';
1028
+ return {
1029
+ ontologyIri: namespace.replace(/[\/#]+$/, ''),
1030
+ namespace,
1031
+ prefix,
1032
+ prefixes: this.buildPrefixMap(ontology),
1033
+ };
1034
+ }
1035
+ buildPrefixMap(ontology) {
1036
+ const map = {};
1037
+ const ns = this.normalizeNamespace(ontology.namespace ?? '');
1038
+ const prefix = ontology.prefix;
1039
+ if (prefix) {
1040
+ map[prefix] = ns;
1041
+ }
1042
+ ontology.ownedImports?.forEach((imp) => {
1043
+ const imported = imp.imported?.ref;
1044
+ const importedNs = imported ? this.normalizeNamespace(imported.namespace ?? '') : undefined;
1045
+ const importedPrefix = imp.prefix ?? imported?.prefix;
1046
+ if (importedPrefix && importedNs) {
1047
+ map[importedPrefix] = importedNs;
1048
+ }
1049
+ });
1050
+ return map;
1051
+ }
1052
+ ontologyTypeIri(ontology) {
1053
+ if (isVocabulary(ontology))
1054
+ return OML.Vocabulary;
1055
+ if (isVocabularyBundle(ontology))
1056
+ return OML.VocabularyBundle;
1057
+ if (isDescription(ontology))
1058
+ return OML.Description;
1059
+ if (isDescriptionBundle(ontology))
1060
+ return OML.DescriptionBundle;
1061
+ return OML.Vocabulary;
1062
+ }
1063
+ importedOntologyIri(imp, ctx) {
1064
+ if (imp.imported?.ref && isOntology(imp.imported.ref)) {
1065
+ return this.normalizeNamespace(imp.imported.ref.namespace ?? '').replace(/[\/#]+$/, '');
1066
+ }
1067
+ const refText = imp.imported?.$refText ?? imp.imported?.refText;
1068
+ if (refText) {
1069
+ return this.resolveFromRefText(refText, ctx)?.replace(/[\/#]+$/, '');
1070
+ }
1071
+ return undefined;
1072
+ }
1073
+ getForwardIri(base, ctx) {
1074
+ if (isRelationEntity(base)) {
1075
+ if (base.forwardRelation?.name) {
1076
+ const ont = this.getOntology(base);
1077
+ const ns = this.normalizeNamespace(ont?.namespace ?? ctx.namespace);
1078
+ return `${ns}${base.forwardRelation.name}`;
1079
+ }
1080
+ const name = (base.ref?.name ?? base.name ?? '').replace(/^\^/, '');
1081
+ return `${ctx.namespace}has${this.toFirstUpper(name)}Forward`;
1082
+ }
1083
+ return this.elementIri(base, ctx) ?? '';
1084
+ }
1085
+ elementIri(element, ctx) {
1086
+ if (!element)
1087
+ return undefined;
1088
+ // If this is a ref wrapper, resolve to the referenced element's IRI.
1089
+ if (element.ref?.ref) {
1090
+ return this.elementIri(element.ref.ref, ctx);
1091
+ }
1092
+ const name = element.name ?? element.ref?.name;
1093
+ if (!name)
1094
+ return undefined;
1095
+ const ontology = this.getOntology(element);
1096
+ const namespace = ontology ? this.normalizeNamespace(ontology.namespace ?? ctx.namespace) : ctx.namespace;
1097
+ return `${namespace}${name.replace(/^\^/, '')}`;
1098
+ }
1099
+ getOntology(element) {
1100
+ let current = element;
1101
+ while (current && !isOntology(current)) {
1102
+ current = current.$container;
1103
+ }
1104
+ return current;
1105
+ }
1106
+ resolveIri(ref, ctx) {
1107
+ if (!ref)
1108
+ return undefined;
1109
+ if (typeof ref === 'string') {
1110
+ return this.resolveFromRefText(ref, ctx);
1111
+ }
1112
+ if (ref.ref && ref.ref.name) {
1113
+ return this.elementIri(ref.ref, ctx);
1114
+ }
1115
+ if (ref.name) {
1116
+ return this.elementIri(ref, ctx);
1117
+ }
1118
+ const refText = ref.$refText ?? ref.refText;
1119
+ if (refText) {
1120
+ return this.resolveFromRefText(refText, ctx);
1121
+ }
1122
+ return undefined;
1123
+ }
1124
+ resolveFromRefText(refText, ctx) {
1125
+ if (refText.startsWith('<') && refText.endsWith('>')) {
1126
+ return refText.slice(1, -1);
1127
+ }
1128
+ const parts = refText.split(':');
1129
+ if (parts.length === 2) {
1130
+ const [prefix, local] = parts;
1131
+ const ns = ctx.prefixes[prefix];
1132
+ if (ns) {
1133
+ return `${ns}${local}`;
1134
+ }
1135
+ }
1136
+ return undefined;
1137
+ }
1138
+ normalizeNamespace(ns) {
1139
+ return ns.replace(/^<|>$/g, '');
1140
+ }
1141
+ termKey(term) {
1142
+ if (term.termType === 'NamedNode') {
1143
+ return `iri:${term.value}`;
1144
+ }
1145
+ if (term.termType === 'Literal') {
1146
+ return `lit:${term.value}@${term.language}^^${term.datatype.value}`;
1147
+ }
1148
+ return `${term.termType}:${term.value}`;
1149
+ }
1150
+ literalKey(value, ctx) {
1151
+ const term = this.toLiteral(value, ctx);
1152
+ return term ? this.termKey(term) : '';
1153
+ }
1154
+ toLiteral(value, ctx) {
1155
+ if (isBooleanLiteral(value)) {
1156
+ return literal(String(value.value), namedNode(XSD.boolean));
1157
+ }
1158
+ if (isIntegerLiteral(value)) {
1159
+ return literal(String(value.value), namedNode(XSD.integer));
1160
+ }
1161
+ if (isDecimalLiteral(value)) {
1162
+ return literal(String(value.value), namedNode(XSD.decimal));
1163
+ }
1164
+ if (isDoubleLiteral(value)) {
1165
+ return literal(String(value.value), namedNode(XSD.double));
1166
+ }
1167
+ if (isQuotedLiteral(value)) {
1168
+ if (value.type) {
1169
+ const dt = this.resolveIri(value.type, ctx);
1170
+ return literal(value.value, dt ? namedNode(dt) : undefined);
1171
+ }
1172
+ if (value.langTag) {
1173
+ return literal(value.value, value.langTag);
1174
+ }
1175
+ return literal(value.value);
1176
+ }
1177
+ return undefined;
1178
+ }
1179
+ toFirstUpper(name) {
1180
+ if (!name)
1181
+ return name;
1182
+ if (name.length === 1)
1183
+ return name.toUpperCase();
1184
+ return `${name[0].toUpperCase()}${name.slice(1)}`;
1185
+ }
1186
+ deduplicateQuads(quads) {
1187
+ const unique = new Map();
1188
+ for (const quad of quads) {
1189
+ const key = `${quad.subject.id}|${quad.predicate.id}|${quad.object.id}|${quad.graph.id}`;
1190
+ if (!unique.has(key)) {
1191
+ unique.set(key, quad);
1192
+ }
1193
+ }
1194
+ return [...unique.values()];
1195
+ }
1196
+ }
1197
+ export function createOwlOntologyQuads() {
1198
+ const triples = [];
1199
+ const ontologyNode = namedNode(OWL_ONTOLOGY_IRI);
1200
+ triples.push(quad(ontologyNode, namedNode(RDF.type), namedNode(OWL.Ontology)));
1201
+ triples.push(quad(ontologyNode, namedNode(OML.type), namedNode(OML.Vocabulary)));
1202
+ triples.push(quad(ontologyNode, namedNode(OML.namespace), namedNode(OWL_ONTOLOGY_NAMESPACE)));
1203
+ triples.push(quad(ontologyNode, namedNode(OML.prefix), literal(OWL_ONTOLOGY_PREFIX)));
1204
+ for (const term of OML_TERMS) {
1205
+ triples.push(quad(namedNode(term.iri), namedNode(RDF.type), namedNode(term.rdfType)));
1206
+ }
1207
+ return triples;
1208
+ }
1209
+ export function owlOntologyPrefixes() {
1210
+ return {
1211
+ oml: OWL_ONTOLOGY_NAMESPACE,
1212
+ rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
1213
+ rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
1214
+ owl: 'http://www.w3.org/2002/07/owl#',
1215
+ };
1216
+ }
1217
+ //# sourceMappingURL=owl-mapper.js.map