Mercurial > code > home > repos > streamed-graph
annotate src/layout/Layout.ts @ 139:cf642d395be4
new simpler Patch class; fancier 'hide' view config support
author | drewp@bigasterisk.com |
---|---|
date | Mon, 08 May 2023 13:05:20 -0700 |
parents | 5a1a79f54779 |
children |
rev | line source |
---|---|
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
1 // Organize graph data into tables (column orders, etc) for the view layer. |
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
3 import Immutable from "immutable"; // mostly using this for the builtin equals() testing, since NamedNode(x)!=NamedNode(x) |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
4 import { NamedNode, Quad, Store, Term } from "n3"; |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
5 import { addToValues, multiColumnSort } from "./algorithm"; |
110 | 6 import { rdf, rdfs } from "./namespaces"; |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
7 import { uniqueSortedTerms, UriPairMap } from "./rdf_value"; |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
8 import { Link, ViewConfig } from "./ViewConfig"; |
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
9 |
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
10 type UriSet = Immutable.Set<NamedNode>; |
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
11 |
103 | 12 interface ColumnHeader { |
118 | 13 rdfTypes: NamedNode[]; |
103 | 14 pred: NamedNode; |
15 } | |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
16 |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
17 export interface AlignedTable { |
103 | 18 columnHeaders: ColumnHeader[]; |
110 | 19 rowHeaders: NamedNode[]; |
20 rows: Term[][][]; | |
103 | 21 } |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
22 |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
23 export interface PredRow { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
24 pred: NamedNode; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
25 objs: Term[]; |
103 | 26 } |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
27 |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
28 export interface SubjRow { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
29 subj: NamedNode; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
30 predRows: PredRow[]; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
31 } |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
32 |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
33 export interface FreeStatements { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
34 subjRows: SubjRow[]; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
35 } |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
36 |
103 | 37 export interface LayoutResult { |
38 sections: (AlignedTable | FreeStatements)[]; | |
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
39 } |
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
40 |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
41 class AlignedTableBuilder { |
117 | 42 subjSet: UriSet = Immutable.Set(); |
43 predSet: UriSet = Immutable.Set(); | |
118 | 44 subjsSeenWithPred: Immutable.Map<NamedNode, NamedNode[]> = Immutable.Map(); |
45 typesSeenForSubj: Immutable.Map<NamedNode, NamedNode[]> = Immutable.Map(); | |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
46 aliases: Immutable.Map<NamedNode, NamedNode> = Immutable.Map(); |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
47 cell = new UriPairMap(); |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
48 constructor( |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
49 public primaryType: NamedNode, |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
50 public joinTypes: NamedNode[], |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
51 public links: Link[] |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
52 ) {} |
118 | 53 |
54 showsType(rdfType: NamedNode): boolean { | |
55 return ( | |
56 this.primaryType.equals(rdfType) || | |
57 Immutable.Set<NamedNode>(this.joinTypes).has(rdfType) | |
58 ); | |
59 } | |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
60 |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
61 // Considering this.links, call adder on more subjs that should be routed to this table. |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
62 detectLinks(graph: Store, adder: (linkedSubj: NamedNode) => void) { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
63 this.links.forEach((link: Link) => { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
64 graph.forEach( |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
65 (q: Quad) => { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
66 const linkedSubj = q.object as NamedNode; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
67 adder(linkedSubj); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
68 this.linkSubjs(q.subject as NamedNode, linkedSubj); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
69 }, |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
70 null, |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
71 link.pred, |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
72 null, |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
73 null |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
74 ); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
75 }); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
76 this.chainLinks(); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
77 } |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
78 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
79 // When you make a row for displayedSubj, also include linkedSubj's data |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
80 private linkSubjs(displayedSubj: NamedNode, linkedSubj: NamedNode) { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
81 this.aliases = this.aliases.set(linkedSubj, displayedSubj); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
82 } |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
83 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
84 // After this.aliases is built; shorten {b: a, c:b} to {b: a, c: a} |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
85 private chainLinks() { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
86 for (let alias of this.aliases.keys()) { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
87 let x = this.aliases.get(alias)!; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
88 while (this.aliases.has(x)) { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
89 x = this.aliases.get(x)!; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
90 } |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
91 this.aliases = this.aliases.set(alias, x); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
92 } |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
93 } |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
94 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
95 // Stream in quads that belong to this table (caller has to work out that |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
96 // part), then call value(). |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
97 addQuad(q: Quad) { |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
98 const unaliasedSubj = q.subject as NamedNode; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
99 const aliasedSubj = this.aliases.get(unaliasedSubj, unaliasedSubj); |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
100 const pred = q.predicate as NamedNode; |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
101 this.subjSet = this.subjSet.add(aliasedSubj); |
110 | 102 this.predSet = this.predSet.add(pred); |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
103 this.cell.add(aliasedSubj, pred, q.object); |
118 | 104 |
105 if (pred.equals(rdf.type)) { | |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
106 this.trackTypes(unaliasedSubj, q.object as NamedNode); |
118 | 107 } |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
108 this.trackSubjs(unaliasedSubj, pred); |
118 | 109 } |
110 | |
120
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
111 private trackTypes(unaliasedSubj: NamedNode<string>, rdfType: NamedNode) { |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
112 this.typesSeenForSubj = addToValues( |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
113 this.typesSeenForSubj, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
114 unaliasedSubj, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
115 rdfType |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
116 ); |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
117 } |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
118 |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
119 private trackSubjs(unaliasedSubj: NamedNode, pred: NamedNode<string>) { |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
120 this.subjsSeenWithPred = addToValues( |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
121 this.subjsSeenWithPred, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
122 pred, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
123 unaliasedSubj |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
124 ); |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
125 } |
110 | 126 |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
127 private displayedPreds(): NamedNode[] { |
110 | 128 let preds = uniqueSortedTerms(this.predSet); |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
129 preds = preds.filter((p) => { |
120
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
130 if (p.equals(rdf.type)) return false; |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
131 if (this.links.filter((l) => l.pred.equals(p)).length) return false; |
120
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
132 return true; |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
133 }); |
120
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
134 preds = multiColumnSort(preds, (elem: NamedNode, index: number) => { |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
135 const types = this.typesSeenWithPred(elem); |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
136 return [ |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
137 elem.equals(rdfs.label) ? 0 : 1, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
138 types.length == 1 ? 0 : 1, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
139 types[0].equals(this.primaryType) ? 0 : 1, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
140 types[0].id, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
141 index, |
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
142 ]; |
110 | 143 }); |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
144 return preds; |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
145 } |
118 | 146 |
116 | 147 gotStatements(): boolean { |
148 return !this.subjSet.isEmpty(); | |
149 } | |
118 | 150 |
119
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
151 private typesSeenWithPred(pred: NamedNode): NamedNode[] { |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
152 const subs = this.subjsSeenWithPred.get(pred, []); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
153 const types: NamedNode[] = []; |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
154 subs.forEach((s) => { |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
155 this.typesSeenForSubj.get(s, []).forEach((t) => { |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
156 types.push(t); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
157 }); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
158 }); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
159 return uniqueSortedTerms(types); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
160 } |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
161 |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
162 private gatherOutputGrid(subjs: NamedNode[], preds: NamedNode[]): Term[][][] { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
163 const out: Term[][][] = []; |
110 | 164 for (let subj of subjs) { |
165 const row: Term[][] = []; | |
166 preds.forEach((pred) => { | |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
110
diff
changeset
|
167 const objs = this.cell.get(subj, pred); |
110 | 168 const uniq = uniqueSortedTerms(objs); |
169 row.push(uniq); | |
170 }); | |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
171 out.push(row); |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
172 } |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
173 return out; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
174 } |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
175 |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
176 value(): AlignedTable { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
177 const subjs = uniqueSortedTerms(this.subjSet); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
178 const preds = this.displayedPreds(); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
179 const outputGrid: Term[][][] = this.gatherOutputGrid(subjs, preds); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
180 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
181 const colHeaders: ColumnHeader[] = preds.map((pred) => { |
119
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
182 return { rdfTypes: this.typesSeenWithPred(pred), pred: pred }; |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
183 }); |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
184 return { columnHeaders: colHeaders, rowHeaders: subjs, rows: outputGrid }; |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
185 } |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
186 } |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
187 |
116 | 188 type SubjectTableBuilders = Immutable.Map< |
189 NamedNode<string>, | |
190 AlignedTableBuilder[] | |
191 >; | |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
192 |
116 | 193 function subjectsToTablesMap( |
194 graph: Store, | |
195 tableBuilders: AlignedTableBuilder[] | |
196 ): SubjectTableBuilders { | |
197 let out: SubjectTableBuilders = Immutable.Map(); | |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
198 graph.forEach( |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
199 (q: Quad) => { |
116 | 200 const s = q.subject as NamedNode; |
118 | 201 const rdfType = q.object as NamedNode; |
116 | 202 tableBuilders.forEach((tb) => { |
118 | 203 if (tb.showsType(rdfType)) { |
120
a7519d92dbc6
refactor addToValues, multiColumnSort
drewp@bigasterisk.com
parents:
119
diff
changeset
|
204 out = addToValues(out, s, tb); |
116 | 205 } |
206 }); | |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
207 }, |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
208 null, |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
209 rdf.type, |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
210 null, |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
211 null |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
212 ); |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
213 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
214 tableBuilders.forEach((tb) => { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
215 tb.detectLinks(graph, (linkedSubj: NamedNode) => { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
216 out = addToValues(out, linkedSubj, tb); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
217 }); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
218 }); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
219 |
116 | 220 return out; |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
221 } |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
222 |
139
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
223 function freeStatmentsSection( |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
224 viewConfig: ViewConfig, |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
225 stmts: Quad[] |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
226 ): FreeStatements { |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
227 const subjs: NamedNode[] = []; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
228 stmts.forEach((q) => { |
139
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
229 if (viewConfig.hidePredFrees.has(q.predicate)) { |
128 | 230 return; |
231 } | |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
232 subjs.push(q.subject as NamedNode); |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
233 }); |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
234 return { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
235 subjRows: uniqueSortedTerms(subjs).map((subj) => { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
236 const preds: NamedNode[] = []; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
237 let po = Immutable.Map<NamedNode, Term[]>(); |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
238 stmts.forEach((q) => { |
139
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
239 if ( |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
240 q.subject.equals(subj) && |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
241 !viewConfig.hidePredFrees.has(q.predicate) |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
242 ) { |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
243 const p = q.predicate as NamedNode; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
244 preds.push(p); |
121 | 245 po = addToValues(po, p, q.object as NamedNode); |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
246 } |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
247 }); |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
248 |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
249 const rows: PredRow[] = []; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
250 uniqueSortedTerms(preds).forEach((p) => { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
251 rows.push({ pred: p, objs: uniqueSortedTerms(po.get(p, [])) }); |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
252 }); |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
253 return { subj: subj, predRows: rows }; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
254 }), |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
255 }; |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
256 } |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
257 |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
258 // The description of how this page should look: sections, tables, etc. |
103 | 259 export class Layout { |
128 | 260 constructor(public viewConfig: ViewConfig) {} |
117 | 261 |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
262 private groupAllStatements( |
116 | 263 graph: Store, |
264 tablesWantingSubject: SubjectTableBuilders, | |
265 ungrouped: Quad[] | |
266 ) { | |
103 | 267 graph.forEach( |
268 (q: Quad) => { | |
139
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
269 if (this.viewConfig.hideGraphEverywhere.has(q.graph)) { |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
270 return; |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
271 } |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
272 |
116 | 273 const tables = tablesWantingSubject.get(q.subject as NamedNode); |
118 | 274 |
116 | 275 if (tables && tables.length) { |
276 tables.forEach((t: AlignedTableBuilder) => t.addQuad(q)); | |
110 | 277 } else { |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
278 ungrouped.push(q); |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
279 } |
103 | 280 }, |
281 null, | |
282 null, | |
283 null, | |
284 null | |
285 ); | |
116 | 286 } |
117 | 287 |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
288 private generateSections( |
128 | 289 viewConfig: ViewConfig, |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
290 tableBuilders: AlignedTableBuilder[], |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
291 ungrouped: Quad[] |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
292 ): (AlignedTable | FreeStatements)[] { |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
293 const sections = []; |
116 | 294 for (const t of tableBuilders) { |
295 if (t.gotStatements()) { | |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
296 sections.push(t.value()); |
116 | 297 } |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
298 } |
116 | 299 if (ungrouped.length) { |
128 | 300 sections.push(freeStatmentsSection(viewConfig, ungrouped)); |
116 | 301 } |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
302 return sections; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
303 } |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
304 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
305 plan(graph: Store): LayoutResult { |
128 | 306 const vcTables = this.viewConfig.tables; |
122
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
307 const tableBuilders = vcTables.map( |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
308 (t) => new AlignedTableBuilder(t.primary, t.joins, t.links) |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
309 ); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
310 |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
311 const tablesWantingSubject = subjectsToTablesMap(graph, tableBuilders); |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
312 const ungrouped: Quad[] = []; |
2e8fa3fec0c8
support joining subjects into wider rows
drewp@bigasterisk.com
parents:
121
diff
changeset
|
313 this.groupAllStatements(graph, tablesWantingSubject, ungrouped); |
139
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
314 return { |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
315 sections: this.generateSections( |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
316 this.viewConfig, |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
317 tableBuilders, |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
318 ungrouped |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
319 ), |
cf642d395be4
new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents:
128
diff
changeset
|
320 }; |
103 | 321 } |
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
322 } |