Skip to content

Commit

Permalink
Make .flow files able to include other brick files
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristin committed Jan 17, 2015
1 parent e6a9bf5 commit 11b53fe
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 77 deletions.
39 changes: 17 additions & 22 deletions bin/dataflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,26 @@ var prompt = require("prompt"),
argv = process.argv.slice(2);

if (argv.length > 0) {
file.open(argv[0], function(err, brick) {
var brick = file.open(argv[0]);
Object.keys(brick.outputs).forEach(function(key) {
var inputPort = new port.InputPort(function(packet) {
console.log(key.grey + " ".grey + packet.data);
this.popPacket();
});

brick.outputs[key].pipe(inputPort);
});

prompt.start();
prompt.message = "";
prompt.delimiter = "";
prompt.get(Object.keys(brick.inputs), function(err, result) {
if (err) {
console.log(err);
} else {
Object.keys(brick.outputs).forEach(function(key) {
var inputPort = new port.InputPort(function(packet) {
console.log(key.grey + " ".grey + packet.data);
this.popPacket();
});

brick.outputs[key].pipe(inputPort);
});

prompt.start();
prompt.message = "";
prompt.delimiter = "";
prompt.get(Object.keys(brick.inputs), function(err, result) {
if (err) {
console.log(err);
} else {
Object.keys(result).forEach(function(element) {
var input = parseFloat(result[element]) || result[element];
brick.inputs[element].pushData(input);
});
}
Object.keys(result).forEach(function(element) {
var input = parseFloat(result[element]) || result[element];
brick.inputs[element].pushData(input);
});
}
});
Expand Down
81 changes: 47 additions & 34 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,57 @@

var fs = require("fs"),
path = require("path"),
_ = require("lodash"),
port = require("./port"),
Composite = require("./brick").Composite;

exports.open = function(file, callback) {
fs.readFile(file, "utf8", function(err, data) {
if (err) {
callback(err);
}
exports.open = function(file) {
file = path.resolve(file);
return open(path.dirname(file), path.basename(file));
};

function open(dir, name) {
var candidates = [
path.join(dir, name),
path.join(dir, name) + ".js",
path.join(dir, name) + ".flow",
path.join(dir, name) + ".json",
path.join(dir, "node_modules", name) + ".js",
path.join(dir, "node_modules", name) + ".flow",
path.join(dir, "node_modules", name) + ".json",
path.join(dir, "node_modules", name, "index.js"),
path.join(dir, "node_modules", name, "index.flow"),
path.join(dir, "node_modules", name, "index.json"),
path.join(dir, "node_modules", name)
];

// Select the first existing file.
var file = _.find(candidates, function(file) {
return fs.existsSync(file);
});

if (file) {
switch (path.extname(file)) {
case ".js":
return new(require(file))();

case ".flow":
callback(null, loadFlow(data, null));
break;
return openFlow(file);

case ".json":
callback(null, loadJson(JSON.parse(data), null));
break;
return loadJson(require(file), null);

default:
callback(new Error("File not supported"));
throw new Error("'" + file + "' is not supported.");
}
});
};
}

throw new Error("Unable to find '" + name + "' in '" + dir + "'.");
}

function loadFlow(data, props) {
var composites = {},
function openFlow(file) {
var data = fs.readFileSync(file, "utf8"),
composites = {},
mainComposite,
composite,
compositeName,
Expand All @@ -52,11 +79,11 @@ function loadFlow(data, props) {
inputPort;

if (from !== "this" && !brickFrom) {
throw new Error("The composite does not contain a brick called " + from + ".");
throw new Error("The composite does not contain a brick called '" + from + "'.");
}

if (to !== "this" && !brickTo) {
throw new Error("The composite does not contain a brick called " + to + ".");
throw new Error("The composite does not contain a brick called '" + to + "'.");
}

if (from === "this") {
Expand All @@ -73,11 +100,11 @@ function loadFlow(data, props) {
}

if (!outputPort) {
throw new Error(from + " does not contain an outout port called " + output + ".");
throw new Error("'" + from + "' does not contain an outout port called '" + output + "'.");
}

if (!inputPort) {
throw new Error(to + " does not contain an input port called " + input + ".");
throw new Error("'" + to + "' does not contain an input port called '" + input + "'.");
}

outputPort.pipe(inputPort);
Expand All @@ -104,19 +131,7 @@ function loadFlow(data, props) {
brickName = brickMatch[1];
brickType = brickMatch[2];

// Search brick in previous composites.
brick = composites[brickType];

// If not found, create one from library.
if (brick === undefined) {
brick = new(require(brickType))();
}

if (brick === undefined) {
throw new Error("Unknown brick type: " + brickType);
}

bricks[brickName] = brick;
bricks[brickName] = composites[brickType] || open(path.dirname(file), brickType);
}

// LINKS
Expand All @@ -126,9 +141,7 @@ function loadFlow(data, props) {

// PROPS.
while ((propMatch = propRegExp.exec(bodyMatch[3])) !== null) {
if (propMatch[1] === "this") {} else {
bricks[propMatch[1]].props[propMatch[2]] = JSON.parse("[" + propMatch[3] + "]")[0];
}
bricks[propMatch[1]].props[propMatch[2]] = JSON.parse("[" + propMatch[3] + "]")[0];
}
}

Expand Down
29 changes: 13 additions & 16 deletions test/dataflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,36 @@ describe("dataflow", function() {
});

it("should create a program with inputs and output", function(done) {
dataflow.open("./test/programs/multiplier.json", function(err, brick) {
var inputPort = new port.InputPort(function(packet) {
var brick = dataflow.open("./test/programs/multiplier.json"),
inputPort = new port.InputPort(function(packet) {
packet.data.should.be.equal(35);
done();
});

brick.outputs.third.pipe(inputPort);
brick.inputs.second.pushData(7);
brick.inputs.first.pushData(5);
});
brick.outputs.third.pipe(inputPort);
brick.inputs.second.pushData(7);
brick.inputs.first.pushData(5);
});

it("should execute a recursive program", function(done) {
dataflow.open("./test/programs/factorial.flow", function(err, brick) {
var inputPort = new port.InputPort(function(packet) {
var brick = dataflow.open("./test/programs/factorial.flow"),
inputPort = new port.InputPort(function(packet) {
packet.data.should.be.equal(120);
done();
});

brick.outputs.result.pipe(inputPort);
brick.inputs.n.pushData(5);
});
brick.outputs.result.pipe(inputPort);
brick.inputs.n.pushData(5);
});

it("should execute a program with composite brick", function(done) {
dataflow.open("./test/programs/composite.json", function(err, brick) {
var inputPort = new port.InputPort(function(packet) {
var brick = dataflow.open("./test/programs/composite.json"),
inputPort = new port.InputPort(function(packet) {
packet.data.should.be.equal(32);
done();
});

brick.outputs.out.pipe(inputPort);
brick.inputs.in.pushData(3);
});
brick.outputs.out.pipe(inputPort);
brick.inputs.in.pushData(3);
});
});
10 changes: 5 additions & 5 deletions test/programs/factorial.flow
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
COMPOSITE Main
BRICKS
inc "../test/bricks/increment"
comp "../test/bricks/compare"
mult "../test/bricks/multiply"
filter "../test/bricks/filter"
addProp "../test/bricks/add-property"
inc "../bricks/increment"
comp "../bricks/compare"
mult "../bricks/multiply"
filter "../bricks/filter"
addProp "../bricks/add-property"

LINKS
this.n | inc.input
Expand Down

0 comments on commit 11b53fe

Please sign in to comment.