@buggyorg/graphtools
Advanced tools
Comparing version 0.2.21 to 0.2.22
@@ -112,2 +112,3 @@ 'use strict'; | ||
} | ||
var parent = graph.parent(elem); | ||
var _iteratorNormalCompletion3 = true; | ||
@@ -121,2 +122,5 @@ var _didIteratorError3 = false; | ||
if (parent !== graph.parent(succ)) { | ||
continue; | ||
} | ||
if (blockedForward(succ, graph, topsort, last)) { | ||
@@ -150,2 +154,3 @@ return true; | ||
} | ||
var parent = graph.parent(elem); | ||
var _iteratorNormalCompletion4 = true; | ||
@@ -159,2 +164,5 @@ var _didIteratorError4 = false; | ||
if (parent !== graph.parent(pred)) { | ||
continue; | ||
} | ||
if (blockedBackward(pred, graph, topsort, first)) { | ||
@@ -273,2 +281,7 @@ return true; | ||
var graph = _graphlib2.default.json.read(JSON.parse(JSON.stringify(_graphlib2.default.json.write(g)))); | ||
_lodash2.default.each(graph.edges(), function (e) { | ||
if (_api.utils.isContinuation(graph, e)) { | ||
graph.removeEdge(e); | ||
} | ||
}); | ||
if (!sameParents(graph, subset) || !graph.isCompound() || !contains(graph, subset)) { | ||
@@ -278,3 +291,3 @@ return false; | ||
markNodes(graph, subset); | ||
var topsort = _graphlib2.default.alg.topsort(graph); | ||
var topsort = _api.graph.topoSort(graph); | ||
var first = firstMarkedIndex(graph, topsort); | ||
@@ -305,3 +318,3 @@ var last = lastMarkedIndex(graph, topsort); | ||
} | ||
var graph = _graphlib2.default.json.read(JSON.parse(JSON.stringify(_graphlib2.default.json.write(g)))); | ||
var graph = _api.graph.clone(g); | ||
if (!name) { | ||
@@ -308,0 +321,0 @@ name = 'comp' + (0, _objectHash2.default)(graph); |
@@ -8,2 +8,4 @@ 'use strict'; | ||
exports.clone = clone; | ||
exports.removeContinuations = removeContinuations; | ||
exports.topoSort = topoSort; | ||
@@ -18,2 +20,6 @@ var _fs = require('fs'); | ||
var _utils = require('./utils'); | ||
var utils = _interopRequireWildcard(_utils); | ||
var _lodash = require('lodash'); | ||
@@ -23,2 +29,4 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -93,2 +101,33 @@ | ||
} | ||
} | ||
/** | ||
* Removes all continuations from a graph (only for debug purposes) | ||
* @param {Graphlib} graph The graph | ||
* @returns {Graphlib} A graph that has no continuations edges | ||
*/ | ||
function removeContinuations(graph) { | ||
var tGraph = clone(graph); | ||
_lodash2.default.each(tGraph.edges(), function (e) { | ||
if (utils.isContinuation(tGraph, e)) { | ||
tGraph.removeEdge(e); | ||
} | ||
}); | ||
return tGraph; | ||
} | ||
/** | ||
* Returns a topological sorting of the graph. Removes all continuations before calculating the topological sorting. | ||
* @param {Graphlib} graph The graph. | ||
* @return {string[]} A sorting of the nodes. | ||
* @throws {Error} If the graph has loops. | ||
*/ | ||
function topoSort(graph) { | ||
try { | ||
var tGraph = removeContinuations(graph); | ||
return _graphlib2.default.alg.topsort(tGraph); | ||
} catch (err) { | ||
_fs2.default.writeFileSync('test.json', JSON.stringify(_graphlib2.default.json.write(tGraph))); | ||
throw Error('[topoSort] Cannot calculate toplogical sorting, graph contains loop.\n' + JSON.stringify(_graphlib2.default.alg.findCycles(tGraph))); | ||
} | ||
} |
@@ -6,3 +6,2 @@ 'use strict'; | ||
}); | ||
exports.finalize = exports.edit = exports.clone = undefined; | ||
exports.prefixName = prefixName; | ||
@@ -29,7 +28,4 @@ exports.isNPG = isNPG; | ||
exports.portDirectionType = portDirectionType; | ||
exports.isContinuation = isContinuation; | ||
var _graph = require('./graph'); | ||
var graphAPI = _interopRequireWildcard(_graph); | ||
var _lodash = require('lodash'); | ||
@@ -41,4 +37,2 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
/** | ||
@@ -50,29 +44,2 @@ * A link is a connection between two nodes that can extend over multiple compound nodes. But it can never leave a recursion. | ||
/** | ||
* Creates a new graph that has the exact same nodes and edges. | ||
* @deprecated Moved to `graph.clone`. Will be removed in the next minor release (0.3.0). | ||
* @param {Graphlib} graph The graph to clone | ||
* @returns {Graphlib} A clone of the input graph. | ||
*/ | ||
/** @module utils */ | ||
var clone = exports.clone = graphAPI.clone; | ||
/** | ||
* Returns the pure JSON representation of the graph without all the graphlib features. | ||
* @deprecated Moved and renamed to `graph.toJSON`. Will be removed in the next minor release (0.3.0). | ||
* @param {Graphlib} graph The graph in graphlib format to convert | ||
* @returns {Object} A JSON representation of the graph. | ||
*/ | ||
var edit = exports.edit = graphAPI.toJSON; | ||
/** | ||
* Parses the pure JSON format to return a graphlib version of the graph. | ||
* @deprecated Moved and renamed to `graph.toJSON`. Will be removed in the next minor release (0.3.0). | ||
* @param {Object} editGraph A JSON representation (e.g. created by edit) of a graph. | ||
* @returns {Graphlib} A graphlib graph of the editGraph | ||
*/ | ||
var finalize = exports.finalize = graphAPI.importJSON; | ||
/** | ||
* Applies the name prefixing for e.g. path names or similar stuff. | ||
@@ -92,2 +59,5 @@ * @param {String} prefix The prefix for the name. | ||
*/ | ||
/** @module utils */ | ||
function isNPG(graph) { | ||
@@ -312,2 +282,12 @@ return !isNG(graph); | ||
throw new Error('The node ' + node + ' does not have a port with the name ' + port); | ||
} | ||
/** | ||
* Returns whether the edge is a continuation link or not | ||
* @param {Graphlib} graph The graph. | ||
* @param {edge} edge The edge to test. | ||
* @return True if the edge is a continuation link, false otherwise. | ||
*/ | ||
function isContinuation(graph, edge) { | ||
return graph.edge(edge) && graph.edge(edge).continuation; | ||
} |
{ | ||
"name": "@buggyorg/graphtools", | ||
"version": "0.2.21", | ||
"version": "0.2.22", | ||
"description": "Tools for processing buggy graphs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/api.js", |
import graphlib from 'graphlib' | ||
import _ from 'lodash' | ||
import hash from 'object-hash' | ||
import { utils } from './api' | ||
import { utils, graph as graphAPI } from './api' | ||
@@ -45,3 +45,5 @@ var markNodes = function (graph, subset) { | ||
if (graph.node(elem).mark) { return true } | ||
var parent = graph.parent(elem) | ||
for (let succ of graph.successors(elem)) { | ||
if (parent !== graph.parent(succ)) { continue } | ||
if (blockedForward(succ, graph, topsort, last)) { return true } | ||
@@ -55,3 +57,5 @@ } | ||
if (graph.node(elem).mark) { return true } | ||
var parent = graph.parent(elem) | ||
for (let pred of graph.predecessors(elem)) { | ||
if (parent !== graph.parent(pred)) { continue } | ||
if (blockedBackward(pred, graph, topsort, first)) { return true } | ||
@@ -85,5 +89,10 @@ } | ||
var graph = graphlib.json.read(JSON.parse(JSON.stringify(graphlib.json.write(g)))) | ||
_.each(graph.edges(), (e) => { | ||
if (utils.isContinuation(graph, e)) { | ||
graph.removeEdge(e) | ||
} | ||
}) | ||
if (!sameParents(graph, subset) || !graph.isCompound() || !contains(graph, subset)) { return false } | ||
markNodes(graph, subset) | ||
var topsort = graphlib.alg.topsort(graph) | ||
var topsort = graphAPI.topoSort(graph) | ||
var first = firstMarkedIndex(graph, topsort) | ||
@@ -108,3 +117,3 @@ var last = lastMarkedIndex(graph, topsort) | ||
if (!isCompoundable(g, subset)) { throw new Error('This subset cannot be compoundified given this particular subset.') } | ||
var graph = graphlib.json.read(JSON.parse(JSON.stringify(graphlib.json.write(g)))) | ||
var graph = graphAPI.clone(g) | ||
if (!name) { | ||
@@ -111,0 +120,0 @@ name = 'comp' + hash(graph) |
import fs from 'fs' | ||
import graphlib from 'graphlib' | ||
import * as utils from './utils' | ||
import _ from 'lodash' | ||
@@ -74,1 +75,32 @@ | ||
} | ||
/** | ||
* Removes all continuations from a graph (only for debug purposes) | ||
* @param {Graphlib} graph The graph | ||
* @returns {Graphlib} A graph that has no continuations edges | ||
*/ | ||
export function removeContinuations (graph) { | ||
var tGraph = clone(graph) | ||
_.each(tGraph.edges(), (e) => { | ||
if (utils.isContinuation(tGraph, e)) { | ||
tGraph.removeEdge(e) | ||
} | ||
}) | ||
return tGraph | ||
} | ||
/** | ||
* Returns a topological sorting of the graph. Removes all continuations before calculating the topological sorting. | ||
* @param {Graphlib} graph The graph. | ||
* @return {string[]} A sorting of the nodes. | ||
* @throws {Error} If the graph has loops. | ||
*/ | ||
export function topoSort (graph) { | ||
try { | ||
var tGraph = removeContinuations(graph) | ||
return graphlib.alg.topsort(tGraph) | ||
} catch (err) { | ||
fs.writeFileSync('test.json', JSON.stringify(graphlib.json.write(tGraph))) | ||
throw Error('[topoSort] Cannot calculate toplogical sorting, graph contains loop.\n' + JSON.stringify(graphlib.alg.findCycles(tGraph))) | ||
} | ||
} |
/** @module utils */ | ||
import * as graphAPI from './graph' | ||
import _ from 'lodash' | ||
@@ -13,26 +12,2 @@ | ||
/** | ||
* Creates a new graph that has the exact same nodes and edges. | ||
* @deprecated Moved to `graph.clone`. Will be removed in the next minor release (0.3.0). | ||
* @param {Graphlib} graph The graph to clone | ||
* @returns {Graphlib} A clone of the input graph. | ||
*/ | ||
export const clone = graphAPI.clone | ||
/** | ||
* Returns the pure JSON representation of the graph without all the graphlib features. | ||
* @deprecated Moved and renamed to `graph.toJSON`. Will be removed in the next minor release (0.3.0). | ||
* @param {Graphlib} graph The graph in graphlib format to convert | ||
* @returns {Object} A JSON representation of the graph. | ||
*/ | ||
export const edit = graphAPI.toJSON | ||
/** | ||
* Parses the pure JSON format to return a graphlib version of the graph. | ||
* @deprecated Moved and renamed to `graph.toJSON`. Will be removed in the next minor release (0.3.0). | ||
* @param {Object} editGraph A JSON representation (e.g. created by edit) of a graph. | ||
* @returns {Graphlib} A graphlib graph of the editGraph | ||
*/ | ||
export const finalize = graphAPI.importJSON | ||
/** | ||
* Applies the name prefixing for e.g. path names or similar stuff. | ||
@@ -258,1 +233,11 @@ * @param {String} prefix The prefix for the name. | ||
} | ||
/** | ||
* Returns whether the edge is a continuation link or not | ||
* @param {Graphlib} graph The graph. | ||
* @param {edge} edge The edge to test. | ||
* @return True if the edge is a continuation link, false otherwise. | ||
*/ | ||
export function isContinuation (graph, edge) { | ||
return graph.edge(edge) && graph.edge(edge).continuation | ||
} |
1498087
12991