comparison src/layout/Layout.ts @ 114:4b33a479dc2f

fix layout test to match new layout return types. clean up UriPairMap
author drewp@bigasterisk.com
date Sat, 19 Mar 2022 16:12:49 -0700
parents 3cdbbd913f1d
children 84551452a9c9
comparison
equal deleted inserted replaced
113:4822d5621463 114:4b33a479dc2f
1 // Organize graph data into tables (column orders, etc) for the view layer. 1 // Organize graph data into tables (column orders, etc) for the view layer.
2 2
3 import Immutable from "immutable"; // mostly using this for the builtin equals() testing, since NamedNode(x)!=NamedNode(x) 3 import Immutable from "immutable"; // mostly using this for the builtin equals() testing, since NamedNode(x)!=NamedNode(x)
4 import { NamedNode, Quad, Store, Term } from "n3"; 4 import { NamedNode, Quad, Store, Term } from "n3";
5 import { rdf, rdfs } from "./namespaces"; 5 import { rdf, rdfs } from "./namespaces";
6 import { uniqueSortedTerms } from "./rdf_value"; 6 import { uniqueSortedTerms, UriPairMap } from "./rdf_value";
7 import { TableDesc, ViewConfig } from "./ViewConfig"; 7 import { TableDesc, ViewConfig } from "./ViewConfig";
8 8
9 type UriSet = Immutable.Set<NamedNode>; 9 type UriSet = Immutable.Set<NamedNode>;
10 export type TypeToSubjs = Immutable.Map<NamedNode, UriSet>; 10 export type TypeToSubjs = Immutable.Map<NamedNode, UriSet>;
11 11
36 36
37 export interface LayoutResult { 37 export interface LayoutResult {
38 sections: (AlignedTable | FreeStatements)[]; 38 sections: (AlignedTable | FreeStatements)[];
39 } 39 }
40 40
41 interface ISP {
42 subj: NamedNode;
43 pred: NamedNode;
44 }
45 const makeSP = Immutable.Record<ISP>({
46 subj: new NamedNode(""),
47 pred: new NamedNode(""),
48 });
49
50 class AlignedTableBuilder { 41 class AlignedTableBuilder {
51 subjSet = Immutable.Set<NamedNode>(); 42 subjSet = Immutable.Set<NamedNode>();
52 predSet = Immutable.Set<NamedNode>(); 43 predSet = Immutable.Set<NamedNode>();
53 cell = Immutable.Map<string, Immutable.Set<Term>>(); 44 cell = new UriPairMap();
54 constructor( 45 constructor(
55 public rdfType: NamedNode /* plus join types, sort instructions */ 46 public rdfType: NamedNode /* plus join types, sort instructions */
56 ) {} 47 ) {}
57 48
58 addQuad(q: Quad) { 49 addQuad(q: Quad) {
59 const subj = q.subject as NamedNode; 50 const subj = q.subject as NamedNode;
60 const pred = q.predicate as NamedNode; 51 const pred = q.predicate as NamedNode;
61 this.subjSet = this.subjSet.add(subj); 52 this.subjSet = this.subjSet.add(subj);
62 this.predSet = this.predSet.add(pred); 53 this.predSet = this.predSet.add(pred);
63 54 this.cell.add(subj, pred, q.object);
64 const key =subj.id+pred.id//makeSP({ subj, pred }); 55 }
65 const cur = this.cell.get(key, undefined); 56
66 const newval = 57 _displayedPreds(): NamedNode[] {
67 cur === undefined ? Immutable.Set([q.object]) : cur.add(q.object); 58 let preds = uniqueSortedTerms(this.predSet);
68 59 preds = preds.filter((p) => {
69 this.cell = this.cell.set(key, newval); 60 return !p.equals(rdf.type);
70 } 61 });
71 62 const tagged = preds.map((p, i) => {
63 if (p.equals(rdfs.label)) {
64 i = -1;
65 }
66 return { sort: i, val: p };
67 });
68 tagged.sort((a, b) => {
69 return a.sort - b.sort;
70 });
71 preds = tagged.map((e) => e.val);
72 return preds;
73 }
74
72 value(): AlignedTable { 75 value(): AlignedTable {
73 let preds = uniqueSortedTerms(this.predSet);
74 const tagged = preds.map((p, i)=>{
75 if (p.equals(rdf.type)) {
76 i=999;
77 }
78 if (p.equals(rdfs.label)) {
79 i=-1
80 }
81 return {sort:i, val: p}
82 })
83 tagged.sort((a,b)=>{
84 return a.sort - b.sort;
85 });
86 preds = tagged.map((e)=>e.val);
87
88 // const omittedColumn = pred.equals(rdf.type);
89 const subjs = uniqueSortedTerms(this.subjSet); 76 const subjs = uniqueSortedTerms(this.subjSet);
77 const preds = this._displayedPreds();
90 const outputGrid: Term[][][] = []; 78 const outputGrid: Term[][][] = [];
91 for (let subj of subjs) { 79 for (let subj of subjs) {
92 const row: Term[][] = []; 80 const row: Term[][] = [];
93 preds.forEach((pred) => { 81 preds.forEach((pred) => {
94 const key = subj.id+pred.id;//makeSP({ subj, pred }); 82 const objs = this.cell.get(subj, pred);
95 const objs = this.cell.get(key, Immutable.Set<Term>([]));
96 const uniq = uniqueSortedTerms(objs); 83 const uniq = uniqueSortedTerms(objs);
97 console.log("cell objs", objs.size, uniq.length);
98 row.push(uniq); 84 row.push(uniq);
99 }); 85 });
100 outputGrid.push(row); 86 outputGrid.push(row);
101 } 87 }
102 88