|
|
@ -47,20 +47,41 @@ export function toCamelCase(text: string) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isJsonLdContextBuilder( |
|
|
|
|
|
|
|
item: ExpandedTermDefinition | JsonLdContextBuilder, |
|
|
|
|
|
|
|
): item is JsonLdContextBuilder { |
|
|
|
|
|
|
|
return !!(typeof item === "object" && item instanceof JsonLdContextBuilder); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* JsonLd Context Builder |
|
|
|
* JsonLd Context Builder |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export class JsonLdContextBuilder { |
|
|
|
export class JsonLdContextBuilder { |
|
|
|
private iriAnnotations: Record<string, Annotation[]> = {}; |
|
|
|
protected iriAnnotations: Record<string, Annotation[]> = {}; |
|
|
|
private iriTypes: Record<string, ExpandedTermDefinition> = {}; |
|
|
|
protected iriTypes: Record< |
|
|
|
private generatedNames: Record<string, string> | undefined; |
|
|
|
string, |
|
|
|
|
|
|
|
ExpandedTermDefinition | JsonLdContextBuilder |
|
|
|
|
|
|
|
> = {}; |
|
|
|
|
|
|
|
protected generatedNames: Record<string, string> | undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getRelevantBuilder(rdfType?: string): JsonLdContextBuilder { |
|
|
|
|
|
|
|
if (!rdfType) return this; |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
!this.iriTypes[rdfType] || |
|
|
|
|
|
|
|
!isJsonLdContextBuilder(this.iriTypes[rdfType]) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
this.iriTypes[rdfType] = new JsonLdContextBuilder(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return this.iriTypes[rdfType] as JsonLdContextBuilder; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addSubject(iri: string, annotations?: Annotation[]) { |
|
|
|
addSubject(iri: string, rdfType?: string, annotations?: Annotation[]) { |
|
|
|
if (!this.iriAnnotations[iri]) { |
|
|
|
const relevantBuilder = this.getRelevantBuilder(rdfType); |
|
|
|
this.iriAnnotations[iri] = []; |
|
|
|
if (!relevantBuilder.iriAnnotations[iri]) { |
|
|
|
|
|
|
|
relevantBuilder.iriAnnotations[iri] = []; |
|
|
|
} |
|
|
|
} |
|
|
|
if (annotations && annotations.length > 0) { |
|
|
|
if (annotations && annotations.length > 0) { |
|
|
|
this.iriAnnotations[iri].push(...annotations); |
|
|
|
relevantBuilder.iriAnnotations[iri].push(...annotations); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -68,22 +89,24 @@ export class JsonLdContextBuilder { |
|
|
|
iri: string, |
|
|
|
iri: string, |
|
|
|
expandedTermDefinition: ExpandedTermDefinition, |
|
|
|
expandedTermDefinition: ExpandedTermDefinition, |
|
|
|
isContainer: boolean, |
|
|
|
isContainer: boolean, |
|
|
|
|
|
|
|
rdfType?: string, |
|
|
|
annotations?: Annotation[], |
|
|
|
annotations?: Annotation[], |
|
|
|
) { |
|
|
|
) { |
|
|
|
this.addSubject(iri, annotations); |
|
|
|
const relevantBuilder = this.getRelevantBuilder(rdfType); |
|
|
|
if (!this.iriTypes[iri]) { |
|
|
|
relevantBuilder.addSubject(iri, undefined, annotations); |
|
|
|
this.iriTypes[iri] = expandedTermDefinition; |
|
|
|
if (!relevantBuilder.iriTypes[iri]) { |
|
|
|
|
|
|
|
relevantBuilder.iriTypes[iri] = expandedTermDefinition; |
|
|
|
if (isContainer) { |
|
|
|
if (isContainer) { |
|
|
|
this.iriTypes[iri]["@container"] = "@set"; |
|
|
|
relevantBuilder.iriTypes[iri]["@isContainer"] = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
const curDef = this.iriTypes[iri]; |
|
|
|
const curDef = relevantBuilder.iriTypes[iri]; |
|
|
|
const newDef = expandedTermDefinition; |
|
|
|
const newDef = expandedTermDefinition; |
|
|
|
// TODO: if you reuse the same predicate with a different cardinality,
|
|
|
|
// TODO: if you reuse the same predicate with a different cardinality,
|
|
|
|
// it will overwrite the past cardinality. Perhapse we might want to
|
|
|
|
// it will overwrite the past cardinality. Perhapse we might want to
|
|
|
|
// split contexts in the various shapes.
|
|
|
|
// split contexts in the various shapes.
|
|
|
|
if (isContainer) { |
|
|
|
if (isContainer) { |
|
|
|
curDef["@container"] = "@set"; |
|
|
|
curDef["@isContainer"] = true; |
|
|
|
} |
|
|
|
} |
|
|
|
// If the old and new versions both have types
|
|
|
|
// If the old and new versions both have types
|
|
|
|
if (curDef["@type"] && newDef["@type"]) { |
|
|
|
if (curDef["@type"] && newDef["@type"]) { |
|
|
@ -165,13 +188,25 @@ export class JsonLdContextBuilder { |
|
|
|
const namesMap = this.generateNames(); |
|
|
|
const namesMap = this.generateNames(); |
|
|
|
Object.entries(namesMap).forEach(([iri, name]) => { |
|
|
|
Object.entries(namesMap).forEach(([iri, name]) => { |
|
|
|
if (this.iriTypes[iri]) { |
|
|
|
if (this.iriTypes[iri]) { |
|
|
|
contextDefnition[name] = { |
|
|
|
let subContext: ExpandedTermDefinition = { |
|
|
|
"@id": |
|
|
|
"@id": |
|
|
|
iri === "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" |
|
|
|
iri === "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" |
|
|
|
? "@type" |
|
|
|
? "@type" |
|
|
|
: iri, |
|
|
|
: iri, |
|
|
|
...this.iriTypes[iri], |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isJsonLdContextBuilder(this.iriTypes[iri])) { |
|
|
|
|
|
|
|
subContext["@context"] = ( |
|
|
|
|
|
|
|
this.iriTypes[iri] as JsonLdContextBuilder |
|
|
|
|
|
|
|
).generateJsonldContext(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
subContext = { |
|
|
|
|
|
|
|
...subContext, |
|
|
|
|
|
|
|
...this.iriTypes[iri], |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contextDefnition[name] = subContext; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
contextDefnition[name] = iri; |
|
|
|
contextDefnition[name] = iri; |
|
|
|
} |
|
|
|
} |
|
|
|