Skip to content

Commit

Permalink
rdflib-utils: add addInstanceToTypeIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo-v committed Jun 14, 2024
1 parent 3a1e9c9 commit 6c743c3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions utils/rdflib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- [ModuleSupport](https://solid-contrib.github.io/data-modules/rdflib-utils/classes/index.ModuleSupport.html)
- [TypeIndexQuery](https://solid-contrib.github.io/data-modules/rdflib-utils/classes/index.TypeIndexQuery.html)
- [addInstanceToTypeIndex](https://solid-contrib.github.io/data-modules/rdflib-utils/functions/index.addInstanceToTypeIndex.html)


## 0.1.1
Expand Down
1 change: 1 addition & 0 deletions utils/rdflib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./web-operations/index.js";
export * from "./identifier/index.js";
export * from "./queries/index.js";
export * from "./module/index.js";
export * from "./update-operations/index.js";

export interface ModuleConfig {
store: IndexedFormula;
Expand Down
8 changes: 8 additions & 0 deletions utils/rdflib/src/queries/TypeIndexQuery.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { IndexedFormula, isNamedNode, NamedNode } from "rdflib";
import { solid } from "../namespaces/index.js";

/**
* Used query data from a type index document
*/
export class TypeIndexQuery {
constructor(
private store: IndexedFormula,
public typeIndexDoc: NamedNode,
) {}

/**
* Look up the instances in the type registration for the given RDF class
* @param type - The RDF class to look up
* @returns A list of the URIs of the found instances
*/
queryInstancesForClass(type: NamedNode) {
const registrations = this.store.each(
null,
Expand Down
51 changes: 51 additions & 0 deletions utils/rdflib/src/update-operations/addInstanceToTypeIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { addInstanceToTypeIndex } from "./addInstanceToTypeIndex";
import { st, sym } from "rdflib";
import { rdf, solid } from "../namespaces";
import { when } from "jest-when";
import { generateId } from "../identifier/index.js";

jest.mock("../identifier/index.js");

describe("addInstanceToTypeIndex", () => {
it("creates a new type registration for type", () => {
when(generateId).mockReturnValue("74807edb");
const operation = addInstanceToTypeIndex(
sym("https://alice.test/settings/privateTypeIndex.ttl"),
"https://alice.test/contacts/1#this",
sym("http://www.w3.org/2006/vcard/ns#AddressBook"),
);
expect(operation.insertions).toContainEqual(
st(
sym("https://alice.test/settings/privateTypeIndex.ttl#74807edb"),
rdf("type"),
solid("TypeRegistration"),
sym("https://alice.test/settings/privateTypeIndex.ttl"),
),
);
expect(operation.insertions).toContainEqual(
st(
sym("https://alice.test/settings/privateTypeIndex.ttl#74807edb"),
solid("forClass"),
sym("http://www.w3.org/2006/vcard/ns#AddressBook"),
sym("https://alice.test/settings/privateTypeIndex.ttl"),
),
);
});

it("links the instance to the registration", () => {
when(generateId).mockReturnValue("74807edb");
const operation = addInstanceToTypeIndex(
sym("https://alice.test/settings/privateTypeIndex.ttl"),
"https://alice.test/contacts/1#this",
sym("http://www.w3.org/2006/vcard/ns#AddressBook"),
);
expect(operation.insertions).toContainEqual(
st(
sym("https://alice.test/settings/privateTypeIndex.ttl#74807edb"),
solid("instance"),
sym("https://alice.test/contacts/1#this"),
sym("https://alice.test/settings/privateTypeIndex.ttl"),
),
);
});
});
32 changes: 32 additions & 0 deletions utils/rdflib/src/update-operations/addInstanceToTypeIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NamedNode, st, sym } from "rdflib";
import { UpdateOperation } from "../web-operations/index.js";
import { generateId } from "../identifier/index.js";
import { rdf, solid } from "../namespaces/index.js";

/**
* Create a new type registration for the given type that links to the given resource instance
* @param typeIndexDoc - The type index document (private or public)
* @param instanceUri - The URI of the instance to add
* @param type - The RDF class to register the instance for
*/
export function addInstanceToTypeIndex(
typeIndexDoc: NamedNode,
instanceUri: string,
type: NamedNode,
): UpdateOperation {
const registrationNode = sym(`${typeIndexDoc.value}#${generateId()}`);
return {
deletions: [],
filesToCreate: [],
insertions: [
st(
registrationNode,
rdf("type"),
solid("TypeRegistration"),
typeIndexDoc,
),
st(registrationNode, solid("forClass"), type, typeIndexDoc),
st(registrationNode, solid("instance"), sym(instanceUri), typeIndexDoc),
],
};
}
1 change: 1 addition & 0 deletions utils/rdflib/src/update-operations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { addInstanceToTypeIndex } from "./addInstanceToTypeIndex.js";

0 comments on commit 6c743c3

Please sign in to comment.