Commit e3eb40e8 authored by Nicolas Biri's avatar Nicolas Biri
Browse files

Integrate jsmf-core in to check properly JSMFAny properties

parent 021266d1
...@@ -16,14 +16,19 @@ Contributors: G. Garcia-Frey ...@@ -16,14 +16,19 @@ Contributors: G. Garcia-Frey
'use strict'; 'use strict';
var _ = require('lodash'); var _ = require('lodash');
var JSMF = require('jsmf-core');
/** @constuctor /** @constuctor
* Initiate a transformation module. * Initiate a transformation module.
*/ */
function Transformation() { function Transformation(rules, helpers) {
this.rules = []; var rs = [];
this.helpers = []; _.forEach(rules, function(x) {rs.push(x);});
this.rules = rs;
var hs = [];
_.forEach(helpers, function(x) {hs.push(x);});
this.helpers = hs;
} }
/** Add a transformation rule to a transformation module. /** Add a transformation rule to a transformation module.
...@@ -34,14 +39,27 @@ function Transformation() { ...@@ -34,14 +39,27 @@ function Transformation() {
* and output an array of created elements. * and output an array of created elements.
* @param {string} r.name - An optional name for the rule, not used yet. * @param {string} r.name - An optional name for the rule, not used yet.
*/ */
Transformation.prototype.addRule = function(r) {this.rules.push(r)}; Transformation.prototype.addRule = function(r) {
if (_.every(['in', 'out'], function(x) {return _.has(r, x);})) {
this.rules.push(r);
} else {
throw new Error('Invalid rule:' + r);
}
};
/** Add a helper to a transformation module. /** Add a helper to a transformation module.
* @param {Object} h - the helper. * @param {Object} h - the helper.
* @param {Function} h.generation - A function that will be applied on an input model * @param {Function} h.generation - A function that will be applied on an input model
* @param {string} h.name - the name of the helper, that will be used to reference the helper when we need it. * @param {string} h.name - the name of the helper, that will be used to reference the helper when we need it.
*/ */
Transformation.prototype.addHelper = function(h) {this.helpers.push(h)}; Transformation.prototype.addHelper = function(h) {
if (_.every(['map', 'name'], function(x) {return _.has(h, x);})) {
this.helpers.push(h);
} else {
throw new Error('Invalid helper:' + h);
}
};
function Context() { function Context() {
this.generated = new Mapping(); this.generated = new Mapping();
...@@ -50,7 +68,7 @@ function Context() { ...@@ -50,7 +68,7 @@ function Context() {
this.generationLog = new Mapping(); this.generationLog = new Mapping();
} }
Context.prototype.addResolution = function(r) {this.referencesResolutions.push(r)}; Context.prototype.addResolution = function(r) {this.referencesResolutions.push(r);};
Context.prototype.assign = function(element, relationName, populators) { Context.prototype.assign = function(element, relationName, populators) {
this.addResolution(new ReferenceResolution(element, relationName, populators)); this.addResolution(new ReferenceResolution(element, relationName, populators));
...@@ -94,7 +112,7 @@ function runRule(rule, context, inputModel, outputModel, debug) { ...@@ -94,7 +112,7 @@ function runRule(rule, context, inputModel, outputModel, debug) {
* @param {string} name - the name of the helper, that will be used to reference the helper when we need it. * @param {string} name - the name of the helper, that will be used to reference the helper when we need it.
*/ */
function Helper(generation, name) { function Helper(generation, name) {
this.map = generator; this.map = generation;
this.name = name; this.name = name;
} }
...@@ -121,7 +139,7 @@ function runResolution(resolution, generated) { ...@@ -121,7 +139,7 @@ function runResolution(resolution, generated) {
function(elem) { function(elem) {
var values = generated.valuesFor(elem) || []; var values = generated.valuesFor(elem) || [];
_.each(values, function(target) { _.each(values, function(target) {
if (relationType === undefined || hasClass(target, relationType)) { if (relationType === JSMF.JSMFAny || relationType === undefined || hasClass(target, relationType)) {
resolution.source[referenceFunctionName](target); resolution.source[referenceFunctionName](target);
} }
}); });
...@@ -131,9 +149,11 @@ function runResolution(resolution, generated) { ...@@ -131,9 +149,11 @@ function runResolution(resolution, generated) {
/** Apply a trsnformation on an input model, and put generated elements in a given ouput model. /** Apply a trsnformation on an input model, and put generated elements in a given ouput model.
* @param {Object} inputModel - The input model. * @param {Object} inputModel - The input model.
* @param {Object} outputModel - The output model. * @param {Object} outputModel - The output model.
* @param {boolean} debug - Store additional information during the transformation.
*/ */
Transformation.prototype.apply = function(inputModel, outputModel, debug) { Transformation.prototype.apply = function(inputModel, outputModel, debug) {
var ctx = new Context(); var ctx = new Context();
outputModel = outputModel || new JSMF.Model('TransformationOutput');
_.forEach(this.helpers, function(h) { _.forEach(this.helpers, function(h) {
runHelper(h, ctx, inputModel, outputModel); runHelper(h, ctx, inputModel, outputModel);
}); });
...@@ -186,6 +206,8 @@ Mapping.prototype.map = function(k, v) { ...@@ -186,6 +206,8 @@ Mapping.prototype.map = function(k, v) {
module.exports = { module.exports = {
Mapping: Mapping, Mapping: Mapping,
Helper: Helper,
Rule: Rule,
Transformation: Transformation Transformation: Transformation
}; };
'use strict';
var JSTL = require('../index');
var should = require('should');
describe('Transformation', function() {
it('can be initialized with rules', function(done) {
var r = new JSTL.Rule(function(x) {}, function(y) {});
var t = new JSTL.Transformation([r]);
t.rules.should.have.length(1);
done();
});
it('can be initialized with helpers', function(done) {
var h = new JSTL.Helper(function() {}, 'Foo');
var t = new JSTL.Transformation(undefined, [h]);
t.helpers.should.have.length(1);
done();
});
it('can be initialized with both', function(done) {
var h = new JSTL.Helper(function() {}, 'Foo');
var r = new JSTL.Rule(function(x) {}, function(y) {});
var t = new JSTL.Transformation([r], [h]);
t.rules.should.have.length(1);
t.helpers.should.have.length(1);
done();
});
});
describe('addHelper', function() {
it('checks that the helper has a name', function(done) {
var t = new JSTL.Transformation();
var noName = {map: function() {}};
(function() {t.addHelper(noName)}).should.throw();
done();
});
it('checks that the helper has a map', function(done) {
var t = new JSTL.Transformation();
var noMap = {name: 'foo'};
(function() {t.addHelper(noMap)}).should.throw();
done();
});
});
describe('addRule', function() {
it('checks that the rule has a in field', function(done) {
var t = new JSTL.Transformation();
var noIn = {out: function() {}};
(function() {t.addRule(noIn)}).should.throw();
done();
});
it('checks that the rule has a out field', function(done) {
var t = new JSTL.Transformation();
var noOut = {in: function() {}};
(function() {t.addRule(noOut)}).should.throw();
done();
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment