语义Web服务的API使用实例:OWL-S API结合matchmaker、推理机(Jena、Pellet)进行语义转换、匹配、组合及调用web服务

引自http://www.cnblogs.com/armymen1980/archive/2008/10/22/1316731.html

OWL-S API支持对语义Web服务的描述,读写,以及调用原子的或者组合的Web服务。
OWL API用来表达Web上的本体,并提 供推理引擎。
JENA是一个构建语义Web应用的JAVA框架,提供了基于规则的推理引擎。

附:OWL-S API指南
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 java.net.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.getProcess().getOutputs().outputAt(0);
    
    // the value of 'input' is the value of 'output' from 'prevPerform' 
    performs[i].addBinding(input, prevPerform, output);
   }
  }

  Perform firstPerform = performs[0];
  Perform lastPerform = performs[services.size()-1];
  boolean createInput = firstPerform.getProcess().getInputs().size() > 0;  
  boolean createOutput = lastPerform.getProcess().getOutputs().size() > 0;

  if(createInput) {
   Input input = firstPerform.getProcess().getInputs().inputAt(0);
   Input newInput = ont.createInput(URIUtils.createURI(baseURI, "TestInput"));
   newInput.setLabel(input.getLabel());
   newInput.setParamType(input.getParamType());
   newInput.setProcess(compositeProcess);
   
   // input of the first perform is directly read from the input of the
   // composite process
   performs[0].addBinding(input, Perform.TheParentPerform, newInput);
  } 
  
  if(createOutput) {
   Output output = lastPerform.getProcess().getOutputs().outputAt(0);
   Output newOutput = ont.createOutput(URIUtils.createURI(baseURI, "TestOutput"));
   newOutput.setLabel(output.getLabel());
   newOutput.setParamType(output.getParamType());
   newOutput.setProcess(compositeProcess);

   // the output of the composite process is the output pf last process
   Result result = ont.createResult();
   result.addBinding(newOutput, lastPerform, output);
   
   compositeProcess.setResult(result);
  } 
  
  return compositeProcess;
 }

 
 public void runTest() throws Exception {
  // create an OWL-S knowledge base
  OWLKnowledgeBase kb = OWLFactory.createKB();

  // create an empty ontology in this KB
  ont = kb.createOntology();
  
  // create an execution engine
  ProcessExecutionEngine exec = OWLSFactory.createExecutionEngine();
  
  // load two services
  Service s1 = kb.readService("http://www.mindswap.org/2004/owl-s/1.1/BookFinder.owl #");
  Service s2 = kb.readService("http://www.mindswap.org/2004/owl-s/1.1/BNPrice.owl #");
  
  // put the services in a list
  List services = new ArrayList();
  services.add(s1);
  services.add(s2);
  
  // create a new service as a sequence of the list
  Service s = createSequenceService(services);
  
  // print the description of new service to standard output
  ont.write(System.out, baseURI);
  System.out.println();
  
  // get the process of the new service
  Process process = s.getProcess();
  // initialize the input values to be empty
  ValueMap values = new ValueMap();
  // get the parameter using the local name
  values.setValue(process.getInputs().inputAt(0), EntityFactory.createDataValue("City of Glass"));
  
  // execute the service
  System.out.print("Executing...");
  values = exec.execute(process, values);
  System.out.println("done");
  
  // get the output param using the index  
  OWLIndividual outValue = values.getIndividualValue(process.getOutput());
  
  // display the result
  System.out.println("Book Price = ");
  System.out.println(Utils.formatRDF(outValue.toRDF()));  
  System.out.println();  
 } 

 public static void main(String[] args) throws Exception {  
  CreateSequence test = new CreateSequence();
  test.runTest();  
 }
}

 

ForEachExample类是对多输入值进行操作的例子,例如,分别输入三个邮政编码,然后调用服务得到三个地区的纬度和经度。


完整代码如下:

import impl.owls.process.execution.ProcessExecutionEngineImpl;

import java.net.URI;

