CreateJavaGrounding.java revision 2
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/*
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * Created on 16.04.2005
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen */
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenpackage examples;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport impl.jena.OWLDataTypeImpl;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport java.net.URI;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
883da299d066d73f2a7995b6718a19aea2f542efAki Tuomiimport org.mindswap.owl.OWLFactory;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owl.OWLOntology;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owl.vocabulary.XSD;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.OWLSFactory;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.grounding.Grounding;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.grounding.JavaAtomicGrounding;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.process.AtomicProcess;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.process.Input;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.process.Output;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.process.execution.ProcessExecutionEngine;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.service.Service;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.query.ValueMap;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen/**
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * <p>
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * This example shows how to create a service grounded to a simple Java method.
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * A sample service is generated and a grounding is created, which matches to
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * the following method call:
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * <br>
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * <pre>public String testIt(int i, Double y)</pre>
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * </p>
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen *
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * @author Michael Daenzer, University of Zurich
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen */
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenpublic class CreateJavaGrounding {
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen public static void main(String[] args) throws Exception {
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen CreateJavaGrounding test = new CreateJavaGrounding();
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen test.run();
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen }
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen public void run() throws Exception {
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen String baseURL = "http://www.ifi.unizh.ch/ddis/ont/owl-s/";
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen String baseURI = baseURL + "MyTestService#";
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen OWLOntology ont = OWLFactory.createOntology(URI.create(baseURI));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Service service = ont.createService(URI.create(baseURI + "MyService"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen AtomicProcess process = ont.createAtomicProcess(URI.create(baseURI + "MyProcess"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen service.setProcess(process);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Input input1 = ont.createInput(URI.create(baseURI + "myInput1"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen input1.setParamType(new OWLDataTypeImpl(XSD.nonNegativeInteger));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen input1.setProcess(process);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Input input2 = ont.createInput(URI.create(baseURI + "myInput2"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen input2.setParamType(new OWLDataTypeImpl(XSD.nonNegativeInteger));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen input2.setProcess(process);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Output output = ont.createOutput(URI.create(baseURI + "myOutput"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen output.setParamType(new OWLDataTypeImpl(XSD.nonNegativeInteger));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen output.setProcess(process);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen JavaAtomicGrounding jAtomicGround = ont.createJavaAtomicGrounding(URI.create(baseURI + "MyJAtomGround"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jAtomicGround.setOutputVar(baseURI + "JPar1", "java.lang.String", output);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jAtomicGround.setInputVar(baseURI + "JIn1", "int", 1, input1);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jAtomicGround.setInputVar(baseURI + "JIn2", "java.lang.Double", 2, input2);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jAtomicGround.setClaz("examples.CreateJavaGrounding");
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jAtomicGround.setMethod("testIt");
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen jAtomicGround.setProcess(process);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Grounding jGrounding = ont.createJavaGrounding(URI.create(baseURI + "MyJGrounding"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jGrounding.addGrounding(jAtomicGround);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen jGrounding.setService(service);
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen ont.write(System.out);
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen
0aaea0919a4980f31d2f43bc81de986b08dcc7dfTimo Sirainen System.out.println();
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen System.out.println( "Executing service:" );
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen ValueMap values = new ValueMap();
300c5b822cde4a1d7247f49c9a0ab9f14c3ae37bTimo Sirainen ProcessExecutionEngine exec = OWLSFactory.createExecutionEngine();
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen // get the parameter using the local name
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen values.setDataValue(process.getInput("myInput1"), "2");
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen values.setDataValue(process.getInput("myInput2"), "3");
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen values = exec.execute(process, values);
51bfd4fbaf2b2148f8229f6a1650f28b61794effTimo Sirainen }
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen public String testIt(int i, Double y) throws Exception {
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen double s = i * y.doubleValue();
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen // something to show correct invocation
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen System.out.println("FirstParameter * SecondParameter = " + s);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen // provoke exception
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen //s = Double.parseDouble("ThisThrowsAnException");
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen return new String("Return value of JavaGrounding " + s);
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen }
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen}
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen