Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@buggyorg/dynatype-network-graph

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@buggyorg/dynatype-network-graph - npm Package Compare versions

Comparing version 0.1.10 to 0.1.12

test/fixtures/map_recursive2_result.png

252

lib/dynatype-network.js

@@ -32,2 +32,3 @@ 'use strict';

var utils = graphtools.utils;

@@ -83,3 +84,6 @@ function isInPort(node) {

function entangleType(type, template) {
if (template[0] === '[' && template[template.length - 1] === ']') {
if (template && template[0] === '[' && template[template.length - 1] === ']') {
if ((typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type === 'type-ref') {
return _lodash2.default.merge({}, type, { template: template });
}
return type.replace(/\[/g, '').replace(/\]/g, '');

@@ -93,2 +97,5 @@ } else {

if (template[0] === '[' && template[template.length - 1] === ']') {
if ((typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type === 'type-ref') {
return _lodash2.default.merge({}, type, { template: template });
}
return '[' + type + ']';

@@ -234,18 +241,31 @@ } else {

function replaceGenerics(graph) {
graph = replaceGenericsInternal(graph);
var typeRefs = determineTypeReferences(graph);
applyKnownTypeRefs(graph, typeRefs);
replaceUnkownTypeReferences(graph);
graph = replaceGenericsInternal(graph);
replaceGenericFunctionTypes(graph);
replaceGenericTypeRefs(graph);
return graph;
}
function replaceGenericsInternal(graph) {
var processGraph = replaceTypeHints(graph);
var nodes = processGraph.nodes();
for (var j = 0; j < nodes.length; j++) {
var paths = replaceGenericInput(graph, nodes[j]);
var paths = replaceGenericInput(processGraph, nodes[j]);
var pathsToReplace = [];
var validType = null;
for (var i = 0; i < paths.length; i++) {
var currentPath = paths[i];
var type = processGraph.node(currentPath[0].node).outputPorts[currentPath[0].port] || processGraph.node(currentPath[0].node).inputPorts[currentPath[0].port];
if (type === 'generic') {
if (isGeneric(type)) {
pathsToReplace = pathsToReplace.concat([currentPath]);
} else {
var validType = type;
validType = type;
// finds the type backwards
replacePathGenerics(processGraph, currentPath, type);
}
}
if (validType === undefined && pathsToReplace.length !== 0) {
if (!validType && pathsToReplace.length !== 0) {
var secondInputs = processGraph.node(nodes[j]).inputPorts;

@@ -255,2 +275,7 @@ var keys = Object.keys(secondInputs);

if (secondInputs[keys[_i]] !== 'generic') {
// takes the type of another input
if (validType !== undefined && validType !== secondInputs[keys[_i]]) {
var error = 'Type mismatch: Two pathes to node ' + nodes[j] + ' have different types: ' + validType + ' and ' + secondInputs[keys[_i]] + '.';
throw new Error(error);
}
validType = secondInputs[keys[_i]];

@@ -260,4 +285,4 @@ }

}
if (validType === undefined && pathsToReplace.length !== 0) {
paths = replaceGenericOutput(graph, nodes[j]);
if (!validType) {
paths = replaceGenericOutput(processGraph, nodes[j]);
for (var _i2 = 0; _i2 < paths.length; _i2++) {

@@ -267,16 +292,20 @@ currentPath = _lodash2.default.filter(paths[_i2], function (node) {

});
type = processGraph.node(currentPath[currentPath.length - 1].node).inputPorts[currentPath[currentPath.length - 1].port] || processGraph.node(currentPath[currentPath.length - 1].node).outputPorts[currentPath[currentPath.length - 1].port];
var lastPathItem = _lodash2.default.last(currentPath);
type = utils.portType(processGraph, lastPathItem.node, lastPathItem.port);
if (type === 'generic') {
pathsToReplace = pathsToReplace.concat([currentPath]);
} else {
} else if (!isTypeRef(type)) {
validType = type;
replacePathGenerics(processGraph, currentPath, type);
// finds the type forwards
replacePathGenericsForward(processGraph, currentPath, validType);
}
}
}
if (validType === undefined && pathsToReplace.length !== 0) {
if (!validType && pathsToReplace.length !== 0) {
throw new Error('Generics could not be replaced: No type found.');
}
for (var p = 0; p < pathsToReplace.length; p++) {
replacePathGenerics(processGraph, pathsToReplace[p], validType, pathsToReplace[p][0].port);
// takes the type of the second input
replacePathGenericsForward(processGraph, pathsToReplace[p], validType);
}

@@ -287,6 +316,159 @@ }

function replacePathGenerics(graph, path, type, firstPort) {
function isTypeRef(type) {
return (typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type === 'type-ref';
}
function isFunction(type) {
return (typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type === 'function';
}
function determineTypeReferences(graph) {
return (0, _lodash2.default)(graph.edges()).map(function (e) {
return _lodash2.default.merge({}, e, { value: graph.edge(e) });
}).filter(function (e) {
return isTypeRef(utils.portType(graph, e.v, e.value.outPort)) && !isTypeRef(utils.portType(graph, e.w, e.value.inPort)) || isTypeRef(utils.portType(graph, e.w, e.value.inPort)) && !isTypeRef(utils.portType(graph, e.v, e.value.outPort));
}).reject(function (e) {
return isGeneric(utils.portType(graph, e.v, e.value.outPort)) || isGeneric(utils.portType(graph, e.w, e.value.inPort));
}).map(function (e) {
var vTypeNode = isTypeRef(utils.portType(graph, e.v, e.value.outPort));
return {
refNode: vTypeNode ? e.v : e.w,
refPort: vTypeNode ? e.value.outPort : e.value.inPort,
reference: vTypeNode ? utils.portType(graph, e.v, e.value.outPort) : utils.portType(graph, e.w, e.value.inPort),
type: vTypeNode ? utils.portType(graph, e.w, e.value.inPort) : utils.portType(graph, e.v, e.value.outPort)
};
}).value();
}
function applyKnownTypeRefs(graph, typeRefs) {
(0, _lodash2.default)(typeRefs).each(function (r) {
var node = graph.node(r.refNode);
var oNode = graph.node(r.reference.node);
node[utils.portDirectionType(graph, r.refNode, r.refPort)][r.refPort] = r.type;
oNode[utils.portDirectionType(graph, r.reference.node, r.reference.port)][r.reference.port] = entangleType(r.type, r.reference.template);
graph.setNode(r.refNode, _lodash2.default.cloneDeep(node));
graph.setNode(r.reference.node, _lodash2.default.cloneDeep(oNode));
});
}
function replaceUnkownTypeReferences(graph) {
(0, _lodash2.default)(graph.nodes()).map(function (n) {
return graph.node(n);
}).each(function (n) {
replacePortReferences(graph, n, 'inputPorts');
replacePortReferences(graph, n, 'outputPorts');
});
}
function replaceGenericFunctionTypes(graph) {
(0, _lodash2.default)(graph.nodes()).map(function (n) {
return graph.node(n);
}).each(function (n) {
replaceFunctionPorts(graph, n, 'inputPorts');
replaceFunctionPorts(graph, n, 'outputPorts');
});
}
function replaceGenericTypeRefs(graph) {
(0, _lodash2.default)(graph.nodes()).map(function (n) {
return graph.node(n);
}).each(function (n) {
if (n.genericType && n.genericType.type === 'type-ref') {
n.genericType = resolveTypeReference(graph, n.genericType);
} else if (n.genericType && n.genericType.type === 'function') {
n.genericType = replaceFunctionTypeReferences(graph, n.genericType);
}
});
}
function replaceFunctionPorts(graph, node, portType) {
node[portType] = _lodash2.default.mapValues(node[portType], function (type, key) {
if (isFunction(type)) {
return replaceFunctionTypeReferences(graph, type);
}
return type;
});
graph.setNode(node.branchPath, _lodash2.default.cloneDeep(node));
}
function replaceFunctionTypeReferences(graph, functionType) {
var resTypeRefs = _lodash2.default.partial(resolveTypeReference, graph);
return {
type: 'function',
arguments: _lodash2.default.mapValues(functionType.arguments, resTypeRefs),
outputs: _lodash2.default.mapValues(functionType.outputs, resTypeRefs),
return: resTypeRefs(functionType.return)
};
}
function resolveTypeReference(graph, typeRef) {
if (isTypeRef(typeRef)) {
return resolveTypeReference(graph, graph.node(typeRef.node)[utils.portDirectionType(graph, typeRef.node, typeRef.port)][typeRef.port]);
} else if (isFunction(typeRef)) {
return replaceFunctionTypeReferences(graph, typeRef);
} else {
return typeRef;
}
}
function replacePortReferences(graph, node, portType) {
node[portType] = _lodash2.default.mapValues(node[portType], function (type, key) {
if (isTypeRef(type)) {
return resolveTypeReference(graph, type);
}
return type;
});
graph.setNode(node.branchPath, _lodash2.default.cloneDeep(node));
}
function replacePathGenericsForward(graph, path, type) {
for (var r = path.length - 1; r >= 0; r--) {
// takes the parameter type if it's defined
if (r === path.length - 1 && type !== undefined) {
var genInput = genericInputs(graph, path[r].node);
var genOutput = genericOutputs(graph, path[r].node);
graph.node(path[r].node).generic = true;
graph.node(path[r].node).genericType = type;
for (var l = 0; l < genInput.length; l++) {
graph.node(path[r].node).inputPorts[genInput[l]] = tangleType(graph.node(path[r].node).genericType, graph.node(path[r].node).inputPorts[genInput[l]]);
}
for (var _l = 0; _l < genOutput.length; _l++) {
graph.node(path[r].node).outputPorts[genOutput[_l]] = tangleType(graph.node(path[r].node).genericType, graph.node(path[r].node).outputPorts[genOutput[_l]]);
}
}
if ((r > 0 || type !== undefined) && path[r].edge) {
var toNode = graph.node(path[r].edge.to);
var toType = graph.parent(path[r].edge.from) === path[r].edge.to ? toNode.outputPorts[path[r].edge.inPort] : toNode.inputPorts[path[r].edge.inPort];
if (isGeneric(toType)) {
if (!toNode.generic || !toNode.genericType) {
throw new Error('Cannot resolve generic type for ' + path[r].edge.to + ' on path ' + JSON.stringify(path));
}
toType = tangleType(toNode.genericType, toType);
toNode[utils.portDirectionType(graph, path[r].edge.to, path[r].edge.inPort)][path[r].edge.inPort] = toType;
}
var fromNode = graph.node(path[r].edge.from);
var fromType = graph.parent(path[r].edge.to) === path[r].edge.from ? fromNode.inputPorts[path[r].edge.outPort] : fromNode.outputPorts[path[r].edge.outPort];
var entangled = fromNode.atomic ? fromNode.genericType || entangleType(toType, fromType) : entangleType(toType, fromType);
if (isGeneric(fromType)) {
if (fromNode.genericType !== undefined && fromNode.atomic && fromNode.genericType !== entangleType(toType, fromType) && !isTypeRef(utils.portType(graph, path[r].edge.from, path[r].edge.outPort)) && !isTypeRef(utils.portType(graph, path[r].edge.to, path[r].edge.inPort))) {
var error = 'Type mismatch: Two pathes to node ' + path[r].edge.from + ' have different types: ' + JSON.stringify(fromNode.genericType) + ' and ' + JSON.stringify(entangleType(toType, fromType)) + '.';
throw new Error(error);
}
fromNode.generic = true;
fromNode.genericType = entangled;
fromNode[utils.portDirectionType(graph, path[r].edge.from, path[r].edge.outPort)][path[r].edge.outPort] = tangleType(entangled, fromType);
}
}
}
var firstNode = path[0].node;
genInput = genericInputs(graph, firstNode);
for (var _l2 = 0; _l2 < genInput.length; _l2++) {
graph.node(firstNode)[utils.portDirectionType(graph, firstNode, genInput[_l2])][genInput[_l2]] = tangleType(graph.node(firstNode).genericType, graph.node(firstNode)[utils.portDirectionType(graph, firstNode, genInput[_l2])][genInput[_l2]]);
}
}
function replacePathGenerics(graph, path, type) {
for (var r = 0; r < path.length - 1; r++) {
var fromNode = graph.node(path[r].edge.from);
var fromType = graph.parent(path[r].edge.to) === path[r].edge.from ? fromNode.inputPorts[path[r].edge.outPort] : fromNode.outputPorts[path[r].edge.outPort];
var fromType = fromNode[utils.portDirectionType(graph, path[r].edge.from, path[r].edge.outPort)][path[r].edge.outPort];
// if the output of this node is not yet set

@@ -299,11 +481,18 @@ if (isGeneric(fromType)) {

fromType = tangleType(fromNode.genericType, fromType);
fromNode.outputPorts[path[r].edge.outPort] = fromType;
fromNode[utils.portDirectionType(graph, path[r].edge.from, path[r].edge.outPort)][path[r].edge.outPort] = fromType;
}
var toNode = graph.node(path[r].edge.to);
var toType = graph.parent(path[r].edge.from) === path[r].edge.to ? toNode.outputPorts[path[r].edge.inPort] : toNode.inputPorts[path[r].edge.inPort];
var toType = toNode[utils.portDirectionType(graph, path[r].edge.to, path[r].edge.inPort)][path[r].edge.inPort];
var entangled = toNode.genericType || entangleType(fromType, toType);
toNode.generic = true;
toNode.genericType = entangled;
if (isGeneric(toType)) {
toNode.inputPorts[path[r].edge.inPort] = tangleType(entangled, toType);
if (toNode.genericType !== undefined && toNode.genericType !== entangleType(fromType, toType) && !isTypeRef(utils.portType(graph, path[r].edge.from, path[r].edge.outPort)) && !isTypeRef(utils.portType(graph, path[r].edge.to, path[r].edge.inPort)) && !isTypeRef(toNode.genericType)) {
var error = 'Type mismatch: Two pathes to node ' + path[r].edge.to + ' have different types: ' + JSON.stringify(toNode.genericType) + ' and ' + JSON.stringify(entangleType(fromType, toType)) + '.';
throw new Error(error);
}
if (isTypeRef(toNode.genericType)) {
entangled = entangleType(fromType, toType);
}
toNode.generic = true;
toNode.genericType = entangled;
toNode[utils.portDirectionType(graph, path[r].edge.to, path[r].edge.inPort)][path[r].edge.inPort] = tangleType(entangled, toType);
}

@@ -314,3 +503,3 @@ }

for (var l = 0; l < genOutput.length; l++) {
graph.node(lastNode).outputPorts[genOutput[l]] = tangleType(graph.node(lastNode).genericType, graph.node(lastNode).outputPorts[genOutput[l]]);
graph.node(lastNode)[utils.portDirectionType(graph, lastNode, genOutput[l])][genOutput[l]] = tangleType(graph.node(lastNode).genericType, graph.node(lastNode)[utils.portDirectionType(graph, lastNode, genOutput[l])][genOutput[l]]);
}

@@ -458,3 +647,24 @@ /* for (var k = 1; k < path.length; k++) {

function isGenericFree(graph) {
return genericNodes(graph).length === 0;
var nodes = graph.nodes();
for (var i = 0; i < nodes.length; i++) {
var inp = graph.node(nodes[i]).inputPorts;
if (inp !== undefined) {
var inpKeys = Object.keys(inp);
for (var j = 0; j < inpKeys.length; j++) {
if (isGeneric(inp[inpKeys[j]])) {
return false;
}
}
}
var outp = graph.node(nodes[i]).outputPorts;
if (outp !== undefined) {
var outpKeys = Object.keys(outp);
for (var _j2 = 0; _j2 < outpKeys.length; _j2++) {
if (isGeneric(outp[outpKeys[_j2]])) {
return false;
}
}
}
}
return true;
}

4

package.json
{
"name": "@buggyorg/dynatype-network-graph",
"version": "0.1.10",
"version": "0.1.12",
"description": "",

@@ -11,3 +11,3 @@ "main": "lib/dynatype-network.js",

"@buggyorg/component-library": "^0.1.6",
"@buggyorg/graphtools": "^0.2.15",
"@buggyorg/graphtools": "^0.2.18",
"commander": "^2.9.0",

@@ -14,0 +14,0 @@ "get-stdin": "^5.0.1",

@@ -5,2 +5,3 @@ var grlib = require('graphlib')

import * as graphtools from '@buggyorg/graphtools'
var utils = graphtools.utils

@@ -56,3 +57,6 @@ function isInPort (node) {

function entangleType (type, template) {
if (template[0] === '[' && template[template.length - 1] === ']') {
if (template && template[0] === '[' && template[template.length - 1] === ']') {
if (typeof (type) === 'object' && type.type === 'type-ref') {
return _.merge({}, type, {template})
}
return type.replace(/\[/g, '').replace(/\]/g, '')

@@ -66,2 +70,5 @@ } else {

if (template[0] === '[' && template[template.length - 1] === ']') {
if (typeof (type) === 'object' && type.type === 'type-ref') {
return _.merge({}, type, {template})
}
return '[' + type + ']'

@@ -187,7 +194,19 @@ } else {

export function replaceGenerics (graph) {
graph = replaceGenericsInternal(graph)
var typeRefs = determineTypeReferences(graph)
applyKnownTypeRefs(graph, typeRefs)
replaceUnkownTypeReferences(graph)
graph = replaceGenericsInternal(graph)
replaceGenericFunctionTypes(graph)
replaceGenericTypeRefs(graph)
return graph
}
function replaceGenericsInternal (graph) {
var processGraph = replaceTypeHints(graph)
var nodes = processGraph.nodes()
for (var j = 0; j < nodes.length; j++) {
var paths = replaceGenericInput(graph, nodes[j])
var paths = replaceGenericInput(processGraph, nodes[j])
var pathsToReplace = []
var validType = null
for (let i = 0; i < paths.length; i++) {

@@ -197,10 +216,11 @@ var currentPath = paths[i]

processGraph.node(currentPath[0].node).inputPorts[currentPath[0].port]
if (type === 'generic') {
if (isGeneric(type)) {
pathsToReplace = pathsToReplace.concat([currentPath])
} else {
var validType = type
validType = type
// finds the type backwards
replacePathGenerics(processGraph, currentPath, type)
}
}
if (validType === undefined && pathsToReplace.length !== 0) {
if (!validType && pathsToReplace.length !== 0) {
var secondInputs = processGraph.node(nodes[j]).inputPorts

@@ -210,2 +230,7 @@ var keys = Object.keys(secondInputs)

if (secondInputs[keys[i]] !== 'generic') {
// takes the type of another input
if (validType !== undefined && validType !== secondInputs[keys[i]]) {
var error = 'Type mismatch: Two pathes to node ' + nodes[j] + ' have different types: ' + validType + ' and ' + secondInputs[keys[i]] + '.'
throw new Error(error)
}
validType = secondInputs[keys[i]]

@@ -215,21 +240,24 @@ }

}
if (validType === undefined && pathsToReplace.length !== 0) {
paths = replaceGenericOutput(graph, nodes[j])
if (!validType) {
paths = replaceGenericOutput(processGraph, nodes[j])
for (let i = 0; i < paths.length; i++) {
currentPath = _.filter(paths[i], (node) => node.port !== null)
type = processGraph.node(currentPath[currentPath.length - 1].node).inputPorts[currentPath[currentPath.length - 1].port] ||
processGraph.node(currentPath[currentPath.length - 1].node).outputPorts[currentPath[currentPath.length - 1].port]
var lastPathItem = _.last(currentPath)
type = utils.portType(processGraph, lastPathItem.node, lastPathItem.port)
if (type === 'generic') {
pathsToReplace = pathsToReplace.concat([currentPath])
} else {
} else if (!isTypeRef(type)) {
validType = type
replacePathGenerics(processGraph, currentPath, type)
// finds the type forwards
replacePathGenericsForward(processGraph, currentPath, validType)
}
}
}
if (validType === undefined && pathsToReplace.length !== 0) {
if (!validType && pathsToReplace.length !== 0) {
throw new Error('Generics could not be replaced: No type found.')
}
for (let p = 0; p < pathsToReplace.length; p++) {
replacePathGenerics(processGraph, pathsToReplace[p], validType, pathsToReplace[p][0].port)
// takes the type of the second input
replacePathGenericsForward(processGraph, pathsToReplace[p], validType)
}

@@ -240,6 +268,166 @@ }

function replacePathGenerics (graph, path, type, firstPort) {
function isTypeRef (type) {
return typeof (type) === 'object' && type.type === 'type-ref'
}
function isFunction (type) {
return typeof (type) === 'object' && type.type === 'function'
}
function determineTypeReferences (graph) {
return _(graph.edges())
.map((e) => _.merge({}, e, {value: graph.edge(e)}))
.filter((e) => isTypeRef(utils.portType(graph, e.v, e.value.outPort)) && !isTypeRef(utils.portType(graph, e.w, e.value.inPort)) ||
isTypeRef(utils.portType(graph, e.w, e.value.inPort)) && !isTypeRef(utils.portType(graph, e.v, e.value.outPort)))
.reject((e) => isGeneric(utils.portType(graph, e.v, e.value.outPort)) ||
isGeneric(utils.portType(graph, e.w, e.value.inPort)))
.map((e) => {
var vTypeNode = isTypeRef(utils.portType(graph, e.v, e.value.outPort))
return {
refNode: (vTypeNode) ? e.v : e.w,
refPort: (vTypeNode) ? e.value.outPort : e.value.inPort,
reference: (vTypeNode) ? utils.portType(graph, e.v, e.value.outPort) : utils.portType(graph, e.w, e.value.inPort),
type: (vTypeNode) ? utils.portType(graph, e.w, e.value.inPort) : utils.portType(graph, e.v, e.value.outPort)
}
})
.value()
}
function applyKnownTypeRefs (graph, typeRefs) {
_(typeRefs)
.each((r) => {
var node = graph.node(r.refNode)
var oNode = graph.node(r.reference.node)
node[utils.portDirectionType(graph, r.refNode, r.refPort)][r.refPort] = r.type
oNode[utils.portDirectionType(graph, r.reference.node, r.reference.port)][r.reference.port] = entangleType(r.type, r.reference.template)
graph.setNode(r.refNode, _.cloneDeep(node))
graph.setNode(r.reference.node, _.cloneDeep(oNode))
})
}
function replaceUnkownTypeReferences (graph) {
_(graph.nodes())
.map((n) => graph.node(n))
.each((n) => {
replacePortReferences(graph, n, 'inputPorts')
replacePortReferences(graph, n, 'outputPorts')
})
}
function replaceGenericFunctionTypes (graph) {
_(graph.nodes())
.map((n) => graph.node(n))
.each((n) => {
replaceFunctionPorts(graph, n, 'inputPorts')
replaceFunctionPorts(graph, n, 'outputPorts')
})
}
function replaceGenericTypeRefs (graph) {
_(graph.nodes())
.map((n) => graph.node(n))
.each((n) => {
if (n.genericType && n.genericType.type === 'type-ref') {
n.genericType = resolveTypeReference(graph, n.genericType)
} else if (n.genericType && n.genericType.type === 'function') {
n.genericType = replaceFunctionTypeReferences(graph, n.genericType)
}
})
}
function replaceFunctionPorts (graph, node, portType) {
node[portType] = _.mapValues(node[portType], (type, key) => {
if (isFunction(type)) {
return replaceFunctionTypeReferences(graph, type)
}
return type
})
graph.setNode(node.branchPath, _.cloneDeep(node))
}
function replaceFunctionTypeReferences (graph, functionType) {
var resTypeRefs = _.partial(resolveTypeReference, graph)
return {
type: 'function',
arguments: _.mapValues(functionType.arguments, resTypeRefs),
outputs: _.mapValues(functionType.outputs, resTypeRefs),
return: resTypeRefs(functionType.return)
}
}
function resolveTypeReference (graph, typeRef) {
if (isTypeRef(typeRef)) {
return resolveTypeReference(graph, graph.node(typeRef.node)[utils.portDirectionType(graph, typeRef.node, typeRef.port)][typeRef.port])
} else if (isFunction(typeRef)) {
return replaceFunctionTypeReferences(graph, typeRef)
} else {
return typeRef
}
}
function replacePortReferences (graph, node, portType) {
node[portType] = _.mapValues(node[portType], (type, key) => {
if (isTypeRef(type)) {
return resolveTypeReference(graph, type)
}
return type
})
graph.setNode(node.branchPath, _.cloneDeep(node))
}
function replacePathGenericsForward (graph, path, type) {
for (var r = path.length - 1; r >= 0; r--) {
// takes the parameter type if it's defined
if (r === path.length - 1 && type !== undefined) {
var genInput = genericInputs(graph, path[r].node)
var genOutput = genericOutputs(graph, path[r].node)
graph.node(path[r].node).generic = true
graph.node(path[r].node).genericType = type
for (let l = 0; l < genInput.length; l++) {
graph.node(path[r].node).inputPorts[genInput[l]] =
tangleType(graph.node(path[r].node).genericType, graph.node(path[r].node).inputPorts[genInput[l]])
}
for (let l = 0; l < genOutput.length; l++) {
graph.node(path[r].node).outputPorts[genOutput[l]] =
tangleType(graph.node(path[r].node).genericType, graph.node(path[r].node).outputPorts[genOutput[l]])
}
}
if ((r > 0 || type !== undefined) && path[r].edge) {
var toNode = graph.node(path[r].edge.to)
var toType = (graph.parent(path[r].edge.from) === path[r].edge.to) ? toNode.outputPorts[path[r].edge.inPort] : toNode.inputPorts[path[r].edge.inPort]
if (isGeneric(toType)) {
if (!toNode.generic || !toNode.genericType) {
throw new Error('Cannot resolve generic type for ' + path[r].edge.to + ' on path ' + JSON.stringify(path))
}
toType = tangleType(toNode.genericType, toType)
toNode[utils.portDirectionType(graph, path[r].edge.to, path[r].edge.inPort)][path[r].edge.inPort] = toType
}
var fromNode = graph.node(path[r].edge.from)
var fromType = (graph.parent(path[r].edge.to) === path[r].edge.from) ? fromNode.inputPorts[path[r].edge.outPort] : fromNode.outputPorts[path[r].edge.outPort]
var entangled = (fromNode.atomic) ? (fromNode.genericType || entangleType(toType, fromType)) : entangleType(toType, fromType)
if (isGeneric(fromType)) {
if (fromNode.genericType !== undefined && fromNode.atomic && fromNode.genericType !== entangleType(toType, fromType) &&
!isTypeRef(utils.portType(graph, path[r].edge.from, path[r].edge.outPort)) &&
!isTypeRef(utils.portType(graph, path[r].edge.to, path[r].edge.inPort))) {
var error = 'Type mismatch: Two pathes to node ' + path[r].edge.from + ' have different types: ' + JSON.stringify(fromNode.genericType) + ' and ' + JSON.stringify(entangleType(toType, fromType)) + '.'
throw new Error(error)
}
fromNode.generic = true
fromNode.genericType = entangled
fromNode[utils.portDirectionType(graph, path[r].edge.from, path[r].edge.outPort)][path[r].edge.outPort] = tangleType(entangled, fromType)
}
}
}
var firstNode = path[0].node
genInput = genericInputs(graph, firstNode)
for (let l = 0; l < genInput.length; l++) {
graph.node(firstNode)[utils.portDirectionType(graph, firstNode, genInput[l])][genInput[l]] =
tangleType(graph.node(firstNode).genericType, graph.node(firstNode)[utils.portDirectionType(graph, firstNode, genInput[l])][genInput[l]])
}
}
function replacePathGenerics (graph, path, type) {
for (var r = 0; r < path.length - 1; r++) {
var fromNode = graph.node(path[r].edge.from)
var fromType = (graph.parent(path[r].edge.to) === path[r].edge.from) ? fromNode.inputPorts[path[r].edge.outPort] : fromNode.outputPorts[path[r].edge.outPort]
var fromType = fromNode[utils.portDirectionType(graph, path[r].edge.from, path[r].edge.outPort)][path[r].edge.outPort]
// if the output of this node is not yet set

@@ -252,11 +440,21 @@ if (isGeneric(fromType)) {

fromType = tangleType(fromNode.genericType, fromType)
fromNode.outputPorts[path[r].edge.outPort] = fromType
fromNode[utils.portDirectionType(graph, path[r].edge.from, path[r].edge.outPort)][path[r].edge.outPort] = fromType
}
var toNode = graph.node(path[r].edge.to)
var toType = (graph.parent(path[r].edge.from) === path[r].edge.to) ? toNode.outputPorts[path[r].edge.inPort] : toNode.inputPorts[path[r].edge.inPort]
var toType = toNode[utils.portDirectionType(graph, path[r].edge.to, path[r].edge.inPort)][path[r].edge.inPort]
var entangled = toNode.genericType || entangleType(fromType, toType)
toNode.generic = true
toNode.genericType = entangled
if (isGeneric(toType)) {
toNode.inputPorts[path[r].edge.inPort] = tangleType(entangled, toType)
if (toNode.genericType !== undefined && toNode.genericType !== entangleType(fromType, toType) &&
!isTypeRef(utils.portType(graph, path[r].edge.from, path[r].edge.outPort)) &&
!isTypeRef(utils.portType(graph, path[r].edge.to, path[r].edge.inPort)) &&
!isTypeRef(toNode.genericType)) {
var error = 'Type mismatch: Two pathes to node ' + path[r].edge.to + ' have different types: ' + JSON.stringify(toNode.genericType) + ' and ' + JSON.stringify(entangleType(fromType, toType)) + '.'
throw new Error(error)
}
if (isTypeRef(toNode.genericType)) {
entangled = entangleType(fromType, toType)
}
toNode.generic = true
toNode.genericType = entangled
toNode[utils.portDirectionType(graph, path[r].edge.to, path[r].edge.inPort)][path[r].edge.inPort] = tangleType(entangled, toType)
}

@@ -267,4 +465,4 @@ }

for (var l = 0; l < genOutput.length; l++) {
graph.node(lastNode).outputPorts[genOutput[l]] =
tangleType(graph.node(lastNode).genericType, graph.node(lastNode).outputPorts[genOutput[l]])
graph.node(lastNode)[utils.portDirectionType(graph, lastNode, genOutput[l])][genOutput[l]] =
tangleType(graph.node(lastNode).genericType, graph.node(lastNode)[utils.portDirectionType(graph, lastNode, genOutput[l])][genOutput[l]])
}

@@ -390,3 +588,24 @@ /* for (var k = 1; k < path.length; k++) {

export function isGenericFree (graph) {
return genericNodes(graph).length === 0
var nodes = graph.nodes()
for (let i = 0; i < nodes.length; i++) {
var inp = graph.node(nodes[i]).inputPorts
if (inp !== undefined) {
var inpKeys = Object.keys(inp)
for (let j = 0; j < inpKeys.length; j++) {
if (isGeneric(inp[inpKeys[j]])) {
return false
}
}
}
var outp = graph.node(nodes[i]).outputPorts
if (outp !== undefined) {
var outpKeys = Object.keys(outp)
for (let j = 0; j < outpKeys.length; j++) {
if (isGeneric(outp[outpKeys[j]])) {
return false
}
}
}
}
return true
}

@@ -13,3 +13,5 @@ /* global describe, it */

var inc_generic = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/inc_generic.json')))
// var type_error = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/type_error.json')))
var type_error = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/type_error.json')))
var type_error2 = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/type_error2.json')))
var type_error3 = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/type_error3.json')))
var facGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/fac.json')))

@@ -39,8 +41,8 @@ var fac2Graph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/fac2.json')))

/* unclear ;)
// unclear ;)
it('replaces an array generic type with an array type', () => {
var newType = dtypenet.replaceGeneric('[generic]', '[string]')
expect(newType).to.equal('[string]')
})*/
expect(newType).to.equal('[[string]]')
})
/*
it('Creates a processgraph with correct translator nodes', function () {

@@ -59,5 +61,5 @@ var d = dtypenet.addTypeConversion(processGraph, convertGraph)

})
/* it('Creates a processgraph with correct translator nodes without generics simple', function () {
it('Creates a processgraph with correct translator nodes without generics simple', function () {
var g = dtypenet.replaceGenerics(processGraphGeneric)
expect(dtypenet.isGenericFree(g)).to.be.true
expect(dtypenet.genericNodes(g)).to.deep.equal([])
var d = dtypenet.addTypeConversion(g, convertGraph)

@@ -69,2 +71,3 @@ var curGraph = grlib.json.write(d)

})
it('Creates a processgraph with correct translator nodes without generics complex', function () {

@@ -78,3 +81,4 @@ var g = dtypenet.replaceGenerics(processGraphGeneric2)

expect(curGraph).to.deep.equal(testgraph)
})*/
})
it('Creates a processgraph without generics very complex', function () {

@@ -86,3 +90,4 @@ var g = dtypenet.replaceGenerics(facGraph)

})
/* it('Propagates generics correctly which pass hierarchy borders', () => {
it('Propagates generics correctly which pass hierarchy borders', () => {
var g = dtypenet.replaceGenerics(fac2Graph)

@@ -95,2 +100,3 @@ expect(dtypenet.isGenericFree(g)).to.be.true

})
it('Replaces generics correctly for nodes with generic input and no generic output', () => {

@@ -103,3 +109,4 @@ var g = dtypenet.replaceGenerics(inc_generic)

expect(curGraph).to.deep.equal(testgraph)
})*/
})
*/
it('Uses type hints to replace generics', () => {

@@ -111,2 +118,3 @@ var hintGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/hint1.json')))

})
/*
it('Resolves generic arrays', () => {

@@ -118,2 +126,3 @@ var arrGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/arr.json')))

})
it('Resolves generic arrays with hints', () => {

@@ -125,3 +134,4 @@ var arrGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/arrHint.json')))

})
/* it('Can handle pack and unpacking arrays', () => {
it('Can handle pack and unpacking arrays', () => {
var arrGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/unpack.json')))

@@ -137,7 +147,15 @@ var genGraph = dtypenet.replaceGenerics(arrGraph)

})
*/
it('Throws an error if there is a type mismatch backwards', () => {
expect(() => dtypenet.replaceGenerics(type_error)).to.throw(Error)
})
it('Throws an error if there is a type mismatch forwards', () => {
expect(() => dtypenet.replaceGenerics(type_error2)).to.throw(Error)
})
it('Throws an error if there is a type mismatch', () => {
expect(() => dtypenet.replaceGenerics(type_error)).to.throw(Error)
expect(() => dtypenet.replaceGenerics(type_error3)).to.throw(Error)
})
*/
/*
it('Can process the map example correctly', () => {

@@ -147,2 +165,3 @@ var mapGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/map.json')))

expect(dtypenet.isGenericFree(genGraph)).to.be.true
// console.log(JSON.stringify(grlib.json.write(genGraph), null, 2))
expect(genGraph.node('mapInc').inputPorts['data']).to.equal('[int64]')

@@ -153,3 +172,3 @@ expect(genGraph.node('mapInc:apply').inputPorts['value']).to.equal('int64')

})
*/
it('finds a way through map', () => {

@@ -166,3 +185,3 @@ var mapGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/map.json')))

})
/*
it('can deduce all array types in recursive map', () => {

@@ -208,17 +227,2 @@ var mapGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/map_recursive.json')))

it('can process graphs with continuations', () => {
var gaGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/factorial_mux.json')))
var typedGraph = dtypenet.replaceGenerics(gaGraph)
expect(typedGraph).to.be.ok
expect(dtypenet.genericNodes(typedGraph)).to.have.length(0)
})
it('can handle ackermann example', () => {
var gaGraph = grlib.json.read(JSON.parse(fs.readFileSync('./test/fixtures/ack.json')))
var typedGraph = dtypenet.replaceGenerics(gaGraph)
expect(typedGraph).to.be.ok
expect(dtypenet.genericNodes(typedGraph)).to.have.length(0)
})
/*
it('backtracks second path if first contains just generics', () => {

@@ -255,6 +259,7 @@ var g = dtypenet.replaceGenerics(secondInput)

var curGraph = grlib.json.write(g)
// fs.writeFileSync('test/fixtures/map_recursive2_result.graphlib', JSON.stringify(grlib.json.write(g), null, 2))
fs.writeFileSync('test/fixtures/map_recursive2_result.graphlib', JSON.stringify(grlib.json.write(g), null, 2))
var testgraph = JSON.parse(fs.readFileSync('test/fixtures/map_recursive2_result.graphlib', 'utf8'))
expect(curGraph).to.deep.equal(testgraph)
})*/
})
*/
})

@@ -32,3 +32,3 @@ {

"outputPorts": {
"output": "string"
"output": "int"
},

@@ -35,0 +35,0 @@ "atomic": true,

@@ -9,237 +9,143 @@ {

{
"v": "out",
"value": {
"id": "io/stdout",
"version": "0.2.0",
"inputPorts": {
"input": "generic"
},
"outputPorts": {},
"atomic": true,
"path": [],
"branchPath": "out",
"branch": "out",
"name": "out",
"nodeType": "process"
}
"v": "EXAMPLE1",
"value": {}
},
{
"v": "in",
"v": "0_CONST1",
"value": {
"id": "io/stdin",
"version": "0.2.0",
"nodeType": "process",
"id": "math/const1",
"version": "0.1.0",
"inputPorts": {},
"outputPorts": {
"output": "string"
"const1": "int"
},
"atomic": true,
"path": [],
"branchPath": "in",
"branch": "in",
"name": "in",
"nodeType": "process"
}
"atomic": true
},
"parent": "EXAMPLE1"
},
{
"v": "inc",
"v": "1_CONST2",
"value": {
"id": "math/inc",
"version": "0.3.0",
"inputPorts": {
"i": "int"
},
"nodeType": "process",
"id": "math/const2",
"version": "0.1.0",
"inputPorts": {},
"outputPorts": {
"inc": "int"
"const2": "bool"
},
"atomic": false,
"implementation": {
"nodes": [
{
"id": "math/add",
"version": "0.1.1",
"inputPorts": {
"s1": "generic",
"s2": "generic"
},
"outputPorts": {
"sum": "int"
},
"atomic": true,
"path": [
{
"meta": "math/inc",
"branch": "inc",
"version": "0.3.0",
"path": []
}
],
"parent": "inc",
"branchPath": "inc:add",
"branch": "add"
},
{
"id": "math/const1",
"version": "0.2.0",
"inputPorts": {},
"outputPorts": {
"output": "int"
},
"atomic": true,
"path": [
{
"meta": "math/inc",
"branch": "inc",
"version": "0.3.0",
"path": []
}
],
"parent": "inc",
"branchPath": "inc:c",
"branch": "c"
}
],
"edges": [
{
"from": "i",
"to": "add:s1"
},
{
"from": "c:output",
"to": "add:s2"
},
{
"from": "add:sum",
"to": "inc"
}
]
},
"path": [],
"branchPath": "inc",
"branch": "inc",
"name": "inc",
"nodeType": "process"
}
"atomic": true
},
"parent": "EXAMPLE1"
},
{
"v": "inc:add",
"v": "2_DEMUX",
"value": {
"id": "math/add",
"version": "0.1.1",
"nodeType": "process",
"name": "DEMUX",
"id": "logic/demux",
"inputPorts": {
"s1": "generic",
"s2": "generic"
"input": "generic",
"control": "generic"
},
"outputPorts": {
"sum": "int"
"outTrue": "generic",
"outFalse": "generic"
},
"atomic": true,
"path": [
{
"meta": "math/inc",
"branch": "inc",
"version": "0.3.0",
"path": []
}
],
"parent": "inc",
"branchPath": "inc:add",
"branch": "add",
"nodeType": "process"
"version": "0.1.0"
},
"parent": "inc"
"parent": "EXAMPLE1"
},
{
"v": "inc:c",
"v": "3_STDOUT",
"value": {
"id": "math/const1",
"version": "0.2.0",
"inputPorts": {},
"outputPorts": {
"output": "int"
"nodeType": "process",
"id": "io/stdout",
"version": "0.1.0",
"inputPorts": {
"input": "string"
},
"atomic": true,
"path": [
{
"meta": "math/inc",
"branch": "inc",
"version": "0.3.0",
"path": []
}
],
"parent": "inc",
"branchPath": "inc:c",
"branch": "c",
"nodeType": "process"
"outputPorts": {},
"atomic": true
},
"parent": "inc"
"parent": "EXAMPLE1"
},
{
"v": "inc_PORT_i",
"v": "4_STDOUT",
"value": {
"nodeType": "inPort",
"portName": "i",
"hierarchyBorder": true,
"process": "inc"
}
"nodeType": "process",
"id": "io/stdout",
"version": "0.1.0",
"inputPorts": {
"input": "string"
},
"outputPorts": {},
"atomic": true
},
"parent": "EXAMPLE1"
},
{
"v": "in_PORT_output",
"v": "0_CONST1_PORT_const1",
"value": {
"nodeType": "outPort",
"portName": "output",
"process": "in"
}
"portName": "const1"
},
"parent": "EXAMPLE1"
},
{
"v": "out_PORT_input",
"v": "1_CONST2_PORT_const2",
"value": {
"nodeType": "inPort",
"portName": "input",
"process": "out"
}
},
{
"v": "inc_PORT_inc",
"value": {
"nodeType": "outPort",
"portName": "inc",
"hierarchyBorder": true,
"process": "inc"
}
"portName": "const2"
},
"parent": "EXAMPLE1"
},
{
"v": "inc:add_PORT_s1",
"v": "2_DEMUX_PORT_input",
"value": {
"nodeType": "inPort",
"portName": "s1",
"process": "inc:add"
"portName": "input"
},
"parent": "inc"
"parent": "EXAMPLE1"
},
{
"v": "inc:add_PORT_s2",
"v": "2_DEMUX_PORT_control",
"value": {
"nodeType": "inPort",
"portName": "s2",
"process": "inc:add"
"portName": "control"
},
"parent": "inc"
"parent": "EXAMPLE1"
},
{
"v": "inc:c_PORT_output",
"v": "2_DEMUX_PORT_outTrue",
"value": {
"nodeType": "outPort",
"portName": "output",
"process": "inc:c"
"portName": "outTrue"
},
"parent": "inc"
"parent": "EXAMPLE1"
},
{
"v": "inc:add_PORT_sum",
"v": "2_DEMUX_PORT_outFalse",
"value": {
"nodeType": "outPort",
"portName": "sum",
"process": "inc:add"
"portName": "outFalse"
},
"parent": "inc"
"parent": "EXAMPLE1"
},
{
"v": "3_STDOUT_PORT_input",
"value": {
"nodeType": "inPort",
"portName": "input"
},
"parent": "EXAMPLE1"
},
{
"v": "4_STDOUT_PORT_input",
"value": {
"nodeType": "inPort",
"portName": "input"
},
"parent": "EXAMPLE1"
}

@@ -249,46 +155,50 @@ ],

{
"v": "in",
"w": "in_PORT_output"
"v": "0_CONST1",
"w": "0_CONST1_PORT_const1"
},
{
"v": "in_PORT_output",
"w": "inc_PORT_i"
"v": "1_CONST2",
"w": "1_CONST2_PORT_const2"
},
{
"v": "out_PORT_input",
"w": "out"
"v": "1_CONST2_PORT_const2",
"w": "2_DEMUX_PORT_control"
},
{
"v": "inc_PORT_inc",
"w": "out_PORT_input"
"v": "0_CONST1_PORT_const1",
"w": "2_DEMUX_PORT_input"
},
{
"v": "inc:add_PORT_s1",
"w": "inc:add"
"v": "2_DEMUX_PORT_input",
"w": "2_DEMUX"
},
{
"v": "inc_PORT_i",
"w": "inc:add_PORT_s1"
"v": "2_DEMUX_PORT_control",
"w": "2_DEMUX"
},
{
"v": "inc:add_PORT_s2",
"w": "inc:add"
"v": "2_DEMUX",
"w": "2_DEMUX_PORT_outTrue"
},
{
"v": "inc:c",
"w": "inc:c_PORT_output"
"v": "2_DEMUX",
"w": "2_DEMUX_PORT_outFalse"
},
{
"v": "inc:c_PORT_output",
"w": "inc:add_PORT_s2"
"v": "2_DEMUX_PORT_outTrue",
"w": "3_STDOUT_PORT_input"
},
{
"v": "inc:add",
"w": "inc:add_PORT_sum"
"v": "2_DEMUX_PORT_outFalse",
"w": "4_STDOUT_PORT_input"
},
{
"v": "inc:add_PORT_sum",
"w": "inc_PORT_inc"
"v": "3_STDOUT_PORT_input",
"w": "3_STDOUT"
},
{
"v": "4_STDOUT_PORT_input",
"w": "4_STDOUT"
}
]
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc