2013年10月11日金曜日

SADI (Semantic Automated Discovery and Integration) の service skeleton (Java) を試す(Eclipse+m2e環境で)

参考:
Generating the SADI service code / BuildingServicesInJava / Tutorial: building a SADI service in Java.


環境:
  • Mavenを使えるようにした(m2eプラグインだったと思う)Eclipse(Kepler/4.3)
  • m2eupdate site情報


ダウンロード:


プロジェクトを作る:
  1. メニューから'File > Import'を選択
  2. ダイアログ中から'Existing Projects into Workspace'を選択
  3. ダイアログ中の'Select archive file:'にdownloadした'sadi-service-skeleton-*.zip'を設定
  4. ダイアログ中の'Finish'ボタンを押す.



サービスのコードを生成する:
  1. メニューから'Run > Run Configurations …'を選択
  2. ダイアログ中,左ペインから'Maven Build > generate sadi service'を選択


  3. ダイアログ中,右ペインのパラメタを修正
    
    serviceName : hello
    serviceClass: com.example.HelloWorldService
    inputClass  : http://sadiframework.org/examples/hello.owl#NamedIndividual
    outputClass : http://sadiframework.org/examples/hello.owl#GreetedIndividual
    contactEmail: your-email-address (そのままでも実行はできる / エンドポイントにて連絡先として公開される)
    (コピペ等で最初や最後に空白が入っていると失敗します)
    



  4. ダイアログ中の'Run'ボタンを押す
  5. 生成ファイルを確認するためにメニューから'File > Refresh'を選択
  6. ファイルを確認
    src/main/javaにcom.exampleパッケージのHelloWorldService.javaができている.
    src/main/webapp/WEB-INF/web.xmlが作られ(無い場合),hello Servletの設定が追加されている.
    src/main/webapp/index.jspが作られ(無い場合),“./hello”へのリンク(サービスのエンドポイント)が追加されている.
    



  7. ちなみに http://sadiframework.org/examples/hello.owl の中身はこんなの
    
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:owl="http://www.w3.org/2002/07/owl#">
        
      <owl:Ontology rdf:about="">
      </owl:Ontology>
      
      <owl:DatatypeProperty rdf:about="http://xmlns.com/foaf/0.1/name">
       <rdfs:isDefinedBy rdf:resource="http://xmlns.com/foaf/0.1/index.rdf"/>
      </owl:DatatypeProperty>
      
      <owl:DatatypeProperty rdf:about="#greeting"/>
      
      <owl:Class rdf:ID="NamedIndividual">
        <owl:equivalentClass>
          <owl:Restriction>
            <owl:onProperty rdf:resource="http://xmlns.com/foaf/0.1/name"/>
            <owl:minCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</owl:minCardinality>
          </owl:Restriction>
        </owl:equivalentClass>
      </owl:Class>
      
      <owl:Class rdf:ID="GreetedIndividual">
        <owl:equivalentClass>
          <owl:Restriction>
            <owl:onProperty rdf:resource="#greeting"/>
            <owl:someValuesFrom rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
          </owl:Restriction>
        </owl:equivalentClass>
      </owl:Class>
      
      <owl:DatatypeProperty rdf:about="#lang"/>
      
      <owl:Class rdf:ID="SecondaryParameters">
        <owl:equivalentClass>
          <owl:Restriction>
            <owl:onProperty rdf:resource="#lang"/>
            <owl:minCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</owl:minCardinality>
          </owl:Restriction>
        </owl:equivalentClass>
      </owl:Class>
      
    </rdf:RDF>
    


サービスのコードを実装する:
HelloWorldService.java中の'public void processInput(Resource input, Resource output)'を実装する.

 public void processInput(Resource input, Resource output) 
 {
  String name = input.getProperty(Vocab.name).getString(); 
  output.addProperty(Vocab.greeting, String.format("Hello, %s!", name));
 }


サービスを起動する:
  1. メニューから'Run > Run Configurations …'を選択
  2. ダイアログ中,左ペインから'Maven Build > run sadi services in Jetty'を選択


  3. ダイアログ中,'Run'ボタンを押す
これでサービスの開始.

