JavaAtomicGroundingImpl.java revision 31
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * Created on 13.04.2005
00d58fcfe8191d6ce7efa801d289a5c0fe88d1aeTimo Sirainenimport java.lang.reflect.InvocationTargetException;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.exceptions.ExecutionException;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.grounding.AtomicGrounding;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.grounding.JavaAtomicGrounding;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.grounding.JavaParameter;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.grounding.JavaVariable;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.grounding.MessageMapList;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenimport org.mindswap.owls.vocabulary.MoreGroundings;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * A JavaAtomicGrounding grounds an OWL-S Service to a Java method invocation. The method call
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * is specified by its method signature in an OWL-S Ontology. The driving parts are:
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * <li>fully qualified class name</li>
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * <li>method name</li>
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * <li>a map of all input parameters (at the time only primitive datatypes and their adapter classes)</li>
f6c1297c26b355c4aec2a08978f51ec3efecb351Timo Sirainen * <li>an output type (at the time only primitive datatypes and their adapter classes)</li>
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen * @author Michael Daenzer, University of Z�rich
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen * @see <a href="http://www.ifi.unizh.ch/ddis/ont/owl_s/MoreGroundings.owl">Grounding Ontology</a>
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.vocabulary.NextOnt
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see impl.owls.grounding.AtomicGroundingImpl
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenpublic class JavaAtomicGroundingImpl extends AtomicGroundingImpl implements JavaAtomicGrounding {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public JavaAtomicGroundingImpl(OWLIndividual ind) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setClaz(java.lang.String)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#getClaz()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return getPropertyAsString(MoreGroundings.javaClass);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setMethod(java.lang.String)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen setProperty(MoreGroundings.javaMethod, method);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* (non-Javadoc)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#getMethod()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return getPropertyAsString(MoreGroundings.javaMethod);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#getDescriptionURL()
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen /* (non-Javadoc)
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#addMessageMap(org.mindswap.owls.process.Parameter, java.lang.String, java.lang.String)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public void addMessageMap(Parameter owlsParameter,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String groundingParameter, String xsltTransformation) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#getInputMap()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#getOutputMap()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.query.ValueMap)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public ValueMap invoke(ValueMap values) throws ExecutionException {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.query.ValueMap, org.mindswap.owl.OWLKnowledgeBase)
b04e691711fd026fc82ba3e0b411420e7da4ec7eTimo Sirainen public ValueMap invoke(ValueMap values, OWLKnowledgeBase kb) throws ExecutionException {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // get and check class
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((strClaz == null) || (strClaz.equals("")))
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: No Java Class defined in Grounding!");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // get and check method
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((strMethod == null) || (strMethod.equals("")))
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: No Java Method defined in Grounding!");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // prepare parameters
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // get class and method reference
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen method = claz.getDeclaredMethod(strMethod, params);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Class " + strClaz + " defined in Grounding not found." );
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Method " + strMethod + " defined in Grounding not found." );
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: " + e.getClass().toString() + " ocurred: " + e.getMessage());
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // prepare inputs
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen OWLValue owlValue = values.getValue(getOWLSParameterAt(i));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen paramValues[i] = ReflectionUtils.getCastedObjectFromStringValue(((OWLDataValue) owlValue).getLexicalValue(), params[i]);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen paramValues[i] = getJavaParameterAt(i, (OWLIndividual) owlValue);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen //System.out.println( "Parameter " + i + " " + paramValues[i] );
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // invoke java method
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen if (method.getReturnType().toString().equalsIgnoreCase("void"))
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen throw new ExecutionException("JavaAtomicGrounding: Error in executed method\n" + e.getTargetException().toString() + " ocurred: ");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Error while executing method\n" + e.getClass().toString() + " ocurred: ");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // set output
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Output in Grounding not specified although method provides a return value.");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen results.setValue(param, EntityFactory.createDataValue(result));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen results.setValue(param, transformResult(result));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private OWLValue transformResult(Object result) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String transformerName = output.getTransformator().trim();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (transformerName != null && !transformerName.trim().equals("")) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Class transformerClass = Class.forName(transformerName);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Constructor constructor = transformerClass.getConstructor(new Class[] {OWLKnowledgeBase.class});
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen JavaClassTransformator transformer = (JavaClassTransformator) constructor.newInstance(new Object[] {getKB()});
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // returns the OWL-S Output Parameter
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen OWLObject owlsParam = getProperty(MoreGroundings.javaOutput).getProperty(MoreGroundings.owlsParameter);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return (owlsParam == null) ? null : (Parameter) owlsParam.castTo(Parameter.class);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // returns the n-th OWL-S Input Parameter
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private Parameter getOWLSParameterAt(int index) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen JavaParameter param = getInputParamter(index);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // returns the n-th Parameter of the Java Method specified in the OWL-S JavaAtomicProcessGrounding
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private Class getJavaParameterTypeAt(int index) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen JavaParameter param = getInputParamter(index);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String transformerName = param.getTransformator().trim();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (transformerName != null && !transformerName.trim().equals("")) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Class transformerClass = Class.forName(transformerName);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Constructor constructor = transformerClass.getConstructor(new Class[] {OWLKnowledgeBase.class});
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen JavaClassTransformator transformer = (JavaClassTransformator) constructor.newInstance(new Object[] {getKB()});
00d58fcfe8191d6ce7efa801d289a5c0fe88d1aeTimo Sirainen return ReflectionUtils.getClassFromString(javaType);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // returns the n-th Parameter of the Java Method specified in the OWL-S JavaAtomicProcessGrounding
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private Object getJavaParameterAt(int index, OWLIndividual owlValue) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen JavaParameter param = getInputParamter(index);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String transformerName = param.getTransformator().trim();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (transformerName != null && !transformerName.trim().equals("")) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen Class transformerClass = Class.forName(transformerName);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen Constructor constructor = transformerClass.getConstructor(new Class[] {OWLKnowledgeBase.class});
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen JavaClassTransformator transformer = (JavaClassTransformator) constructor.newInstance(new Object[] {getKB()});
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return transformer.transformFromOWL(owlValue);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return ReflectionUtils.getClassFromString(javaType);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setOutputVar(java.lang.String, java.lang.String, org.mindswap.owls.process.Output)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public void setOutput(String name, String type, Output owlsParameter) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen OWLIndividual ind = getOntology().createInstance(MoreGroundings.JavaVariable, URI.create(name));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen ind.setProperty(MoreGroundings.javaType, type);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen ind.setProperty(MoreGroundings.owlsParameter, owlsParameter);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setInputVar(java.lang.String, java.lang.String, int, org.mindswap.owls.process.Input)
OWLIndividual ind = getOntology().createInstance(MoreGroundings.JavaParameter, URIUtils.createURI(name));
private void removeAll() {
public void delete() {
removeAll();
super.delete();
return null;
return null;
int pos = i;
pos = j;
return list;