Skip to content

Commit

Permalink
Multiple parent URI tags + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ddvlanck committed Oct 27, 2023
1 parent 3af2a43 commit 60d15a5
Showing 1 changed file with 62 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ import { convertToCase, getTagValue, ignore } from '../utils/utils';
@injectable()
export class ElementConverterHandler extends ConverterHandler<EaElement> {
public async filterIgnoredObjects(
model: DataRegistry,
model: DataRegistry
): Promise<DataRegistry> {
model.elements = model.elements.filter(x => !ignore(x));
model.elements = model.elements.filter((x) => !ignore(x));

return model;
}

public async convert(
model: DataRegistry,
uriRegistry: UriRegistry,
store: QuadStore,
store: QuadStore
): Promise<QuadStore> {
// All elements will be processed and receive a URI, but only elements on the target diagram
// will be passed to the OutputHandler. This flow is necessary because element types could be
// in other packages and their URIs are needed to refer to in the output file.
model.elements
.filter(x => model.targetDiagram.elementIds.includes(x.id))
.forEach(object =>
store.addQuads(this.createQuads(object, uriRegistry, model)));
.filter((x) => model.targetDiagram.elementIds.includes(x.id))
.forEach((object) =>
store.addQuads(this.createQuads(object, uriRegistry, model))
);

return store;
}
Expand All @@ -43,18 +44,18 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {

public async assignUris(
model: DataRegistry,
uriRegistry: UriRegistry,
uriRegistry: UriRegistry
): Promise<UriRegistry> {
uriRegistry.elementIdUriMap = new Map<number, URL>();
uriRegistry.elementNameToElementMap = new Map<string, EaElement[]>();

model.elements.forEach(element => {
model.elements.forEach((element) => {
const externalUri = getTagValue(element, TagNames.ExternalUri, null);

if (externalUri) {
uriRegistry.elementIdUriMap.set(element.id, new URL(externalUri));
uriRegistry.elementNameToElementMap.set(element.name, [
...uriRegistry.elementNameToElementMap.get(element.name) || [],
...(uriRegistry.elementNameToElementMap.get(element.name) || []),
element,
]);

Expand All @@ -65,7 +66,7 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
const packageTagValue = getTagValue(
element,
TagNames.DefiningPackage,
null,
null
);

if (packageTagValue) {
Expand All @@ -74,24 +75,24 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {

if (referencedPackages && referencedPackages.length > 1) {
this.logger.warn(
`[ElementConverterHandler]: Multiple packages discovered through name tag "${packageTagValue}".`,
`[ElementConverterHandler]: Multiple packages discovered through name tag "${packageTagValue}".`
);
}

if (!referencedPackages) {
throw new Error(
`[ElementConverterHandler]: Package tag was defined, but unable to find the object for package ${packageTagValue}.`,
`[ElementConverterHandler]: Package tag was defined, but unable to find the object for package ${packageTagValue}.`
);
}

elementBaseUri = uriRegistry.packageIdUriMap.get(
referencedPackages[0].packageId,
referencedPackages[0].packageId
)!;
} else if (uriRegistry.packageIdUriMap.has(element.packageId)) {
elementBaseUri = uriRegistry.packageIdUriMap.get(element.packageId)!;
} else {
this.logger.warn(
`[ElementConverterHandler]: Unable to find base URI for element (${element.path}).`,
`[ElementConverterHandler]: Unable to find base URI for element (${element.path}).`
);
elementBaseUri = new URL(uriRegistry.fallbackBaseUri);
}
Expand All @@ -103,7 +104,7 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {

uriRegistry.elementIdUriMap.set(element.id, elementUri);
uriRegistry.elementNameToElementMap.set(element.name, [
...uriRegistry.elementNameToElementMap.get(element.name) || [],
...(uriRegistry.elementNameToElementMap.get(element.name) || []),
element,
]);
});
Expand All @@ -114,31 +115,31 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
public createQuads(
object: EaElement,
uriRegistry: UriRegistry,
model: DataRegistry,
model: DataRegistry
): RDF.Quad[] {
const quads: RDF.Quad[] = [];
const objectInternalId = this.df.namedNode(
`${this.baseUrnScheme}:${object.osloGuid}`,
`${this.baseUrnScheme}:${object.osloGuid}`
);
const objectUri = uriRegistry.elementIdUriMap.get(object.id);

if (!objectUri) {
throw new Error(
`[ElementConverterHandler]: Unable to find URI for element (${object.path}).`,
`[ElementConverterHandler]: Unable to find URI for element (${object.path}).`
);
}

const objectUriNamedNode = this.df.namedNode(objectUri.toString());

quads.push(
this.df.quad(objectInternalId, ns.oslo('assignedURI'), objectUriNamedNode),
this.df.quad(objectInternalId, ns.oslo('assignedURI'), objectUriNamedNode)
);

this.addDefinitions(
object,
objectInternalId,
this.df.defaultGraph(),
quads,
quads
);
this.addLabels(object, objectInternalId, this.df.defaultGraph(), quads);
this.addUsageNotes(object, objectInternalId, this.df.defaultGraph(), quads);
Expand All @@ -147,12 +148,12 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
// we need to compare it to the base URI of the package
// which holds the target diagram.
const packageBaseUri = uriRegistry.packageIdUriMap.get(
model.targetDiagram.packageId,
model.targetDiagram.packageId
);

if (!packageBaseUri) {
throw new Error(
`[ElementConverterHandler]: Unnable to find URI for the package in which the target diagram (${model.targetDiagram.name}) was placed.`,
`[ElementConverterHandler]: Unnable to find URI for the package in which the target diagram (${model.targetDiagram.name}) was placed.`
);
}

Expand All @@ -161,16 +162,16 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
objectInternalId,
packageBaseUri.toString(),
uriRegistry.elementIdUriMap,
quads,
quads
);

quads.push(
...this.getParentInformationQuads(
object,
objectInternalId,
uriRegistry,
model,
),
model
)
);

quads.push(...this.getCodelistQuads(object, objectInternalId));
Expand All @@ -179,19 +180,19 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
case ElementType.Enumeration:
case ElementType.Class:
quads.push(
this.df.quad(objectInternalId, ns.rdf('type'), ns.owl('Class')),
this.df.quad(objectInternalId, ns.rdf('type'), ns.owl('Class'))
);
break;

case ElementType.DataType:
quads.push(
this.df.quad(objectInternalId, ns.rdf('type'), ns.rdfs('Datatype')),
this.df.quad(objectInternalId, ns.rdf('type'), ns.rdfs('Datatype'))
);
break;

default:
throw new Error(
`[ElementConverterHandler]: Object type (${object.type}) is not supported.`,
`[ElementConverterHandler]: Object type (${object.type}) is not supported.`
);
}

Expand All @@ -202,48 +203,52 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
object: EaElement,
objectInternalId: RDF.NamedNode,
uriRegistry: UriRegistry,
model: DataRegistry,
model: DataRegistry
): RDF.Quad[] {
const quads: RDF.Quad[] = [];

// We search for a parent URI via the "parent" tag on the element
// TODO: it will be possible to set multiple parentURI tags
const parentURI = getTagValue(object, TagNames.ParentUri, null);
if (parentURI) {
quads.push(
this.df.quad(
objectInternalId,
ns.rdfs('subClassOf'),
this.df.namedNode(parentURI),
),
);
// ODO: use function getTagValue to get parentURI tags
const parentURITags = object.tags.filter(
(x) => x.tagName === TagNames.ParentUri
);
if (parentURITags.length > 0) {
parentURITags.forEach((x) => {
quads.push(
this.df.quad(
objectInternalId,
ns.rdfs('subClassOf'),
this.df.namedNode(x.tagValue)
)
);
});
}

// We also search for parent relationships via connectors
// Connectors array is used here, because NormalizedConnectors array doesn't have this type
const parentClassConnectors = model.connectors.filter(
x =>
(x) =>
x.type === ConnectorType.Generalization &&
x.sourceObjectId === object.id,
x.sourceObjectId === object.id
);

parentClassConnectors.forEach(parentClassConnector => {
parentClassConnectors.forEach((parentClassConnector) => {
const parentClassObject = model.elements.find(
x => x.id === parentClassConnector.destinationObjectId,
(x) => x.id === parentClassConnector.destinationObjectId
);

if (!parentClassObject) {
throw new Error(
`[ElementConverterHandler]: Unable to find parent object for class (${object.path}).`,
`[ElementConverterHandler]: Unable to find parent object for class (${object.path}).`
);
}

const parentInternalId = this.df.namedNode(
`${this.baseUrnScheme}:${parentClassObject.osloGuid}`,
`${this.baseUrnScheme}:${parentClassObject.osloGuid}`
);

quads.push(
this.df.quad(objectInternalId, ns.rdfs('subClassOf'), parentInternalId),
this.df.quad(objectInternalId, ns.rdfs('subClassOf'), parentInternalId)
);

// In case the parent object is not visible on the target diagram
Expand All @@ -253,11 +258,11 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
!model.targetDiagram.connectorsIds.includes(parentClassConnector.id)
) {
const parentAssignedUri = uriRegistry.elementIdUriMap.get(
parentClassObject.id,
parentClassObject.id
);
if (!parentAssignedUri) {
throw new Error(
`[ElementConverterHandler]: Unable to find the assigned URI for parent of class (${object.path}).`,
`[ElementConverterHandler]: Unable to find the assigned URI for parent of class (${object.path}).`
);
}

Expand All @@ -267,36 +272,36 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
parentClassObject,
parentInternalId,
referencedEntitiesGraph,
quads,
quads
);

this.addLabels(
parentClassObject,
parentInternalId,
referencedEntitiesGraph,
quads,
quads
);

this.addUsageNotes(
parentClassObject,
parentInternalId,
referencedEntitiesGraph,
quads,
quads
);

quads.push(
this.df.quad(
parentInternalId,
ns.rdf('type'),
ns.owl('Class'),
referencedEntitiesGraph,
referencedEntitiesGraph
),
this.df.quad(
parentInternalId,
ns.oslo('assignedURI'),
this.df.namedNode(parentAssignedUri.toString()),
referencedEntitiesGraph,
),
referencedEntitiesGraph
)
);
}
});
Expand All @@ -306,7 +311,7 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {

private getCodelistQuads(
object: EaElement,
objectInternalId: RDF.NamedNode,
objectInternalId: RDF.NamedNode
): RDF.Quad[] {
const quads: RDF.Quad[] = [];

Expand All @@ -318,8 +323,8 @@ export class ElementConverterHandler extends ConverterHandler<EaElement> {
this.df.quad(
objectInternalId,
ns.oslo('codelist'),
this.df.namedNode(codelistUri),
),
this.df.namedNode(codelistUri)
)
);
}

Expand Down

0 comments on commit 60d15a5

Please sign in to comment.