利用API对OWL-S描述的服务进行操作 转贴
查询邮政编码的服务用OWL-S描述,位于http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl
对该文件调用API中的类库进行操作,最重要的是如下几段代码:
OWLKnowledgeBase kb = OWLFactory.createKB();
service = kb.readService("http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl");
process = service.getProcess(); // 读取服务的process
// 输入初始值为0,输入值为"College Park"和"MD",函数values.setDataValue得到这两个值,调用服务,并将结果输出。
values = new ValueMap();
values.setDataValue(process.getInput("City"), "College Park");
values.setDataValue(process.getInput("State"), "MD");
values = exec.execute(process, values);
完整代码如下:
import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLIndividual;
import org.mindswap.owl.OWLKnowledgeBase;
import org.mindswap.owls.OWLSFactory;
import org.mindswap.owls.process.AtomicProcess;
import org.mindswap.owls.process.Process;
import org.mindswap.owls.process.execution.ProcessExecutionEngine;
import org.mindswap.owls.process.execution.ProcessExecutionListener;
import org.mindswap.owls.profile.Profile;
import org.mindswap.owls.service.Service;
import org.mindswap.query.ValueMap;
import org.mindswap.utils.Utils;
import org.mindswap.wsdl.WSDLOperation;
import org.mindswap.wsdl.WSDLService;
public class RunService {
Service service;
Profile profile;
Process process;
WSDLService s;
WSDLOperation op;
String inValue;
String outValue;
ValueMap values;
ProcessExecutionEngine exec;
public RunService() {
// create an execution engine
exec = OWLSFactory.createExecutionEngine();
// Attach a listener to the execution engine
exec.addExecutionListener(new ProcessExecutionListener() {
public void setCurrentExecuteService(Process p) {
System.out.println("Start executing process " + p);
}
public void printMessage(String message) {
}
public void finishExecution(int retCode) {
System.out.println("Finished execution " +
((retCode == ProcessExecutionListener.EXEC_ERROR)
? "with errors" : "successfully"));
}
});
}
public void runZipCode() throws Exception {
OWLKnowledgeBase kb = OWLFactory.createKB();
service = kb.readService("http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl");
process = service.getProcess();
// initialize the input values to be empty
values = new ValueMap();
values.setDataValue(process.getInput("City"), "College Park");
values.setDataValue(process.getInput("State"), "MD");
values = exec.execute(process, values);
// get the result
OWLIndividual out = values.getIndividualValue(process.getOutput());
// display the results
System.out.println("Executed service '" + service + "'");
System.out.println("Grounding WSDL: " +
((AtomicProcess) process).getGrounding().getDescriptionURL());
System.out.println("City = " + "College Park");
System.out.println("State = " + "MD");
System.out.println("Output = ");
System.out.println(Utils.formatRDF(out.toRDF()));
System.out.println();
}
public static void main(String[] args) throws Exception {
RunService test = new RunService();
try {
test.runZipCode();
} catch(Exception e) {
e.printStackTrace();
}
}
}
在ECLISPE下运行,得到输出:
Start executing process http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl#ZipCodeFinderProcess
Finished execution successfully
Executed service 'http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl#ZipCodeFinderService'
Grounding WSDL: http://www.tilisoft.com/ws/LocInfo/ZipCode.asmx?WSDL
City = College Park
State = MD
Output =
ZipCode:
zip: 20740
同样,对http://www.mindswap.org/2004/owl-s/1.1/BookFinder.owl和http://www.mindswap.org/2004/owl-s/1.1/Dictionary.owl等描述的服务进行操作,也可以得到正确的输出。