Beispiel #1
0
 /**
  * Instantiate a class by reflection. This method will not throw exceptions but will return null
  * on error.
  *
  * @param className The class name to instantiate.
  * @param outputClass The class that specifies the type that should be returned. This will usually
  *     be a superclass of the given class name.
  * @param <T> The desired return type.
  * @return A new instance of the given class name cast as the output class, or null if the class
  *     cannot be instantiated.
  */
 public static <T> T instantiate(String className, Class<T> outputClass) {
   try {
     Class<? extends T> clazz = Class.forName(className).asSubclass(outputClass);
     if (Modifier.isAbstract(clazz.getModifiers())) {
       return null;
     }
     return clazz.newInstance();
   } catch (ClassNotFoundException e) {
     Logger.info("Could not find class " + className);
   } catch (IllegalAccessException e) {
     Logger.info("Could not instantiate class " + className);
   } catch (InstantiationException e) {
     Logger.info("Could not instantiate class " + className);
   }
   return null;
 }
Beispiel #2
0
 @Override
 protected void think(int time, ChangeSet changed, Collection<Command> heard) {
   // If we're not hurt or buried run for a refuge!
   Civilian me = me();
   // Remove all entities except me
   model.removeAllEntities();
   model.addEntity(me);
   int damage = me.isDamageDefined() ? me.getDamage() : 0;
   int hp = me.isHPDefined() ? me.getHP() : 0;
   int buriedness = me.isBuriednessDefined() ? me.getBuriedness() : 0;
   if (hp <= 0 || hp < consciousThreshold) {
     // Unconscious (or dead): do nothing
     Logger.info("Unconcious or dead");
     sendRest(time);
     return;
   }
   if (damage > 0 && random.nextDouble() < ouchProbability) {
     Logger.info("Shouting in pain");
     say(OUCH, time);
   }
   if (buriedness > 0 && random.nextDouble() < helpProbability) {
     Logger.info("Calling for help");
     say(HELP, time);
   }
   if (damage == 0 && buriedness == 0) {
     // Run for the refuge
     /*List<EntityID> path = search.search(me().getPosition(), refugeIDs, connectivityGraph, distanceMatrix);
     if (path != null) {
         Logger.info("Heading for a refuge");
         sendMove(time, path);
         return;
     }
     else {*/
     Logger.info("Moving randomly");
     sendMove(time, randomWalk());
     return;
     // }
   }
   Logger.info("Not moving: damage = " + damage + ", buriedness = " + buriedness);
   sendRest(time);
 }
Beispiel #3
0
 @Override
 protected void postConnect() {
   super.postConnect();
   //        model.indexClass(StandardEntityURN.REFUGE);
   helpProbability = config.getFloatValue(HELP_PROBABILITY_KEY, DEFAULT_HELP_PROBABILITY);
   ouchProbability = config.getFloatValue(OUCH_PROBABILITY_KEY, DEFAULT_OUCH_PROBABILITY);
   consciousThreshold = config.getIntValue(CONSCIOUS_THRESHOLD_KEY, DEFAULT_CONSCIOUS_THRESHOLD);
   Logger.info("Civilian " + getID() + " connected");
   Civilian me = me();
   // Remove all entities except me
   model.removeAllEntities();
   model.addEntity(me);
 }
Beispiel #4
0
 /**
  * Instantiate a factory class by reflection. Essentially the same as {@link #instantiate(String,
  * Class)} except that it will look for a static field called INSTANCE that contains an instance
  * of the right class. If this doesn't exist (or in inaccessable or the wrong type) then a
  * constructor will be used.
  *
  * @param classname The class name to instantiate.
  * @param outputClass The class that specifies the type that should be returned. This will usually
  *     be a superclass of the given class name.
  * @param <T> The desired return type.
  * @return The content of the INSTANCE field if it exists; a new instance of the given class name
  *     cast as the output class, or null if the class cannot be instantiated.
  */
 public static <T> T instantiateFactory(String classname, Class<T> outputClass) {
   Class<? extends T> clazz;
   try {
     clazz = Class.forName(classname).asSubclass(outputClass);
   } catch (ClassNotFoundException e) {
     Logger.info("Could not find class " + classname);
     return null;
   } catch (ClassCastException e) {
     Logger.info(classname + " is not a subclass of " + outputClass.getName(), e);
     return null;
   }
   // Is there a singleton instance called INSTANCE?
   try {
     Field field = clazz.getField("INSTANCE");
     if (Modifier.isStatic(field.getModifiers())) {
       try {
         Object o = field.get(null);
         if (o != null) {
           return outputClass.cast(o);
         }
       } catch (IllegalAccessException e) {
         Logger.info(
             "Could not access INSTANCE field in class " + classname + ": trying constructor.");
       } catch (ClassCastException e) {
         Logger.info(
             "Could not cast INSTANCE field to "
                 + outputClass
                 + " in class "
                 + classname
                 + ": trying constructor.");
       }
     }
   } catch (NoSuchFieldException e) {
     Logger.info("No INSTANCE field in class " + classname);
     // No singleton instance. Try instantiating it.
   }
   try {
     return clazz.newInstance();
   } catch (IllegalAccessException e) {
     Logger.info("Could not instantiate class " + classname);
   } catch (InstantiationException e) {
     Logger.info("Could not instantiate class " + classname);
   }
   return null;
 }