0

Hi guys need some help with this issue, i have one formula string (PerilRateP001_0+PerilRateP002_1+PerilRateP003_2+PerilRateP004_3)*SumInsured*1*(0.01) and also i have given json

scope {
  BasicRate: '0.0512',
  PerilRate: [ { 'Perils Code': 'AIRCRAFT DAMAGE', 'Basis Indicator': 'P' } ],
  ClauseRate: [
    {
      ClauseCode: 'SMOKE DAMAGE',
      'Basis Indicator': 'P',
      ApplyTo: 'F&L'
    }
  ],
  'Questionnaire 1': [ { QuestLoading1: 'Yes' } ],
  SumInsured: '100000',
  ClauseFactorRate: '0.002925',
  PerilRateP001_0: '0.0060',
  ClauseRateC022_0: '10.0000',
  Loading: '10',
  productId: 'FNL0001',
  BasicPremium1: 51.2
}

now what i want to do is i want to substitute value of variables in formula string from given json.the json does not have all the variables present in the formula string so what i want to do is if the variable is associated with + or - sign substitute it with 0 and if the variable is associated with * or / sign then substitute it with 1. Any idea how can i achieve it. Have been struggling with it from 2 days and still unable to figure out the solution any help is greatly appreciated

  • The most resilient solution would be an expression parser, particularly if you need to then evaluate it (avoiding `eval`). – Dave Newton Mar 03 '20 at 17:45
  • That is not JSON. Is that your actual data format? – str Mar 03 '20 at 17:49
  • @DaveNewton i am using math.js library and have access to methods like evaluate and parse but i am not sure how to use parse in correct way. Will parse function be sufficient for solving the above problem? –  Mar 03 '20 at 17:50
  • @str what i have posted here is what appears in my console. The actual data is json only –  Mar 03 '20 at 17:52
  • Is it always the same equation ? Or at least always the same variables in the equation ? – François Huppé Mar 03 '20 at 17:52
  • @FrançoisHuppé no its just one of the dynamic formulas that will get generated in the insurance application i am working on. All the formulas are dynamic and the json is also dynamic –  Mar 03 '20 at 18:01

1 Answers1

0

Using math.js, here is how you could do:

var data = {
  BasicRate: '0.0512',
  PerilRate: [ { 'Perils Code': 'AIRCRAFT DAMAGE', 'Basis Indicator': 'P' } ],
  ClauseRate: [
    {
      ClauseCode: 'SMOKE DAMAGE',
      'Basis Indicator': 'P',
      ApplyTo: 'F&L'
    }
  ],
  'Questionnaire 1': [ { QuestLoading1: 'Yes' } ],
  SumInsured: '100000',
  ClauseFactorRate: '0.002925',
  PerilRateP001_0: '0.0060',
  ClauseRateC022_0: '10.0000',
  Loading: '10',
  productId: 'FNL0001',
  BasicPremium1: 51.2
};

var formula = '(PerilRateP001_0+PerilRateP002_1+PerilRateP003_2+PerilRateP004_3)*SumInsured*1*(0.01)';

// parse formula into an expression tree
var formulaTree = math.parse(formula);

// use filter to find variables present in the formula
var variables = formulaTree.filter(function (node) {
    // check if node is a variable
    if(node.isSymbolNode){ 
        // check if variable is in data array   
        if(data[node.name]){  
            formula = formula.replace(node.name, data[node.name]);
        }
        else{
            // replace missing variable with 0
            formula = formula.replace(node.name, 0);

            // fix product and division by 0
            formula = formula.replace('*0', '*1');
            formula = formula.replace('/0', '/1'); 
        }
    }
});

console.log(formula);
console.log(math.evaluate(formula));
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.1/math.js"></script>
François Huppé
  • 1,805
  • 1
  • 3
  • 14
  • That's a lot of work for something MathJS already does. Completely removing variables is nice for debugging, but useless for math. – Dave Newton Mar 03 '20 at 18:40
  • But the problem is to find and replace the formula's unknown variables by ones or zeros. I'm not sure math.js evaluate method can directly do this.... – François Huppé Mar 03 '20 at 18:51
  • No, it won't--you're right, I missed the info at the bottom of the question. Parsing is the answer. – Dave Newton Mar 03 '20 at 19:24