CreateJavaGrounding.java revision 2
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * Created on 16.04.2005
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.grounding.JavaAtomicGrounding;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.process.AtomicProcess;
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainenimport org.mindswap.owls.process.execution.ProcessExecutionEngine;
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 * <pre>public String testIt(int i, Double y)</pre>
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen * @author Michael Daenzer, University of Zurich
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen public static void main(String[] args) throws Exception {
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen CreateJavaGrounding test = new CreateJavaGrounding();
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen String baseURL = "http://www.ifi.unizh.ch/ddis/ont/owl-s/";
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen OWLOntology ont = OWLFactory.createOntology(URI.create(baseURI));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Service service = ont.createService(URI.create(baseURI + "MyService"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen AtomicProcess process = ont.createAtomicProcess(URI.create(baseURI + "MyProcess"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Input input1 = ont.createInput(URI.create(baseURI + "myInput1"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen input1.setParamType(new OWLDataTypeImpl(XSD.nonNegativeInteger));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Input input2 = ont.createInput(URI.create(baseURI + "myInput2"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen input2.setParamType(new OWLDataTypeImpl(XSD.nonNegativeInteger));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen Output output = ont.createOutput(URI.create(baseURI + "myOutput"));
b3e4b3e7585f644a4d95293ca7bca19bcbf70c50Timo Sirainen output.setParamType(new OWLDataTypeImpl(XSD.nonNegativeInteger));
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 Grounding jGrounding = ont.createJavaGrounding(URI.create(baseURI + "MyJGrounding"));
300c5b822cde4a1d7247f49c9a0ab9f14c3ae37bTimo Sirainen ProcessExecutionEngine exec = OWLSFactory.createExecutionEngine();
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");
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);