annotate src/layout/Layout.ts @ 119:8715633f5213

fancier column sorting to bring preds from the same type together
author drewp@bigasterisk.com
date Sun, 20 Mar 2022 01:17:35 -0700
parents c2923b20bf5c
children a7519d92dbc6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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";
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
5 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
6 import { uniqueSortedTerms, UriPairMap } from "./rdf_value";
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
7 import { ViewConfig } from "./ViewConfig";
88
ac7ad087d474 graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff changeset
8
ac7ad087d474 graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff changeset
9 type UriSet = Immutable.Set<NamedNode>;
ac7ad087d474 graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff changeset
10
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
11 interface ColumnHeader {
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
12 rdfTypes: NamedNode[];
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
13 pred: NamedNode;
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
14 }
108
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
15
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
16 export interface AlignedTable {
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
17 columnHeaders: ColumnHeader[];
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
18 rowHeaders: NamedNode[];
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
19 rows: Term[][][];
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
20 }
108
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
21
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
22 export interface PredRow {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
23 pred: NamedNode;
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
24 objs: Term[];
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
25 }
108
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
26
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
27 export interface SubjRow {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
28 subj: NamedNode;
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
29 predRows: PredRow[];
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
30 }
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 export interface FreeStatements {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
33 subjRows: SubjRow[];
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
34 }
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
35
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
36 export interface LayoutResult {
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
37 sections: (AlignedTable | FreeStatements)[];
88
ac7ad087d474 graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff changeset
38 }
ac7ad087d474 graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff changeset
39
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
40 class AlignedTableBuilder {
117
069c1f70afa5 cleanup
drewp@bigasterisk.com
parents: 116
diff changeset
41 subjSet: UriSet = Immutable.Set();
069c1f70afa5 cleanup
drewp@bigasterisk.com
parents: 116
diff changeset
42 predSet: UriSet = Immutable.Set();
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
43 subjsSeenWithPred: Immutable.Map<NamedNode, NamedNode[]> = Immutable.Map();
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
44 typesSeenForSubj: Immutable.Map<NamedNode, NamedNode[]> = Immutable.Map();
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
45
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
46 cell = new UriPairMap();
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
47 constructor(public primaryType: NamedNode, public joinTypes: NamedNode[]) {}
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
48
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
49 showsType(rdfType: NamedNode): boolean {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
50 return (
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
51 this.primaryType.equals(rdfType) ||
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
52 Immutable.Set<NamedNode>(this.joinTypes).has(rdfType)
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
53 );
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
54 }
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
55
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
56 addQuad(q: Quad) {
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
57 const subj = q.subject as NamedNode;
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
58 const pred = q.predicate as NamedNode;
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
59 this.subjSet = this.subjSet.add(subj);
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
60 this.predSet = this.predSet.add(pred);
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
61 this.cell.add(subj, pred, q.object);
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
62
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
63 if (pred.equals(rdf.type)) {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
64 this.trackTypes(subj, q.object as NamedNode);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
65 }
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
66 this.trackSubjs(subj, pred);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
67 }
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
68
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
69 private trackTypes(subj: NamedNode<string>, rdfType: NamedNode) {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
70 let cur = this.typesSeenForSubj.get(subj, undefined);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
71 if (cur === undefined) {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
72 cur = [];
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
73 this.typesSeenForSubj = this.typesSeenForSubj.set(subj, cur);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
74 }
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
75 cur.push(rdfType);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
76 }
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
77
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
78 private trackSubjs(subj: NamedNode, pred: NamedNode<string>) {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
79 let cur = this.subjsSeenWithPred.get(pred, undefined);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
80 if (cur === undefined) {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
81 cur = [];
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
82 this.subjsSeenWithPred = this.subjsSeenWithPred.set(pred, cur);
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
83 }
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
84 cur.push(subj);
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
85 }
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
86
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
87 _displayedPreds(): NamedNode[] {
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
88 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
89 preds = preds.filter((p) => {
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
90 return !p.equals(rdf.type);
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
91 });
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
92 const tagged = preds.map((p, i) => {
119
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
93 const types = this.typesSeenWithPred(p);
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
94 return {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
95 sort: [
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
96 p.equals(rdfs.label) ? 0 : 1,
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
97 types.length == 1 ? 0 : 1,
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
98 types[0].equals(this.primaryType) ? 0 : 1,
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
99 types[0],
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
100 i,
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
101 ],
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
102 val: p,
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
103 };
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
104 });
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
105 tagged.sort((a, b) => {
119
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
106 let ib = 0;
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
107 for (let k1 of a.sort) {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
108 const k2 = b.sort[ib];
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
109 if (!Immutable.is(k1, k2)) {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
110 if (typeof k1 === "number") {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
111 if (typeof k2 === "number") {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
112 return k1 - k2;
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
113 } else {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
114 throw new Error(`${k1} vs ${k2}`);
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
115 }
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
116 } else {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
117 return (k1 as NamedNode).id.localeCompare((k2 as NamedNode).id);
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
118 }
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
119 }
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
120 ib++;
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
121 }
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
122 return 0;
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
123 });
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
124 preds = tagged.map((e) => e.val);
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
125 return preds;
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
126 }
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
127
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
128 gotStatements(): boolean {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
129 return !this.subjSet.isEmpty();
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
130 }
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
131
119
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
132 private typesSeenWithPred(pred: NamedNode): NamedNode[] {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
133 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
134 const types: NamedNode[] = [];
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
135 subs.forEach((s) => {
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
136 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
137 types.push(t);
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
138 });
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
139 });
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
140 return uniqueSortedTerms(types);
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
141 }
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
142
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
143 value(): AlignedTable {
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
144 const subjs = uniqueSortedTerms(this.subjSet);
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
145 const preds = this._displayedPreds();
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
146 const outputGrid: Term[][][] = [];
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
147 for (let subj of subjs) {
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
148 const row: Term[][] = [];
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
149 preds.forEach((pred) => {
114
4b33a479dc2f fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents: 110
diff changeset
150 const objs = this.cell.get(subj, pred);
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
151 const uniq = uniqueSortedTerms(objs);
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
152 row.push(uniq);
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
153 });
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
154 outputGrid.push(row);
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
155 }
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
156
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
157 const headers: ColumnHeader[] = preds.map((pred) => {
119
8715633f5213 fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents: 118
diff changeset
158 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
159 });
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
160 return { columnHeaders: headers, rowHeaders: subjs, rows: outputGrid };
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
161 }
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
162 }
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
163
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
164 type SubjectTableBuilders = Immutable.Map<
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
165 NamedNode<string>,
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
166 AlignedTableBuilder[]
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
167 >;
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
168
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
169 function subjectsToTablesMap(
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
170 graph: Store,
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
171 tableBuilders: AlignedTableBuilder[]
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
172 ): SubjectTableBuilders {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
173 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
174 graph.forEach(
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
175 (q: Quad) => {
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
176 const s = q.subject as NamedNode;
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
177 const rdfType = q.object as NamedNode;
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
178 tableBuilders.forEach((tb) => {
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
179 if (tb.showsType(rdfType)) {
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
180 let cur = out.get(s, undefined);
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
181 if (cur === undefined) {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
182 cur = [];
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
183 out = out.set(s, cur);
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
184 }
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
185 cur.push(tb);
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
186 }
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
187 });
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
188 },
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
189 null,
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
190 rdf.type,
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
191 null,
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
192 null
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
193 );
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
194 return out;
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
195 }
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
196
108
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
197 function freeStatmentsSection(stmts: Quad[]): FreeStatements {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
198 const subjs: NamedNode[] = [];
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
199 stmts.forEach((q) => {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
200 subjs.push(q.subject as NamedNode);
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
201 });
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
202 return {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
203 subjRows: uniqueSortedTerms(subjs).map((subj) => {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
204 const preds: NamedNode[] = [];
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
205 let po = Immutable.Map<NamedNode, Term[]>();
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
206 stmts.forEach((q) => {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
207 if (q.subject.equals(subj)) {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
208 const p = q.predicate as NamedNode;
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
209 preds.push(p);
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
210 po = po.set(p, po.get(p, []));
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
211 po.get(p)?.push(q.object as Term);
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
212 }
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
213 });
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
214
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
215 const rows: PredRow[] = [];
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
216 uniqueSortedTerms(preds).forEach((p) => {
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
217 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
218 });
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
219 return { subj: subj, predRows: rows };
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
220 }),
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
221 };
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
222 }
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
223
5e6840229a05 rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents: 106
diff changeset
224 // The description of how this page should look: sections, tables, etc.
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
225 export class Layout {
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
226 constructor(public viewConfig?: ViewConfig) {}
117
069c1f70afa5 cleanup
drewp@bigasterisk.com
parents: 116
diff changeset
227
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
228 _groupAllStatements(
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
229 graph: Store,
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
230 tablesWantingSubject: SubjectTableBuilders,
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
231 ungrouped: Quad[]
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
232 ) {
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
233 graph.forEach(
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
234 (q: Quad) => {
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
235 const tables = tablesWantingSubject.get(q.subject as NamedNode);
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
236
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
237 if (tables && tables.length) {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
238 tables.forEach((t: AlignedTableBuilder) => t.addQuad(q));
110
3cdbbd913f1d table displays now just barely
drewp@bigasterisk.com
parents: 108
diff changeset
239 } else {
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
240 ungrouped.push(q);
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
241 }
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
242 },
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
243 null,
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
244 null,
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
245 null,
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
246 null
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
247 );
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
248 }
117
069c1f70afa5 cleanup
drewp@bigasterisk.com
parents: 116
diff changeset
249
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
250 plan(graph: Store): LayoutResult {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
251 const ungrouped: Quad[] = [];
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
252
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
253 const tableBuilders = this.viewConfig
118
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
254 ? this.viewConfig.tables.map(
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
255 (t) => new AlignedTableBuilder(t.primary, t.joins)
c2923b20bf5c support multi labels per column
drewp@bigasterisk.com
parents: 117
diff changeset
256 )
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
257 : [];
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
258
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
259 const tablesWantingSubject = subjectsToTablesMap(graph, tableBuilders);
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
260 this._groupAllStatements(graph, tablesWantingSubject, ungrouped);
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
261 const res: LayoutResult = { sections: [] };
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
262 for (const t of tableBuilders) {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
263 if (t.gotStatements()) {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
264 res.sections.push(t.value());
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
265 }
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
266 }
116
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
267 if (ungrouped.length) {
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
268 res.sections.push(freeStatmentsSection(ungrouped));
dd3325cc023e multiple table support in Layout
drewp@bigasterisk.com
parents: 115
diff changeset
269 }
104
1aea03d306af WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents: 103
diff changeset
270 return res;
103
f12feced00ce WIP rewriting Layout
drewp@bigasterisk.com
parents: 97
diff changeset
271 }
88
ac7ad087d474 graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff changeset
272 }