annotate src/ViewConfig.ts @ 103:f12feced00ce

WIP rewriting Layout
author drewp@bigasterisk.com
date Sat, 12 Mar 2022 00:42:00 -0800
parents ab7dca42afbd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
1 // Load requested view (rdf data) and provide access to it
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
2 import { DataFactory, NamedNode, Store } from "n3";
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
3 import { fetchAndParse, n3Graph } from "./fetchAndParse";
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
4 import { EX, RDF } from "./namespaces";
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
5 import { labelOrTail, uriValue } from "./rdf_value";
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
6 const Uri = DataFactory.namedNode;
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
7
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
8 function firstElem<E>(seq: Iterable<E>): E {
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
9 for (let e of seq) {
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
10 return e;
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
11 }
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
12 throw new Error("no elems");
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
13 }
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
14
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
15 export interface TableDesc {
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
16 uri: NamedNode;
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
17 primary: NamedNode;
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
18 joins: NamedNode[];
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
19 }
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
20
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
21 export class ViewConfig {
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
22 graph: Store;
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
23 viewRoot!: NamedNode;
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
24 url?: string;
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
25 tables: TableDesc[] = [];
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
26
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
27 constructor() {
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
28 this.graph = new Store();
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
29 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
30
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
31 async readFromUrl(url: string | "") {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
32 if (!url) {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
33 return;
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
34 }
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
35 await fetchAndParse(url, this.graph);
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
36
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
37 this._read();
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
38 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
39
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
40 async readFromGraph(n3: string) {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
41 this.graph = await n3Graph(n3);
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
42 this._read();
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
43 }
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
44
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
45 _read() {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
46 this.viewRoot = firstElem(
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
47 this.graph.getSubjects(RDF("type"), EX("View"), null)
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
48 ) as NamedNode;
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
49 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) {
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
50 const tableType = uriValue(this.graph, table, EX("primaryType"));
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
51 const joins: NamedNode[] = [];
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
52 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
53 joins.push(joinType as NamedNode);
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
54 }
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
55 joins.sort();
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
56 this.tables.push({
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
57 uri: table as NamedNode,
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
58 primary: tableType,
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
59 joins: joins,
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
60 });
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
61 }
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
62 this.tables.sort();
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
63 }
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
64
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
65 label(): string {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
66 return this.url !== undefined
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
67 ? labelOrTail(this.graph, Uri(this.url))
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
68 : "unnamed";
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
69 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
70 }