owl-s API一个简单例子

owl-s语义Web服务标记语言, 在Tim Berners-Lee的提出Semantic Web之后,Semantic Web服务业也得到了快速的发展。

相关的工具很多,主流的框架有DARPA组织和wsmo组织,后者是欧洲提出的,前者组织提出的框架是主流的。

owl-s前身是daml-s,其地址:http://www.daml.org/index.html

现在owl-s的最新版本是owl-s1.2

工具API有owl-sAPI,有mindswap组织维护,地址:http://www.mindswap.org/

该组织提供了许多工具API和工具,以及一些demo例子。

现在该网站已经停止了更新,最新的owl-sAPI由OSIRIS组织维护地址:http://on.cs.unibas.ch/owls-api/index.html

该网站提供的最新owl-sAPI3.0接口对应owl-s1.2

下面结合其提供的工具,我们给出一个例子

先用其提供的WSDL2OWLS工具,生成owls文件。

首先利用axis1.4发布一个服务

packagecom.hello;

publicclassSayHello{

publicStringsayHello(Stringname)

{

return"HelloWorld"+name;

}

}

将其发布为服务后生成wsdl文件:

<?xml version="1.0" encoding="UTF-8" ?>

-<wsdl:definitionstargetNamespace="http://localhost:8080/axis/services/SayHelloService"xmlns:apachesoap="http://xml.apache.org/xml-soap"xmlns:impl="http://localhost:8080/axis/services/SayHelloService"xmlns:intf="http://localhost:8080/axis/services/SayHelloService"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:xsd="http://www.w3.org/2001/XMLSchema">

-<!--

WSDLcreatedbyApacheAxisversion:1.4

BuiltonApr22,2006(06:55:48PDT)

-->

-<wsdl:messagename="sayHelloResponse">

<wsdl:partname="sayHelloReturn"type="xsd:string"/>

</wsdl:message>

-<wsdl:messagename="sayHelloRequest">

<wsdl:partname="name"type="xsd:string"/>

</wsdl:message>

-<wsdl:portTypename="SayHello">

-<wsdl:operationname="sayHello"parameterOrder="name">

<wsdl:inputmessage="impl:sayHelloRequest"name="sayHelloRequest"/>

<wsdl:outputmessage="impl:sayHelloResponse"name="sayHelloResponse"/>

</wsdl:operation>

</wsdl:portType>

-<wsdl:bindingname="SayHelloServiceSoapBinding"type="impl:SayHello">

<wsdlsoap:bindingstyle="rpc"transport="http://schemas.xmlsoap.org/soap/http"/>

-<wsdl:operationname="sayHello">

<wsdlsoap:operationsoapAction=""/>

-<wsdl:inputname="sayHelloRequest">

<wsdlsoap:bodyencodingstyle="http://schemas.xmlsoap.org/soap/encoding/"namespace="http://hello.com"use="encoded"/>

</wsdl:input>

-<wsdl:outputname="sayHelloResponse">

<wsdlsoap:bodyencodingstyle="http://schemas.xmlsoap.org/soap/encoding/"namespace="http://localhost:8080/axis/services/SayHelloService"use="encoded"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

-<wsdl:servicename="SayHelloService">

-<wsdl:portbinding="impl:SayHelloServiceSoapBinding"name="SayHelloService">

<wsdlsoap:addresslocation="http://localhost:8080/axis/services/SayHelloService"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

下面是生成的owl文件

<?xmlversion="1.0"?>

<rdf:RDF

xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

xmlns:expr="http://www.daml.org/services/owl-s/1.2/generic/Expression.owl#"

xmlns:service="http://www.daml.org/services/owl-s/1.2/Service.owl#"

xmlns:process="http://www.daml.org/services/owl-s/1.2/Process.owl#"

xmlns:owl="http://www.w3.org/2002/07/owl#"

xmlns="http://www.example.org/service.owl"

xmlns:xsd="http://www.w3.org/2001/XMLSchema#"

xmlns:swrl="http://www.w3.org/2003/11/swrl#"

