Skip to content

Commit d310dab

Browse files
jg10-mastodon-socialangelo-v
authored andcommitted
feat(core): add Store.findMembers
1 parent 2cc1e61 commit d310dab

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

core/src/Store.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { when } from "jest-when";
2-
import { sym } from "rdflib";
2+
import { quad, sym } from "rdflib";
33
import { Parser as SparqlParser, Update } from "sparqljs";
44
import { AuthenticatedFetch, PodOsSession } from "./authentication";
55
import { Store } from "./Store";
@@ -450,6 +450,32 @@ describe("Store", () => {
450450
);
451451
});
452452
});
453+
454+
describe("findMembers", () => {
455+
it("finds instances of classes and subclasses", () => {
456+
const store = new Store({} as PodOsSession);
457+
store.graph.addAll([
458+
quad(
459+
sym("http://recipe.test/1"),
460+
sym("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
461+
sym("http://schema.org/Recipe"),
462+
),
463+
quad(
464+
sym("http://recipe.test/RecipeClass"),
465+
sym("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
466+
sym("http://schema.org/Recipe"),
467+
),
468+
quad(
469+
sym("http://recipe.test/2"),
470+
sym("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
471+
sym("http://recipe.test/RecipeClass"),
472+
),
473+
]);
474+
const members = store.findMembers("http://schema.org/Recipe");
475+
expect(members).toContain("http://recipe.test/1");
476+
expect(members).toContain("http://recipe.test/2");
477+
});
478+
});
453479
});
454480

455481
export function thenSparqlUpdateIsSentToUrl(

core/src/Store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,12 @@ export class Store {
133133
async executeUpdate(operation: UpdateOperation) {
134134
await executeUpdate(this.fetcher, this.updater, operation);
135135
}
136+
137+
/**
138+
* Finds instances of the given class or its sub-classes
139+
* @param classUri
140+
*/
141+
findMembers(classUri: string): string[] {
142+
return Object.keys(this.graph.findMemberURIs(sym(classUri)));
143+
}
136144
}

0 commit comments

Comments
 (0)