Skip to content

Commit

Permalink
Create store util functions + update generators
Browse files Browse the repository at this point in the history
  • Loading branch information
ddvlanck committed Feb 14, 2024
1 parent ab85a49 commit 8baff13
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 50 deletions.
1 change: 1 addition & 0 deletions packages/oslo-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from '@oslo-core/logging/LogUtil';
export * from '@oslo-core/logging/LoggerFactory';
export * from '@oslo-core/logging/VoidLoggerFactory';
export * from '@oslo-core/logging/WinstonLoggerFactory';
export * from '@oslo-core/utils/storeUtils';
1 change: 0 additions & 1 deletion packages/oslo-core/lib/utils/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DataFactory } from 'rdf-data-factory';

const factory = new DataFactory();

// TODO: remove example.org from prefixes
enum Prefixes {
adms = 'http://www.w3.org/ns/adms#',
dcat = 'http://www.w3.org/ns/dcat#',
Expand Down
116 changes: 116 additions & 0 deletions packages/oslo-core/lib/utils/storeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type * as RDF from '@rdfjs/types';
import type { QuadStore } from '@oslo-core/store/QuadStore';
import { ns } from '@oslo-core/utils/namespaces';

export function getApplicationProfileLabel(
subject: RDF.Term,
store: QuadStore,
language: string | null = null,
): RDF.Literal | undefined {
const labels = store.getLabels(subject);

if (labels.some(x => x.predicate.equals(ns.oslo('apLabel')))) {
return <RDF.Literal>labels
.find(x => x.predicate.equals(ns.oslo('apLabel')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object
}

if (labels.some(x => x.predicate.equals(ns.oslo('vocLabel')))) {
return <RDF.Literal>
labels.find(x => x.predicate.equals(ns.oslo('vocLabel')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}
return <RDF.Literal | undefined>labels.find(x => x.predicate.equals(ns.oslo('diagramLabel')))?.object;
}

export function getVocabularyLabel(
subject: RDF.Term,
store: QuadStore,
language: string | null = null,
): RDF.Literal | undefined {
const labels = store.getLabels(subject);

if (labels.some(x => x.predicate.equals(ns.oslo('vocLabel')))) {
return <RDF.Literal>labels
.find(x => x.predicate.equals(ns.oslo('vocLabel')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}
return <RDF.Literal | undefined>labels.find(x => x.predicate.equals(ns.oslo('diagramLabel')))?.object;
}

export function getApplicationProfileDefinition(
subject: RDF.Term,
store: QuadStore,
language: string | null = null,
): RDF.Literal | undefined {
const definitions = store.getDefinitions(subject);

if (definitions.some(x => x.predicate.equals(ns.oslo('apDefinition')))) {
return <RDF.Literal>definitions
.find(x => x.predicate.equals(ns.oslo('apDefinition')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}

if (definitions.some(x => x.predicate.equals(ns.oslo('vocDefinition')))) {
return <RDF.Literal>definitions
.find(x => x.predicate.equals(ns.oslo('vocDefinition')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}

return undefined;
}

export function getVocabularyDefinition(
subject: RDF.Term,
store: QuadStore,
language: string | null = null,
): RDF.Literal | undefined {
const definitions = store.getDefinitions(subject);

if (definitions.some(x => x.predicate.equals(ns.oslo('vocDefinition')))) {
return <RDF.Literal>definitions
.find(x => x.predicate.equals(ns.oslo('vocDefinition')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}

return undefined;
}

export function getApplicationProfileUsageNote(
subject: RDF.Term,
store: QuadStore,
language: string | null = null,
): RDF.Literal | undefined {
const usageNotes = store.getUsageNotes(subject);

if (usageNotes.some(x => x.predicate.equals(ns.oslo('apUsageNote')))) {
return <RDF.Literal>usageNotes
.find(x => x.predicate.equals(ns.oslo('apUsageNote')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}

if (usageNotes.some(x => x.predicate.equals(ns.oslo('vocUsageNote')))) {
return <RDF.Literal>usageNotes
.find(x => x.predicate.equals(ns.oslo('vocUsageNote')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}

return undefined;
}

export function getVocabularyUsageNote(
subject: RDF.Term,
store: QuadStore,
language: string | null = null,
): RDF.Literal | undefined {
const usageNotes = store.getUsageNotes(subject);

if (usageNotes.some(x => x.predicate.equals(ns.oslo('vocUsageNote')))) {
return <RDF.Literal>usageNotes
.find(x => x.predicate.equals(ns.oslo('vocUsageNote')) &&
(<RDF.Literal>x.object).language === (language || ''))?.object;
}

return undefined;
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
Logger,
ns,
ServiceIdentifier,
QuadStore
QuadStore,
getApplicationProfileLabel
} from '@oslo-flanders/core';

import type * as RDF from '@rdfjs/types';
Expand Down Expand Up @@ -79,8 +80,7 @@ export class JsonldContextGenerationService implements IService {
const labelUriMap: Map<string, RDF.NamedNode[]> = new Map();

uris.forEach(uri => {
const label = this.store.getApLabel(uri, this.configuration.language) || this.store.getApLabel(uri) ||
this.store.getVocLabel(uri, this.configuration.language) || this.store.getVocLabel(uri) || this.store.getDiagramLabel(uri);
const label = getApplicationProfileLabel(uri, this.store, this.configuration.language);

if (!label) {
return;
Expand Down Expand Up @@ -109,8 +109,7 @@ export class JsonldContextGenerationService implements IService {
const duplicates = this.identifyDuplicateLabels(classSubjects);

classSubjects.forEach(subject => {
const label = this.store.getApLabel(subject, this.configuration.language) || this.store.getApLabel(subject) ||
this.store.getVocLabel(subject, this.configuration.language) || this.store.getVocLabel(subject) || this.store.getDiagramLabel(subject);
const label = getApplicationProfileLabel(subject, this.store, this.configuration.language);

if (!label) {
this.logger.warn(`No label found for class ${subject.value} in language ${this.configuration.language}.`);
Expand Down Expand Up @@ -152,8 +151,7 @@ export class JsonldContextGenerationService implements IService {
return;
}

const label = this.store.getApLabel(subject, this.configuration.language) || this.store.getApLabel(subject) ||
this.store.getVocLabel(subject, this.configuration.language) || this.store.getVocLabel(subject) || this.store.getDiagramLabel(subject);
const label = getApplicationProfileLabel(subject, this.store, this.configuration.language);

if (!label) {
this.logger.error(`No label found for attribute ${subject.value} in language "${this.configuration.language}" or without language tag.`);
Expand Down Expand Up @@ -183,8 +181,7 @@ export class JsonldContextGenerationService implements IService {
return;
}

const domainLabel = this.store.getApLabel(domain, this.configuration.language) || this.store.getApLabel(domain) ||
this.store.getVocLabel(domain, this.configuration.language) || this.store.getVocLabel(domain) || this.store.getDiagramLabel(domain);
const domainLabel = getApplicationProfileLabel(domain, this.store, this.configuration.language);

if (!domainLabel) {
this.logger.error(`No label found for domain ${domain.value} of attribute ${subject.value}.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ns,
Logger,
ServiceIdentifier,
getVocabularyDefinition,
getVocabularyUsageNote,
} from '@oslo-flanders/core';

import type * as RDF from '@rdfjs/types';
Expand Down Expand Up @@ -102,7 +104,7 @@ export class RdfVocabularyGenerationService implements IService {
),
);

const definition = this.store.getVocDefinition(subject, this.configuration.language) || this.store.getVocDefinition(subject);
const definition = getVocabularyDefinition(subject, this.store, this.configuration.language);
if (!definition) {
this.logger.error(`Unable to find the definition for class ${subject.value}.`);
} else {
Expand Down Expand Up @@ -132,7 +134,7 @@ export class RdfVocabularyGenerationService implements IService {
);
});

const usageNote = this.store.getVocUsageNote(subject, this.configuration.language) || this.store.getVocUsageNote(subject);
const usageNote = getVocabularyUsageNote(subject, this.store, this.configuration.language);
if (usageNote) {
quads.push(
this.dataFactory.quad(
Expand Down Expand Up @@ -227,7 +229,7 @@ export class RdfVocabularyGenerationService implements IService {
);
}

const definition = this.store.getVocDefinition(id, this.configuration.language) || this.store.getVocDefinition(id);
const definition = getVocabularyDefinition(id, this.store, this.configuration.language)
if (!definition) {
this.logger.error(`Unable to find the definition for property ${id.value}.`);
} else {
Expand All @@ -240,7 +242,7 @@ export class RdfVocabularyGenerationService implements IService {
);
}

const usageNote = this.store.getVocUsageNote(id, this.configuration.language) || this.store.getVocDefinition(id);
const usageNote = getVocabularyUsageNote(id, this.store, this.configuration.language);
if (usageNote) {
quads.push(
this.dataFactory.quad(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { writeFile, mkdir } from 'fs/promises';
import { resolve, dirname } from 'path';
import type { IService } from '@oslo-flanders/core';
import { ns, Logger, QuadStore, ServiceIdentifier } from '@oslo-flanders/core';

import { ns, Logger, QuadStore, ServiceIdentifier, getApplicationProfileLabel, getVocabularyLabel, getApplicationProfileDefinition, getVocabularyDefinition, getApplicationProfileUsageNote, getVocabularyUsageNote } from '@oslo-flanders/core';
import type * as RDF from '@rdfjs/types';
import { inject, injectable } from 'inversify';
import * as nj from 'nunjucks';
Expand Down Expand Up @@ -104,23 +103,14 @@ export class HtmlRespecGenerationService implements IService {
const assignedUri = this.store.getAssignedUri(subjectId);
const parents = this.store.getParentsOfClass(subjectId);

let label;
let definition;
let usageNote;
if (this.configuration.specificationType === SpecificationType.ApplicationProfile) {
label = this.store.getApLabel(subjectId, this.configuration.language) || this.store.getApLabel(subjectId) ||
this.store.getVocLabel(subjectId, this.configuration.language) || this.store.getVocLabel(subjectId) || this.store.getDiagramLabel(subjectId);

definition = this.store.getApDefinition(subjectId, this.configuration.language) || this.store.getApDefinition(subjectId) ||
this.store.getVocDefinition(subjectId, this.configuration.language) || this.store.getVocDefinition(subjectId);

usageNote = this.store.getApUsageNote(subjectId, this.configuration.language) || this.store.getApUsageNote(subjectId) ||
this.store.getVocUsageNote(subjectId, this.configuration.language) || this.store.getVocUsageNote(subjectId);
} else {
label = this.store.getVocLabel(subjectId, this.configuration.language) || this.store.getVocLabel(subjectId) || this.store.getDiagramLabel(subjectId);
definition = this.store.getVocDefinition(subjectId, this.configuration.language) || this.store.getVocDefinition(subjectId)
usageNote = this.store.getVocUsageNote(subjectId, this.configuration.language) || this.store.getVocUsageNote(subjectId)
}
const label = this.configuration.specificationType === SpecificationType.ApplicationProfile ?
getApplicationProfileLabel(subjectId, this.store, this.configuration.language) : getVocabularyLabel(subjectId, this.store, this.configuration.language);

const definition = this.configuration.specificationType === SpecificationType.ApplicationProfile ?
getApplicationProfileDefinition(subjectId, this.store, this.configuration.language) : getVocabularyDefinition(subjectId, this.store, this.configuration.language);

const usageNote = this.configuration.specificationType === SpecificationType.ApplicationProfile ?
getApplicationProfileUsageNote(subjectId, this.store, this.configuration.language) : getVocabularyUsageNote(subjectId, this.store, this.configuration.language)

const parentAssignedUris: string[] = [];
parents.forEach(parent => {
Expand Down Expand Up @@ -155,23 +145,14 @@ export class HtmlRespecGenerationService implements IService {
const minCount = this.store.getMinCardinality(subjectId);
const maxCount = this.store.getMaxCardinality(subjectId);

let label;
let definition;
let usageNote;
if (this.configuration.specificationType === SpecificationType.ApplicationProfile) {
label = this.store.getApLabel(subjectId, this.configuration.language) || this.store.getApLabel(subjectId) ||
this.store.getVocLabel(subjectId, this.configuration.language) || this.store.getVocLabel(subjectId) || this.store.getDiagramLabel(subjectId);

definition = this.store.getApDefinition(subjectId, this.configuration.language) || this.store.getApDefinition(subjectId) ||
this.store.getVocDefinition(subjectId, this.configuration.language) || this.store.getVocDefinition(subjectId);

usageNote = this.store.getApUsageNote(subjectId, this.configuration.language) || this.store.getApUsageNote(subjectId) ||
this.store.getVocUsageNote(subjectId, this.configuration.language) || this.store.getVocUsageNote(subjectId);
} else {
label = this.store.getVocLabel(subjectId, this.configuration.language) || this.store.getVocLabel(subjectId) || this.store.getDiagramLabel(subjectId);
definition = this.store.getVocDefinition(subjectId, this.configuration.language) || this.store.getVocDefinition(subjectId)
usageNote = this.store.getVocUsageNote(subjectId, this.configuration.language) || this.store.getVocUsageNote(subjectId)
}
const label = this.configuration.specificationType === SpecificationType.ApplicationProfile ?
getApplicationProfileLabel(subjectId, this.store, this.configuration.language) : getVocabularyLabel(subjectId, this.store, this.configuration.language);

const definition = this.configuration.specificationType === SpecificationType.ApplicationProfile ?
getApplicationProfileDefinition(subjectId, this.store, this.configuration.language) : getVocabularyDefinition(subjectId, this.store, this.configuration.language);

const usageNote = this.configuration.specificationType === SpecificationType.ApplicationProfile ?
getApplicationProfileUsageNote(subjectId, this.store, this.configuration.language) : getVocabularyUsageNote(subjectId, this.store, this.configuration.language)

const domain = this.store.getDomain(subjectId);
if (!domain) {
Expand Down

0 comments on commit 8baff13

Please sign in to comment.