Hitachi Vantara Pentaho Community Forums
Results 1 to 6 of 6

Thread: Executing a field a user defined java expression

  1. #1
    Join Date
    Mar 2012
    Posts
    10

    Default Executing a field a user defined java expression

    So, dumb question time.

    Say that I've built up a string in a field that's a valid Java expression. For example, I've built up a field to contain the string

    Code:
    ((someField == null || someField.isEmpty() || someField.startsWith("8872")) ? otherField.concat(someOtherField.substring(Integer.parseInt(startValue), Integer.parseInt(endValue)):someField.concat(aDifferentField)
    and now I want to execute that as an expression in a User Defined Java Expression step. (Yes, someField, someOtherField, otherField, aDifferentField, "8872", startValue and endValue are all arbitrary fields that are unknown until run time.) Is there a way to do that?

  2. #2
    Join Date
    Oct 2010
    Posts
    369

    Default

    ifcourse its not a dump question may be set variable and get startvalue and endvalue with $startvalue in java expression

  3. #3
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    Java expressions (UDJE) are not meant for this.
    Try a Java class (UDJC) or JavaScript (MJSV).
    MJSV gives you access to runtime evaluation (eval).
    With UDJC you will need a different approach, though.
    So long, and thanks for all the fish.

  4. #4
    Join Date
    Mar 2012
    Posts
    10

    Default

    OK, so I've switched to the Modified Java Script Value step. Here's what I'm trying:

    I've got three rows and they're all type String.

    rowOne has the value "TestName"
    rowTwo has the value "SomeDataY"
    rowThree has the value "rowOne + rowTwo.substring(4,9)"

    This is the test script in the MJSV step that I'm running.

    Code:
    var expressionToEval = new java.lang.String(rowThree);
    
    var strExpression = new java.lang.String("rowOne + rowTwo.substring(4,9)");
    
    
    var updateRawString = eval("rowOne + rowTwo.substring(4,9)"); //Evaluates to "TestNameDataY"
    
    
    var updateRow2String = eval(expressionToEval); //Evaluates to "rowOne + rowTwo.substring(4,9)"
    
    
    var updateFromRow = eval(rowThree); //Evaluates to "rowOne + rowTwo.substring(4,9)"
    
    
    var updateJavaString = eval(strExpression); //Evaluates to "rowOne + rowTwo.substring(4,9)"
    All of the update values save updateRawString are "rowOne + rowTwo.substring(4,9)" which is not what I'm looking for. What I want to have happen is what updateRawString gets set to, which is "TestNameDataY". Is there something I need to do to a JS var to get the eval function to treat it the same way it treats a static string?

  5. #5
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    eval() expects a string, not a Java object - proof:

    Code:
    rowOne = rowOne.toString();
    rowTwo = rowTwo.toString();
    var result = eval(rowThree.toString());
    So long, and thanks for all the fish.

  6. #6
    Join Date
    Mar 2012
    Posts
    10

    Default

    But expressionToEval... Wasn't that... Didn't I already try...

    Well, anyway that did the trick! ​Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.