annotate src/layout/ViewConfig.ts @ 122:2e8fa3fec0c8

support joining subjects into wider rows
author drewp@bigasterisk.com
date Sun, 20 Mar 2022 14:12:03 -0700
parents 3cdbbd913f1d
children 5a1a79f54779
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
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
15 export interface Link {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
16 // If you display a subject u1 with a `pred` edge to u2, then treat u2 as an alias of u1.
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
17 pred: NamedNode;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
18 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
19
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
20 interface TableDesc {
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
21 uri: NamedNode;
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
22 primary: NamedNode;
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
23 joins: NamedNode[];
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
24 links: Link[];
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
25 }
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
26
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
27 export class ViewConfig {
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
28 graph: Store;
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
29 viewRoot!: NamedNode;
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
30 url?: string;
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
31 tables: TableDesc[] = [];
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
32
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
33 constructor() {
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
34 this.graph = new Store();
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
35 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
36
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
37 async readFromUrl(url: string | "") {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
38 if (!url) {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
39 return;
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
40 }
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 106
diff changeset
41 this.url = url;
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
42 await fetchAndParse(url, this.graph);
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
43
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
44 this._read();
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
45 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
46
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
47 async readFromGraph(n3: string) {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
48 this.graph = await n3Graph(n3);
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
49 this._read();
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
50 }
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
51
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
52 _read() {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
53 this.viewRoot = firstElem(
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
54 this.graph.getSubjects(RDF("type"), EX("View"), null)
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
55 ) as NamedNode;
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
56 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
57 const tableType = uriValue(this.graph, table, EX("primaryType"));
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
58 const joins: NamedNode[] = [];
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
59 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
60 joins.push(joinType as NamedNode);
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
61 }
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
62 joins.sort();
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
63
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
64 const links: Link[] = [];
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
65 for (let linkDesc of this.graph.getObjects(table, EX("link"), null)) {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
66 links.push({
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
67 pred: uriValue(this.graph, linkDesc, EX("predicate")),
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
68 });
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
69 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
70
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
71 this.tables.push({
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
72 uri: table as NamedNode,
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
73 primary: tableType,
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
74 joins: joins,
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
75 links: links,
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
76 });
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
77 }
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
78 this.tables.sort();
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
79 }
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
80
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
81 label(): string {
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
82 return this.url !== undefined
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
83 ? labelOrTail(this.graph, Uri(this.url))
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
84 : "unnamed";
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
85 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
86 }