@buggyorg/graphtools
Advanced tools
Comparing version
@@ -22,2 +22,3 @@ 'use strict'; | ||
exports.insertComponent = insertComponent; | ||
exports.updateComponent = updateComponent; | ||
exports.removeComponent = removeComponent; | ||
@@ -96,2 +97,13 @@ exports.addMetaInformation = addMetaInformation; | ||
/** | ||
* Creates a change set to update a component with a given value | ||
* @param {string} compId The componentId of the component. | ||
* @param {Object} mergeValue An object that contains parts of a component that should be set. | ||
* E.g. `{isType: true}` will update the field `isType` in the component and sets it to `true`. | ||
* @returns {ChangeSet} A change set containing the operation. | ||
*/ | ||
function updateComponent(compId, mergeValue) { | ||
return { type: 'changeSet', operation: 'mergeComponent', query: compId, value: mergeValue }; | ||
} | ||
function removeComponent(id) { | ||
@@ -225,2 +237,9 @@ return { type: 'changeSet', operation: 'remove', query: 'components', filter: function filter(n) { | ||
var applyMergeByComponent = function applyMergeByComponent(graph, cId, value) { | ||
var idx = _lodash2.default.findIndex(graph.components, function (c) { | ||
return Component.id(c) === cId; | ||
}); | ||
return _lodash2.default.merge(graph.components[idx], value); | ||
}; | ||
/** | ||
@@ -266,3 +285,8 @@ * Apply a changeSet on the given graph. | ||
applyMergeByPath(graph, changeSet.query, changeSet.value); | ||
return graph; | ||
} | ||
if (changeSet.operation === 'mergeComponent') { | ||
applyMergeByComponent(graph, changeSet.query, changeSet.value); | ||
return graph; | ||
} | ||
var refs = getReferences(graph, changeSet); | ||
@@ -269,0 +293,0 @@ switch (changeSet.operation) { |
@@ -31,2 +31,3 @@ 'use strict'; | ||
exports.isValid = isValid; | ||
exports.assertValid = assertValid; | ||
exports.createNode = createNode; | ||
@@ -176,2 +177,17 @@ | ||
function assertValid(comp) { | ||
if ((typeof comp === 'undefined' ? 'undefined' : _typeof(comp)) !== 'object') { | ||
throw new Error('Component is not an object, but it is: ' + comp); | ||
} | ||
if (typeof comp.componentId !== 'string' || comp.componentId.length === 0) { | ||
throw new Error('Component must have a valid id (string with at least one character), but it is: ' + comp.componentId); | ||
} | ||
if (ports(comp).length === 0) { | ||
throw new Error('Component "' + id(comp) + '" must have at least one port.'); | ||
} | ||
if (typeof comp.version !== 'string' || !_semver2.default.valid(comp.version)) { | ||
throw new Error('Component "' + id(comp) + '" must have a valid version, but it is: ' + comp.version); | ||
} | ||
} | ||
/** | ||
@@ -178,0 +194,0 @@ * Create a node from a component. |
@@ -37,3 +37,5 @@ 'use strict'; | ||
/** @module Conversion */ | ||
/** @module Conversion | ||
* We might not need this....? | ||
*/ | ||
@@ -50,3 +52,3 @@ function mapToGrlibPort(port) { | ||
outputPorts: (0, _lodash2.default)(Node.outputPorts(node)).map(mapToGrlibPort).fromPairs().value(), | ||
id: node.meta, | ||
id: node.componentId, | ||
branchPath: node.id | ||
@@ -60,3 +62,3 @@ }, _lodash2.default.omit(node, ['id', 'ports'])) | ||
v: edge.from, w: edge.to, | ||
label: edge.from + '@' + edge.outPort + '→' + edge.to + '@' + edge.inPort, | ||
label: edge.from + '@' + edge.outPort + '\u2192' + edge.to + '@' + edge.inPort, | ||
value: { outPort: edge.outPort, inPort: edge.inPort } | ||
@@ -93,3 +95,3 @@ }; | ||
ports: mapToPortNodePorts(node.value), | ||
meta: node.value.id | ||
componentId: node.value.id | ||
}); | ||
@@ -100,3 +102,3 @@ } | ||
if (node.v.indexOf('defco_') === 0) { | ||
return CS.insertComponent(_lodash2.default.merge({}, replacePorts(node), { meta: node.v.slice('defco_'.length) })); | ||
return CS.insertComponent(_lodash2.default.merge({}, replacePorts(node), { componentId: node.v.slice('defco_'.length) })); | ||
} | ||
@@ -103,0 +105,0 @@ return CS.insertNode(_lodash2.default.merge({ id: node.v }, replacePorts(node))); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.removeComponent = exports.addComponent = exports.hasComponent = exports.component = undefined; | ||
exports.updateComponent = exports.removeComponent = exports.addComponent = exports.hasComponent = exports.component = undefined; | ||
exports.components = components; | ||
@@ -19,2 +19,6 @@ exports.componentIds = componentIds; | ||
var _omit = require('lodash/fp/omit'); | ||
var _omit2 = _interopRequireDefault(_omit); | ||
var _component = require('../component'); | ||
@@ -84,5 +88,7 @@ | ||
throw new Error('Cannot add undefined component to graph.'); | ||
} else if (!Component.isValid(comp)) { | ||
throw new Error('Cannot add invalid component to graph. Are you missing the component-id, the version or a port?\nComponent: ' + JSON.stringify(comp)); | ||
} | ||
/* else if (!Component.isValid(comp)) { | ||
throw new Error('Cannot add invalid component to graph. Are you missing the component-id, the version or a port?\nComponent: ' + JSON.stringify(comp)) | ||
} */ | ||
Component.assertValid(comp); | ||
} | ||
@@ -94,4 +100,4 @@ | ||
* @description Add a component to the graph. [Performance O(|V| + |E|)] | ||
* @param {Component} comp The component object that should be added. | ||
* @param {PortGraph} graph The graph. | ||
* @param {Component} comp The component object that should be added. | ||
* @returns {PortGraph} A new graph that includes the component. | ||
@@ -111,4 +117,4 @@ */ | ||
* @description Removes a component from the graph. [Performance O(|V| + |E|)] | ||
* @param {Component|string} comp The component that shall be removed, either the component object or the component id. | ||
* @param {PortGraph} graph The graph. | ||
* @param {Component|string} comp The component that shall be removed, either the component object or the component id. | ||
* @returns {PortGraph} A new graph without the given component. | ||
@@ -118,2 +124,17 @@ */ | ||
return changeSet.applyChangeSet(graph, changeSet.removeComponent(Component.id(comp))); | ||
}); | ||
/** | ||
* @function | ||
* @name updateComponent | ||
* @description Update an existing component in the graph. | ||
* @param {Component|string} comp The component that will be update, either the component object or the component id. | ||
* @param {Object} merge Updated values of the component. It is not possible to change the component id with this method. | ||
* @param {PortGraph} graph The graph | ||
*/ | ||
var updateComponent = exports.updateComponent = (0, _curry2.default)(function (comp, merge, graph) { | ||
if (!hasComponent(comp, graph)) { | ||
throw new Error('Cannot update non existing component: "' + Component.id(comp) + '"'); | ||
} | ||
return changeSet.applyChangeSet(graph, changeSet.updateComponent(Component.id(comp), (0, _omit2.default)('componentId', merge))); | ||
}); |
@@ -341,3 +341,3 @@ 'use strict'; | ||
// remove node in its compound and replace the graphs on the path | ||
return replaceNode(basePath, removeNode.apply(undefined, [(0, _compoundPath.rest)(path), parentGraph].concat(cb)), graph); | ||
return replaceNode(basePath, removeNodeInternal.apply(undefined, [(0, _compoundPath.rest)(path), deleteEdges, parentGraph].concat(cb)), graph); | ||
}); | ||
@@ -344,0 +344,0 @@ |
@@ -158,3 +158,3 @@ 'use strict'; | ||
function hierarchy(graph, node) { | ||
var h = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var h = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
@@ -191,9 +191,9 @@ return node ? hierarchy(graph, graph.parent(node), _lodash2.default.concat([node], h)) : h; | ||
function linkName(link) { | ||
var portNames = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; | ||
var portNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
var value = link.value || {}; | ||
if (portNames) { | ||
return '[' + link.v + '@' + value.outPort + '→' + link.w + '@' + value.inPort + ']'; | ||
return '[' + link.v + '@' + value.outPort + '\u2192' + link.w + '@' + value.inPort + ']'; | ||
} else { | ||
return '[' + link.v + '→' + link.w + ']'; | ||
return '[' + link.v + '\u2192' + link.w + ']'; | ||
} | ||
@@ -200,0 +200,0 @@ } |
{ | ||
"name": "@buggyorg/graphtools", | ||
"version": "0.4.0-pre.9", | ||
"version": "0.4.0-pre.10", | ||
"description": "Tools for processing buggy graphs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/api.js", |
@@ -52,2 +52,13 @@ /** @module ChangeSet | ||
/** | ||
* Creates a change set to update a component with a given value | ||
* @param {string} compId The componentId of the component. | ||
* @param {Object} mergeValue An object that contains parts of a component that should be set. | ||
* E.g. `{isType: true}` will update the field `isType` in the component and sets it to `true`. | ||
* @returns {ChangeSet} A change set containing the operation. | ||
*/ | ||
export function updateComponent (compId, mergeValue) { | ||
return {type: 'changeSet', operation: 'mergeComponent', query: compId, value: mergeValue} | ||
} | ||
export function removeComponent (id) { | ||
@@ -175,2 +186,7 @@ return {type: 'changeSet', operation: 'remove', query: 'components', filter: (n) => Component.equal(n, id)} | ||
const applyMergeByComponent = (graph, cId, value) => { | ||
var idx = _.findIndex(graph.components, (c) => Component.id(c) === cId) | ||
return _.merge(graph.components[idx], value) | ||
} | ||
/** | ||
@@ -214,3 +230,8 @@ * Apply a changeSet on the given graph. | ||
applyMergeByPath(graph, changeSet.query, changeSet.value) | ||
return graph | ||
} | ||
if (changeSet.operation === 'mergeComponent') { | ||
applyMergeByComponent(graph, changeSet.query, changeSet.value) | ||
return graph | ||
} | ||
var refs = getReferences(graph, changeSet) | ||
@@ -217,0 +238,0 @@ switch (changeSet.operation) { |
@@ -133,2 +133,17 @@ /** | ||
export function assertValid (comp) { | ||
if (typeof (comp) !== 'object') { | ||
throw new Error('Component is not an object, but it is: ' + comp) | ||
} | ||
if (typeof (comp.componentId) !== 'string' || comp.componentId.length === 0) { | ||
throw new Error('Component must have a valid id (string with at least one character), but it is: ' + comp.componentId) | ||
} | ||
if (ports(comp).length === 0) { | ||
throw new Error('Component "' + id(comp) + '" must have at least one port.') | ||
} | ||
if (typeof (comp.version) !== 'string' || !semver.valid(comp.version)) { | ||
throw new Error('Component "' + id(comp) + '" must have a valid version, but it is: ' + comp.version) | ||
} | ||
} | ||
/** | ||
@@ -135,0 +150,0 @@ * Create a node from a component. |
import find from 'lodash/fp/find' | ||
import curry from 'lodash/fp/curry' | ||
import omit from 'lodash/fp/omit' | ||
import * as Component from '../component' | ||
@@ -58,5 +59,7 @@ import * as changeSet from '../changeSet' | ||
throw new Error('Cannot add undefined component to graph.') | ||
} else if (!Component.isValid(comp)) { | ||
} | ||
/* else if (!Component.isValid(comp)) { | ||
throw new Error('Cannot add invalid component to graph. Are you missing the component-id, the version or a port?\nComponent: ' + JSON.stringify(comp)) | ||
} | ||
} */ | ||
Component.assertValid(comp) | ||
} | ||
@@ -68,4 +71,4 @@ | ||
* @description Add a component to the graph. [Performance O(|V| + |E|)] | ||
* @param {Component} comp The component object that should be added. | ||
* @param {PortGraph} graph The graph. | ||
* @param {Component} comp The component object that should be added. | ||
* @returns {PortGraph} A new graph that includes the component. | ||
@@ -85,4 +88,4 @@ */ | ||
* @description Removes a component from the graph. [Performance O(|V| + |E|)] | ||
* @param {Component|string} comp The component that shall be removed, either the component object or the component id. | ||
* @param {PortGraph} graph The graph. | ||
* @param {Component|string} comp The component that shall be removed, either the component object or the component id. | ||
* @returns {PortGraph} A new graph without the given component. | ||
@@ -93,1 +96,17 @@ */ | ||
}) | ||
/** | ||
* @function | ||
* @name updateComponent | ||
* @description Update an existing component in the graph. | ||
* @param {Component|string} comp The component that will be update, either the component object or the component id. | ||
* @param {Object} merge Updated values of the component. It is not possible to change the component id with this method. | ||
* @param {PortGraph} graph The graph | ||
*/ | ||
export const updateComponent = curry((comp, merge, graph) => { | ||
if (!hasComponent(comp, graph)) { | ||
throw new Error('Cannot update non existing component: "' + Component.id(comp) + '"') | ||
} | ||
return changeSet.applyChangeSet(graph, | ||
changeSet.updateComponent(Component.id(comp), omit('componentId', merge))) | ||
}) |
@@ -281,3 +281,3 @@ | ||
// remove node in its compound and replace the graphs on the path | ||
return replaceNode(basePath, removeNode(pathRest(path), parentGraph, ...cb), graph) | ||
return replaceNode(basePath, removeNodeInternal(pathRest(path), deleteEdges, parentGraph, ...cb), graph) | ||
}) | ||
@@ -284,0 +284,0 @@ |
@@ -115,2 +115,11 @@ /* eslint-env mocha */ | ||
}) | ||
it('can update a component in the graph', () => { | ||
var graph = changeSet.applyChangeSets(Graph.empty(), [ | ||
changeSet.insertComponent({componentId: 'A'}), | ||
changeSet.updateComponent('A', {isType: true}) | ||
]) | ||
expect(graph.components[0]).to.have.property('isType') | ||
expect(graph.components[0].isType).to.be.true | ||
}) | ||
}) |
@@ -7,2 +7,3 @@ /* global describe, it */ | ||
import * as Node from '../src/node' | ||
import _ from 'lodash' | ||
@@ -147,2 +148,26 @@ var expect = chai.expect | ||
}) | ||
it('Can replace nodes in compounds without affecting their edges', () => { | ||
var cmp = Graph.flow( | ||
Graph.addNode({ | ||
name: 'Source', | ||
ports: [ | ||
{ port: 'out', kind: 'output', type: 'generic' } | ||
] | ||
}), | ||
Graph.addNode({ | ||
name: 'Sink', | ||
ports: [ | ||
{ port: 'in', kind: 'input', type: 'number' } | ||
] | ||
}), | ||
Graph.addEdge({ from: 'Source@out', to: 'Sink@in' }) | ||
)(Graph.compound({ })) | ||
var graph = Graph.addNode(cmp, Graph.empty()) | ||
expect(Graph.edges(graph)).to.have.length(1) | ||
var node = Graph.nodesDeepBy((n) => n.name === 'Source', graph)[0] | ||
var newNode = _.cloneDeep(node) | ||
var newGraph = Graph.replaceNode(node, newNode, graph) | ||
expect(Graph.edges(newGraph)).to.have.length(1) | ||
}) | ||
}) |
2010862
3.83%155
9.93%10847
3.71%