JavaAtomicGroundingImpl.java revision 31
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen/*
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * Created on 13.04.2005
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenpackage impl.owls.grounding;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport java.lang.reflect.Constructor;
00d58fcfe8191d6ce7efa801d289a5c0fe88d1aeTimo Sirainenimport java.lang.reflect.InvocationTargetException;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport java.lang.reflect.Method;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport java.net.URI;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport java.net.URL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.exceptions.ExecutionException;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owl.EntityFactory;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owl.OWLDataValue;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owl.OWLFactory;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenimport org.mindswap.owl.OWLIndividual;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenimport org.mindswap.owl.OWLIndividualList;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenimport org.mindswap.owl.OWLKnowledgeBase;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owl.OWLObject;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owl.OWLValue;
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;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.process.Input;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.process.Output;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.process.Parameter;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenimport org.mindswap.owls.vocabulary.MoreGroundings;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.owls.vocabulary.OWLS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.query.ValueMap;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.utils.ReflectionUtils;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenimport org.mindswap.utils.URIUtils;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen/**
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 * <ul>
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>
f6c1297c26b355c4aec2a08978f51ec3efecb351Timo Sirainen * </ul>
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen * @author Michael Daenzer, University of Z�rich
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen *
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 Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenpublic class JavaAtomicGroundingImpl extends AtomicGroundingImpl implements JavaAtomicGrounding {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /**
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @param ind
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public JavaAtomicGroundingImpl(OWLIndividual ind) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen super(ind);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setClaz(java.lang.String)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public void setClaz(String claz) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen setProperty(MoreGroundings.javaClass, claz);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#getClaz()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public String getClaz() {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return getPropertyAsString(MoreGroundings.javaClass);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setMethod(java.lang.String)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public void setMethod(String method) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen setProperty(MoreGroundings.javaMethod, method);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* (non-Javadoc)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen * @see org.mindswap.owls.grounding.JavaAtomicGrounding#getMethod()
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen */
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen public String getMethod() {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return getPropertyAsString(MoreGroundings.javaMethod);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#getDescriptionURL()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public URL getDescriptionURL() {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen }
b04e691711fd026fc82ba3e0b411420e7da4ec7eTimo Sirainen
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 */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public void addMessageMap(Parameter owlsParameter,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String groundingParameter, String xsltTransformation) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#getInputMap()
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public MessageMapList getInputMap() {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#getOutputMap()
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen */
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen public MessageMapList getOutputMap() {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.query.ValueMap)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen public ValueMap invoke(ValueMap values) throws ExecutionException {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return invoke(values, OWLFactory.createKB());
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* (non-Javadoc)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen * @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.query.ValueMap, org.mindswap.owl.OWLKnowledgeBase)
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen */
b04e691711fd026fc82ba3e0b411420e7da4ec7eTimo Sirainen public ValueMap invoke(ValueMap values, OWLKnowledgeBase kb) throws ExecutionException {
b04e691711fd026fc82ba3e0b411420e7da4ec7eTimo Sirainen Class claz = null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Method method = null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Class params[] = null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Object paramValues[] = null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen ValueMap results = new ValueMap();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Object result = null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // get and check class
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String strClaz = getClaz();
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 String strMethod = getMethod();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((strMethod == null) || (strMethod.equals("")))
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: No Java Method defined in Grounding!");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // prepare parameters
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen int paramSize = getInputParameters().size();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen params = new Class[paramSize];
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen for (int i = 0; i < paramSize; i++)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen params[i] = getJavaParameterTypeAt(i);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // get class and method reference
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen try {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen claz = Class.forName(strClaz);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen method = claz.getDeclaredMethod(strMethod, params);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen } catch (ClassNotFoundException e){
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Class " + strClaz + " defined in Grounding not found." );
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (NoSuchMethodException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Method " + strMethod + " defined in Grounding not found." );
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen } catch (Exception e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: " + e.getClass().toString() + " ocurred: " + e.getMessage());
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // prepare inputs
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen paramValues = new Object[paramSize];
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen for (int i = 0; i < params.length; i++) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen OWLValue owlValue = values.getValue(getOWLSParameterAt(i));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (owlValue.isDataValue())
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen paramValues[i] = ReflectionUtils.getCastedObjectFromStringValue(((OWLDataValue) owlValue).getLexicalValue(), params[i]);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen else if (owlValue.isIndividual()) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen paramValues[i] = getJavaParameterAt(i, (OWLIndividual) owlValue);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen //System.out.println( "Parameter " + i + " " + paramValues[i] );
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // invoke java method
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen try {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Object obj = claz.newInstance();
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen if (method.getReturnType().toString().equalsIgnoreCase("void"))
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen method.invoke(obj, paramValues);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen result = method.invoke(obj, paramValues);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen } catch (InvocationTargetException e) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen throw new ExecutionException("JavaAtomicGrounding: Error in executed method\n" + e.getTargetException().toString() + " ocurred: ");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (Exception e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Error while executing method\n" + e.getClass().toString() + " ocurred: ");
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // set output
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (result != null) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen Parameter param = getOWLSOutput();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (param == null)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen throw new ExecutionException("JavaAtomicGrounding: Output in Grounding not specified although method provides a return value.");
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if(param.getParamType().isDataType())
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen results.setValue(param, EntityFactory.createDataValue(result));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen results.setValue(param, transformResult(result));
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return results;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private OWLValue transformResult(Object result) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen JavaVariable output = getOutput();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (output == null)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String transformerName = output.getTransformator().trim();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (transformerName != null && !transformerName.trim().equals("")) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen try {
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 return transformer.transformToOWL(result);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (ClassNotFoundException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (InstantiationException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (IllegalAccessException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (IllegalArgumentException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen } catch (InvocationTargetException e) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen e.printStackTrace();
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen } catch (SecurityException e) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (NoSuchMethodException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // returns the OWL-S Output Parameter
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private Parameter getOWLSOutput() {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen OWLObject owlsParam = getProperty(MoreGroundings.javaOutput).getProperty(MoreGroundings.owlsParameter);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return (owlsParam == null) ? null : (Parameter) owlsParam.castTo(Parameter.class);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen // returns the n-th OWL-S Input Parameter
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen private Parameter getOWLSParameterAt(int index) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen JavaParameter param = getInputParamter(index);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen if (param == null)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return param.getOWLSParameter();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
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 if (param == null)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String transformerName = param.getTransformator().trim();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String javaType = param.getJavaType();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (transformerName != null && !transformerName.trim().equals("")) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen try {
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 return transformer.getJavaClass();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (ClassNotFoundException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen } catch (InstantiationException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (IllegalAccessException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (SecurityException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (NoSuchMethodException e) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen e.printStackTrace();
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen } catch (IllegalArgumentException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (InvocationTargetException e) {
00d58fcfe8191d6ce7efa801d289a5c0fe88d1aeTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
00d58fcfe8191d6ce7efa801d289a5c0fe88d1aeTimo Sirainen } else {
00d58fcfe8191d6ce7efa801d289a5c0fe88d1aeTimo Sirainen return ReflectionUtils.getClassFromString(javaType);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
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 if (param == null)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String transformerName = param.getTransformator().trim();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen String javaType = param.getJavaType();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (transformerName != null && !transformerName.trim().equals("")) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen try {
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 } catch (ClassNotFoundException e) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (InstantiationException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (IllegalAccessException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (SecurityException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (NoSuchMethodException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (IllegalArgumentException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } catch (InvocationTargetException e) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen e.printStackTrace();
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return null;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } else {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return ReflectionUtils.getClassFromString(javaType);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
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 */
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 setProperty(MoreGroundings.javaOutput, ind);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
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)
*/
public void setInputParameter(String name, String type, int index, Input owlsParameter) {
OWLIndividual ind = getOntology().createInstance(MoreGroundings.JavaParameter, URIUtils.createURI(name));
ind.setProperty(MoreGroundings.javaType, type);
ind.setProperty(MoreGroundings.owlsParameter, owlsParameter);
ind.setProperty(MoreGroundings.paramIndex, Integer.toString(index));
addProperty(MoreGroundings.hasJavaParameter, ind);
}
@Override
public String getGroundingType() {
return AtomicGrounding.GROUNDING_JAVA;
}
@Override
public String toString() {
return getClaz() + "." + getMethod();
}
private void removeAll() {
if (hasProperty(MoreGroundings.javaClass))
removeProperties(MoreGroundings.javaClass);
if (hasProperty(MoreGroundings.javaMethod))
removeProperties(MoreGroundings.javaMethod);
// TODO an rdf:type property of the related input stays persistent. why????
if (hasProperty(MoreGroundings.hasJavaParameter)) {
OWLIndividualList indList = getProperties(MoreGroundings.hasJavaParameter);
for (int i = 0; i < indList.size(); i++) {
OWLIndividual ind = indList.individualAt(i);
if (ind.hasProperty(MoreGroundings.javaType))
ind.removeProperties(MoreGroundings.javaType);
if (ind.hasProperty(MoreGroundings.owlsParameter))
ind.removeProperties(MoreGroundings.owlsParameter);
if (ind.hasProperty(MoreGroundings.paramIndex))
ind.removeProperties(MoreGroundings.paramIndex);
removeProperty(MoreGroundings.hasJavaParameter, ind);
ind.delete();
}
}
if (hasProperty(MoreGroundings.javaOutput)) {
OWLIndividual ind = getProperty(MoreGroundings.javaOutput);
if (ind.hasProperty(MoreGroundings.javaType))
ind.removeProperties(MoreGroundings.javaType);
if (ind.hasProperty(MoreGroundings.owlsParameter))
ind.removeProperties(MoreGroundings.owlsParameter);
removeProperties(MoreGroundings.javaOutput);
ind.delete();
}
if (hasProperty(MoreGroundings.owlsProcess))
removeProperties(MoreGroundings.owlsProcess);
if (hasProperty(OWLS.Grounding.owlsProcess))
removeProperties(OWLS.Grounding.owlsProcess);
}
@Override
public void delete() {
removeAll();
super.delete();
}
public JavaParameter getInputParamter(Input input) {
OWLIndividualList list = getPropertiesAs(MoreGroundings.hasJavaParameter, JavaParameter.class);
for (int i = 0; i < list.size(); i++) {
OWLIndividual ind = list.individualAt(i).getProperty(MoreGroundings.owlsParameter);
if (ind.getURI().equals(input.getURI()))
return (JavaParameter) list.individualAt(i);
}
return null;
}
public JavaParameter getInputParamter(int index) {
OWLIndividualList list = getPropertiesAs(MoreGroundings.hasJavaParameter, JavaParameter.class);
for (int i = 0; i < list.size(); i++) {
String curIndex = list.individualAt(i).getProperty(MoreGroundings.paramIndex).getLexicalValue();
if (Integer.valueOf(curIndex).intValue() == index)
return (JavaParameter) list.individualAt(i);
}
return null;
}
public JavaVariable getOutput() {
return (JavaVariable) getPropertyAs(MoreGroundings.javaOutput, JavaVariable.class);
}
public OWLIndividualList getInputParameters() {
OWLIndividualList list = getPropertiesAs(MoreGroundings.hasJavaParameter, JavaParameter.class);
list.add("tempPlaceForSwap");
// loop through every element
for (int i = 0; i < list.size() - 2; i++) {
int element = Integer.parseInt(list.individualAt(i).getProperty(
MoreGroundings.paramIndex).getLexicalValue());
int min = Integer.MAX_VALUE;
int pos = i;
// find the minimal element in the rest
for (int j = i + 1; j < list.size() - 1; j++) {
int temp = Integer.parseInt(list.individualAt(j).getProperty(
MoreGroundings.paramIndex).getLexicalValue());
if (temp < min) {
min = temp;
pos = j;
}
}
// swap it to the front
if (min < element) {
list.set(list.size() -1, list.get(i));
list.set(i, list.get(pos));
list.set(pos, list.get(list.size() - 1));
}
}
list.remove(list.size() - 1);
return list;
}
public String getTransformator(JavaVariable variable) {
return getPropertyAsString(MoreGroundings.transformatorClass);
}
public void setTransformator(String transformClass) {
setProperty(MoreGroundings.transformatorClass, transformClass);
}
}