|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount } from 'svelte' |
| 3 | + import * as d3 from 'd3' |
| 4 | + import type { Property, Trait, Theorem } from '@/models' |
| 5 | + import { formula } from '@pi-base/core' |
| 6 | + import * as katex from 'katex' |
| 7 | + import type { Writable } from 'svelte/store' |
| 8 | +
|
| 9 | + export let selected: Writable<Property | Theorem> |
| 10 | + export let property: Property |
| 11 | + export let traits: [Property, Trait][] |
| 12 | + export let theorems: Theorem[] |
| 13 | +
|
| 14 | + function renderLatex(s: string) { |
| 15 | + return s.replaceAll(/\$[^$]+\$/g, p => { |
| 16 | + return (katex as any).default.renderToString(p.replaceAll('$', '')) |
| 17 | + }) |
| 18 | + } |
| 19 | +
|
| 20 | + type DragEvent = d3.D3DragEvent<HTMLElement, unknown, Node> |
| 21 | +
|
| 22 | + type Node = d3.SimulationNodeDatum & { |
| 23 | + property: Property |
| 24 | + trait?: Trait |
| 25 | + degree: number |
| 26 | + pinned?: { x?: number; y?: number } |
| 27 | + } |
| 28 | +
|
| 29 | + type Link = d3.SimulationLinkDatum<Node> & { theorem: Theorem } |
| 30 | +
|
| 31 | + let nodes: Node[] |
| 32 | + let links: Link[] |
| 33 | +
|
| 34 | + let el: SVGSVGElement |
| 35 | +
|
| 36 | + const width = 600 |
| 37 | + const height = 600 |
| 38 | + const bound = 200 |
| 39 | + const textWidth = 500 |
| 40 | +
|
| 41 | + $: { |
| 42 | + // Build graph |
| 43 | + // - one node per property |
| 44 | + // - theorems correspond to edges linking "when" and "then" () |
| 45 | + const inodes: Record<string, Node> = {} |
| 46 | + links = [] |
| 47 | +
|
| 48 | + for (const theorem of theorems) { |
| 49 | + for (const a of formula.properties(theorem.when)) { |
| 50 | + inodes[a.id] ||= { property: a, degree: 0 } |
| 51 | + inodes[a.id].degree++ |
| 52 | +
|
| 53 | + for (const c of formula.properties(theorem.then)) { |
| 54 | + inodes[c.id] ||= { property: c, degree: 0 } |
| 55 | + inodes[c.id].degree++ |
| 56 | +
|
| 57 | + links.push({ |
| 58 | + source: a.id.toString(), |
| 59 | + target: c.id.toString(), |
| 60 | + theorem, |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + for (const [property, trait] of traits) { |
| 67 | + inodes[property.id] ||= { property, degree: 0 } |
| 68 | + inodes[property.id].trait = trait |
| 69 | + inodes[property.id].fx = -1 * bound |
| 70 | + inodes[property.id].pinned = { x: -1 * bound } |
| 71 | + } |
| 72 | +
|
| 73 | + inodes[property.id].fx = bound |
| 74 | + inodes[property.id].fy = 1 |
| 75 | + inodes[property.id].pinned = { x: bound, y: 1 } |
| 76 | +
|
| 77 | + nodes = Object.values(inodes) |
| 78 | + } |
| 79 | +
|
| 80 | + onMount(() => { |
| 81 | + const color = d3.scaleOrdinal(d3.schemeTableau10) |
| 82 | +
|
| 83 | + const simulation = d3 |
| 84 | + .forceSimulation(nodes) |
| 85 | + .force( |
| 86 | + 'link', |
| 87 | + d3.forceLink<Node, Link>(links).id(d => d.property.id.toString()), |
| 88 | + ) |
| 89 | + .force( |
| 90 | + 'charge', |
| 91 | + d3.forceManyBody<Node>().strength(d => -100 * (d.pinned ? 10 : 1)), |
| 92 | + ) |
| 93 | + .force( |
| 94 | + 'x', |
| 95 | + d3.forceX().strength(d => { |
| 96 | + const x = d.x || 0 |
| 97 | + return Math.min((x / bound) * (x / bound), 1) |
| 98 | + }), |
| 99 | + ) |
| 100 | +
|
| 101 | + const svg = d3 |
| 102 | + .select(el) |
| 103 | + .attr('width', width) |
| 104 | + .attr('height', height) |
| 105 | + .attr('viewBox', [-width / 2, -height / 2, width, height]) |
| 106 | + .attr('style', 'max-width: 100%; width: 100%; height: 100%;') |
| 107 | +
|
| 108 | + const text = svg |
| 109 | + .append('g') |
| 110 | + .attr('class', 'labels') |
| 111 | + .selectAll('foreignObject') |
| 112 | + .data(nodes) |
| 113 | + .enter() |
| 114 | + .append('foreignObject') |
| 115 | + .attr( |
| 116 | + 'requiredFeatures', |
| 117 | + 'http://www.w3.org/TR/SVG11/feature#Extensibility', |
| 118 | + ) |
| 119 | + .attr('width', `${textWidth}px`) |
| 120 | + .attr('height', '2em') |
| 121 | +
|
| 122 | + const link = svg |
| 123 | + .append('g') |
| 124 | + .attr('stroke', '#777') |
| 125 | + .attr('stroke-opacity', 0.6) |
| 126 | + .selectAll('path') |
| 127 | + .data(links) |
| 128 | + .join('path') |
| 129 | + .attr('stroke-width', 2) |
| 130 | + .attr('marker-mid', 'url(#arrowhead)') |
| 131 | + .on('mouseover', function () { |
| 132 | + const datum = d3.select(this).datum() as { theorem: Theorem } |
| 133 | + if (datum.theorem) { |
| 134 | + $selected = datum.theorem |
| 135 | + } |
| 136 | + }) |
| 137 | +
|
| 138 | + text |
| 139 | + .append('xhtml:div') |
| 140 | + .html(d => (d.degree > 2 || d.pinned ? renderLatex(d.property.name) : '')) |
| 141 | + .style('width', '100%') |
| 142 | + .style('text-align', 'center') |
| 143 | +
|
| 144 | + const node = svg |
| 145 | + .append('g') |
| 146 | + .selectAll('circle') |
| 147 | + .data(nodes) |
| 148 | + .enter() |
| 149 | + .append('circle') |
| 150 | + .attr('stroke', '#888') |
| 151 | + .attr('stroke-width', d => (d.pinned ? 3 : 1.5)) |
| 152 | + .attr('r', d => { |
| 153 | + if (d.pinned) { |
| 154 | + return 10 |
| 155 | + } else if (d.degree > 2) { |
| 156 | + return 8 |
| 157 | + } else { |
| 158 | + return 5 |
| 159 | + } |
| 160 | + }) |
| 161 | + .attr('fill', d => (d.pinned || d.degree > 2 ? color('1') : color('2'))) |
| 162 | + .on('mouseover', function () { |
| 163 | + const datum = d3.select(this).datum() as { property: Property } |
| 164 | + if (datum.property) { |
| 165 | + $selected = datum.property |
| 166 | + } |
| 167 | + }) |
| 168 | +
|
| 169 | + node.append('title').text(d => d.property.name) |
| 170 | +
|
| 171 | + node.call( |
| 172 | + d3 |
| 173 | + .drag<any, Node, unknown>() |
| 174 | + .on('start', dragstarted) |
| 175 | + .on('drag', dragged) |
| 176 | + .on('end', dragended), |
| 177 | + ) |
| 178 | +
|
| 179 | + simulation.on('tick', () => { |
| 180 | + link.attr('d', ({ source, target }: Link) => { |
| 181 | + const { x: x1 = 0, y: y1 = 0 } = source as Node |
| 182 | + const { x: x2 = 0, y: y2 = 0 } = target as Node |
| 183 | + const mx = (x2 - x1) / 2 + x1 |
| 184 | + const my = (y2 - y1) / 2 + y1 |
| 185 | + return `M${x1},${y1}L${mx},${my}L${x2},${y2}` |
| 186 | + }) |
| 187 | +
|
| 188 | + node.attr('cx', (d: Node) => d.x!).attr('cy', (d: Node) => d.y!) |
| 189 | +
|
| 190 | + text |
| 191 | + .attr('x', (d: Node) => d.x! - textWidth / 2) |
| 192 | + .attr('y', (d: Node) => d.y! + 8) |
| 193 | + }) |
| 194 | +
|
| 195 | + function dragstarted({ active, subject }: DragEvent) { |
| 196 | + if (!active) simulation.alphaTarget(0.3).restart() |
| 197 | +
|
| 198 | + subject.fx = subject.pinned?.x || subject.x |
| 199 | + subject.fy = subject.pinned?.y || subject.y |
| 200 | + } |
| 201 | +
|
| 202 | + function dragged({ subject, x, y }: DragEvent) { |
| 203 | + subject.fx = subject.pinned?.x || x |
| 204 | + subject.fy = subject.pinned?.y || y |
| 205 | + } |
| 206 | +
|
| 207 | + function dragended({ active, subject }: DragEvent) { |
| 208 | + if (!active) simulation.alphaTarget(0) |
| 209 | +
|
| 210 | + subject.fx = subject.pinned?.x |
| 211 | + subject.fy = subject.pinned?.y |
| 212 | + } |
| 213 | + }) |
| 214 | +</script> |
| 215 | + |
| 216 | +<svg xmlns="http://www.w3.org/2000/svg" bind:this={el}> |
| 217 | + <defs> |
| 218 | + <marker |
| 219 | + id="arrowhead" |
| 220 | + markerWidth="3" |
| 221 | + markerHeight="3" |
| 222 | + refX="0" |
| 223 | + refY="1.5" |
| 224 | + orient="auto" |
| 225 | + > |
| 226 | + <polygon points="0,0 3,1.5 0,3" fill="#888" /> |
| 227 | + </marker> |
| 228 | + </defs> |
| 229 | +</svg> |
0 commit comments