Esempio n. 1
0
  public Action choose(WumpusWorld.Hunter hunter, Set<Action> options) {
    System.out.format("%n%n");
    updateInternal(hunter);
    Room here = worldToPrivate.get(hunter.location());

    // check to see if we need to make a new decision
    if (decision != null && !decision.done(options)) {
      return decision.step(options);
    }

    // kill any adjacent, known wumpuses
    for (Room wumpus : here.adjacent) {
      if (wumpusKB.known(wumpus) && wumpusKB.evaluate(wumpus)) {
        System.out.println("DIE!");
        decision = new KillDecision(hunter, wumpus);
        return decision.step(options);
      }
    }

    // find any known wumpuses to kill
    for (Room wumpus : privateToWorld.keySet()) {
      if (wumpusKB.known(wumpus) && wumpusKB.evaluate(wumpus)) {
        Set<Room> safeRooms = safeRooms();
        safeRooms.add(wumpus);

        List<Room> path = path(here, wumpus, safeRooms);
        path.remove(wumpus);
        System.out.println("I've got it!");
        decision = new PathDecision(hunter, path);
        return decision.step(options);
      }
    }

    // find safe rooms to explore
    {
      System.out.println(safeRooms());
      List<Room> path = path(here, safeRooms());
      if (path != null) {
        System.out.println("Walking!");
        decision = new PathDecision(hunter, path);
        return decision.step(options);
      }
    }

    // find bats to exploit
    for (Room bat : privateToWorld.keySet()) {
      if (batKB.known(bat) && batKB.evaluate(bat)) {
        Set<Room> safeRooms = safeRooms();
        safeRooms.add(bat);
        System.out.println("Find a Bat!");
        List<Room> path = path(here, bat, safeRooms);
        return decision.step(options);
      }
    }

    // otherwise, take your 3 chances... they're only arrows!
    decision = new KillDecision(hunter, Groups.any(here.adjacent));
    return decision.step(options);
  }
