@buggyorg/rewrite

Tools for replacing parts of a graph.

Usage no npm install needed!

<script type="module">
  import buggyorgRewrite from 'https://cdn.skypack.dev/@buggyorg/rewrite';
</script>

README

Rewrite

Tools for replacing parts of a graph.

Usage

The generic replacement method is


g1 = API.apply(g, set, matcher, generator)
  • g is a graph
  • set is an arbitrary set of candidates for the matcher
  • matcher takes a candidate and the graph and returns false iff there is no match, otherwise the return value is arbitrary
  • generator takes the return value of the matcher and the graph and returns a new graph

A few candidate sets are provided:


g1 = API.applyNode(matcher, generator)(g)
g2 = API.applyEdge(matcher, generator)(g)
g3 = API.applyPort(matcher, generator)(g)
g4 = API.applyComponent(matcher, generator)(g)

These replacement rules can be iterated like this:


// matches nothing
const rule1 = API.applyNode((node, g) => false, (match, g) => null)
// matches everything, does nothing
const ruleTrivial = API.applyNode((node, g) => true, (match, g) => g)
const rewriter = API.rewrite([rule1, rule2], maxIterations)
g1 = rewriter(g)

where maxIterations is optional (default: Infinity)

Written with StackEdit.