?? daml_s_renderer.java
字號:
package zeus.ontology.service;
import zeus.actors.AgentContext;
import zeus.concepts.OntologyDb;
import zeus.concepts.Task;
import zeus.concepts.Fact;
import zeus.concepts.Restriction;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;
public class DAML_S_Renderer
implements InstanceRenderer, ProfileRenderer, ProcessRenderer {
//Toggles whether to use subclass of Service methodology or schema
//route.
public final static boolean SUBCLASS = true;
private HashMap namespaces;
private final static String processPrefix = "procmod";
private final static String profilePrefix = "servprof";
private final static String instanceRangePrefix = "range";
private final static String ontologyPrefix = "ont";
public DAML_S_Renderer() {
namespaces = new HashMap();
namespaces.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns");
namespaces.put("rdfs", "http://www.w3.org/2000/01/rdf-schema");
namespaces.put("daml", "http://www.daml.org/2001/03/daml+oil");
namespaces.put("process",
"http://www.daml.org/services/daml-s/0.7/Process");
namespaces.put("service",
"http://www.daml.org/services/daml-s/0.7/Service");
namespaces.put("profile",
"http://www.daml.org/services/daml-s/0.7/Profile");
namespaces.put("xsd", "http://www.w3.org/2000/10/XMLSchema.xsd");
}
/**
* Insert the XML header, including all the entity definitions.
*/
private String makeHeader() {
String output = "<?xml version='1.0' encoding='ISO-8859-1'?>\n" +
"<!DOCTYPE uridef[\n";
for(Iterator i = namespaces.keySet().iterator() ; i.hasNext() ; ) {
String key = (String)i.next();
output += " <!ENTITY " + key + " \"" + namespaces.get(key) + "\" >\n";
}
output += "]>\n";
return output;
}
/**
* Begin the RDF item, including all the namespaces.
*/
private String startRDF() {
String output = "<rdf:RDF\n";
for(Iterator i = namespaces.keySet().iterator() ; i.hasNext() ; ) {
String key = (String)i.next();
if(key.matches("(?i)default")) {
output += " xmlns =\"&default;#\"\n";
}
else {
output += " xmlns:" + key + "=\"&" + key + ";#\"\n";
}
}
output += ">\n";
return output;
}
/**
* Render the ontology item, including the imports statements for
* all referenced files.
*/
private String makeOntology(String versionInfo) {
String output = "<daml:ontology rdf:about=\"\" >\n";
output += " <daml:versionInfo>" + versionInfo + "</daml:versionInfo>\n";
for(Iterator i = namespaces.keySet().iterator() ; i.hasNext() ; ) {
String key = (String)i.next();
output += " <daml:imports rdf:resource=\"&" + key + ";\" />\n";
}
output += "</daml:ontology>\n\n";
return output;
}
public String renderInstance(Task task, AgentContext context) {
String instance = context.whoami() + "__" + task.getName();
String serviceClass;
String serviceInstance = "";
if(SUBCLASS) {
serviceClass = profilePrefix + ":" + task.getName() + "Service";
}
else {
serviceClass = "service:Service";
}
namespaces.put(processPrefix, "http://" + context.whereAmI() +
"/services/classes/" + task.getName() + "/" +
task.getName() + "Process.daml");
namespaces.put(profilePrefix, "http://" + context.whereAmI() +
"/services/classes/" + task.getName() + "/" +
task.getName() + "Profile.daml");
namespaces.put(instanceRangePrefix, "http://" + context.whereAmI() +
"/services/instances/" + context.whoami() + "/" +
instance + "Range.xsd");
namespaces.put("default", "http://" + context.whereAmI() +
"/services/instances/" + context.whoami() + "/" + instance +
"Instance.daml");
serviceInstance += makeHeader() + startRDF() + makeOntology("");
serviceInstance += "<" + serviceClass + " rdf:ID=\"" + instance + "\">\n";
serviceInstance += " <profile:providedBy>\n";
String item;
serviceInstance += " <profile:ServiceProvider rdf:ID=\"" +
context.whoami() + "\">\n";
/* FIXME
item = info.getProviderName();
if(item != null && item.length() > 0) {
serviceInstance += " <profile:name>" + item
+ "</profile:name>\n";
}
*/
item = task.getPhoneInfo();
if(item != null && item.length() > 0) {
serviceInstance += " <profile:phone>" + item
+ "</profile:phone>\n";
}
item = task.getFaxInfo();
if(item != null && item.length() > 0) {
serviceInstance += " <profile:fax>" + item
+ "</profile:fax>\n";
}
item = task.getEmailInfo();
if(item != null && item.length() > 0) {
serviceInstance += " <profile:email>" + item
+ "</profile:email>\n";
}
item = task.getPhysicalInfo();
if(item != null && item.length() > 0) {
serviceInstance += " <profile:physicalAddress>" +
item + "</profile:physicalAddress>\n";
}
serviceInstance += " <profile:webURL>" + context.whereAmI()
+ "</profile:webURL>\n";
serviceInstance += " </profile:ServiceProvider>\n";
serviceInstance += " </profile:providedBy>\n";
item = task.getGeoInfo();
if(item != null && item.length() > 0) {
serviceInstance += " <profile:geographicRadius>" +
item + "</profile:geographicRadius>\n";
}
serviceInstance += " <service:presents rdf:resource=\"&" + profilePrefix +
";#" + task.getName() + "\" />\n";
serviceInstance += " <service:describedBy rdf:resource=\"&" +
processPrefix + ";#" + task.getName() + "\" />\n";
if(SUBCLASS) {
//State instance properties here
serviceInstance += generateInstanceValues(task);
}
serviceInstance += "</" + serviceClass + ">\n";
if(!SUBCLASS) {
//Pass restrictions off to range schema
serviceInstance += generateFactRestrictions(task);
}
serviceInstance += "</rdf:RDF>\n";
return serviceInstance;
}
/**
* Generate the values of certain variables for this instance of the
* Service subclass.
*/
private String generateInstanceValues(Task task) {
String output = "";
List restrictions = task.getRestrictions();
for(Iterator i = restrictions.iterator() ; i.hasNext() ; ) {
Restriction item = (Restriction)i.next();
output += generateRestriction(lookupFactId(item.getFactName(), task),
item.getAttributeName(),
item.getRestriction());
}
return output;
}
/**
* Translate a single restriction
*/
private String generateRestriction(String factName, String attributeName,
String restriction) {
String output = "";
if(restriction.matches(".*\\|.*")) {
String[] pieces = restriction.split("\\|");
output += " <daml:unionOf rdf:parseType=\"daml:collection\">\n";
for(int index = 0 ; index < pieces.length ; index++ ) {
output += generateRestriction(factName, attributeName, pieces[index]);
}
output += " </daml:unionOf>\n";
}
else if(restriction.matches(".*&.*")) {
String[] pieces = restriction.split("&");
for(int index = 0 ; index < pieces.length ; index++ ) {
output += generateRestriction(factName, attributeName, pieces[index]);
}
}
else if(restriction.matches("((.*<.*)|(.*>.*))")) {
//Cannot deal with inequality
return "";
}
else {
restriction = restriction.replaceAll("=", "").trim();
if(false) {
output +=
" <" + profilePrefix + ":" + factName + ">\n" +
" <" + profilePrefix + ":" + attributeName + ">" + restriction +
"</" + profilePrefix + ":" + attributeName + ">\n" +
" </" + profilePrefix + ":" + factName + ">\n";
}
else {
output +=
" <" + profilePrefix + ":" + factName + " " + processPrefix + ":" +
attributeName + "=\"" + restriction + "\" />\n";
}
}
return output;
}
/**
* Lookup the type of fact behind a particular variable name.
*/
private String lookupFactId(String factId, Task task) {
Fact[] facts = task.getPreconditions();
for(int index = 0 ; index < facts.length ; index++) {
if(facts[index].getId().equals(factId)) {
return facts[index].getType();
}
}
facts = task.getPostconditions();
for(int index = 0 ; index < facts.length ; index++) {
if(facts[index].getId().equals(factId)) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -