annotate src/view_loader.ts @ 97:26c55d5d5202

WIP on views & joins
author drewp@bigasterisk.com
date Fri, 11 Feb 2022 20:27:02 -0800
parents 47d3b5a5bd5e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
1 // Load requested view and provide access to it
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
2 import { Store, NamedNode, DataFactory } from "n3";
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
3 import { fetchAndParse } from "./fetchAndParse";
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
4 import { RDF, EX } from "./namespaces";
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
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
21
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
22 export class View {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
23 graph: Store;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
24 ready: Promise<null>;
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
25 viewRoot!: NamedNode;
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
26
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
27 constructor(public url: string | "") {
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
28 (window as any).v = this; //debug
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
29 this.graph = new Store();
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
30 this.ready = new Promise((res, rej) => {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
31 if (url) {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
32 fetchAndParse(url).then((s2) => {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
33 this.graph = s2;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
34 res(null);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
35 });
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
36 } else {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
37 res(null);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
38 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
39 });
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
40 this.ready.then(() => {
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
41 this.viewRoot = firstElem(
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
42 this.graph.getSubjects(RDF("type"), EX("View"), null)
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
43 ) as NamedNode;
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
44 });
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
45 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
46
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
47 label() {
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
48 return labelOrTail(this.graph, Uri(this.url));
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
49 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
50
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
51 // filtered+ordered list of types to show at the top level
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
52 toplevelTables(typesPresent: NamedNode[]): TableDesc[] {
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
53 const ret: TableDesc[] = [];
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
54 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
55 const tableType = uriValue(this.graph, table, EX("primaryType"));
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
56 const joins: NamedNode[] = [];
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
57 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
58 joins.push(joinType as NamedNode);
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
59 }
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
60 joins.sort();
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
61 ret.push({ uri: table as NamedNode, primary: tableType, joins: joins });
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
62 }
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
63 ret.sort();
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
64 return ret;
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
65 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
66 }