/
ontology.service.ts
188 lines (165 loc) · 7.04 KB
/
ontology.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Injectable } from '@angular/core';
import {
Cardinality,
Constants, ReadOntology,
ResourcePropertyDefinitionWithAllLanguages
} from '@dasch-swiss/dsp-js';
import { Observable, of } from 'rxjs';
import { CacheService } from 'src/app/main/cache/cache.service';
import {
DefaultProperties,
DefaultProperty,
PropertyCategory
} from './default-data/default-properties';
/**
* helper methods for the ontology editor
*/
@Injectable({
providedIn: 'root'
})
export class OntologyService {
// list of default property types
defaultProperties: PropertyCategory[] = DefaultProperties.data;
constructor(
private _cache: CacheService
) { }
/**
* create a unique name (id) for resource classes or properties;
*
* @param ontologyIri
* @param [label]
* @returns unique name
*/
setUniqueName(ontologyIri: string, label?: string, type?: 'class' | 'prop'): string {
if (label && type) {
// build name from label
// normalize and replace spaces and special chars
return type + '-' + label.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[\u00a0-\u024f]/g, '').replace(/[\])}[{(]/g, '').replace(/\s+/g, '-').replace(/\//g, '-').toLowerCase();
} else {
// build randomized name
// the name starts with the three first character of ontology iri to avoid a start with a number followed by randomized string
return this.getOntologyName(ontologyIri).substring(0, 3) + Math.random().toString(36).substring(2, 5) + Math.random().toString(36).substring(2, 5);
}
}
/**
* get the ontolgoy name from ontology iri
*
* @param {string} ontologyIri
* @returns string
*/
getOntologyName(ontologyIri: string): string {
const array = ontologyIri.split('/');
const pos = array.length - 2;
return array[pos].toLowerCase();
}
/**
* get the name from the iri
* @param iri
* @returns name from iri
*/
getNameFromIri(iri: string): string {
const array = iri.split(Constants.HashDelimiter);
return array[1];
}
/**
* convert cardinality values (multiple? & required?) from form to DSP-JS cardinality enum 1-n, 0-n, 1, 0-1
* @param {boolean} multiple
* @param {boolean} required
* @returns Cardinality
*/
translateCardinality(multiple: boolean, required: boolean): Cardinality {
if (multiple && required) {
// cardinality 1-n (at least one)
return Cardinality._1_n;
} else if (multiple && !required) {
// cardinality 0-n (may have many)
return Cardinality._0_n;
} else if (!multiple && required) {
// cardinality 1 (required)
return Cardinality._1;
} else {
// cardinality 0-1 (optional)
return Cardinality._0_1;
}
}
getSuperProperty(property: ResourcePropertyDefinitionWithAllLanguages): string {
// get ontology from property info
const ontoIri = property.id.split(Constants.HashDelimiter)[0];
let superPropIri: string;
// get iri from sub properties
if (property.subPropertyOf.length) {
for (const subProp of property.subPropertyOf) {
const baseOntoIri = subProp.split(Constants.HashDelimiter)[0];
// compare with knora base ontology
if (baseOntoIri !== Constants.KnoraApiV2) {
// the property is not a subproperty of knora base ontology
// get property iri from another ontology
this._cache.get('currentProjectOntologies').subscribe(
(ontologies: ReadOntology[]) => {
const onto = ontologies.find(i => i.id === baseOntoIri);
superPropIri = onto.properties[subProp].subPropertyOf[0];
}
);
}
if (superPropIri) {
break;
}
}
}
return (superPropIri ? superPropIri : undefined);
}
/**
* get default property information for a certain ontology property
*/
getDefaultPropType(property: ResourcePropertyDefinitionWithAllLanguages): Observable<DefaultProperty> {
let propType: DefaultProperty;
for (const group of this.defaultProperties) {
if (property.subPropertyOf.length) {
for (const subProp of property.subPropertyOf) {
// if subProp is of type "link to" or "part of" we have to check the subproperty;
// otherwise we get the necessary property info from the objectType
if (subProp === Constants.HasLinkTo || subProp === Constants.IsPartOf) {
propType = (group.elements.find(i =>
i.guiEle === property.guiElement && i.subPropOf === subProp
));
} else {
if (property.objectType === Constants.IntValue && subProp === Constants.SeqNum) {
// if the property is of type number, but sub property of SeqNum,
// select the correct default prop params
propType = (group.elements.find(i =>
i.objectType === property.objectType && i.subPropOf === Constants.SeqNum
));
} else if (property.objectType === Constants.TextValue) {
// if the property is of type text value, we have to check the gui element
// to get the correct default prop params
propType = (group.elements.find(i =>
i.guiEle === property.guiElement && i.objectType === property.objectType
));
} else {
// in all other cases the gui-element resp. the subProp is not relevant
// because the object type is unique
propType = (group.elements.find(i =>
i.objectType === property.objectType
));
}
}
if (propType) {
break;
}
}
if (propType) {
break;
}
}
}
if (!propType) {
// property type could not be found in the list of default properties
// maybe it's not supported or it's a subproperty of another prop.
// e.g. if propDef.objectType === Constants.GeomValue || propDef.subPropertyOf[0] === Constants.HasRepresentation
// --> TODO: check if it's a subproperty of another one in this ontology
return of (DefaultProperties.unsupported);
}
// return of(propType);
return of (propType);
}
}