# HG changeset patch # User drewp@bigasterisk.com # Date 1647731569 25200 # Node ID 4b33a479dc2fed95183bd08134676d67ecca48e9 # Parent 4822d5621463573b173d1eda06aa9c78e41b235e fix layout test to match new layout return types. clean up UriPairMap diff -r 4822d5621463 -r 4b33a479dc2f src/layout/Layout.test.ts --- 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", () => { diff -r 4822d5621463 -r 4b33a479dc2f src/layout/Layout.ts --- 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; @@ -38,19 +38,10 @@ sections: (AlignedTable | FreeStatements)[]; } -interface ISP { - subj: NamedNode; - pred: NamedNode; -} -const makeSP = Immutable.Record({ - subj: new NamedNode(""), - pred: new NamedNode(""), -}); - class AlignedTableBuilder { subjSet = Immutable.Set(); predSet = Immutable.Set(); - cell = Immutable.Map>(); + 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([])); + 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); diff -r 4822d5621463 -r 4b33a479dc2f src/layout/rdf_value.ts --- 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.Set>() didn't seem to work. +export class UriPairMap { + _d = new Map(); + + _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; + } +}