⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daml_s_renderer.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	  return facts[index].getType();
	}
      }
 
      return "";
  }

  /**
   * Generate the restriction references to the range schema file.
   */
  private String generateFactRestrictions(Task task) {

    String output = "";

    Fact[] inputs = task.getPreconditions();
    Fact[] outputs = task.getPostconditions();

    for(int index = 0 ; index < inputs.length ; index++ ) {
      output += generateRestriction(inputs[index]);
    }

    for(int index = 0 ; index < outputs.length ; index++ ) {
      output += generateRestriction(outputs[index]);
    }

    return output;
  }

  /**
   * Generate the restriction references for a particular fact object.
   */
  private String generateRestriction(Fact fact) {

    String output = "";

    String[] attrs = fact.listAttributes();

    for(int index = 0 ; index < attrs.length ; index++) {

      output += "  <daml:Property rdf:about=\"&" + processPrefix + ";#" +
	fact.getType() + "." + attrs[index] + "\" >\n";
      output += "    <daml:range rdf:resource=\"&" + instanceRangePrefix +
	";#" + fact.getType() + "." + attrs[index] + ".type\" />\n";
      output += "  </daml:Property>\n";
    }

    return output;
  }

  /**
   * Produce a DAML-S service profile. Context is only used to
   * retrieve the address of the local platform, so the method could
   * easily be adapted to produce a profile at generation, rather than
   * runtime.
   */
  public String renderProfile(Task task, AgentContext context) {
    return renderProfile(task, context.whereAmI());
  }

  /**
   * Host is the IP address / domain name of the platform
   */
  public String renderProfile(Task task, String host) {

    String name = task.getName();

    namespaces.put(processPrefix, "http://" + host + "/services/classes/" +
		   name + "/" + name + "Process.daml");
    namespaces.put("default", "http://" + host + "/services/classes/" +
		   name + "/" + name + "Profile.daml");

    String serviceProfile = makeHeader() + startRDF() + makeOntology("");

    if(SUBCLASS) {
      serviceProfile += generateServiceClass(task) + "\n";
    }

    serviceProfile += "<service:serviceProfile rdf:ID=\"" + name + "\" >\n";

    serviceProfile += "  <profile:textDescription>\"" + task.getTextInfo() +
      "\"</profile:textDescription>\n";
   
    serviceProfile += "  <profile:has_process rdf:resource=\"" +
      "&" + processPrefix + ";#" + name + "\" />\n";

    serviceProfile += generateIOPEs(task);

    serviceProfile += "</service:serviceProfile>\n";

    serviceProfile += "</rdf:RDF>";

    return serviceProfile;
  }

  /**
   * Generate the collection of IOPEs for the profile
   */
  private String generateIOPEs(Task task) {

    String output = "";

    Fact[] inputs = task.getPreconditions();
    Fact[] outputs = task.getPostconditions();

    for(int index = 0 ; index < inputs.length ; index++) {
      if(inputs[index].isReadOnly()) {
	output += generateParameter(inputs[index], "precondition");
      }
      else {
	output += generateParameter(inputs[index], "input");
      }
    }

    for(int index = 0 ; index < outputs.length ; index++) {
      if(outputs[index].isReadOnly()) {
	output += generateParameter(outputs[index], "effect");
      }
      else {
	output += generateParameter(outputs[index], "output");
      }
    }

    return output;
  }

  /**
   * Generate a particular IOPE for the profile, of the type
   * specified.
   */
  private String generateParameter(Fact fact, String type) {

    String output = "";

    output += "  <profile:" + type + ">\n";

    output += "    <profile:ParameterDescription rdf:ID=\"" + fact.getType() +
      "IOPE\" >\n";
    output += "      <profile:parameterName>" +
      fact.getId().replaceAll("\\?", "") + "</profile:parameterName>\n";
    output += "      <profile:restrictedTo rdf:resource=\"&" + processPrefix +
      ";#" + fact.getType() + "\" />\n";
    output += "      <profile:refersTo rdf:resource=\"&" + processPrefix +
      ";#" + fact.getType() + "\" />\n";
    output += "    </profile:ParameterDescription>\n";

    output += "  </profile:" + type + ">\n";

    return output;
  }

  /**
   * Generate the subclass of Service that will allow instance
   * information to be recorded in the service declaration.
   */
  private String generateServiceClass(Task task) {

    String output = "";

    output += "<daml:Class rdf:ID=\"" + task.getName() + "Service\" >\n";

    output += "  <daml:subclassof rdf:resource=\"&service;#Service\" />\n";

    output += "</daml:Class>\n";

    output += generateServiceClassProperties(task);

    return output;
  }

  /**
   * Genreate the properties of the service subclass, and attribute
   * them to the class.
   */
  private String generateServiceClassProperties(Task task) {

    String output = "";

    Fact[] inputs = task.getPreconditions();
    Fact[] outputs = task.getPostconditions();

    for(int index = 0 ; index < inputs.length ; index++) {
      output += generateProperty(inputs[index], task);
    }

    for(int index = 0 ; index < outputs.length ; index++) {
      output += generateProperty(outputs[index], task);
    }

    return output;
  }

  /**
   * Generate an individual property of the service subclass.
   */
  private String generateProperty(Fact fact, Task task) {

    String output = "";

    output += "<daml:Property rdf:ID=\"" + fact.getType() + "\" >\n";
    output += "  <daml:domain rdf:resource=\"#" + task.getName() +
      "Service\" />\n";
    output += "  <daml:range rdf:resource=\"#" + fact.getType() +
      "IOPE\" />\n";
    output += "</daml:Property>\n";

    return output;
  }


  public String renderProcess(Task task, AgentContext context) {

    //FIXME: Need to determine name of ontology file
    String ontology = task.getName() + ".daml";

    namespaces.put(ontologyPrefix, "http://" + context.whereAmI() +
		   "/services/ontologies/" + ontology);

    String output = makeHeader() + startRDF() + makeOntology("");

    output += "<process:ProcessModel rdf:ID=\"" + task.getName() +
      "_Process\">\n";
    output += "  <process:hasProcess rdf:resource=\"" + task.getName() +
      "\">\n";
    output += "</process:ProcessModel>\n";

    output += "<daml:Class rdf:ID=\"" + task.getName() + "\" >\n";
    output += "  <daml:subclassOf rdf:resource=\"" +
      "&process;#AtomicProcess\" />\n";
    output += "<daml:Class>\n\n";

    //Refine classes into IOPEs
    output += makeIOPEs(task);

    output += "</rdf:RDF>\n";

    return output;
  }

  /**
   * Make IOPEs for the process
   */
  private String makeIOPEs(Task task) {

    String output = "";

    Fact[] inputs = task.getPreconditions();
    Fact[] outputs = task.getPostconditions();

    for(int index = 0 ; index < inputs.length ; index++) {
      if(inputs[index].isReadOnly()) {
	output += generateIOPE(inputs[index], task, "precondition");
      }
      else {
	output += generateIOPE(inputs[index], task, "input");
      }
    }

    for(int index = 0 ; index < outputs.length ; index++) {
      if(outputs[index].isReadOnly()) {
	output += generateIOPE(outputs[index], task, "effect");
      }
      else {
	output += generateIOPE(outputs[index], task, "output");
      }
    }

    return output;
  }

  /**
   * Generate an IOPE property for the process
   */
  private String generateIOPE(Fact fact, Task task, String type) {

    String output = "";

    output += "<daml:Property rdf:ID=\"" + fact.getType() + "\" >\n";
    output += "  <daml:subclassOf rdf:resource=\"&process;#" + type +"\" />\n";
    output += "  <daml:domain rdf:resource=\"" + task.getName() + "\" />\n";
    output += "  <daml:range rdf:resource=\"&" + ontologyPrefix + ";#" +
      fact.getType() + "\" />\n";
    output += "</daml:Property>\n";

    return output;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -