91 lines
1.9 KiB
JavaScript
91 lines
1.9 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
/**
|
||
|
* Module dependencies.
|
||
|
*/
|
||
|
var mongoose = require('mongoose'),
|
||
|
Schema = mongoose.Schema,
|
||
|
_ = require('lodash'),
|
||
|
tree = require('mongoose-tree'),
|
||
|
math = require('math'),
|
||
|
deasync = require('fibers');
|
||
|
|
||
|
|
||
|
var BooleanExpresssionSchema = new Schema({
|
||
|
expressionString: {
|
||
|
type: String,
|
||
|
},
|
||
|
result: {
|
||
|
type: Boolean,
|
||
|
}
|
||
|
});
|
||
|
|
||
|
/*
|
||
|
BooleanExpresssionSchema.plugin(tree, {
|
||
|
pathSeparator : '#' // Default path separator
|
||
|
onDelete : 'DELETE' // Can be set to 'DELETE' or 'REPARENT'. Default: 'REPARENT'
|
||
|
numWorkers: 5 // Number of stream workers
|
||
|
idType: Schema.ObjectId // Type used for _id. Can be, for example, String generated by shortid module
|
||
|
});
|
||
|
*/
|
||
|
|
||
|
BooleanExpresssionSchema.methods.evaluate = function(){
|
||
|
|
||
|
//Get headNode
|
||
|
var headNode = math.parse(expressionString);
|
||
|
var expressionScope = {};
|
||
|
var that = this;
|
||
|
|
||
|
//Create scope
|
||
|
headNode.traverse(function (node, path, parent) {
|
||
|
if(node.type === 'SymbolNode'){
|
||
|
|
||
|
Fiber()
|
||
|
mongoose.model('Field')
|
||
|
.findOne({_id: node.name}).exec(function(err, field){
|
||
|
if(err) {
|
||
|
console.log(err);
|
||
|
throw new Error(err);
|
||
|
}
|
||
|
|
||
|
if(!!_.parseInt(field.fieldValue)){
|
||
|
that.expressionScope[node.name] = _.parseInt(field.fieldValue);
|
||
|
}else {
|
||
|
that.expressionScope[node.name] = field.fieldValue;
|
||
|
}
|
||
|
console.log('_id: '+node.name);
|
||
|
console.log('value: '+that.expressionScope[node.name]);
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var code = node.compile();
|
||
|
var result = code.eval(expressionScope);
|
||
|
|
||
|
this.result = result;
|
||
|
return result;
|
||
|
});
|
||
|
|
||
|
mongoose.model('BooleanStatement', BooleanStatementSchema);
|
||
|
|
||
|
/**
|
||
|
* Form Schema
|
||
|
*/
|
||
|
var LogicJumpSchema = new Schema({
|
||
|
created: {
|
||
|
type: Date,
|
||
|
default: Date.now
|
||
|
},
|
||
|
lastModified: {
|
||
|
type: Date,
|
||
|
},
|
||
|
|
||
|
BooleanExpression: {
|
||
|
type: Schema.Types.ObjectId,
|
||
|
ref: 'BooleanExpression'
|
||
|
},
|
||
|
|
||
|
});
|
||
|
|
||
|
mongoose.model('LogicJump', LogicJumpSchema);
|