| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | |
| 2 | var _ = require('lodash'); |
| 3 | |
| 4 | /* |
| 5 | * Args: |
| 6 | * rules - object with the monitoring param to simulator function mapping |
| 7 | * see data/simmp.json |
| 8 | */ |
| 9 | SimMp = function(rules) { |
| 10 | this.rules = _.clone(rules, true); |
| 11 | }; |
| 12 | |
| 13 | //SimMp.prototype.map_rule = function(mp_id) { |
| 14 | // return this.rules['mp-mapper'][mp_id]; |
| 15 | //} |
| 16 | |
| 17 | // Use the monitoring param id for now |
| 18 | SimMp.prototype.createSimMonitorFunc = function(mp) { |
| 19 | |
| 20 | // Define our core simulation function here |
| 21 | // |
| 22 | // min, max inclusive |
| 23 | var rand_func = function(min, max) { |
| 24 | return Math.floor(Math.random() * (max-min+1)) + min; |
| 25 | } |
| 26 | |
| 27 | var funcs = { |
| 28 | // transmit and receive rate |
| 29 | tx_rc_rate: function(value, elapsed_seconds) { |
| 30 | // Ignore elapsed time for first implementation of transmit and |
| 31 | // receive rate simulation. |
| 32 | // This is just a quick and dirty and simple implementation to make |
| 33 | // the monitoring params change, stay within bounds, and not swing |
| 34 | // wildly. |
| 35 | var min_val = mp.min_value; |
| 36 | var max_val = mp.max_value; |
| 37 | // Set an outer bound of maxmium change from current value |
| 38 | // Tweak bin_count to set how much the value can swing from the |
| 39 | // last value |
| 40 | var bin_count = 10; |
| 41 | // Set the range we can generate the new value based on a function |
| 42 | // of the difference of the max and min values |
| 43 | var max_delta = (max_val - min_val) / bin_count; |
| 44 | console.log('Setting max_delta = %s', max_delta); |
| 45 | var new_val = rand_func( |
| 46 | Math.max(min_val, value-max_delta), |
| 47 | Math.min(max_val, value+max_delta)); |
| 48 | //console.log("Generated value: %s", new_val); |
| 49 | return new_val; |
| 50 | }, |
| 51 | packet_size: function(value, elapsed_seconds) { |
| 52 | // Stub method just returns value unchanged |
| 53 | // TODO: Figure out how we want to vary packet sizes |
| 54 | return value; |
| 55 | }, |
| 56 | accumulate: function(value, elapsed_seconds) { |
| 57 | // NOT TESTED. Basic idea. Will want to add variablility |
| 58 | // how fast we accumulate |
| 59 | var accumulate_rate = 0.1; |
| 60 | var new_value = value + (elapsed_seconds * accumulate_rate); |
| 61 | return new_value; |
| 62 | } |
| 63 | // add growth function |
| 64 | }; |
| 65 | |
| 66 | // Declare our monitoring param id to sim function mapping here |
| 67 | // TODO: Move out to a yaml/json file and make this function belong to |
| 68 | // a 'Class' |
| 69 | //var mapper = { |
| 70 | // 'tx-rate-pp1': funcs['tx_rc_rate'], |
| 71 | // 'rc-rate-pp1': funcs['tx_rc_rate'] |
| 72 | //}; |
| 73 | |
| 74 | var sim_func_name = this.rules['mp-mapper'][mp.id]; |
| 75 | if (sim_func_name) { |
| 76 | return funcs[sim_func_name]; |
| 77 | } else { |
| 78 | console.log('No time step sim function found for monitoring param with id "%s", using constant value', mp.id); |
| 79 | return function(value, elapsed_seconds) { |
| 80 | return value; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | module.exports = { |
| 86 | SimMp: SimMp |
| 87 | }; |