0

Is there a problem with calling an expression inside another expression ? Cause when I do:

create expression int js:hexToBin(hex) [
  var bin = '';
  for (var i = 0; i <= hex.length - 1; i += 1) {
    bin += ('0000' + parseInt(hex.substr(i, 1), 16).toString(2)).slice(-4);
  };
  parseInt(bin, 2);
];

create expression int js:getTemperature(hex) [
  hexToBin(hex.substring(4, 6));
];

I got an error \"hexToBin\" is not defined. (getTemperature#2). But when I put all of the code in getTemperature it works properly.

Gaetan L.
  • 649
  • 6
  • 20

2 Answers2

0

The JavaScript code is not aware of the EPL expressions. It would be interesting to research whether Rhino could be told of the EPL expressions.

user3613754
  • 816
  • 1
  • 5
  • 5
0

As already said in the expression you are in JavaScript and it has no idea of esper.

But an expression does not need to contain a single function. It is just some entry to a script so you can define multiple JavaScript functions within a single expression if you want to structure the code.

TyrManuZ
  • 2,039
  • 1
  • 14
  • 23
  • Okay but I need multiple functions to access to my hexToBin function. If I create an expression getTemperature and include hexToBin as a JS function inside, I would have to duplicate hexToBin if I have another expression getSomethingElse that needs it as well, wholdn't I? I need a way to call a function from multiple functions. – Gaetan L. Jun 29 '16 at 10:26
  • Not 100% sure how everything is suposed to look in the end. So far the only thing the getTemperature does is a substring. You have this functionality also available in esper directly (through the .subsctring function of Java String). Maybe you only need the hexToBin function – TyrManuZ Jun 29 '16 at 11:05
  • Sure I can figure out something in this case, but I was wondering if it would be able to factorize code for more complex cases? – Gaetan L. Jun 29 '16 at 11:54
  • Maybe this helps http://stackoverflow.com/questions/650377/javascript-rhino-use-library-or-include-other-scripts – goodie Jun 29 '16 at 20:59