Skip to content

Commit cff6529

Browse files
authored
Merge pull request #22 from bci-oss/feature/support-entity-instances-of-type-rdf-lang
Add support for entity instances of type rdf lang string
2 parents a70136d + 1283b25 commit cff6529

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

src/instantiator/characteristic/enumeration-characteristic-instantiator.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ export class EnumerationCharacteristicInstantiator extends CharacteristicInstant
113113
// create the related instance and attach the metamodel element to it
114114
const entityInstance = new DefaultEntityInstance(quad.object.value.split('#')[1], entity, descriptions);
115115
entityInstanceQuads.forEach(quad => {
116-
entityInstance[quad.predicate.value.split('#')[1]] = quad.object.value;
116+
const predicateKey = this.getPredicateKey(quad);
117+
if (Util.isBlankNode(quad.object)) {
118+
entityInstance[predicateKey] =
119+
this.solveBlankNodeValues([...this.metaModelElementInstantiator.rdfModel.resolveBlankNodes(quad.object.value)]);
120+
}
121+
else {
122+
entityInstance[predicateKey] = quad.object.value;
123+
}
117124
});
118125

119126
return entityInstance;
@@ -124,4 +131,21 @@ export class EnumerationCharacteristicInstantiator extends CharacteristicInstant
124131
shouldProcess(nameNode: NamedNode): boolean {
125132
return this.metaModelElementInstantiator.sammC.EnumerationCharacteristic().equals(nameNode);
126133
}
134+
135+
solveBlankNodeValues(resolvedBlankNodes: Array<Quad>) {
136+
return resolvedBlankNodes.length > 0
137+
? resolvedBlankNodes.map(item => this.createLanguageObject(item))
138+
: '';
139+
}
140+
141+
private getPredicateKey(quad: Quad): string {
142+
return quad.predicate.value.split('#')[1];
143+
}
144+
145+
private createLanguageObject(quad: Quad): any {
146+
return (quad.object as any).language
147+
? {value: quad.object.value, language: (quad.object as any).language}
148+
: quad.object.value;
149+
}
150+
127151
}

test/characteristic.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
collectionCharacteristicClassString,
3232
eitherCharacteristicClass,
3333
enumerationCharacteristicClassEntity,
34+
enumerationCharacteristicWithEntityInstanceClass
3435
} from './models/characteristics';
3536
import DoneCallback = jest.DoneCallback;
3637

@@ -168,6 +169,42 @@ describe('Characteristics tests', (): void => {
168169
});
169170
});
170171

