ScriptableCondition implements IConditionalExecution { public boolean shouldExecute( final Map currentInputs, final Log logger ) throws Exception { boolean shouldExecute = this.getDefaultResult(); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName( this.getScriptLanguage() ); if ( engine == null ) { throw new IllegalArgumentException( Messages.getInstance().getErrorString( "ScriptableCondition.ERROR_0001_ENGINE_NOT_AVAILABLE", this.getScriptLanguage() ) ); } Object inputValue; IActionParameter inputParameter; String inputName = null; Iterator inputs = currentInputs.entrySet().iterator(); Map.Entry mapEntry; while ( inputs.hasNext() ) { mapEntry = (Map.Entry) inputs.next(); inputName = (String) mapEntry.getKey(); if ( this.getIgnoreInputNamesWithMinus() && inputName.indexOf( '-' ) >= 0 ) { logger.info( Messages.getInstance().getString( "ScriptableCondition.INFO_IGNORED_INPUT", inputName ) ); continue; } inputParameter = (IActionParameter) mapEntry.getValue(); inputValue = inputParameter.getValue(); engine.put( inputName, inputValue ); } engine.put( "out", System.out ); engine.put( "rule", this ); Object resultObject = engine.eval( this.getScript() ); if ( resultObject instanceof Boolean ) { return ( (Boolean) resultObject ).booleanValue(); } else if ( resultObject instanceof String ) { return ( "true".equalsIgnoreCase( resultObject.toString() ) ) || ( "yes".equalsIgnoreCase( resultObject.toString() ) ); } else if ( resultObject instanceof Number ) { return ( (Number) resultObject ).intValue() > 0; } else if ( resultObject instanceof IPentahoResultSet ) { return ( (IPentahoResultSet) resultObject ).getRowCount() > 0; } logger.info( Messages.getInstance().getString( "ScriptableCondition.INFO_DEFAULT_RESULT_RETURNED" ) ); return shouldExecute; } ScriptableCondition(); String getScript(); void setScript( final String script ); void setScriptLanguage( final String value ); void setDefaultResult( final boolean value ); boolean getDefaultResult(); String getScriptLanguage(); void setIgnoreInputNamesWithMinus( final boolean value ); boolean getIgnoreInputNamesWithMinus(); void setListAvailableEngines( final boolean value ); boolean shouldExecute( final Map currentInputs, final Log logger );  }
@Test public void shouldExecute() throws Exception { ScriptableCondition scriptableCondition = new ScriptableCondition(); scriptableCondition.setScript( "var result = 'true'; result;" ); scriptableCondition.shouldExecute( Collections.EMPTY_MAP, logger ); }