Firefoxでアクセスした場合( http://localhost:8080/sadi-services/hello ):


Safariでアクセスした場合はRDFが返ってくる:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="?xsl" ?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:mygrid="http://www.mygrid.org.uk/mygrid-moby-service#"
    xmlns:j.0="http://protege.stanford.edu/plugins/owl/dc/protege-dc.owl#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <mygrid:serviceDescription rdf:about="">
    <mygrid:hasOperation>
      <mygrid:operation>
        <mygrid:outputParameter>
          <mygrid:parameter>
            <mygrid:objectType rdf:resource="http://sadiframework.org/examples/hello.owl#GreetedIndividual"/>
          </mygrid:parameter>
        </mygrid:outputParameter>
        <mygrid:inputParameter>
          <mygrid:parameter>
            <mygrid:objectType rdf:resource="http://sadiframework.org/examples/hello.owl#NamedIndividual"/>
          </mygrid:parameter>
        </mygrid:inputParameter>
      </mygrid:operation>
    </mygrid:hasOperation>
    <mygrid:providedBy>
      <mygrid:organisation>
        <mygrid:authoritative rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean"
        >false</mygrid:authoritative>
        <j.0:creator rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
        >your-email-address</j.0:creator>
      </mygrid:organisation>
    </mygrid:providedBy>
    <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
    >hello</rdfs:label>
    <mygrid:hasServiceNameText rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
    >hello</mygrid:hasServiceNameText>
  </mygrid:serviceDescription>
</rdf:RDF>


比較テストがあるのでやってみる:
  1. メニューから'Run > Run Configurations …'を選択
  2. ダイアログ中,左ペインから'Maven Build > test sadi service'を選択


  3. ダイアログ中,右ペインのパラメタを修正
    
    serviceURL: http://localhost:8080/sadi-services/hello
    input     : http://sadiframework.org/test/hello-input.rdf
    expected  : http://sadiframework.org/test/hello-output.rdf (比較対象/予定されている出力(を,
    Modle.write()で書き出し直したモノ)
    



  4. ちなみにhello-input.rdfの中身はこんなの

    
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:foaf="http://xmlns.com/foaf/0.1/"
        xmlns:hello="http://sadiframework.org/examples/hello.owl#">
    
     <hello:NamedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
      <foaf:name>Guy Incognito</foaf:name>
     </hello:NamedIndividual>
    
    </rdf:RDF>
    

    さらにちなむと,これをcom.hp.hpl.jena.rdf.model.Modelのreadメソッド読み込んでwriteメソッドで書き出すと以下のようになる.
    
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:foaf="http://xmlns.com/foaf/0.1/"
        xmlns:hello="http://sadiframework.org/examples/hello.owl#" > 
      <rdf:Description rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
        <foaf:name>Guy Incognito</foaf:name>
        <rdf:type rdf:resource="http://sadiframework.org/examples/hello.owl#NamedIndividual"/>
      </rdf:Description>
    </rdf:RDF>
    
  5. ちなみにhello-output.rdfの中身はこんなの

    
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:j.0="http://sadiframework.org/examples/hello.owl#" > 
      <rdf:Description rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
        <j.0:greeting>Hello, Guy Incognito!</j.0:greeting>
        <rdf:type rdf:resource="http://sadiframework.org/examples/hello.owl#GreetedIndividual"/>
      </rdf:Description>
    </rdf:RDF>
    
  6. ダイアログ中,'Run'ボタンを押す
上手く行けばConsoleに'[INFO] BUILD SUCCESS'と出ている.


curlを使ってアクセスしてみる:

$ cat hello-input.rdf
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:hello="http://sadiframework.org/examples/hello.owl#">

 <hello:NamedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
  <foaf:name>Guy Incognito</foaf:name>
 </hello:NamedIndividual>

</rdf:RDF>
$ curl -d '@hello-input.rdf' http://localhost:8080/sadi-services/hello
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://sadiframework.org/examples/hello.owl#">
  <j.0:GreetedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
    <j.0:greeting>Hello, Guy Incognito!</j.0:greeting>
  </j.0:GreetedIndividual>
</rdf:RDF>

2回分並べて入力してみる:

$ cat hello-input2.rdf
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:hello="http://sadiframework.org/examples/hello.owl#">

 <hello:NamedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
  <foaf:name>Guy Incognito</foaf:name>
 </hello:NamedIndividual>

 <hello:NamedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#2">
  <foaf:name>Gal Incognito</foaf:name>
 </hello:NamedIndividual>

</rdf:RDF>
$ curl -d '@hello-input2.rdf' http://localhost:8080/sadi-services/hello
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://sadiframework.org/examples/hello.owl#">
  <j.0:GreetedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#2">
    <j.0:greeting>Hello, Gal Incognito!</j.0:greeting>
  </j.0:GreetedIndividual>
  <j.0:GreetedIndividual rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
    <j.0:greeting>Hello, Guy Incognito!</j.0:greeting>
  </j.0:GreetedIndividual>
</rdf:RDF>

0 件のコメント: