ラベル sadi の投稿を表示しています。 すべての投稿を表示
ラベル sadi の投稿を表示しています。 すべての投稿を表示

2013年10月12日土曜日

先のPOST( SADI service の hello )をTomcatで動かしてみる

参考:
先のPOST

結果:
webapps以下にsaid-servicesを用意して配置


手順:
  1. ${TOMCAT_HOME}/webappsに'sadi-services'ディレクトリを用意(${TOMCAT_WEBAPPS_SADI-SARVICES}とする)する
  2. Eclipseのsadi-servicesプロジェクト(${ECLIPSE_SADI-SARVICES}とする)から
    'said-services'ディレクトリ(${TOMCAT_WEBAPPS_SADI-SARVICES})に以下を持ってくる
    • ${ECLIPSE_SADI-SARVICES}/src/main/webapp/index.jsp
      → ${TOMCAT_WEBAPPS_SADI-SARVICES}/index.jsp
    • ${ECLIPSE_SADI-SARVICES}/src/main/webapp/WEB-INF
      → ${TOMCAT_WEBAPPS_SADI-SARVICES}/WEB-INF
    • ${ECLIPSE_SADI-SARVICES}/target/classes
      → ${TOMCAT_WEBAPPS_SADI-SARVICES}/WEB-INF/classes
  3. 'sadi-services'ディレクトリ(${TOMCAT_WEBAPPS_SADI-SARVICES})に
    'lib'ディレクトリを用意( ${TOMCAT_WEBAPPS_SADI-SARVICES}/lib )する
  4. 'lib'ディレクトリ( ${TOMCAT_WEBAPPS_SADI-SARVICES}/lib )に上記画像にある各jarを複写する
    • それぞれのjarは,先のPOSTを実行しているのであれば,$HOME/.m2/repositoryの下に入っています.
    • commons-httpclient-*, xlightweb-*-taverna, xSocket-*は'/sadi-services/hello?xsl'にアクセスしなければ(Firefoxで開かなければ)いらない(かも)

もっと簡単な手順:
  1. Eclipseのsadi-servicesプロジェクト(${ECLIPSE_SADI-SARVICES}とする)に移る.
    $ cd ${ECLIPSE_SADI-SARVICES}
  2. mavenのpackageコマンドを実行する
    $ mvn package
  3. targetディレクトリにプロジェクト名のwarができてる.
    $ ls target/sadi-services.war
  4. このwarをTomcatのwebappsにおけばTomcatが展開するので,動かせる
    (EclipseのJavaとTomcatのJavaのヴァージョンの差異に注意)
    $ cp -p target/sadi-services.war ${TOMCAT_HOME}/webapps/


もっとちょっと簡単な手順:
  1. Eclipseのメニューから'Run > Run Configureations …'を選択
  2. ダイアログ中,左ペインで'Maven Build'を選択
  3. ダイアログ中,左ペイン,上部の左隅アイコン('New launch configuration')を押す
  4. ダイアログ中,左ペイン,'Maven Build > New_configuration (1)'が追加されたので選択
  5. ダイアログ中,右ペインを編集

  6. 'Run'を押す
  7. メニューから'File > Refresh'を選択
  8. targetディレクトリにプロジェクト名のwar( 'sadi-services.war' )ができてる.
  9. このwarをTomcatのwebappsにおけばTomcatが展開するので,動かせる
    (EclipseのJavaとTomcatのJavaのヴァージョンの差異に注意)

2013年10月11日金曜日

先のPOST( SADI service の hello )のクライアントをJavaで作ってみる

参考:
先のPOST

Build Path周り:
を,含むプロジェクト全体の図.
  • それぞれのjarは,先のPOSTを実行しているのであれば,$HOME/.m2/repositoryの下に入っています.
  • 面倒なら先のプロジェクトに居候してしまえばパスは通っているので簡単かもしれません.
  • コードをパッと見るとsadi-clientとjenaがあればよさそうですが,依存関係の都合で色々いれなくてはなりませんでした.


("slf4j-api-*.jar"だけでなく, "slf4j-log4j12-*.jar"が必要な場合もあるようです)

コード(Client.java):

package sample;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import ca.wilkinsonlab.sadi.SADIException;
import ca.wilkinsonlab.sadi.client.ServiceImpl;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;

public class Client {

