Package com.cloutree.modelevaluator.impl.pmml.scripting

Examples of com.cloutree.modelevaluator.impl.pmml.scripting.ScriptProcessor


      log.log(Level.WARNING, "Model-File not set properly");
      result.addError("Model-File not set properly");
      return result;
    }
   
    ScriptProcessor processor = ScriptFactory.getScriptProcessor(ScriptFactory.Types.JAVASCRIPT);
   
    // Do PreProcessing on Parameters
    if(this.preProcessor != null && !this.preProcessor.isEmpty())
      processor.doScriptProcessing(this.preProcessor, parameters);
   
    // Compile parameters for R
    String rParamaterAssignString = "params <- data.frame(";
    boolean initial = true;
    for(String key : parameters.keySet()) {
      try {
        String obj = (String)parameters.get(key);
        if(initial) {
          rParamaterAssignString = rParamaterAssignString + key + "=" + obj;
          initial= false;
        } else {
          rParamaterAssignString = rParamaterAssignString + "," + key + "=" + obj;
        }
      } catch(ClassCastException e) {
        log.log(Level.WARNING, "Parameter " + key + "->" + parameters.get(key) + " seems to be no String, which was expected for native R! Continouing without this parameter now...");
        result.addError("Parameter " + key + "->" + parameters.get(key) + " could not be read (String expected)!");
      }
    }
   
    //Get model name out of file
    this.engine.eval("modelname<-load('"+ this.modelFile.getFile().getPath());
    String modelName = this.engine.eval("modelname").asString();
   
    REXP rResult = this.engine.eval("predict(" + modelName + "," + rParamaterAssignString);
   
    if(rResult == null || rResult.getType() == REXP.XT_NULL) {
      result.addError("Empty R result, model has an error");
    } else {
      Map<String, Object> tempPredictions = new HashMap<String, Object>();
      this.processRResult(rResult, tempPredictions, "result");
    }
   
    // Do Post-Processing
    if(this.postProcessor != null && !this.postProcessor.isEmpty())
      processor.doScriptProcessing(this.postProcessor, (Map<String, Object>) result.getOutputValues());
      processor.doScriptProcessing(this.postProcessor, (Map<String, Object>) result.getPredictedValues());
   
    return result;
  }
View Full Code Here


    PmmlPredictiveModelResult result = new PmmlPredictiveModelResult(this, parameters);
    Map<FieldName, Object> arguments = new LinkedHashMap<FieldName, Object>();
    List<FieldName> activeFields = this.evaluator.getActiveFields();
    Map<FieldName, ?> pmmlResult;
 
    ScriptProcessor processor = ScriptFactory.getScriptProcessor(ScriptFactory.Types.JAVASCRIPT);
   
    // Do PreProcessing on Parameters
    if(this.preProcessor != null && !this.preProcessor.isEmpty())
      processor.doScriptProcessing(this.preProcessor, parameters);
   
    for(FieldName activeField : activeFields){
      DataField dataField = this.evaluator.getDataField(activeField);
      Object value = parameters.get(dataField.getName().getValue());
     
      if(value == null || value.toString().isEmpty()) {
          result.addError("No parameter found for: " + dataField.getName());
          log.log(Level.WARNING, "No parameter found for: " + dataField.getName());
      } else {
          try {
        arguments.put(activeField, evaluator.prepare(activeField, value));
          } catch (Exception e) {
        result.addError("Field " + activeField.getValue() + " has invalid value " + value);
        log.log(Level.SEVERE, e.getMessage());
        result.setValid(false);
        return result;
          }
      }
 
    }
   
    try {
        pmmlResult = evaluator.evaluate(arguments);
    } catch (Exception e) {
        result.addError("Unable to evaluate model: " + e.getMessage());
        log.log(Level.WARNING, "Unable to evaluate model: " + e.getMessage());
        return result;
    }
   
    List<FieldName> predictedFields = evaluator.getPredictedFields();
    for(FieldName predictedField : predictedFields){
      DataField dataField = evaluator.getDataField(predictedField);
      Object predictedValue = pmmlResult.get(predictedField);
      result.addPredictedValue(dataField.getName().getValue(), predictedValue);
    }
 
    List<FieldName> resultFields = this.evaluator.getOutputFields();
    for(FieldName resultField : resultFields){
      OutputField outputField = evaluator.getOutputField(resultField);
      Object outputValue = pmmlResult.get(resultField);
      result.addOutputValue(outputField.getName().getValue(), outputValue);
    }
   
    if(this.postProcessor != null && !this.postProcessor.isEmpty()) {
      processor.doScriptProcessing(this.postProcessor, (Map<String, Object>) result.getOutputValues());
      processor.doScriptProcessing(this.postProcessor, (Map<String, Object>) result.getPredictedValues());
    }
   
    return result;
    }
View Full Code Here

TOP

Related Classes of com.cloutree.modelevaluator.impl.pmml.scripting.ScriptProcessor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.