예제 #1
1
  public void doRFileEvaluate(final IScope scope, final IContainer param) {
    int size = param.length(scope);
    // if ( size == 0 ) { throw GamaRuntimeException.error("Missing Parameter Exception", scope); }

    final String RFile = getPath();
    try {
      // Call R
      RCaller caller = new RCaller();

      String RPath = GamaPreferences.LIB_R.value(scope).getPath();
      caller.setRscriptExecutable(RPath);

      double[] vectorParam = new double[param.length(scope)];

      int k = 0;
      for (Object o : param.iterable(scope)) {
        vectorParam[k++] = Cast.asFloat(scope, o);
      }

      RCode c = new RCode();
      // Adding the parameters
      c.addDoubleArray("vectorParam", vectorParam);

      // Adding the codes in file
      List<String> R_statements = new ArrayList<String>();

      // tmthai.begin----------------------------------------------------------------------------
      String fullPath = FileUtils.constructAbsoluteFilePath(scope, RFile, true);
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute_param.RScript:" + RPath);
        GuiUtils.debug("Stats.R_compute_param.Param:" + vectorParam.toString());
        GuiUtils.debug("Stats.R_compute_param.RFile:" + RFile);
        GuiUtils.debug("Stats.R_compute_param.fullPath:" + fullPath);
      }

      // FileReader fr = new FileReader(RFile);
      FileReader fr = new FileReader(fullPath);
      // tmthai.end----------------------------------------------------------------------------

      BufferedReader br = new BufferedReader(fr);
      String statement;

      while ((statement = br.readLine()) != null) {
        c.addRCode(statement);
        R_statements.add(statement);
        // java.lang.System.out.println(statement);
      }
      br.close();
      fr.close();
      caller.setRCode(c);

      GamaMap<String, IList> result = GamaMapFactory.create(Types.STRING, Types.LIST);

      String var = computeVariable(R_statements.get(R_statements.size() - 1).toString());
      caller.runAndReturnResult(var);

      // DEBUG:
      // java.lang.System.out.println("Name: '" + R_statements.length(scope) + "'");
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute_param.R_statements.length: '" + R_statements.size() + "'");
      }

      for (String name : caller.getParser().getNames()) {
        String[] results = null;
        results = caller.getParser().getAsStringArray(name);
        // java.lang.System.out.println("Name: '" + name + "'");
        if (DEBUG) {
          GuiUtils.debug(
              "Stats.R_compute_param.caller.Name: '"
                  + name
                  + "' length: "
                  + results.length
                  + " - Value: "
                  + results.toString());
        }

        result.put(name, GamaListFactory.create(scope, Types.NO_TYPE, results));
      }

      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute_param.return:" + result.serialize(false));
      }

      setBuffer(result);

    } catch (Exception ex) {

      throw GamaRuntimeException.error("RCallerExecutionException " + ex.getMessage(), scope);
    }
  }
예제 #2
0
  public void init(IScope scope) {
    isAgentCreated = false;

    varmap = GamaMapFactory.create(Types.NO_TYPE, Types.NO_TYPE);
    numvarmap = GamaMapFactory.create(Types.NO_TYPE, Types.NO_TYPE);
    qualivarmap = GamaMapFactory.create(Types.NO_TYPE, Types.NO_TYPE);
    metadatahistory = new GamaObjectMatrix(0, 0, msi.gaml.types.Types.NO_TYPE);
    lastdetailedvarvalues = new GamaObjectMatrix(0, 0, msi.gaml.types.Types.NO_TYPE);
    averagehistory = new GamaFloatMatrix(0, 0);
    stdevhistory = new GamaFloatMatrix(0, 0);
    minhistory = new GamaFloatMatrix(0, 0);
    maxhistory = new GamaFloatMatrix(0, 0);
    distribhistoryparams = new GamaObjectMatrix(0, 0, msi.gaml.types.Types.NO_TYPE);
    distribhistory = new GamaObjectMatrix(0, 0, msi.gaml.types.Types.NO_TYPE);
    IList premlist = GamaListFactory.create(Types.NO_TYPE);
    premlist.add(0);
    premlist.add(0);
    // premlist.add(0);
    distribhistory.set(scope, 0, 0, premlist);

    /*multi_metadatahistory = new GamaObjectMatrix(0,0);
    		multi_lastdetailedvarvalues = new GamaObjectMatrix(0,0);
    		multi_averagehistory = new GamaFloatMatrix(0,0);
    		multi_stdevhistory = new GamaFloatMatrix(0,0);
    		multi_minhistory = new GamaFloatMatrix(0,0);
    		multi_maxhistory = new GamaFloatMatrix(0,0);
    		multi_distribhistoryparams = new GamaObjectMatrix(0,0);
    		multi_distribhistory = new GamaObjectMatrix(0,0);
    		GamaList deuzlist=new GamaList();
    		deuzlist.add(0);
    		deuzlist.add(0);
    		//deuzlist.add(0);
    		multi_distribhistory.set(scope, 0, 0, deuzlist);
    */
  }
예제 #3
0
 @Override
 public GamaMap mapValue(
     final IScope scope, final IType keyType, final IType contentsType, final boolean copy)
     throws GamaRuntimeException {
   final IList<IAgent> agents = listValue(scope, contentsType, false);
   // Default behavior : Returns a map containing the names of agents as keys and the agents
   // themselves as values
   final GamaMap result =
       GamaMapFactory.create(Types.STRING, scope.getModelContext().getTypeNamed(getName()));
   for (final IAgent agent : agents.iterable(scope)) {
     result.put(agent.getName(), agent);
   }
   return result;
 }
예제 #4
0
  public void doRFileEvaluate(final IScope scope) {
    final String RFile = getPath();
    try {
      // Call R
      RCaller caller = new RCaller();

      String RPath = GamaPreferences.LIB_R.value(scope).getPath();
      caller.setRscriptExecutable(RPath);
      // caller.setRscriptExecutable("\"" + RPath + "\"");
      // if(java.lang.System.getProperty("os.name").startsWith("Mac"))
      // {
      // caller.setRscriptExecutable(RPath);
      // }

      RCode c = new RCode();
      List<String> R_statements = new ArrayList<String>();

      // tmthai.begin----------------------------------------------------------------------------
      String fullPath = FileUtils.constructAbsoluteFilePath(scope, RFile, true);
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute.RScript:" + RPath);
        GuiUtils.debug("Stats.R_compute.RFile:" + RFile);
        GuiUtils.debug("Stats.R_compute.fullPath:" + fullPath);
      }

      // FileReader fr = new FileReader(RFile);
      FileReader fr = new FileReader(fullPath);
      // tmthai.end----------------------------------------------------------------------------

      BufferedReader br = new BufferedReader(fr);
      String statement;
      while ((statement = br.readLine()) != null) {
        c.addRCode(statement);
        R_statements.add(statement);
        // java.lang.System.out.println(statement);
        if (DEBUG) {
          GuiUtils.debug("Stats.R_compute.statement:" + statement);
        }
      }

      fr.close();
      br.close();
      caller.setRCode(c);

      GamaMap<String, IList> result = GamaMapFactory.create(Types.STRING, Types.LIST);

      String var = computeVariable(R_statements.get(R_statements.size() - 1).toString());
      caller.runAndReturnResult(var);
      for (String name : caller.getParser().getNames()) {
        Object[] results = null;
        results = caller.getParser().getAsStringArray(name);
        // for (int i = 0; i < results.length; i++) {
        // java.lang.System.out.println(results[i]);
        // }
        if (DEBUG) {
          GuiUtils.debug(
              "Stats.R_compute_param.caller.Name: '"
                  + name
                  + "' length: "
                  + results.length
                  + " - Value: "
                  + results.toString());
        }
        result.put(name, GamaListFactory.createWithoutCasting(Types.NO_TYPE, results));
      }
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute.return:" + result.serialize(false));
      }
      // return result;
      setBuffer(result);

    } catch (Exception ex) {

      throw GamaRuntimeException.error("RCallerExecutionException " + ex.getMessage(), scope);
    }
  }