172+
describe('Enumeration class with Entity Instance', (): void => {
173+
beforeAll((done: DoneCallback): void => {
174+
loader = new AspectModelLoader();
175+
subscription = loader.loadSelfContainedModel(enumerationCharacteristicWithEntityInstanceClass).subscribe((_aspect: TestAspect): void => {
176+
aspect = _aspect;
177+
testProperty = aspect.properties[0];
178+
done();
179+
});
180+
});
181+
182+
it('should have enumeration type', (): void => {
183+
expect(testProperty.characteristic instanceof DefaultEnumeration).toBe(true);
184+
});
185+
186+
it('should have correct model urn', (): void => {
187+
expect((testProperty.characteristic.dataType as DefaultEntity).properties[1].aspectModelUrn)
188+
.toEqual('urn:samm:org.eclipse.esmf.test:1.0.0#resultTypeDescription');
189+
});
190+
191+
it('should have two entries', (): void => {
192+
const enumeration = testProperty.characteristic as DefaultEnumeration;
193+
expect(enumeration.values.length).toBe(2);
194+
});
195+
196+
it('Result type values should be instanceof DefaultEntityInstance class', (): void => {
197+
const enumeration = testProperty.characteristic as DefaultEnumeration;
198+
expect(enumeration.values[0] instanceof DefaultEntityInstance).toBe(true);
199+
});
200+
201+
afterAll((): void => {
202+
if (subscription) {
203+
subscription.unsubscribe();
204+
}
205+
});
206+
});
207+
171208
describe('Either class with list and simple characteristic', (): void => {
172209
beforeAll((done: DoneCallback): void => {
173210
loader = new AspectModelLoader();

test/models/characteristics.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,71 @@ export const enumerationCharacteristicClassEntity = `
130130
:resultStateAttributeValue "ok"^^xsd:string .
131131
`;
132132

133+
export const enumerationCharacteristicWithEntityInstanceClass = `
134+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
135+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
136+
@prefix samm-e: <urn:samm:org.eclipse.esmf.samm:entity:2.1.0#> .
137+
@prefix unit: <urn:samm:org.eclipse.esmf.samm:unit:2.1.0#> .
138+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
139+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
140+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
141+
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
142+
143+
:TestAspect a samm:Aspect ;
144+
samm:properties ( :result :metadata ) ;
145+
samm:operations ( ) ;
146+
samm:events ( ) .
147+
148+
:result a samm:Property ;
149+
samm:preferredName "result"@en ;
150+
samm:characteristic :ResultTypes .
151+
152+
:metadata a samm:Property ;
153+
samm:characteristic :MetadataCharacteristic .
154+
155+
:ResultTypes a samm-c:Enumeration ;
156+
samm:preferredName "result types"@en ;
157+
samm:description "The collection of known result types"@en ;
158+
samm:dataType :ResultType ;
159+
samm-c:values ( :ResultType1 :ResultType2 ) .
160+
161+
:MetadataCharacteristic a samm:Characteristic ;
162+
samm:dataType xsd:string .
163+
164+
:ResultType a samm:Entity ;
165+
samm:preferredName "Result Type"@en ;
166+
samm:properties ( :resultTypeNumber [ samm:property :resultTypeDescription; samm:notInPayload true ] ) .
167+
168+
:ResultType1 a :ResultType ;
169+
:resultTypeDescription ( "ein"@de "one"@en ) ;
170+
:resultTypeNumber "1"^^xsd:positiveInteger .
171+
172+
:ResultType2 a :ResultType ;
173+
:resultTypeDescription ( "zwei"@de "two"@en ) ;
174+
:resultTypeNumber "2"^^xsd:positiveInteger .
175+
176+
:resultTypeNumber a samm:Property ;
177+
samm:preferredName "Result type number"@en ;
178+
samm:description "result type number"@en ;
179+
samm:characteristic :ResultNumbers .
180+
181+
:resultTypeDescription a samm:Property ;
182+
samm:preferredName "Result Type Description"@en ;
183+
samm:description "Description of result type"@en ;
184+
samm:characteristic :ResultTypeDescription .
185+
186+
:ResultNumbers a samm-c:Enumeration ;
187+
samm:preferredName "Result Numbers"@en ;
188+
samm:description "result numbers"@en ;
189+
samm:dataType xsd:positiveInteger ;
190+
samm-c:values ( "1"^^xsd:positiveInteger "2"^^xsd:positiveInteger "21"^^xsd:positiveInteger "30"^^xsd:positiveInteger "35"^^xsd:positiveInteger "40"^^xsd:positiveInteger "41"^^xsd:positiveInteger "42"^^xsd:positiveInteger "43"^^xsd:positiveInteger "50"^^xsd:positiveInteger "60"^^xsd:positiveInteger "61"^^xsd:positiveInteger "70"^^xsd:positiveInteger "75"^^xsd:positiveInteger ) .
191+
192+
:ResultTypeDescription a samm-c:Collection ;
193+
samm:preferredName "Result Type Description"@en ;
194+
samm:description "Descriptions of result types in English and German"@en ;
195+
samm:dataType rdf:langString .
196+
`;
197+
133198
export const eitherCharacteristicClass = `
134199
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
135200
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#>.

0 commit comments

Comments
 (0)