xmlns:grounding="http://www.daml.org/services/owl-s/1.2/Grounding.owl#"

xmlns:profile="http://www.daml.org/services/owl-s/1.2/Profile.owl#"

xmlns:list="http://www.daml.org/services/owl-s/1.2/generic/ObjectList.owl#"

xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"

xml:base="http://www.example.org/service.owl">

<owl:Ontologyrdf:about="">

<owl:importsrdf:resource="http://www.daml.org/services/owl-s/1.2/Grounding.owl"/>

<owl:importsrdf:resource="http://www.daml.org/services/owl-s/1.2/Profile.owl"/>

<owl:importsrdf:resource="http://www.daml.org/services/owl-s/1.2/Service.owl"/>

</owl:Ontology>

<service:Servicerdf:id="sayHelloService">

<service:supports>

<grounding:WsdlGroundingrdf:id="sayHelloGrounding"/>

</service:supports>

<service:describedBy>

<process:AtomicProcessrdf:id="sayHelloProcess"/>

</service:describedBy>

<service:presents>

<profile:Profilerdf:id="sayHelloProfile"/>

</service:presents>

</service:Service>

<profile:Profilerdf:about="#sayHelloProfile">

<profile:hasOutput>

<process:Outputrdf:id="sayHelloReturn">

<process:parameterTyperdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI"

>http://www.w3.org/2001/XMLSchema#string</process:parameterType>

<rdfs:label>sayHelloReturn</rdfs:label>

</process:Output>

</profile:hasOutput>

<profile:hasInput>

      <process:Input rdf:id="name">

//输入的名字参数

<process:parameterTyperdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI"

>http://www.w3.org/2001/XMLSchema#string</process:parameterType>

<rdfs:label>name</rdfs:label>

</process:Input>

</profile:hasInput>

<profile:textDescription>Autogeneratedfromhttp://localhost:8080/axis/services/SayHelloService?wsdl</profile:textDescription>

<profile:serviceName>sayHello</profile:serviceName>

<service:presentedByrdf:resource="#sayHelloService"/>

</profile:Profile>

<process:AtomicProcessrdf:about="#sayHelloProcess">

<process:hasOutputrdf:resource="#sayHelloReturn"/>

<process:hasInputrdf:resource="#name"/>

<service:describesrdf:resource="#sayHelloService"/>

<rdfs:label>sayHelloProcess</rdfs:label>

</process:AtomicProcess>

<grounding:WsdlGroundingrdf:about="#sayHelloGrounding">

<grounding:hasAtomicProcessGrounding>

<grounding:WsdlAtomicProcessGroundingrdf:id="sayHelloAtomicProcessGrounding"/>

</grounding:hasAtomicProcessGrounding>

<service:supportedByrdf:resource="#sayHelloService"/>

</grounding:WsdlGrounding>

<grounding:WsdlAtomicProcessGroundingrdf:about="#sayHelloAtomicProcessGrounding">

<grounding:wsdlOutput>

<grounding:WsdlOutputMessageMap><grounding:wsdlMessagePart>http://localhost:8080/axis/services/SayHelloService?wsdl#sayHelloReturn</grounding:wsdlMessagePart>

<grounding:owlsParameterrdf:resource="#sayHelloReturn"/>

</grounding:WsdlOutputMessageMap>

</grounding:wsdlOutput>

<grounding:wsdlInput>

<grounding:WsdlInputMessageMap><grounding:wsdlMessagePart>http://localhost:8080/axis/services/SayHelloService?wsdl#name</grounding:wsdlMessagePart>

<grounding:owlsParameterrdf:resource="#name"/>

</grounding:WsdlInputMessageMap>

</grounding:wsdlInput>

<grounding:wsdlOutputMessagerdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://localhost:8080/axis/services/SayHelloService#sayHelloResponse</grounding:wsdlOutputMessage>

<grounding:wsdlInputMessagerdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://localhost:8080/axis/services/SayHelloService#sayHelloRequest</grounding:wsdlInputMessage>

<grounding:wsdlDocumentrdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI"

>http://localhost:8080/axis/services/SayHelloService?wsdl</grounding:wsdlDocument>

<grounding:wsdlOperation>

<grounding:WsdlOperationRef>

<grounding:operationrdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI"

>http://localhost:8080/axis/services/SayHelloService?wsdl#sayHello</grounding:operation>

</grounding:WsdlOperationRef>

</grounding:wsdlOperation>

<grounding:owlsProcessrdf:resource="#sayHelloProcess"/>

</grounding:WsdlAtomicProcessGrounding>

</rdf:RDF>

OWL-SAPI支持对语义Web服务的描述,读写,以及调用原子的或者组合的Web服务。

该OWL-SAPI的主要功能如下:

读/写服务描述:

OWL-SAPI中有两个重要的接口: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输出。

网上一堆文章都是拿别人的例子贴,这里给出一个简单的例子

上面生成了owl文件,下面贴出代码演示执行该服务的代码

packageexamples;

importjava.net.URI;

importorg.mindswap.owl.OWLClass;

importorg.mindswap.owl.OWLDataProperty;

importorg.mindswap.owl.OWLDataValue;

importorg.mindswap.owl.OWLFactory;

importorg.mindswap.owl.OWLIndividual;

importorg.mindswap.owl.OWLKnowledgeBase;

importorg.mindswap.owl.OWLObjectProperty;

importorg.mindswap.owl.OWLValue;

importorg.mindswap.owls.OWLSFactory;

importorg.mindswap.owls.process.AtomicProcess;

importorg.mindswap.owls.process.Process;

importorg.mindswap.owls.process.execution.DefaultProcessMonitor;

importorg.mindswap.owls.process.execution.ProcessExecutionEngine;

importorg.mindswap.owls.process.variable.Input;

importorg.mindswap.owls.process.variable.Output;

importorg.mindswap.owls.service.Service;

importorg.mindswap.query.ValueMap;

importorg.mindswap.utils.Utils;

publicclassRunService

{

Serviceservice;

Processprocess;

StringinValue;

StringoutValue;

ValueMap<Input,OWLValue>inputs;

ValueMap<Output,OWLValue>outputs;

ProcessExecutionEngineexec;

publicRunService(){

exec=OWLSFactory.createExecutionEngine();

exec.addMonitor(newDefaultProcessMonitor());

}

publicvoidrun()throwsException{

finalOWLKnowledgeBasekb=OWLFactory.createKB();

service = kb.readService(URI.create("http://localhost:8080/axis/sayHello.owl"));

//这里也可以放在文件系统的某个目录里,举个例子,将owl文件放在工程当前目录下

//URI.create("file:./Hello.owl");

process=service.getProcess();

inputs=newValueMap<Input,OWLValue>();

inputs.setValue(process.getInput("name"), kb.createDataValue("!!!"));

//这里的input的要和上面本体owl文件里的input的ID同名注意

outputs=exec.execute(process,inputs,kb);

OWLDataValueres=outputs.getDataValue(process.getOutput().getName());

System.out.println("Executedservice'"+service+"'");

System.out.println("GroundingWSDL:"+((AtomicProcess)process).getGrounding().getDescriptionURL());

System.out.println("Output=");

System.out.println(res.getValue());

}

publicstaticvoidmain(finalString[]args)throwsException{

finalRunServicetest=newRunService();

test.run();

}

}

结果如下:

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/Service.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/Profile.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/ActorDefault.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/ServiceParameter.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/ServiceCategory.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/Process.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/generic/ObjectList.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/generic/Expression.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://www.daml.org/services/owl-s/1.2/Grounding.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://on.cs.unibas.ch/owl-s/1.2/MoreGroundings.owl#...

INFO[main](Vocabulary.java:117)-Loadingontologyhttp://on.cs.unibas.ch/owl-s/1.2/FLAService.owl#...

StartexecutingprocessAtomicProcesssayHelloProcess

