Skip to content

Commit

Permalink
implement lazy mode - upgrade graalvm
Browse files Browse the repository at this point in the history
Issue #822
  • Loading branch information
rsoika committed Apr 23, 2023
1 parent 2f93f9b commit d92fb86
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class RuleEngine {

private static Logger logger = Logger.getLogger(RuleEngine.class.getName());

private Context context = null;
private Context _context = null;
private String languageId;

/**
Expand All @@ -99,7 +99,7 @@ public RuleEngine() {
}

/**
* This method initializes the script engine.
* This method initializes the script engine with a specific languageId.
*
* @param scriptLanguage
*/
Expand All @@ -115,31 +115,37 @@ public RuleEngine(final String languageID) {
/**
* This method initializes a context with default configuration.
*
* We set the option 'WarnInterpreterOnly' to false.
* See also here: https://www.graalvm.org/22.0/reference-manual/js/FAQ/
*
* Issue #821
*
* @param languageId
*/
void init(final String languageId) {
long l=System.currentTimeMillis();
this.languageId = languageId;
// context = Context.newBuilder(languageId).allowAllAccess(true).build();
context = Context.newBuilder(languageId) //
.option("engine.WarnInterpreterOnly", "false") //
.allowAllAccess(true) //
.build();
logger.info("...init RuleEngine took " + (System.currentTimeMillis()-l) + "ms");
// lazy initialization - see getContext()
_context=null;
}

/**
* Returns the current polyglot context
*
* This method implements a lazy initialization of the context.
* See Issue #822
*
* We also set the option 'WarnInterpreterOnly' to false.
* See also here: https://www.graalvm.org/22.0/reference-manual/js/FAQ/
*
* Issue #821
* @return
*/
public Context getContext() {
return context;
// init context the first time?
if (_context==null) {
long l=System.currentTimeMillis();
_context = Context.newBuilder(languageId) //
.option("engine.WarnInterpreterOnly", "false") //
.allowAllAccess(true) //
.build();
logger.info("...init RuleEngine took " + (System.currentTimeMillis()-l) + "ms");
}
return _context;
}

/**
Expand All @@ -150,11 +156,11 @@ public Context getContext() {
* @param value
*/
public void putMember(String identifier, Object value) {
context.getBindings(languageId).putMember(identifier, value);
getContext().getBindings(languageId).putMember(identifier, value);
}

public Value eval(String script) {
Value result = context.eval(languageId, script);
Value result = getContext().eval(languageId, script);
return result;
}

Expand Down Expand Up @@ -277,7 +283,7 @@ public ItemCollection evaluateBusinessRule(String script, ItemCollection workite
public ItemCollection convertResult() {

// do we have a result object?
Value resultValue = context.getBindings(languageId).getMember("result");
Value resultValue = getContext().getBindings(languageId).getMember("result");
if (resultValue == null) {
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
<microprofile.version>3.0</microprofile.version>
<microprofile-metrics.version>2.3</microprofile-metrics.version>
<lucene.version>7.7.3</lucene.version>
<graalvm.version>22.3.1</graalvm.version>

<!-- 20.2.0 22.3.1 -->
<graalvm.version>22.3.2</graalvm.version>
</properties>


Expand Down

0 comments on commit d92fb86

Please sign in to comment.