OWLSAPI示例.docx
《OWLSAPI示例.docx》由会员分享,可在线阅读,更多相关《OWLSAPI示例.docx(22页珍藏版)》请在冰点文库上搜索。
OWLSAPI示例
OWL-SAPI指南
文章分类:
Java编程
引自
Maryland 大学计算机系的Evren Sirin 开发。
OWL-S API的类库主要建立在Axis, Jena 以及Pellet上。
Apache Axis 是Apache Web Service项目中的子项目,其最初起源于IBM的"SOAP4J",应该属于最早的一批用于构造基于SOAP应用的Framework。
它支持WSDL1.1,可自动由Java Object生成WSDL。
Jena主要用来处理RDF,主要使用Jena的推理能力从本体推断模型知识。
Pellet是一个开源的基于JAVA的OWL推理机。
包中还自带两个jar包形式的java类库,owl-s.jar和upnp.jar。
该OWL-S API的主要功能如下:
读/写服务描述:
OWL-S API中有两个重要的接口:
OWLOntology和OWLKnowledgeBase。
OWLOntology代表了存储在单个文件中的信息,而OWLKnowledgeBase是许多Ontology的集合。
RDF数据可以加载到OWLOntology上,只有OWLOntology对象才能组合起来。
OWLKnowledgeBase中只有一个Ontology是用来存储数据的(例如执行之后,新的实例会被加到这个OWLOntology上。
函数OWLKnowledgeBase.read(URI)从给定的Ontology读取信息,并产生OWLOntology。
函数OWLOntology.getService()用来获取ontology中的服务实例。
如果有许多服务,则用OWLOntology.getServices()获取。
然后,函数OWLKnowledgeBase.readService(URI)以及OWLKnowledgeBase.readServices(URI)将会读取服务。
如果函数调用发生错误将会产生null输出。
函数OWLOntology.write(Writer)可以使包含服务的ontology组合起来。
这是一个例子:
// create a URI for the service (note that this is a 0.9 version file)
URI uri = new URI("http:
//www.mindswap.org/2004/owl-s/0.9/ZipCodeFinder.owl");
// create a KB
OWLKnowledgeBase kb = OWLFactory.createKB();
// create a generic reader and a 1.0 writer
OWLOntology ont = kb.read(uri);
// get the service
Service service = ont.getService();
// write the output to console (a file stream can also be used here)
ont.write(System.out);
将旧服务描述转换为新描述。
验证
缓存Ontology
执行服务:
执行服务意味着执行它的process。
Process应该有有效的grounding说明,以便有效的调用服务。
WSDL和UPnP的grounding由API支持,函数ProcessExecutionEngine.execute(Process, ValueMap)可以执行一个process,ValueMap表示输入的值,这个函数返回输出值。
举例如下:
// create an execution engine
ProcessExecutionEngine exec = OWLSFactory.createExecutionEngine();
// load the service description
Service service = kb.readService("http:
//www.mindswap.org/2004/owl-s/1.0/Dictionary.owl");
// get the process of the service
Process process = service.getProcess();
// create an empty value map
ValueMap values = new ValueMap();
// set the value of input parameter
values.setDataValue(process.getInput("InputString"), "computer");
// execute the process with the given input bindings
values = exec.execute(process, values);
// get the output value as a string
String outValue = values.getStringValue(process.getOutput());
// display the result
System.out.println("Output = " + outValue);
执行跟踪功能:
当执行复杂的服务时,知道执行的过程是很有用的,ProcessExecutionListener就是为这一目的设计的。
ProcessExecutionEngine.addExecutionListener(ProcessExecutionListener)就可以为执行器添加这么一个监听器。
生成复合过程
可以用程序产生服务的descriptions, profile或者processes描述。
OWLOntology接口实现了这个功能。
/**
*
* Create a new Sequence from the processes of the given services and put them in a new
* Service.
*
* @param services List of Services
* @param baseURI The base URI for the generated service
* @return The Service which is a Sequence of the given services
*/
Service createSequenceService(List services, String baseURI) {
// create an empty ontology
OWLOntology ont = OWLFactory.createOntology();
// create a new service
Service service = ont.createService(URI.create(baseURI + "Service"));
// create a new composite process
CompositeProcess process = ont.createCompositeProcess(URI.create(baseURI + "Process"));
// create a new sequence construct
Sequence sequence = ont.createSequence();
// put the sequence into composite process
compositeProcess.setComposedOf(sequence);
for(int i = 0; i < services.size(); i++) {
// get the service from the list
Service s = (Service) services.get(i);
// get the process fron the service
Process p = s.getProcess();
// create a perform construct
Perform perform = ont.createPreform();
perform.setProcess(p);
// put the process into the sequence
sequence.addComponent(p);
// create data flow if necessary...
}
// create profile...
// create grounding
return service;
}
支持功能。
API中包含了org.mindswap.owls.wsdl这个包,可以用来读写WSDL描述的服务。
执行OWL-S服务就是通过这个包实现的。
这个功能是建立在AXIS包1.1上的。
其他应用:
类CreateSequence表明了如何将一系列服务串联起来,并自动产生一个描述新服务的profile,这个类假定每个服务都是单输入单输出的(除了第一个服务和最后一个服务),这样前面的服务的输出将作为后一个服务的输入。
第一个服务不需要有输入,最后一个服务不需要有输出。
组合服务的名字命名为[Service1 + Service2 + ... + ServiceN]。
最后在main函数中进行测试,将BookFinder.owl和BNPrice.owl进行组合,输入"City of Glass",先查通过BookFinder.owl查到书号,然后将书号输入BNPrice.owl,得到输出:
Executing...done
Book Price =
Price:
currency:
USD
amount:
14.00
完整代码如下:
import .URI;
import java.util.ArrayList;
import java.util.List;
import org.mindswap.owl.EntityFactory;
import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLIndividual;
import org.mindswap.owl.OWLKnowledgeBase;
import org.mindswap.owl.OWLOntology;
import org.mindswap.owls.OWLSFactory;
import org.mindswap.owls.grounding.Grounding;
import org.mindswap.owls.process.AtomicProcess;
import org.mindswap.owls.process.CompositeProcess;
import org.mindswap.owls.process.Input;
import org.mindswap.owls.process.Output;
import org.mindswap.owls.process.Perform;
import org.mindswap.owls.process.Process;
import org.mindswap.owls.process.ProcessList;
import org.mindswap.owls.process.Result;
import org.mindswap.owls.process.Sequence;
import org.mindswap.owls.process.execution.ProcessExecutionEngine;
import org.mindswap.owls.profile.Profile;
import org.mindswap.owls.service.Service;
import org.mindswap.query.ValueMap;
import org.mindswap.utils.URIUtils;
import org.mindswap.utils.Utils;
/**
* An example to show how service descriptions can be created on the fly, saved and executed.
*
* @author Evren Sirin
*/
public class CreateSequence {
public static final URI baseURI = URI.create("http:
//www.example.org/BookPrice.owl#");
OWLOntology ont;
public CreateSequence() {
}
/**
*
* Create a new Sequence from the processes of the given services and put them in a new
* Service object with a automatically generated Profile. This function assumes that
* each service in the list has exactly one input and one output (except the first and
* last one) such that in the resulting Service the output of each service will be fed
* as input to the next one. The first service does not have to have an input and the
* last one does not need to have an output. The resulting service will have an input
* (or an output) depending on this.
*
* @param services List of Service objects
* @param baseURI The base URI for the generated service
* @return The Service which is a Sequence of the given services
*/
Service createSequenceService(List services) {
Service service = ont.createService(URIUtils.createURI(baseURI, "TestService"));
CompositeProcess process = ont.createCompositeProcess(URIUtils.createURI(baseURI, "TestProcess"));
Profile profile = ont.createProfile(URIUtils.createURI(baseURI, "TestProfile"));
Grounding grounding = ont.createGrounding(URIUtils.createURI(baseURI, "TestGrounding"));
System.out.println(ont.getKB().getServices());
service.setProfile(profile);
service.setProcess(process);
service.setGrounding(grounding);
createSequenceProcess(process, services);
createProfile(profile, process);
ProcessList list = process.getComposedOf().getAllProcesses();
for(int i = 0; i < list.size(); i++) {
Process pc = list.processAt(i);
if(pc instanceof AtomicProcess) {
grounding.addGrounding(((AtomicProcess)pc).getGrounding());
}
}
profile.setLabel(createLabel(services));
profile.setTextDescription(profile.getLabel());
service.setProfile(profile);
service.setProcess(process);
service.setGrounding(grounding);
return service;
}
/**
*
* Create a label for the composite service based on the labels of services. Basically
* return the string [Service1 + Service2 + ... + ServiceN] as the label
*
* @param services List of Servie objects
* @return
*/
String createLabel(List services) {
String label = "[";
for(int i = 0; i < services.size(); i++) {
Service s = (Service) services.get(i);
if(i > 0) label += " + ";
label += s.getLabel();
}
label += "]";
return label;
}
/**
*
* Create a Profile for the composite service. We only set the input and output of the profile
* based on the process.
*
* @param profile
* @param process
* @return
*/
Profile createProfile(Profile profile, Process process) {
for(int i = 0; i < process.getInputs().size(); i++) {
Input input = process.getInputs().inputAt(i);
profile.addInput(input);
}
for(int i = 0; i < process.getOutputs().size(); i++) {
Output output = process.getOutputs().outputAt(i);
profile.addOutput(output);
}
return profile;
}
/**
*
* Create a Sequence process for the processes of given services. Creates the DataFlow asssuming each
* service has one output and one intput (except first and last one).
*
* @param compositeProcess
* @param grounding
* @param services
* @return
*/
CompositeProcess createSequenceProcess(CompositeProcess compositeProcess, List services) {
Sequence sequence = ont.createSequence();
compositeProcess.setComposedOf(sequence);
Perform[] performs = new Perform[services.size()];
for(int i = 0; i < services.size(); i++) {
Service s = (Service) services.get(i);
Process p = s.getProcess();
performs[i] = ont.createPerform();
performs[i].setProcess(p);
sequence.addComponent(performs[i]);
if(i > 0) {
Perform prevPerform = performs[i - 1];
Input input = p.getInputs().inputAt(0);
Output output = prevPerform.getPr