Inputs[nameDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#name]

Outputs[sayHelloReturnDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#sayHelloReturn]

Inputs:

name=!!!

ExecutionfinishedforAtomicProcesssayHelloProcess

Inputs[nameDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#name]

Outputs[sayHelloReturnDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#sayHelloReturn]

Outputs:

sayHelloReturn=HelloWorld!!!

Executedservice'Servicehttp://www.example.org/service.owl#sayHelloService

Profiles[http://www.example.org/service.owl#sayHelloProfilehttp://www.daml.org/services/owl-s/1.2/Profile.owl#Profile

Inputs[nameDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#name]

Outputs[sayHelloReturnDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#sayHelloReturn]

]

ProcessAtomicProcesssayHelloProcess

Inputs[nameDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#name]

Outputs[sayHelloReturnDatatype[http://www.w3.org/2001/XMLSchema#string->classjava.lang.String]http://www.example.org/service.owl#sayHelloReturn]

Groundings[http://www.example.org/service.owl#sayHelloGrounding]

GroundingWSDL:http://localhost:8080/axis/services/SayHelloService?wsdl

Output=

Hello World !!!

最后注意补充一下:

本文的测试的服务是用axis1.4开发的,用axis2 1.5版本我测试有问题,而且在看了OWL-S API3.0的lib目录发现他里面的服务也是用的axis1.4,解析wsdl文件的jar包是wsdl4j1.5,

在axis2里引入了axisom机制,所以在这里执行时会有异常.

Execution failed:

org.mindswap.exceptions.ExecutionException:TheServiceClassobjectdoesnotimplementtherequiredmethodinthefollowingform:OMElementsayHello(OMElemente)

org.mindswap.exceptions.ExecutionException:TheServiceClassobjectdoesnotimplementtherequiredmethodinthefollowingform:OMElementsayHello(OMElemente)

atimpl.owls.grounding.WSDLAtomicGroundingImpl.invoke(WSDLAtomicGroundingImpl.java:190)

atimpl.owls.process.execution.ProcessExecutionEngineImpl.executeAtomicProcess(ProcessExecutionEngineImpl.java:303)

atimpl.owls.process.execution.ProcessExecutionEngineImpl.executeAtomicProcess(ProcessExecutionEngineImpl.java:89)

atimpl.owls.process.AtomicProcessImpl.execute(AtomicProcessImpl.java:102)

atimpl.owls.process.execution.ProcessExecutionEngineImpl.execute(ProcessExecutionEngineImpl.java:252)

atexamples.RunService.runDictionary(RunService.java:320)

atexamples.RunService.runAll(RunService.java:370)

atexamples.RunService.main(RunService.java:390)

Causedby:TheServiceClassobjectdoesnotimplementtherequiredmethodinthefollowingform:OMElementsayHello(OMElemente)

atorg.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)

atorg.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)

atorg.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)

atorg.apache.xerces.parsers.AbstractSAXParser.endElement(UnknownSource)

atorg.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(UnknownSource)

atorg.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(UnknownSource)

atorg.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(UnknownSource)

atorg.apache.xerces.parsers.XML11Configuration.parse(UnknownSource)

atorg.apache.xerces.parsers.XML11Configuration.parse(UnknownSource)

atorg.apache.xerces.parsers.XMLParser.parse(UnknownSource)

atorg.apache.xerces.parsers.AbstractSAXParser.parse(UnknownSource)

atorg.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(UnknownSource)

atjavax.xml.parsers.SAXParser.parse(SAXParser.java:395)

atorg.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)

atorg.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)

atorg.apache.axis.Message.getSOAPEnvelope(Message.java:435)

atorg.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)

atorg.apache.axis.client.AxisClient.invoke(AxisClient.java:206)

atorg.apache.axis.client.Call.invokeEngine(Call.java:2784)

atorg.apache.axis.client.Call.invoke(Call.java:2767)

atorg.apache.axis.client.Call.invoke(Call.java:1910)

atorg.mindswap.wsdl.WSDLOperation.invoke(WSDLOperation.java:234)

atimpl.owls.grounding.WSDLAtomicGroundingImpl.invoke(WSDLAtomicGroundingImpl.java:186)

... 7 more

引自http://hi.baidu.com/wxmsona/blog/item/4292f5c78d4ad9109c163d4b.html

相关推荐