comparison src/layout/ViewConfig.ts @ 106:2468f2227d22

make src/layout/ and src/render/ separation
author drewp@bigasterisk.com
date Sun, 13 Mar 2022 22:00:30 -0700
parents src/ViewConfig.ts@ab7dca42afbd
children 3cdbbd913f1d
comparison
equal deleted inserted replaced
105:4bb8c7775c83 106:2468f2227d22
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";
5 import { labelOrTail, uriValue } from "./rdf_value";
6 const Uri = DataFactory.namedNode;
7
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
15 export interface TableDesc {
16 uri: NamedNode;
17 primary: NamedNode;
18 joins: NamedNode[];
19 }
20
21 export class ViewConfig {
22 graph: Store;
23 viewRoot!: NamedNode;
24 url?: string;
25 tables: TableDesc[] = [];
26
27 constructor() {
28 this.graph = new Store();
29 }
30
31 async readFromUrl(url: string | "") {
32 if (!url) {
33 return;
34 }
35 await fetchAndParse(url, this.graph);
36
37 this._read();
38 }
39
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;
49 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) {
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();
56 this.tables.push({
57 uri: table as NamedNode,
58 primary: tableType,
59 joins: joins,
60 });
61 }
62 this.tables.sort();
63 }
64
65 label(): string {
66 return this.url !== undefined
67 ? labelOrTail(this.graph, Uri(this.url))
68 : "unnamed";
69 }
70 }