import org.mindswap.owl.OWLClass;
import org.mindswap.owl.OWLDataProperty;
import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLIndividual;
import org.mindswap.owl.OWLKnowledgeBase;
import org.mindswap.owl.OWLOntology;
import org.mindswap.owl.list.RDFList;
import org.mindswap.owls.OWLSFactory;
import org.mindswap.owls.process.CompositeProcess;
import org.mindswap.owls.process.ForEach;
import org.mindswap.owls.process.Input;
import org.mindswap.owls.process.Local;
import org.mindswap.owls.process.Perform;
import org.mindswap.owls.process.Process;
import org.mindswap.owls.process.execution.ProcessExecutionEngine;
import org.mindswap.owls.service.Service;
import org.mindswap.owls.vocabulary.OWLS;
import org.mindswap.query.ValueMap;

/**
 * 
 * Example to show how to create and execute a forEach control construct.
 * 
 * @author Evren Sirin
 */
public class ForEachExample {
 public void run() throws Exception {
  String ns = "http://www.example.org/test #";  
  
  // print the inputs and outputs during each iteration of the loop
  ProcessExecutionEngineImpl.DEBUG = true;
  ProcessExecutionEngine exec = OWLSFactory.createExecutionEngine(); 
  
  OWLKnowledgeBase kb = OWLFactory.createKB();  
  
  Service service = kb.readService("http://www.mindswap.org/2004/owl-s/1.1/FindLatLong.owl" );
  Process process = service.getProcess();
  
  OWLOntology ont = kb.createOntology();
  CompositeProcess cp = ont.createCompositeProcess();
  Input in = ont.createInput(URI.create( ns + "in" ));
  in.setParamType(OWLS.ObjList.List());
  cp.addInput(in);

  // create a ForEach construct
  ForEach forEach = ont.createForEach();
  Local loopVar = ont.createLocal( URI.create( ns + "loopVar") );
  cp.setComposedOf(forEach);
  forEach.setListValue( Perform.TheParentPerform, in );
  forEach.setLoopVar( loopVar );

  // perform the process by passing the loop variable 
  Perform perform = ont.createPerform();
  perform.setProcess(process);
  perform.addBinding(process.getInput(), Perform.TheParentPerform, loopVar);
  
  forEach.setComponent(perform);
  
  // display how the construct looks like in RDF/XML
  ont.write(System.out);
  
  // create some zip code values
     String zipcodeOnt = "http://www.daml.org/2001/10/html/zipcode-ont #";
     OWLClass ZipCode = kb.getClass(URI.create(zipcodeOnt + "ZipCode"));
     OWLDataProperty zip = kb.getDataProperty(URI.create(zipcodeOnt + "zip"));
     
     OWLIndividual zip1 = ont.createInstance(ZipCode);
     zip1.setProperty(zip, "20740");
     OWLIndividual zip2 = ont.createInstance(ZipCode);
     zip2.setProperty(zip, "11430");
     OWLIndividual zip3 = ont.createInstance(ZipCode);
     zip3.setProperty(zip, "94102");
     
     // put them in a list
     RDFList list = ont.createList(zip1).add(zip2).add(zip3);
  
     ValueMap values = new ValueMap();
  values.setValue(cp.getInput("in"), list);
  
  exec.execute( cp , values );
 } 
 
 public static void main(String[] args) throws Exception {  
  ForEachExample test = new ForEachExample();
  
  test.run();  
 }
}


运行后得到输出:

Executing AtomicProcess http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #FindLatLongProcess
Inputs:
(http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #ZipCode=
  <j.0:ZipCode>
    <j.0:zip>20740</j.0:zip>
  </j.0:ZipCode>
)
Invoking http://cheeso.members.winisp.net/zips/ZipService.asmx?WSDL
Result:
(http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #LatLong=
  <j.0:LatLon>
    <j.0:latitude>38.996303</j.0:latitude>
    <j.0:longitude>-76.929891</j.0:longitude>
  </j.0:LatLon>
)

Executing AtomicProcess http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #FindLatLongProcess
Inputs:
(http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #ZipCode=
  <rdf:Description>
    <j.0:zip>11430</j.0:zip>
  </rdf:Description>
)
Invoking http://cheeso.members.winisp.net/zips/ZipService.asmx?WSDL
Result:
(http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #LatLong=
  <j.0:LatLon>
    <j.0:latitude>-0</j.0:latitude>
    <j.0:longitude>-0</j.0:longitude>
  </j.0:LatLon>
)

