Example #1
0
  @Override
  public Object report(final org.nlogo.nvm.Context context) throws LogoException {
    LogoList list = argEvalList(context, 0);
    int start = argEvalIntValue(context, 1);
    int stop = argEvalIntValue(context, 2);
    int size = list.size();
    if (start < 0) {
      throw new EngineException(
          context,
          this,
          I18N.errorsJ().getN("org.nlogo.prim.etc._sublist.startIsLessThanZero", start));
    } else if (stop < start) {
      throw new EngineException(
          context,
          this,
          I18N.errorsJ().getN("org.nlogo.prim.etc._sublist.endIsLessThanStart", stop, start));

    } else if (stop > size) {
      throw new EngineException(
          context,
          this,
          I18N.errorsJ().getN("org.nlogo.prim.etc._sublist.endIsGreaterThanListSize", stop, size));
    }
    return list.logoSublist(start, stop);
  }
Example #2
0
 public double report_1(Context context, double d0, double d1) throws LogoException {
   if (d1 == 0) {
     throw new EngineException(
         context, this, I18N.errorsJ().get("org.nlogo.prim.etc.$common.divByZero"));
   }
   return validDouble(d0 - (StrictMath.floor(d0 / d1) * d1));
 }
Example #3
0
 @Override
 public Object report(final org.nlogo.nvm.Context context) throws LogoException {
   int index = argEvalIntValue(context, 0);
   Object obj = args[1].report(context);
   if (index < 0) {
     throw new EngineException(
         context, this, I18N.errorsJ().getN("org.nlogo.prim.etc.$common.negativeIndex", index));
   }
   if (obj instanceof LogoList) {
     LogoList list = (LogoList) obj;
     if (index >= list.size()) {
       throw new EngineException(
           context,
           this,
           I18N.errorsJ()
               .getN(
                   "org.nlogo.prim.etc.$common.indexExceedsListSize",
                   index,
                   Dump.logoObject(list),
                   list.size()));
     }
     return list.get(index);
   } else if (obj instanceof String) {
     String string = (String) obj;
     if (index >= string.length()) {
       throw new EngineException(
           context,
           this,
           I18N.errorsJ()
               .getN(
                   "org.nlogo.prim.etc.$common.indexExceedsListSize",
                   index,
                   Dump.logoObject(string),
                   string.length()));
     }
     return string.substring(index, index + 1);
   } else {
     throw new ArgumentTypeException(
         context, this, 1, Syntax.ListType() | Syntax.StringType(), obj);
   }
 }
  public void deleteFile(String filePath) throws java.io.IOException {
    org.nlogo.api.File file = findOpenFile(filePath);

    if (file != null) {
      throw new java.io.IOException("You need to close the file before deletion");
    }
    java.io.File checkFile = new java.io.File(filePath);
    if (!checkFile.exists()) {
      throw new java.io.IOException(
          I18N.errorsJ().get("org.nlogo.workspace.DefaultFileManager.cannotDeleteNonExistantFile"));
    }
    if (!checkFile.canWrite()) {
      throw new java.io.IOException("Modification to this file is denied.");
    }
    if (!checkFile.isFile()) {
      throw new java.io.IOException(
          I18N.errorsJ().get("org.nlogo.workspace.DefaultFileManager.canOnlyDeleteFiles"));
    }

    if (!checkFile.delete()) {
      throw new java.io.IOException("Deletion failed.");
    }
  }
Example #5
0
 // identical to perform_1() above... BUT with a profiling hook added
 public void profiling_perform_1(final org.nlogo.nvm.Context context) {
   if (!context.atTopActivation()) {
     context.finished = true;
   } else {
     if (context.activation.procedure().isReporter()
         || context.activation.procedure().isTask()
             && context.activation.procedure().parent().isReporter()) {
       throw new EngineException(
           context,
           this,
           I18N.errorsJ()
               .getN("org.nlogo.prim.etc._stop.notAllowedInsideToReport", displayName()));
     }
     workspace.profilingTracer().closeCallRecord(context, context.activation);
     context.stop();
   }
 }
  public void ensureMode(org.nlogo.api.FileMode openMode) throws java.io.IOException {
    if (!hasCurrentFile()) {
      throw new java.io.IOException(
          I18N.errorsJ().get("org.nlogo.workspace.DefaultFileManager.noOpenFile"));
    }

    if (currentFile.mode() == org.nlogo.api.FileModeJ.NONE()) {
      try {
        currentFile.open(openMode);
      } catch (java.io.FileNotFoundException ex) {
        throw new java.io.IOException(
            "The file " + currentFile.getAbsolutePath() + " cannot be found");
      } catch (java.io.IOException ex) {
        throw new java.io.IOException(ex.getMessage());
      }
    } else if (currentFile.mode() != openMode) {
      String mode = (currentFile.mode() == org.nlogo.api.FileModeJ.READ()) ? "READING" : "WRITING";

      throw new java.io.IOException("You can only use " + mode + " primitives with this file");
    }
  }
Example #7
0
 public void perform_1(final org.nlogo.nvm.Context context) {
   // check: are we in an ask?
   if (!context.atTopActivation()) {
     // if so, then "stop" means that this agent prematurely
     // finishes its participation in the ask.
     context.finished = true;
   } else {
     // if we're not in an ask, then "stop" means to exit this procedure
     // immediately.  first we must check that it's a command procedure
     // and not a reporter procedure.
     if (context.activation.procedure().isReporter()
         || context.activation.procedure().isTask()
             && context.activation.procedure().parent().isReporter()) {
       throw new EngineException(
           context,
           this,
           I18N.errorsJ()
               .getN("org.nlogo.prim.etc._stop.notAllowedInsideToReport", displayName()));
     }
     context.stop();
   }
 }
Example #8
0
 @Override
 public Object report(Context context) {
   LogoList list = argEvalList(context, 0);
   double winner = 0;
   Double boxedWinner = null;
   for (Object elt : list) {
     if (elt instanceof Double) {
       Double boxedValue = (Double) elt;
       double value = boxedValue.doubleValue();
       if (boxedWinner == null || value > winner) {
         winner = value;
         boxedWinner = boxedValue;
       }
     }
   }
   if (boxedWinner == null) {
     throw new EngineException(
         context,
         this,
         I18N.errorsJ()
             .getN("org.nlogo.prim._max.cantFindMaxOfListWithNoNumbers", Dump.logoObject(list)));
   }
   return boxedWinner;
 }
Example #9
0
 public double report_1(Context context, AgentSet sourceSet, Reporter block) throws LogoException {
   block.checkAgentSetClass(sourceSet, context);
   Context freshContext = new Context(context, sourceSet);
   int result = 0;
   for (AgentSet.Iterator iter = sourceSet.iterator(); iter.hasNext(); ) {
     Agent tester = iter.next();
     Object value = freshContext.evaluateReporter(tester, block);
     if (!(value instanceof Boolean)) {
       throw new EngineException(
           context,
           this,
           I18N.errorsJ()
               .getN(
                   "org.nlogo.prim.$common.expectedBooleanValue",
                   displayName(),
                   Dump.logoObject(tester),
                   Dump.logoObject(value)));
     }
     if (((Boolean) value).booleanValue()) {
       result++;
     }
   }
   return result;
 }