 public static void main(String[] args) throws SADIException, IOException {
  Model inputModel = ModelFactory.createDefaultModel();
  if (true) {
   Resource type = ResourceFactory
     .createResource("http://sadiframework.org/examples/hello.owl#NamedIndividual");
   Resource s = inputModel
     .createResource(
       "http://sadiframework.org/examples/hello-input.rdf#1",
       type);
   s.addProperty(
     inputModel.createProperty("http://xmlns.com/foaf/0.1/name"),
     "Guy Incognito");
  } else {
   inputModel.read(new FileInputStream(new File("./hello-input.rdf")),
     "", "RDF/XML");
  }
  inputModel.write(System.out, "RDF/XML");

  String serviceURI = "http://localhost:8080/sadi-services/hello";
  ServiceImpl service = new ServiceImpl(serviceURI);

  Model outputModel = service.invokeServiceUnparsed(inputModel);
  outputModel.write(System.out, "RDF/XML");
 }
}

結果:
当たり前だけど, service側が動いている前提です.

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://xmlns.com/foaf/0.1/"
    xmlns:j.1="http://sadiframework.org/examples/hello.owl#" > 
  <rdf:Description rdf:about="http://sadiframework.org/examples/hello-input.rdf#1">
    <j.0:name>Guy Incognito</j.0:name>
    <rdf:type rdf:resource="http://sadiframework.org/examples/hello.owl#NamedIndividual"/>
  </rdf:Description>
</rdf: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>

先のPOST( SADI service の hello )から真似っこだけで,数値2入力の足し算サービスを追加してみる

Owlを用意( ./sample.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:Class rdf:ID="inputValues">
    <owl:equivalentClass>
      <owl:Restriction>
        <owl:onProperty rdf:resource="#number1"/>
        <owl:someValuesFrom rdf:resource="http://www.w3.org/2001/XMLSchema#int"/>
      </owl:Restriction>
    </owl:equivalentClass>
    <owl:equivalentClass>
      <owl:Restriction>
        <owl:onProperty rdf:resource="#number2"/>
        <owl:someValuesFrom rdf:resource="http://www.w3.org/2001/XMLSchema#int"/>
      </owl:Restriction>
    </owl:equivalentClass>
  </owl:Class>
  
  <owl:Class rdf:ID="outputValue">
    <owl:equivalentClass>
      <owl:Restriction>
        <owl:onProperty rdf:resource="#result"/>
        <owl:someValuesFrom rdf:resource="http://www.w3.org/2001/XMLSchema#int"/>
      </owl:Restriction>
    </owl:equivalentClass>
  </owl:Class>

</rdf:RDF>



サービスのコードを生成する:
やり方は先のPOST参照のこと.パラメータは以下の通り.

serviceName : add
serviceClass: com.example.AddService
inputClass  : file:./sample.owl#inputValues
outputClass : file:./sample.owl#outputValue
contactEmail: your-email-address (そのままでも実行できる / エンドポイントにて連絡先として公開される)


サービスのコードを修正・実装する:
AddService.java中の'static final class Vocab {'を修正する.
  • 'public static final Resource int ='と変数名が型になってしまったので'int'を'intType'と変更

 static final class Vocab {
  private static Model m_model = ModelFactory.createDefaultModel();

  public static final Property result = m_model
    .createProperty("file:./sample.owl#result");
  public static final Property number1 = m_model
    .createProperty("file:./sample.owl#number1");
  public static final Property number2 = m_model
    .createProperty("file:./sample.owl#number2");
  public static final Resource outputValue = m_model
    .createResource("file:./sample.owl#outputValue");
  public static final Resource intType = m_model
    .createResource("http://www.w3.org/2001/XMLSchema#int");
  public static final Resource inputValues = m_model
    .createResource("file:./sample.owl#inputValues");
 }

AddService.java中の'public void processInput(Resource input, Resource output)'を実装する.

 public void processInput(Resource input, Resource output) 
 {
  int n1 = input.getProperty(Vocab.number1).getInt();
  int n2 = input.getProperty(Vocab.number2).getInt();
  int result = n1 + n2;
  output.addProperty(Vocab.result, result + "");
 }


サービスを起動する:
やり方は先のPOST参照のこと.

Firefoxでアクセスすると( http://localhost:8080/sadi-services/add ):



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

$ cat add-input.rdf 
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:sample="file:./sample.owl#">

 <sample:inputValues rdf:about="file:./sample.owl#1">
  <sample:number1>1000000000</sample:number1>
  <sample:number2>20</sample:number2>
 </sample:inputValues>

</rdf:RDF>
$ curl -d '@add-input.rdf' http://localhost:8080/sadi-services/add
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="file:./sample.owl#">
  <j.0:outputValue rdf:about="file:./sample.owl#1">
    <j.0:result>1000000020</j.0:result>
  </j.0:outputValue>
</rdf:RDF>

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>