-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update RepresentationMetadata to store triples
- Loading branch information
Showing
37 changed files
with
575 additions
and
247 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,130 @@ | ||
/** | ||
* Contains metadata relevant to a representation. | ||
*/ | ||
import type { Quad } from 'rdf-js'; | ||
import { quad as createQuad, literal, namedNode } from '@rdfjs/data-model'; | ||
import { Store } from 'n3'; | ||
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js'; | ||
|
||
/** | ||
* Metadata corresponding to a {@link Representation}. | ||
* Stores the metadata triples and provides methods for easy access. | ||
*/ | ||
export interface RepresentationMetadata { | ||
export class RepresentationMetadata { | ||
private store: Store; | ||
private id: NamedNode | BlankNode; | ||
|
||
/** | ||
* All metadata triples of the resource. | ||
* @param identifier - Identifier of the resource relevant to this metadata. | ||
* A blank node will be generated if none is provided. | ||
* Strings will be converted to named nodes. @ignored | ||
* @param quads - Quads to fill the metadata with. @ignored | ||
* | ||
* `@ignored` tags are necessary for Components-Generator.js | ||
*/ | ||
raw: Quad[]; | ||
public constructor(identifier?: NamedNode | BlankNode | string, quads?: Quad[]) { | ||
this.store = new Store(quads); | ||
if (identifier) { | ||
if (typeof identifier === 'string') { | ||
this.id = namedNode(identifier); | ||
} else { | ||
this.id = identifier; | ||
} | ||
} else { | ||
this.id = this.store.createBlankNode(); | ||
} | ||
} | ||
|
||
/** | ||
* Optional metadata profiles. | ||
* @returns All metadata quads. | ||
*/ | ||
profiles?: string[]; | ||
public quads(): Quad[] { | ||
return this.store.getQuads(null, null, null, null); | ||
} | ||
|
||
/** | ||
* Optional size of the representation. | ||
* Identifier of the resource this metadata is relevant to. | ||
* Will update all relevant triples if this value gets changed. | ||
*/ | ||
byteSize?: number; | ||
public get identifier(): NamedNode | BlankNode { | ||
return this.id; | ||
} | ||
|
||
public set identifier(id: NamedNode | BlankNode) { | ||
const quads = this.quads().map((quad): Quad => { | ||
if (quad.subject.equals(this.id)) { | ||
return createQuad(id, quad.predicate, quad.object, quad.graph); | ||
} | ||
if (quad.object.equals(this.id)) { | ||
return createQuad(quad.subject, quad.predicate, id, quad.graph); | ||
} | ||
return quad; | ||
}); | ||
this.store = new Store(quads); | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Optional content type of the representation. | ||
* @param quads - Quads to add to the metadata. | ||
*/ | ||
contentType?: string; | ||
public addQuads(quads: Quad[]): void { | ||
this.store.addQuads(quads); | ||
} | ||
|
||
/** | ||
* Optional encoding of the representation. | ||
* @param quads - Quads to remove from the metadata. | ||
*/ | ||
encoding?: string; | ||
public removeQuads(quads: Quad[]): void { | ||
this.store.removeQuads(quads); | ||
} | ||
|
||
/** | ||
* Optional language of the representation. | ||
* Adds a value linked to the identifier. Strings get converted to literals. | ||
* @param predicate - Predicate linking identifier to value. | ||
* @param object - Value to add. | ||
*/ | ||
language?: string; | ||
public add(predicate: NamedNode, object: NamedNode | Literal | string): void { | ||
this.store.addQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object); | ||
} | ||
|
||
/** | ||
* Optional timestamp of the representation. | ||
* Removes the given value from the metadata. Strings get converted to literals. | ||
* @param predicate - Predicate linking identifier to value. | ||
* @param object - Value to remove. | ||
*/ | ||
dateTime?: Date; | ||
public remove(predicate: NamedNode, object: NamedNode | Literal | string): void { | ||
this.store.removeQuad(this.id, predicate, typeof object === 'string' ? literal(object) : object); | ||
} | ||
|
||
/** | ||
* Optional link relationships of the representation. | ||
* Removes all values linked through the given predicate. | ||
* @param predicate - Predicate to remove. | ||
*/ | ||
linkRel?: { [id: string]: Set<string> }; | ||
public removeAll(predicate: NamedNode): void { | ||
this.removeQuads(this.store.getQuads(this.id, predicate, null, null)); | ||
} | ||
|
||
/** | ||
* @param predicate - Predicate to get the value for. | ||
* | ||
* @throws Error | ||
* If there are multiple matching values. | ||
* | ||
* @returns The corresponding value. Undefined if there is no match | ||
*/ | ||
public get(predicate: NamedNode): Term | undefined { | ||
const quads = this.store.getQuads(this.id, predicate, null, null); | ||
if (quads.length === 0) { | ||
return; | ||
} | ||
if (quads.length > 1) { | ||
throw new Error(`Multiple results for ${predicate.value}`); | ||
} | ||
return quads[0].object; | ||
} | ||
|
||
/** | ||
* Optional slug of the representation. | ||
* Used to suggest the URI for the resource created. | ||
* Sets the value for the given predicate, removing all other instances. | ||
* @param predicate - Predicate linking to the value. | ||
* @param object - Value to set. | ||
*/ | ||
slug?: string; | ||
public set(predicate: NamedNode, object: NamedNode | Literal | string): void { | ||
this.removeAll(predicate); | ||
this.add(predicate, object); | ||
} | ||
} |
Oops, something went wrong.