changeset 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 4822d5621463
children 84551452a9c9
files src/layout/Layout.test.ts src/layout/Layout.ts src/layout/rdf_value.ts
diffstat 3 files changed, 56 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/layout/Layout.test.ts	Sat Mar 19 15:45:03 2022 -0700
+++ b/src/layout/Layout.test.ts	Sat Mar 19 16:12:49 2022 -0700
@@ -82,12 +82,14 @@
       expect(sec0.columnHeaders).toEqual([
         { rdfType: EX("T1"), pred: EX("color") },
         { rdfType: EX("T1"), pred: EX("size") },
+        // and doesn't include rdf:type as a column header here
       ]);
+      expect(sec0.rowHeaders).toEqual([EX("a"), EX("b"), EX("c"), EX("e")]);
       expect(sec0.rows).toEqual([
-        [EX("a"), EX("red"), null],
-        [EX("b"), EX("blue"), null],
-        [EX("c"), null, null],
-        [EX("e"), null, EX("small")],
+        [[EX("red")], []],
+        [[EX("blue")], []],
+        [[], []],
+        [[], [EX("small")]],
       ]);
     });
     it("leaves the rest ungrouped", async () => {
@@ -104,7 +106,7 @@
       });
     });
   });
-  it("makes a table out of ungrouped triples with the same type", async () => {});
+  it.skip("makes a table out of ungrouped triples with the same type", async () => {});
 });
 
 // describe("equality", () => {
--- 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);
--- a/src/layout/rdf_value.ts	Sat Mar 19 15:45:03 2022 -0700
+++ b/src/layout/rdf_value.ts	Sat Mar 19 16:12:49 2022 -0700
@@ -61,3 +61,33 @@
   });
   return uniques;
 }
+
+// A default dict of Term[] with (NamedNode,NamedNode) pairs as keys.
+//
+// Immutable.Map<Immutable.Record<Uri,Uri>, Immutable.Set<Term>>() didn't seem to work.
+export class UriPairMap {
+  _d = new Map<string, Term[]>();
+
+  _key(k1: NamedNode, k2: NamedNode): string {
+    return k1.id + "|" + k2.id;
+  }
+
+  add(k1: NamedNode, k2: NamedNode, v: Term) {
+    const key = this._key(k1, k2);
+    let cur = this._d.get(key);
+    if (cur === undefined) {
+      cur = [];
+      this._d.set(key, cur);
+    }
+    cur.push(v);
+  }
+
+  get(k1: NamedNode, k2: NamedNode): Term[] {
+    const key = this._key(k1, k2);
+    const v = this._d.get(key);
+    if (v === undefined) {
+      return [];
+    }
+    return v;
+  }
+}