Mercurial > code > home > repos > streamed-graph
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 |
rev | line source |
---|---|
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 | |
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 | 21 uri: NamedNode; |
22 primary: NamedNode; | |
23 joins: NamedNode[]; | |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
110
diff
changeset
|
24 links: Link[]; |
97 | 25 } |
26 | |
102 | 27 export class ViewConfig { |
93 | 28 graph: Store; |
94 | 29 viewRoot!: NamedNode; |
102 | 30 url?: string; |
31 tables: TableDesc[] = []; | |
95 | 32 |
102 | 33 constructor() { |
93 | 34 this.graph = new Store(); |
35 } | |
95 | 36 |
102 | 37 async readFromUrl(url: string | "") { |
38 if (!url) { | |
39 return; | |
40 } | |
110 | 41 this.url = url; |
102 | 42 await fetchAndParse(url, this.graph); |
43 | |
44 this._read(); | |
94 | 45 } |
95 | 46 |
102 | 47 async readFromGraph(n3: string) { |
48 this.graph = await n3Graph(n3); | |
49 this._read(); | |
50 } | |
51 | |
52 _read() { | |
53 this.viewRoot = firstElem( | |
54 this.graph.getSubjects(RDF("type"), EX("View"), null) | |
55 ) as NamedNode; | |
94 | 56 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) { |
97 | 57 const tableType = uriValue(this.graph, table, EX("primaryType")); |
58 const joins: NamedNode[] = []; | |
59 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) { | |
60 joins.push(joinType as NamedNode); | |
61 } | |
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 | 71 this.tables.push({ |
72 uri: table as NamedNode, | |
73 primary: tableType, | |
74 joins: joins, | |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
110
diff
changeset
|
75 links: links, |
102 | 76 }); |
94 | 77 } |
102 | 78 this.tables.sort(); |
79 } | |
80 | |
81 label(): string { | |
82 return this.url !== undefined | |
83 ? labelOrTail(this.graph, Uri(this.url)) | |
84 : "unnamed"; | |
93 | 85 } |
86 } |