예제 #1
0
 /**
  * Searches in persisted long term memory if this memory object already exists. The criteria are
  * its type and info.
  *
  * @param type
  * @param info
  * @return the existing memory object, or null if there aren't any.
  */
 private MemoryObject checksIfMemoryExists(String type, Object info) {
   MemoryObject ltmMO = null;
   File pathName = new File(path); // gets the element at the index of the List
   String[] fileNames = pathName.list(); // lists all files in the directory
   if (fileNames != null) {
     for (int i = 0; i < fileNames.length; i++) {
       File f =
           new File(
               pathName.getPath(),
               fileNames[i]); // getPath converts abstract path to path in String,
       if (!f.isDirectory()) {
         //             System.out.println(f.getCanonicalPath());
         MemoryObject recoveredMO = this.deserializeMO(f);
         if (type.equalsIgnoreCase(recoveredMO.getName()) && info.equals(recoveredMO.getI())) {
           ltmMO = recoveredMO;
           break;
         }
       }
     }
     if (ltmMO != null) {
       if (rawMemory != null) rawMemory.addMemoryObject(ltmMO);
     }
   }
   return ltmMO;
 }
예제 #2
0
  /**
   * Stores Information of a given type in long term memory. /TODO At this moment, long term memory
   * is persisted in a json file in hard disk. This should be changed to use a DB such as postgre or
   * mysql.
   *
   * @param type
   * @param string
   */
  public void learn(MemoryObject mo) {
    MemoryObject hasMemory = this.checksIfMemoryExists(mo.getName(), mo.getI());

    if (hasMemory == null) { // doesn't exist yet
      int endIndex = ((String) mo.getI()).length();
      if (endIndex > 8) {
        endIndex = 8;
      }
      String filename =
          mo.getName()
              + "_"
              + ((String) mo.getI()).substring(0, endIndex)
              + "_"
              + mo.getTimestamp().toString().replace(":", "-");
      String extension = ".mo";
      // SERIALIZE

      try {
        // Serialize to a file
        ObjectOutput out =
            new ObjectOutputStream(new FileOutputStream(path + filename + extension));
        out.writeObject(mo);
        out.close();

        // Serialize to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bos);
        out.writeObject(mo);
        out.close();

        // Get the bytes of the serialized object
        byte[] buf = bos.toByteArray();
      } catch (IOException e) {
        System.out.println("Couldn't create file with serialized memory object.");
      }
    }
  }
예제 #3
0
  /**
   * Retrieves from long term memory a given type and info of memory object. It first looks for it
   * in the memory objects already loaded in ram LTM. If it fails to find it, it then looks for it
   * on disk.
   *
   * @param type
   * @param info
   * @return memory object.
   */
  public MemoryObject retrieve(String type, String info) {
    MemoryObject retrievedMO = null;
    boolean isInRAM = false;
    // Check if has already been loaded
    for (MemoryObject ramMO : this.ltmMOs) {
      if (ramMO.getName().equalsIgnoreCase(type) && ramMO.getI().equals(info)) {
        retrievedMO = ramMO;
        isInRAM = true;
        break;
      }
    }

    if (!isInRAM) { // Couldn't find in ram, look for it in disk
      retrievedMO = this.checksIfMemoryExists(type, info);
      if (retrievedMO != null) {
        ltmMOs.add(retrievedMO);
      }
    }
    return retrievedMO;
  }