Esempio n. 2
0
  /**
   * Save our decision to the database.
   *
   * @param parent - the parent of the decision
   * @param ptype - the parent's type
   * @return the unique ID
   */
  public int toDatabase(int parent, RationaleElementType ptype) {
    RationaleDB db = RationaleDB.getHandle();
    Connection conn = db.getConnection();
    String updateQuery = "";
    int ourid = 0;

    RationaleUpdateEvent l_updateEvent;

    // find out if this requirement is already in the database
    Statement stmt = null;
    ResultSet rs = null;

    String subsReq = "No";
    if (!alts) subsReq = "Yes";

    try {
      stmt = conn.createStatement();

      if (inDatabase(parent, ptype)) {
        // set up Designer update string
        String updateD;
        if (designer == null) updateD = "D.designer = null";
        else updateD = "D.designer = " + designer.getID();

        updateQuery =
            "UPDATE PATTERNDECISIONS D "
                + "SET D.parent = "
                + new Integer(parent).toString()
                + ", D.ptype = '"
                + ptype.toString()
                + "', D.phase = '"
                + devPhase.toString()
                + "', D.description = '"
                + RationaleDBUtil.escape(description)
                + "', D.type = '"
                + type.toString()
                + "', D.name = '"
                + RationaleDBUtil.escape(name)
                + "', D.status = '"
                + status.toString()
                + "', D.subdecreq = '"
                + subsReq
                + "', "
                + updateD
                + " WHERE "
                + "D.id = "
                + this.id
                + " ";
        stmt.execute(updateQuery);

        l_updateEvent = m_eventGenerator.MakeUpdated();
      } else {
        if (!fromXML) id = RationaleDB.findAvailableID("PATTERNDECISIONS");

        String parentSt;
        String parentTSt;

        // now, we have determined that the decision is new
        if ((this.parent < 0) || (ptype == null)) {
          parentSt = "NULL";
          parentTSt = "None";
        } else {
          parentSt = new Integer(this.parent).toString();
          parentTSt = ptype.toString();
        }
        String updateD;
        if (designer == null) updateD = "null";
        else updateD = new Integer(designer.getID()).toString();

        updateQuery =
            "INSERT INTO PATTERNDECISIONS "
                + "(id, name, description, type, status, phase, subdecreq, parent, ptype, designer) "
                + "VALUES ("
                + id
                + ", '"
                + RationaleDBUtil.escape(this.name)
                + "', '"
                + RationaleDBUtil.escape(this.description)
                + "', '"
                + this.type.toString()
                + "', '"
                + this.status.toString()
                + "', '"
                + this.devPhase.toString()
                + "', '"
                + subsReq
                + "', "
                + parentSt
                + ", '"
                + parentTSt
                + "', "
                + updateD
                + ")";

        stmt.execute(updateQuery);

        /*
        //Now, associate with pattern.
        //Get id first
        updateQuery = "SELECT id FROM patterndecisions where name='" +
        RationaleDBUtil.escape(this.name) + "'";
        rs = stmt.executeQuery(updateQuery);

        if (rs.next())
        {
        	ourid = rs.getInt("id");
        	rs.close();
        	//We have found out our ID and the insert is a success.
        	//get association set up.
        	updateQuery = "INSERT into pattern_decision values (" + parent +
        	"ourid" + "DECISION)";
        }

        //And now, we have patternID and patterndecisionID. We can insert into relationship entry
        updateQuery = "INSERT INTO pattern_decision values (" + parentPattern + ", " +
        ourid + ", " + "'Decision')";
        stmt.execute(updateQuery);
        */
        l_updateEvent = m_eventGenerator.MakeCreated();
      }
      // in either case, we want to update any sub-requirements in case
      // they are new!
      // now, we need to get our ID
      updateQuery =
          "SELECT id FROM PATTERNDECISIONS where name='" + RationaleDBUtil.escape(this.name) + "'";
      rs = stmt.executeQuery(updateQuery);

      if (rs.next()) {
        ourid = rs.getInt("id");
        rs.close();
      } else {
        ourid = -1;
      }
      this.id = ourid;

      Enumeration alts = alternatives.elements();
      while (alts.hasMoreElements()) {
        Alternative alt = (Alternative) alts.nextElement();
        //				System.out.println("Saving alternative from decision");
        alt.toDatabase(ourid, RationaleElementType.DECISION);
      }

      Enumeration decs = subDecisions.elements();
      while (decs.hasMoreElements()) {
        Decision dec = (Decision) decs.nextElement();
        dec.toDatabase(ourid, RationaleElementType.DECISION);
      }

      Enumeration quests = questions.elements();
      while (quests.hasMoreElements()) {
        Question quest = (Question) quests.nextElement();
        quest.toDatabase(ourid, RationaleElementType.DECISION);
      }

      // finally, the history

      Enumeration hist = history.elements();
      while (hist.hasMoreElements()) {
        History his = (History) hist.nextElement();
        his.toDatabase(ourid, RationaleElementType.DECISION);
      }

      // need to update our relationships with the constraints
      Enumeration conkids = this.constraints.elements();
      while (conkids.hasMoreElements()) {
        Constraint kid = (Constraint) conkids.nextElement();
        // if the parent ID is not zero, then update the parent-child relationship

        updateQuery =
            "SELECT * from ConDecRelationships WHERE "
                + "constr = "
                + new Integer(kid.getID()).toString()
                + " and decision = "
                + new Integer(ourid).toString();

        rs = stmt.executeQuery(updateQuery.toUpperCase());
        if (rs.next()) {
          rs.close();
        } else {
          String insertRel =
              "INSERT INTO ConDecRelationships (constr, decision) "
                  + "VALUES ("
                  + new Integer(kid.getID()).toString()
                  + ", "
                  + new Integer(ourid).toString()
                  + ")";
          System.out.println(insertRel.toUpperCase());
          stmt.execute(insertRel);
        }
        kid.toDatabase(ourid);
      } // checking parent

      m_eventGenerator.Broadcast(l_updateEvent);
    } catch (SQLException ex) {
      // handle any errors
      RationaleDB.reportError(ex, "Error in PatternDecision.toDatabase", updateQuery);

    } finally {
      RationaleDB.releaseResources(stmt, rs);
    }

    return ourid;
  }