Executing AtomicProcess http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #FindLatLongProcess
Inputs:
(http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #ZipCode=
  <rdf:Description>
    <j.0:zip>94102</j.0:zip>
  </rdf:Description>
)
Invoking http://cheeso.members.winisp.net/zips/ZipService.asmx?WSDL
Result:
(http://www.mindswap.org/2004/owl-s/1.1/FindLobjectatLong.owl #LatLong=
  <j.0:LatLon>
    <j.0:latitude>-0</j.0:latitude>
    <j.0:longitude>-0</j.0:longitude>
  </j.0:LatLon>
)

 

Matchmaker这个例子说明了如何对服务进行匹配,实现服务的组合。服务的输出与服务的输入进行匹配,分别运 用"EXACT, SUBSUME以及RELAXED"作为匹配标准。这里应用了Pellet推理机。

对以下几个文件进行输入输出匹配:
http://www.mindswap.org/2004/owl-s/1.1/BNPrice.owl
http://www.mindswap.org/2004/owl-s/1.1/BookFinder.owl
http://www.mindswap.org/2004/owl-s/1.1/CurrencyConverter.owl
http://www.mindswap.org/2004/owl-s/1.1/Dictionary.owl
http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl
http://www.mindswap.org/2004/owl-s/1.1/FindLatLong.owl
http://www.mindswap.org/2004/owl-s/1.1/BabelFishTranslator.owl

得到运行结果:
Reading http://www.mindswap.org/2004/owl-s/1.1/BNPrice.owl
Reading http://www.mindswap.org/2004/owl-s/1.1/BookFinder.owl
Reading http://www.mindswap.org/2004/owl-s/1.1/CurrencyConverter.owl
Reading http://www.mindswap.org/2004/owl-s/1.1/Dictionary.owl
Reading http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl
Reading http://www.mindswap.org/2004/owl-s/1.1/FindLatLong.owl
Reading http://www.mindswap.org/2004/owl-s/1.1/BabelFishTranslator.owl #
Computing matches...

Matches:
EXACT BNPriceService.BookPrice -> CurrencyConverterService.InputPrice
EXACT BookFinderService.BookInfo -> BNPriceService.BookInfo
EXACT CurrencyConverterService.OutputPrice -> CurrencyConverterService.InputPrice
EXACT DictionaryService.OutputString -> BookFinderService.BookName
EXACT DictionaryService.OutputString -> DictionaryService.InputString
EXACT DictionaryService.OutputString -> ZipCodeFinderService.City
EXACT DictionaryService.OutputString -> ZipCodeFinderService.State
EXACT DictionaryService.OutputString -> BabelFishTranslatorService.InputString
EXACT ZipCodeFinderService.ZipCode -> FindLatLongService.ZipCode
EXACT BabelFishTranslatorService.OutputString -> BookFinderService.BookName
EXACT BabelFishTranslatorService.OutputString -> DictionaryService.InputString
EXACT BabelFishTranslatorService.OutputString -> ZipCodeFinderService.City
EXACT BabelFishTranslatorService.OutputString -> ZipCodeFinderService.State
EXACT BabelFishTranslatorService.OutputString -> BabelFishTranslatorService.InputString

 


完整代码如下:


import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLIndividual;
import org.mindswap.owl.OWLKnowledgeBase;
import org.mindswap.owl.OWLType;
import org.mindswap.owls.process.Input;
import org.mindswap.owls.process.Output;
import org.mindswap.owls.service.Service;
import org.mindswap.query.ValueMap;

/**
 * An example that finds service matches for composition. The outputs of services are matched with
 * the inputs of services using one of EXACT, SUBSUME and RELAXED match criteria. Pellet reasoner is
 * used to find matches but can be replaced with any other reasoner.
 *
 * @author Evren Sirin
 */
public class Matchmaker {   
    OWLKnowledgeBase kb;
   
    public static class Match {
        public static String[] MATCHES = {"EXACT", "SUBSUME", "RELAXED", "FAIL"};
        public static int EXACT   = 0;
        public static int SUBSUME = 1;
        public static int RELAXED = 2;
        public static int FAIL    = 3;       
       
        int matchType;
        boolean listMatch;
        Service outputService;
        Output output;
        Service inputService;
        Input input;
       
        public Match(int matchType, Output output, Input input) {
            this.matchType = matchType;
            this.outputService = output.getService();
            this.output = output;
            this.inputService = input.getService();
            this.input = input;
        }
       
