/**
  * Look for rabbits adjacent to the current location. Only the first live rabbit is eaten.
  *
  * @return Where food was found, or null if it wasn't.
  */
 private Location findFood() {
   Field field = getField();
   List<Location> adjacent = field.adjacentLocations(getLocation());
   Iterator<Location> it = adjacent.iterator();
   while (it.hasNext()) {
     Location where = it.next();
     Object animal = field.getObjectAt(where);
     if (animal instanceof Rabbit) {
       Rabbit rabbit = (Rabbit) animal;
       if (rabbit.isAlive()) {
         rabbit.setDead();
         foodLevel = RABBIT_FOOD_VALUE;
         // Remove the dead rabbit from the field.
         return where;
       }
     }
     if (animal instanceof Fox) {
       Fox fox = (Fox) animal;
       if (fox.isAlive()) {
         fox.setDead();
         foodLevel = WOLVES_FOOD_VALUE;
         // Remove the dead rabbit from the field.
         return where;
       }
     }
   }
   return null;
 }
示例#2
0
  /**
   * Run the simulation from its current state for a single step. Iterate over the whole field
   * updating the state of each fox and rabbit.
   */
  public void simulateOneStep() {
    step++;

    // Provide space for newborn rabbits.
    List<Rabbit> newRabbits = new ArrayList<Rabbit>();
    // Let all rabbits act.
    for (Iterator<Rabbit> it = rabbits.iterator(); it.hasNext(); ) {
      Rabbit rabbit = it.next();
      rabbit.run(newRabbits);
      if (!rabbit.isAlive()) {
        it.remove();
      }
    }

    // Provide space for newborn foxes.
    List<Fox> newFoxes = new ArrayList<Fox>();
    // Let all foxes act.
    for (Iterator<Fox> it = foxes.iterator(); it.hasNext(); ) {
      Fox fox = it.next();
      fox.hunt(newFoxes);
      if (!fox.isAlive()) {
        it.remove();
      }
    }

    // Add the newly born foxes and rabbits to the main lists.
    rabbits.addAll(newRabbits);
    foxes.addAll(newFoxes);

    view.showStatus(step, field);
  }
示例#3
0
  /**
   * Look for foxes and wolfs adjacent to the current location. Only the first live foxes or wolf is
   * eaten.
   *
   * @return Where food was found, or null if it wasn't.
   */
  private Location findFood() {
    Field field = getField();
    List<Location> adjacent = field.adjacentLocations(getLocation());
    Iterator<Location> it = adjacent.iterator();
    while (it.hasNext()) {
      Location where = it.next();
      Object animal = field.getObjectAt(where);
      if (animal instanceof Wolf) {
        Wolf wolf = (Wolf) animal;
        if (wolf.isAlive()) {
          wolf.setDead();
          setFoodLevel(WOLFS_FOOD_VALUE);
          return where;
        }
      } else if (animal instanceof Fox) {
        Fox fox = (Fox) animal;
        if (fox.isAlive()) {
          fox.setDead();
          setFoodLevel(FOX_FOOD_VALUE);
          return where;
        }

      } else if (animal instanceof Rabbit) {
        Rabbit rabbit = (Rabbit) animal;
        if (rabbit.isAlive()) {
          rabbit.setDead();
          setFoodLevel(RABBIT_FOOD_VALUE);
          return where;
        }
      }
    }
    return null;
  }
示例#4
0
  public static String annotation(String entity) {

    String uri = "";
    uri = Spotlight.getDBpLookup(entity);
    if (uri == "") uri = Fox.annotate(entity);
    return uri;
  }
示例#5
0
  protected void placeFoxes() {
    foxes = new ArrayList<>(par.numFoxes);

    double range = par.arenaSize / par.numFoxes;

    for (int i = 0; i < par.numFoxes; i++) {
      Fox f = new Fox(this, field);
      Double2D newLoc = null;
      if (par.randomFoxPosition) {
        newLoc =
            new Double2D(
                par.foxX * par.arenaSize,
                i * range + f.getRadius() + random.nextDouble() * (range - f.getRadius() * 2));
      } else {
        newLoc = new Double2D(par.foxX * par.arenaSize, i * range + range / 2);
      }
      f.setLocation(newLoc);
      foxes.add(f);
      schedule.scheduleRepeating(f);
    }
  }
示例#6
0
 @Test
 public void stateTest() {
   assertEquals("The state should be Fox.", State.FOX, fox.who());
 }
示例#7
0
  static void readQALDfile() {

    DocumentBuilderFactory qaldFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder qaldBuilder;

    JSONParser parser = new JSONParser();
    String anno = "";
    String unanno = "";
    int failcount = 0;
    try {
      qaldBuilder = qaldFactory.newDocumentBuilder();
      Document doc = qaldBuilder.newDocument();
      Element mainRootElement = doc.createElementNS("http://github.com/AKSW/AskNow", "NQSforQALD");
      doc.appendChild(mainRootElement);

      Object obj =
          parser.parse(
              new FileReader("/Users/mohnish/git2/sandbox/src/main/resources/qald6test.json"));

      JSONObject jsonObject = (JSONObject) obj;
      // JSONArray questions = (JSONArray) jsonObject.get("questions");

      // String output = null;

      JSONArray quald = (JSONArray) jsonObject.get("questions");
      Iterator<JSONObject> questions = quald.iterator();
      while (questions.hasNext()) {
        JSONObject quesObj = questions.next();
        Object ids = quesObj.get("id");
        // int idi = Integer.parseInt(ids);
        // if (idi<=300){
        //	continue;
        // }
        String ques = null;
        // ystem.out.println(id );
        JSONArray alllang = (JSONArray) quesObj.get("question");
        Iterator<JSONObject> onelang = alllang.iterator();
        while (onelang.hasNext()) {
          JSONObject engques = onelang.next();
          ques = (String) engques.get("string");
          break;
        }

        anno = Fox.annotate(ques);
        if (anno == "") {
          anno = Spotlight.getDBpLookup(ques);
        }
        // mainRootElement.appendChild(getNQSxml(doc, ids.toString() , ques,
        // getNQS(ques),nertags.toString()));
        if (anno != "") System.out.println("Id is " + ids.toString() + "  " + anno);
        else {
          unanno = unanno + ids.toString() + "  " + ques + "\n";
          failcount++;
        }
      }
      System.out.println("Fail count for fox is" + failcount + "\n" + unanno);

      // System.out.println(output);
      // try{
      // out.println( output);
      // Transformer transformer = TransformerFactory.newInstance().newTransformer();
      // transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      // DOMSource source = new DOMSource(doc);
      // StreamResult result = new StreamResult(new
      // File("/Users/mohnish/git2/AskNow/src/main/resources/qald6test-nqs.xml"));
      // StreamResult console = new StreamResult(System.out);
      // transformer.transform(source, result);
      // out.println( console);

      // System.out.println("\nXML DOM Created Successfully..");
      // } catch (TransformerConfigurationException e) {
      // TODO Auto-generated catch block
      //	e.printStackTrace();
      // } catch (TransformerFactoryConfigurationError e) {
      // TODO Auto-generated catch block
      //	e.printStackTrace();
      // } catch (TransformerException e) {
      // TODO Auto-generated catch block
      //	e.printStackTrace();
      // }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    } catch (ParserConfigurationException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  }
示例#8
0
  public void someMethod() {
    Fox fox = foxBuilder.speed(QUICK).colour(BROWN).legs(4).longTail().gender(MALE).build();

    fox.jumpOver(dog);
  }