`
yydcj
  • 浏览: 59966 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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-s API,有mindswap组织维护,地址:http://www.mindswap.org/
该组织提供了许多工具API和工具,以及一些demo例子。
现在该网站已经停止了更新,最新的owl-s API由OSIRIS组织维护地址:http://on.cs.unibas.ch/owls-api/index.html
该网站提供的最新owl-s API3.0接口对应owl-s1.2
下面结合其提供的工具,我们给出一个例子
先用其提供的WSDL2OWLS工具,生成owls文件。
首先利用axis1.4发布一个服务
package com.hello;
public class SayHello {
public String sayHello(String name)
{
return "Hello World "+name ;
}
}
将其发布为服务后生成wsdl文件:

<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="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 ">
- <!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
- <wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string" />
</wsdl:message>
- <wsdl:portType name="SayHello">
- <wsdl:operation name="sayHello" parameterOrder="name">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="SayHelloServiceSoapBinding" type="impl:SayHello">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http " />
- <wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/ " namespace="http://hello.com " use="encoded" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/ " namespace="http://localhost:8080/axis/services/SayHelloService " use="encoded" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="SayHelloService">
- <wsdl:port binding="impl:SayHelloServiceSoapBinding" name="SayHelloService">
<wsdlsoap:address location="http://localhost:8080/axis/services/SayHelloService " />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

下面是生成的owl文件


<?xml version="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:Ontology rdf:about="">
    <owl:imports rdf:resource="http://www.daml.org/services/owl-s/1.2/Grounding.owl"/ >
    <owl:imports rdf:resource="http://www.daml.org/services/owl-s/1.2/Profile.owl"/ >
    <owl:imports rdf:resource="http://www.daml.org/services/owl-s/1.2/Service.owl"/ >
</owl:Ontology>
<service:Service rdf:ID="sayHelloService">
    <service:supports>
      <grounding:WsdlGrounding rdf:ID="sayHelloGrounding"/>
    </service:supports>
    <service:describedBy>
      <process:AtomicProcess rdf:ID="sayHelloProcess"/>
    </service:describedBy>
    <service:presents>
      <profile:Profile rdf:ID="sayHelloProfile"/>
    </service:presents>
</service:Service>
<profile:Profile rdf:about="#sayHelloProfile">
    <profile:hasOutput>
      <process:Output rdf:ID="sayHelloReturn">
        <process:parameterType rdf: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:parameterType rdf: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>Auto generated from http://localhost:8080/axis/services/SayHelloService?wsdl</profile:textDescription >
    <profile:serviceName>sayHello</profile:serviceName>
    <service:presentedBy rdf:resource="#sayHelloService"/>
</profile:Profile>
<process:AtomicProcess rdf:about="#sayHelloProcess">
    <process:hasOutput rdf:resource="#sayHelloReturn"/>
    <process:hasInput rdf:resource="#name"/>
    <service:describes rdf:resource="#sayHelloService"/>
    <rdfs:label>sayHelloProcess</rdfs:label>
</process:AtomicProcess>
<grounding:WsdlGrounding rdf:about="#sayHelloGrounding">
    <grounding:hasAtomicProcessGrounding>
      <grounding:WsdlAtomicProcessGrounding rdf:ID="sayHelloAtomicProcessGrounding"/>
    </grounding:hasAtomicProcessGrounding>
    <service:supportedBy rdf:resource="#sayHelloService"/>
</grounding:WsdlGrounding>
<grounding:WsdlAtomicProcessGrounding rdf:about="#sayHelloAtomicProcessGrounding">
    <grounding:wsdlOutput>
      <grounding:WsdlOutputMessageMap>        <grounding:wsdlMessagePart>http://localhost:8080/axis/services/SayHelloService?wsdl#sayHelloReturn</grounding:wsdlMessagePart>
        <grounding:owlsParameter rdf:resource="#sayHelloReturn"/>
      </grounding:WsdlOutputMessageMap>
    </grounding:wsdlOutput>
    <grounding:wsdlInput>
      <grounding:WsdlInputMessageMap>        <grounding:wsdlMessagePart>http://localhost:8080/axis/services/SayHelloService?wsdl#name</grounding:wsdlMessagePart>
        <grounding:owlsParameter rdf:resource="#name"/>
      </grounding:WsdlInputMessageMap>
    </grounding:wsdlInput>
    <grounding:wsdlOutputMessage rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI "    >http://localhost:8080/axis/services/SayHelloService#sayHelloResponse</grounding:wsdlOutputMessage>
    <grounding:wsdlInputMessage rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI "    >http://localhost:8080/axis/services/SayHelloService#sayHelloRequest</grounding:wsdlInputMessage>
    <grounding:wsdlDocument rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI "
    >http://localhost:8080/axis/services/SayHelloService?wsdl</grounding:wsdlDocument>
    <grounding:wsdlOperation>
      <grounding:WsdlOperationRef>
        <grounding:operation rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI "
        >http://localhost:8080/axis/services/SayHelloService?wsdl#sayHello</grounding:operation>
      </grounding:WsdlOperationRef>
    </grounding:wsdlOperation>
    <grounding:owlsProcess rdf:resource="#sayHelloProcess"/>
</grounding:WsdlAtomicProcessGrounding>
</rdf:RDF>


OWL-S API支持对语义Web服务的描述,读写,以及调用原子的或者组合的Web服务。
该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输出。
网上一堆文章都是拿别人的例子贴,这里给出一个简单的例子
上面生成了owl文件,下面贴出代码演示执行该服务的代码


package examples;
import java.net.URI;
import org.mindswap.owl.OWLClass;
import org.mindswap.owl.OWLDataProperty;
import org.mindswap.owl.OWLDataValue;
import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLIndividual;
import org.mindswap.owl.OWLKnowledgeBase;
import org.mindswap.owl.OWLObjectProperty;
import org.mindswap.owl.OWLValue;
import org.mindswap.owls.OWLSFactory;
import org.mindswap.owls.process.AtomicProcess;
import org.mindswap.owls.process.Process;
import org.mindswap.owls.process.execution.DefaultProcessMonitor;
import org.mindswap.owls.process.execution.ProcessExecutionEngine;
import org.mindswap.owls.process.variable.Input;
import org.mindswap.owls.process.variable.Output;
import org.mindswap.owls.service.Service;
import org.mindswap.query.ValueMap;
import org.mindswap.utils.Utils;
public class RunService
{
Service service;
Process process;
String inValue;
String outValue;
ValueMap<Input, OWLValue> inputs;
ValueMap<Output, OWLValue> outputs;
ProcessExecutionEngine exec;
public RunService() {
exec = OWLSFactory.createExecutionEngine();
exec.addMonitor(new DefaultProcessMonitor());
}
public void run() throws Exception {
    final OWLKnowledgeBase kb = OWLFactory.createKB();
service = kb.readService(URI.create("http://localhost:8080/axis/sayHello.owl "));

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

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


process = service.getProcess();
inputs = new ValueMap<Input, OWLValue>();
inputs.setValue(process.getInput("name "), kb.createDataValue("!!!"));

//这里的input的要和上面本体owl文件里的input的ID同名注意
outputs = exec.execute(process, inputs, kb);
OWLDataValue res=outputs.getDataValue(process.getOutput().getName());
System.out.println("Executed service '" + service + "'");
System.out.println("Grounding WSDL: " + ((AtomicProcess) process).getGrounding().getDescriptionURL());
System.out.println("Output = ");
System.out.println(res.getValue());
}

public static void main(final String[] args) throws Exception {
final RunService test = new RunService();
test.run();
}
}
结果如下:
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/Service.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/Profile.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/ActorDefault.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/ServiceParameter.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/ServiceCategory.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/Process.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/generic/ObjectList.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/generic/Expression.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://www.daml.org/services/owl-s/1.2/Grounding.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://on.cs.unibas.ch/owl-s/1.2/MoreGroundings.owl # ...
INFO [main] (Vocabulary.java:117) - Loading ontology http://on.cs.unibas.ch/owl-s/1.2/FLAService.owl # ...
Start executing process Atomic Process    sayHelloProcess
Inputs            [name Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#name ]
Outputs           [sayHelloReturn Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#sayHelloReturn ]
Inputs:
name = !!!
Execution finished for Atomic Process    sayHelloProcess
Inputs            [name Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#name ]
Outputs           [sayHelloReturn Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#sayHelloReturn ]
Outputs:
sayHelloReturn = Hello World !!!
Executed service 'Service    http://www.example.org/service.owl#sayHelloService
Profiles   [http://www.example.org/service.owl#sayHelloProfile http://www.daml.org/services/owl-s/1.2/Profile.owl#Profile
Inputs [name Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#name ]
Outputs [sayHelloReturn Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#sayHelloReturn ]
]
Process    Atomic Process    sayHelloProcess
Inputs            [name Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#name ]
Outputs           [sayHelloReturn Datatype[http://www.w3.org/2001/XMLSchema#string -> class java.lang.String] http://www.example.org/service.owl#sayHelloReturn ]
Groundings [http://www.example.org/service.owl#sayHelloGrounding]
Grounding WSDL: 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: The ServiceClass object does not implement the required method in the following form: OMElement sayHello(OMElement e)
org.mindswap.exceptions.ExecutionException: The ServiceClass object does not implement the required method in the following form: OMElement sayHello(OMElement e)
at impl.owls.grounding.WSDLAtomicGroundingImpl.invoke(WSDLAtomicGroundingImpl.java:190)
at impl.owls.process.execution.ProcessExecutionEngineImpl.executeAtomicProcess(ProcessExecutionEngineImpl.java:303)
at impl.owls.process.execution.ProcessExecutionEngineImpl.executeAtomicProcess(ProcessExecutionEngineImpl.java:89)
at impl.owls.process.AtomicProcessImpl.execute(AtomicProcessImpl.java:102)
at impl.owls.process.execution.ProcessExecutionEngineImpl.execute(ProcessExecutionEngineImpl.java:252)
at examples.RunService.runDictionary(RunService.java:320)
at examples.RunService.runAll(RunService.java:370)
at examples.RunService.main(RunService.java:390)
Caused by: The ServiceClass object does not implement the required method in the following form: OMElement sayHello(OMElement e)
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:1910)
at org.mindswap.wsdl.WSDLOperation.invoke(WSDLOperation.java:234)
at impl.owls.grounding.WSDLAtomicGroundingImpl.invoke(WSDLAtomicGroundingImpl.java:186)
... 7 more

 

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics