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 }
|
110
|
35 this.url = url;
|
102
|
36 await fetchAndParse(url, this.graph);
|
|
37
|
|
38 this._read();
|
94
|
39 }
|
95
|
40
|
102
|
41 async readFromGraph(n3: string) {
|
|
42 this.graph = await n3Graph(n3);
|
|
43 this._read();
|
|
44 }
|
|
45
|
|
46 _read() {
|
|
47 this.viewRoot = firstElem(
|
|
48 this.graph.getSubjects(RDF("type"), EX("View"), null)
|
|
49 ) as NamedNode;
|
94
|
50 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) {
|
97
|
51 const tableType = uriValue(this.graph, table, EX("primaryType"));
|
|
52 const joins: NamedNode[] = [];
|
|
53 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
|
|
54 joins.push(joinType as NamedNode);
|
|
55 }
|
|
56 joins.sort();
|
102
|
57 this.tables.push({
|
|
58 uri: table as NamedNode,
|
|
59 primary: tableType,
|
|
60 joins: joins,
|
|
61 });
|
94
|
62 }
|
102
|
63 this.tables.sort();
|
|
64 }
|
|
65
|
|
66 label(): string {
|
|
67 return this.url !== undefined
|
|
68 ? labelOrTail(this.graph, Uri(this.url))
|
|
69 : "unnamed";
|
93
|
70 }
|
|
71 }
|