Ejemplo n.º 1
0
 public Object report(Argument args[], Context context) throws ExtensionException {
   // create a NetLogo list for the result
   LogoListBuilder list = new LogoListBuilder();
   int n;
   // use typesafe helper method from
   // org.nlogo.api.Argument to access argument
   try {
     n = args[0].getIntValue();
   } catch (LogoException e) {
     throw new ExtensionException(e.getMessage());
   }
   if (n < 0) {
     // signals a NetLogo runtime error to the modeler
     throw new ExtensionException("input must be positive");
   }
   // populate the list. note that we use Double objects; NetLogo
   // numbers are always Doubles
   for (int i = 0; i < n; i++) {
     list.add(Double.valueOf(i));
   }
   return list.toLogoList();
 }
Ejemplo n.º 2
0
  @Override
  public Object report(final org.nlogo.nvm.Context context) {
    LinkedHashMap<LogoHashObject, MutableInteger> counts =
        new LinkedHashMap<LogoHashObject, MutableInteger>();
    LogoList list = argEvalList(context, 0);
    for (Iterator<Object> it = list.iterator(); it.hasNext(); ) {
      Object srcElt = it.next();
      LogoHashObject logoElt = new LogoHashObject(srcElt);
      if (counts.containsKey(logoElt)) {
        MutableInteger i = counts.get(logoElt);
        i.value_$eq(i.value() + 1);
      } else {
        counts.put(logoElt, new MutableInteger(1));
      }
    }

    Iterator<LogoHashObject> keys = counts.keySet().iterator();
    int currMaxCount = 0;
    while (keys.hasNext()) {
      LogoHashObject currKey = keys.next();
      int currVal = counts.get(currKey).value();
      if (currVal > currMaxCount) {
        currMaxCount = currVal;
      }
    }

    keys = counts.keySet().iterator();
    LogoListBuilder modes = new LogoListBuilder();
    while (keys.hasNext()) {
      LogoHashObject currKey = keys.next();
      int currVal = counts.get(currKey).value();
      if (currVal == currMaxCount) {
        modes.add(currKey.sourceObject());
      }
    }
    return modes.toLogoList();
  }