Mercurial > code > home > repos > streamed-graph
annotate src/suffixLabels.ts @ 82:43e016fa302e
client now works again, reads graph events
author | drewp@bigasterisk.com |
---|---|
date | Wed, 17 Nov 2021 15:37:45 -0800 |
parents | 8b4dc9e87b56 |
children | 26c55d5d5202 |
rev | line source |
---|---|
36 | 1 import { Term } from "n3"; |
11 | 2 |
36 | 3 type SuffixesNode = { usedBy?: string; children: Map<string, SuffixesNode> }; |
4 type DisplayNode = { label?: string; link?: string }; | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
5 class SuffixLabels { |
36 | 6 displayNodes: Map<string, DisplayNode>; |
7 usedSuffixes: SuffixesNode; | |
8 constructor() { | |
9 this.displayNodes = new Map(); | |
10 this.usedSuffixes = { usedBy: undefined, children: new Map() }; | |
11 } | |
12 | |
13 planDisplayForNode(node: Term) { | |
14 const uri = node.value; | |
15 this._planDisplayForUri(uri); | |
16 } | |
17 | |
18 _planDisplayForUri(uri: string) { | |
19 if (this.displayNodes.has(uri)) { | |
20 return; | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
21 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
36 | 23 const segments = uri.split("/"); |
24 let curs = this.usedSuffixes; | |
25 let label: string | undefined = undefined; | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
26 |
36 | 27 for (let i = segments.length - 1; i >= 0; i--) { |
28 const seg = segments[i]; | |
29 if (curs.usedBy && curs.usedBy != uri) { | |
30 this._prependClashingUri(curs); | |
31 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
36 | 33 if (!curs.children.has(seg)) { |
34 const child: SuffixesNode = { usedBy: undefined, children: new Map() }; | |
35 curs.children.set(seg, child); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
36 |
36 | 37 if (label === undefined) { |
38 label = SuffixLabels._tailSegments(uri, segments.length - i); | |
39 child.usedBy = uri; | |
40 } | |
41 } | |
42 curs = curs.children.get(seg)!; | |
43 } | |
44 this.displayNodes.set(uri, { label: label }); | |
45 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
46 |
36 | 47 _prependClashingUri(curs: SuffixesNode) { |
48 // Claim: When a clash is discovered, only 1 uri needs to | |
49 // change its length, and there will be only one child node to | |
50 // follow, and the clashing uri can be changed to prepend that | |
51 // one child (since we'll see it again if that one wasn't | |
52 // enough). | |
53 const clashNode: DisplayNode = this.displayNodes.get(curs.usedBy!)!; | |
54 const nextLeftSeg = curs.children.entries().next().value; | |
55 if (nextLeftSeg[1].usedBy) { | |
56 throw new Error("unexpected"); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
57 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
58 |
36 | 59 clashNode.label = nextLeftSeg[0] + "/" + clashNode.label; |
60 nextLeftSeg[1].usedBy = curs.usedBy; | |
61 curs.usedBy = undefined; | |
62 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
63 |
36 | 64 // a substring to show for this uri |
65 getLabelForNode(node: string) { | |
66 return this.displayNodes.get(node)!.label; | |
67 } | |
68 | |
69 static _tailSegments(uri: string, n: number) { | |
70 let i = uri.length; | |
71 for (let rep = 0; rep < n; rep++) { | |
72 i = uri.lastIndexOf("/", i - 1); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
73 } |
36 | 74 return uri.substr(i + 1); |
75 } | |
76 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
77 |
36 | 78 export { SuffixLabels }; |