diff 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
line wrap: on
line diff
--- a/src/layout/Layout.ts	Sat Mar 19 15:45:03 2022 -0700
+++ b/src/layout/Layout.ts	Sat Mar 19 16:12:49 2022 -0700
@@ -3,7 +3,7 @@
 import Immutable from "immutable"; // mostly using this for the builtin equals() testing, since NamedNode(x)!=NamedNode(x)
 import { NamedNode, Quad, Store, Term } from "n3";
 import { rdf, rdfs } from "./namespaces";
-import { uniqueSortedTerms } from "./rdf_value";
+import { uniqueSortedTerms, UriPairMap } from "./rdf_value";
 import { TableDesc, ViewConfig } from "./ViewConfig";
 
 type UriSet = Immutable.Set<NamedNode>;
@@ -38,19 +38,10 @@
   sections: (AlignedTable | FreeStatements)[];
 }
 
-interface ISP {
-  subj: NamedNode;
-  pred: NamedNode;
-}
-const makeSP = Immutable.Record<ISP>({
-  subj: new NamedNode(""),
-  pred: new NamedNode(""),
-});
-
 class AlignedTableBuilder {
   subjSet = Immutable.Set<NamedNode>();
   predSet = Immutable.Set<NamedNode>();
-  cell = Immutable.Map<string, Immutable.Set<Term>>();
+  cell = new UriPairMap();
   constructor(
     public rdfType: NamedNode /* plus join types, sort instructions */
   ) {}
@@ -60,41 +51,36 @@
     const pred = q.predicate as NamedNode;
     this.subjSet = this.subjSet.add(subj);
     this.predSet = this.predSet.add(pred);
+    this.cell.add(subj, pred, q.object);
+  }
 
-    const key =subj.id+pred.id//makeSP({ subj, pred });
-    const cur = this.cell.get(key, undefined);
-    const newval =
-      cur === undefined ? Immutable.Set([q.object]) : cur.add(q.object);
-
-    this.cell = this.cell.set(key, newval);
-  }
-  
-  value(): AlignedTable {
+  _displayedPreds(): NamedNode[] {
     let preds = uniqueSortedTerms(this.predSet);
-    const tagged = preds.map((p, i)=>{
-      if (p.equals(rdf.type)) {
-        i=999;
-      }
+    preds = preds.filter((p) => {
+      return !p.equals(rdf.type);
+    });
+    const tagged = preds.map((p, i) => {
       if (p.equals(rdfs.label)) {
-        i=-1
+        i = -1;
       }
-      return {sort:i, val: p}
-    })
-    tagged.sort((a,b)=>{
+      return { sort: i, val: p };
+    });
+    tagged.sort((a, b) => {
       return a.sort - b.sort;
     });
-    preds = tagged.map((e)=>e.val);
+    preds = tagged.map((e) => e.val);
+    return preds;
+  }
 
-    // const omittedColumn = pred.equals(rdf.type);
+  value(): AlignedTable {
     const subjs = uniqueSortedTerms(this.subjSet);
+    const preds = this._displayedPreds();
     const outputGrid: Term[][][] = [];
     for (let subj of subjs) {
       const row: Term[][] = [];
       preds.forEach((pred) => {
-        const key = subj.id+pred.id;//makeSP({ subj, pred });
-        const objs = this.cell.get(key, Immutable.Set<Term>([]));
+        const objs = this.cell.get(subj, pred);
         const uniq = uniqueSortedTerms(objs);
-        console.log("cell objs", objs.size, uniq.length);
         row.push(uniq);
       });
       outputGrid.push(row);