        public String toString() {
            String str = "";
           
            str += MATCHES[matchType] + " ";
            if(listMatch)
                str += ".LIST";
            str += outputService.getLocalName() + "." + output.getLocalName();
            str += " -> ";
            str += inputService.getLocalName() + "." + input.getLocalName();
           
            return str;
        }
    }
   
    public Matchmaker() {
        kb = OWLFactory.createKB();
  
       kb.setReasoner("Pellet");
    }

    public void addOntology( String ont )  throws FileNotFoundException, URISyntaxException {
        System.out.println( "Reading " + ont );
        kb.read( new URI( ont ) );
    }
   
    public void addOntology( URI ont )  throws FileNotFoundException {
        System.out.println( "Reading " + ont );
        kb.read( ont );
    }
   
    public List findServices(boolean getProducers) {
        String hasParameter = getProducers ? "process:hasOutput" : "process:hasInput";
       
        String queryString =
            "SELECT * " +           
            "WHERE " +
            "    (?process rdf:type process:Process)" +
            "    (?process " + hasParameter + " ?param)" +
            "USING " +
            "      process FOR <http://www.daml.org/services/owl-s/1.1/Process.owl #>";
 
        return kb.query( queryString );
    }

    public List findOutputs() {
        return findServices(true);
    }
   
    public List findInputs() {
        return findServices(false);       
    }
   
    public int getMatchType(OWLType outputType, OWLType inputType) {
        if(outputType.isEquivalent(inputType))
           return Match.EXACT;
        else if(outputType.isSubTypeOf(inputType))
           return Match.SUBSUME;       
        else if(inputType.isSubTypeOf(outputType))
            return Match.RELAXED;
        else
            return Match.FAIL;
    }

 public List displayAllMatches() {
  List matches = new ArrayList();
  
  System.out.println( "Computing matches..." );
  
  List producers = findOutputs();
  List consumers = findInputs();
  
  Iterator i = producers.iterator();
  while( i.hasNext() ) {
      ValueMap binding = (ValueMap) i.next();
      Output output = (Output) ((OWLIndividual) binding.getValue("param")).castTo(Output.class);
      OWLType outputType = output.getParamType();
     
      Iterator j = consumers.iterator();
      while( j.hasNext() ) {
          binding = (ValueMap) j.next() ;
       Input input = (Input) ((OWLIndividual) binding.getValue("param")).castTo(Input.class);
       OWLType inputType = input.getParamType();
      
//       System.out.println("Trying " +
//           URIUtils.getLocalName(outputType.getURI()) + " " +
//           URIUtils.getLocalName(inputType.getURI()) + " " +
//           producer.getLocalName() + " " +
//           output.getLocalName() + " " +
//           consumer.getLocalName() + " " +
//           input.getLocalName()
//       );
      
          int matchType = getMatchType(outputType, inputType);
          if(matchType != Match.FAIL)
              matches.add(new Match(matchType, output, input));          
      }
  }
  
  return matches;
 }

    public static void printIterator(Iterator i) {
        if(i.hasNext()) {
         while (i.hasNext())
             System.out.println( i.next() );
        }      
        else
            System.out.println("<EMPTY>");
       
        System.out.println();
    }
 
    public static void main(String[] args) throws FileNotFoundException, URISyntaxException {
        Matchmaker matchmaker = new Matchmaker();
       
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/BNPrice.owl ");
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/BookFinder.owl ");
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/CurrencyConverter.owl ");
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/Dictionary.owl ");
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/ZipCodeFinder.owl ");
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/FindLatLong.owl ");
        matchmaker.addOntology("http://www.mindswap.org/2004/owl-s/1.1/BabelFishTranslator.owl #");
       
        List matches = matchmaker.displayAllMatches();
        System.out.println();
        System.out.println("Matches:");       
        printIterator(matches.iterator());
    }
}

 
对其他几个类作简单介绍:

OWLSValidator主要是对OWL-S文件的有效性进行检验。
PreconditionCheck说明了服务如何被执行。
OWLSExtensions 说明了API包的可扩展性,显示了默认Profile的可以扩充为用户自定义的Profile。
Translator主要是对不同版本的 service description进行转换,以保证兼容性。
WSDL2OWLS主要是将WDSL描述的服务自动转化为OWL-S描述。

你可能感兴趣的:(apache,.net,Web,应用服务器,J#)