Esempio n. 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);
  }
 /**
  * Checks the type of all the fuzzy sets.
  *
  * @param l The list where the fuzzy sets are.
  * @return 1 if all discrete, 2 if all piecewise, 3 if all continuous or 0 if all the sets are not
  *     the same type.
  */
 public static int allType(LogoList l) {
   int discrete = 0;
   int piecewise = 0;
   int continuous = 0;
   // Count the type of all the fuzzy sets inside the list
   for (Object o : l) {
     FuzzySet f = (FuzzySet) o;
     if (f.isContinuous()) {
       if (f instanceof PiecewiseLinearSet) {
         piecewise++;
       }
       continuous++;
     } else {
       discrete++;
     }
   }
   // If all the sets of the list are discrete return 1
   if (discrete == l.size()) {
     return 1;
   }
   // If all the sets of the list are piecewise return 2
   // Check first piecewise linear cause piecewise linear is also
   // continuous
   if (piecewise == l.size()) {
     return 2;
   }
   // If all the sets of the list are continuous return 3
   if (continuous == l.size()) {
     return 3;
   }
   // If the sets are mixed return 0;
   return 0;
 }
 /**
  * Build the label of the operation sets.
  *
  * @param l The list with the fuzzy sets.
  * @param operation The string to concatenate the set's label.
  * @return The resulting string.
  */
 public static String buildLabel(LogoList l, String operation) {
   String name = "(" + operation + " of Sets: " + ((FuzzySet) l.first()).getLabel();
   for (int i = 1; i < l.size(); i++) {
     name += "," + ((FuzzySet) l.get(i)).getLabel();
   }
   name += ")";
   return name;
 }
Esempio n. 4
0
 @Override
 public Object report(final org.nlogo.nvm.Context context) throws LogoException {
   Object obj = args[1].report(context);
   if (obj instanceof LogoList) {
     Object value = args[0].report(context);
     LogoList list = (LogoList) obj;
     for (Iterator<Object> it = list.iterator(); it.hasNext(); ) {
       if (Equality.equals(value, it.next())) {
         return Boolean.TRUE;
       }
     }
     return Boolean.FALSE;
   } else if (obj instanceof String) {
     return ((String) obj).indexOf(argEvalString(context, 0)) != -1 ? Boolean.TRUE : Boolean.FALSE;
   } else if (obj instanceof AgentSet) {
     Agent agent = argEvalAgent(context, 0);
     AgentSet agentset = (AgentSet) obj;
     if (agent instanceof Turtle) {
       if (agent.id == -1) {
         return Boolean.FALSE;
       }
       if (agentset.type() != Turtle.class) {
         return Boolean.FALSE;
       }
       if (agentset == world.turtles()) {
         return Boolean.TRUE;
       }
       if (world.isBreed(agentset)) {
         return agentset == ((Turtle) agent).getBreed() ? Boolean.TRUE : Boolean.FALSE;
       }
     }
     if (agent instanceof Link) {
       if (agent.id == -1) {
         return Boolean.FALSE;
       }
       if (agentset.type() != Link.class) {
         return Boolean.FALSE;
       }
       if (agentset == world.links()) {
         return Boolean.TRUE;
       }
       if (world.isBreed(agentset)) {
         return agentset == ((Link) agent).getBreed() ? Boolean.TRUE : Boolean.FALSE;
       }
     } else if (agent instanceof Patch) {
       if (agentset.type() != Patch.class) {
         return Boolean.FALSE;
       }
       if (agentset == world.patches()) {
         return Boolean.TRUE;
       }
     }
     return agentset.contains(agent) ? Boolean.TRUE : Boolean.FALSE;
   } else {
     throw new ArgumentTypeException(
         context, this, 1, Syntax.ListType() | Syntax.StringType() | Syntax.AgentsetType(), obj);
   }
 }
 /**
  * Calculate the parameters and the universe of continuous sets.
  *
  * @param l Logolist containing all the fuzzysets.
  * @return A tuple containing the resulting parameters(List<FuzzySet>) and the universe(double[]).
  */
 public static Tuple<FuzzySet> continuousParamsUniverse(LogoList l) {
   List<FuzzySet> params = new ArrayList<FuzzySet>();
   // Get and add the first fuzzy set
   FuzzySet f = (FuzzySet) l.first();
   params.add(f);
   // Get the universe of the first fuzzy set
   double[] universe = f.getUniverse();
   // Iterate over all the fuzzy sets
   for (int i = 1; i < l.size(); i++) {
     // Add fuzzy sets as parameters to the new set
     f = (FuzzySet) l.get(i);
     params.add(f);
     // Calculate the new universe
     universe = DegreeOfFulfillment.andInterval(universe, f.getUniverse());
   }
   return new Tuple<FuzzySet>(params, universe);
 }
 /**
  * Calculate the parameters and the universe of discrete operations.
  *
  * @param l the list where the sets are.
  * @param operator The operation to perform.
  * @return A tuple containing the resulting parameters(List<double[]>) and universe(double[]).
  */
 public static Tuple<double[]> discreteOperations(LogoList l, Command operator) {
   // First discrete set and his parameters
   PointSet set = (PointSet) l.first();
   List<double[]> resultParameters = set.getParameters();
   double[] universe = new double[2];
   // Iterate over the sets
   for (int i = 1; i < l.size(); i++) {
     set = (PointSet) l.get(i);
     // Calculate the parameters of two sets
     resultParameters = discretePairOperation(resultParameters, set.getParameters(), operator);
   }
   // Calculate universe
   double[] point = resultParameters.get(0);
   universe[0] = point[0];
   point = resultParameters.get(resultParameters.size() - 1);
   universe[1] = point[0];
   return new Tuple<double[]>(resultParameters, universe);
 }
Esempio n. 7
0
 @Override
 public LogoList toLogoList() {
   ArrayList<Agent> result = new ArrayList<Agent>();
   for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
     Agent agent = iter.next();
     result.add(agent);
   }
   Collections.sort(result);
   return LogoList.fromJava(result);
 }
Esempio n. 8
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);
   }
 }
Esempio n. 9
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();
  }