102
|
1 // Load requested view (rdf data) and provide access to it
|
|
2 import { DataFactory, NamedNode, Store } from "n3";
|
|
3 import { fetchAndParse, n3Graph } from "./fetchAndParse";
|
|
4 import { EX, RDF } from "./namespaces";
|
95
|
5 import { labelOrTail, uriValue } from "./rdf_value";
|
94
|
6 const Uri = DataFactory.namedNode;
|
93
|
7
|
94
|
8 function firstElem<E>(seq: Iterable<E>): E {
|
|
9 for (let e of seq) {
|
|
10 return e;
|
|
11 }
|
|
12 throw new Error("no elems");
|
|
13 }
|
|
14
|
97
|
15 export interface TableDesc {
|
|
16 uri: NamedNode;
|
|
17 primary: NamedNode;
|
|
18 joins: NamedNode[];
|
|
19 }
|
|
20
|
102
|
21 export class ViewConfig {
|
93
|
22 graph: Store;
|
94
|
23 viewRoot!: NamedNode;
|
102
|
24 url?: string;
|
|
25 tables: TableDesc[] = [];
|
95
|
26
|
102
|
27 constructor() {
|
93
|
28 this.graph = new Store();
|
|
29 }
|
95
|
30
|
102
|
31 async readFromUrl(url: string | "") {
|
|
32 if (!url) {
|
|
33 return;
|
|
34 }
|
|
35 await fetchAndParse(url, this.graph);
|
|
36
|
|
37 this._read();
|
94
|
38 }
|
95
|
39
|
102
|
40 async readFromGraph(n3: string) {
|
|
41 this.graph = await n3Graph(n3);
|
|
42 this._read();
|
|
43 }
|
|
44
|
|
45 _read() {
|
|
46 this.viewRoot = firstElem(
|
|
47 this.graph.getSubjects(RDF("type"), EX("View"), null)
|
|
48 ) as NamedNode;
|
94
|
49 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) {
|
97
|
50 const tableType = uriValue(this.graph, table, EX("primaryType"));
|
|
51 const joins: NamedNode[] = [];
|
|
52 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
|
|
53 joins.push(joinType as NamedNode);
|
|
54 }
|
|
55 joins.sort();
|
102
|
56 this.tables.push({
|
|
57 uri: table as NamedNode,
|
|
58 primary: tableType,
|
|
59 joins: joins,
|
|
60 });
|
94
|
61 }
|
102
|
62 this.tables.sort();
|
|
63 }
|
|
64
|
|
65 label(): string {
|
|
66 return this.url !== undefined
|
|
67 ? labelOrTail(this.graph, Uri(this.url))
|
|
68 : "unnamed";
|
93
|
69 }
|
|
70 }
|