2013年10月11日金曜日

先の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>

0 件のコメント: