2ronwalf/*
2ronwalf * Created on 13.04.2005
2ronwalf */
2ronwalfpackage impl.owls.grounding;
2ronwalf
31daenzeroramaimport java.lang.reflect.Constructor;
2ronwalfimport java.lang.reflect.InvocationTargetException;
2ronwalfimport java.lang.reflect.Method;
2ronwalfimport java.net.URI;
2ronwalfimport java.net.URL;
2ronwalf
2ronwalfimport org.mindswap.exceptions.ExecutionException;
2ronwalfimport org.mindswap.owl.EntityFactory;
2ronwalfimport org.mindswap.owl.OWLDataValue;
2ronwalfimport org.mindswap.owl.OWLFactory;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLIndividualList;
2ronwalfimport org.mindswap.owl.OWLKnowledgeBase;
2ronwalfimport org.mindswap.owl.OWLObject;
2ronwalfimport org.mindswap.owl.OWLValue;
23daenzeroramaimport org.mindswap.owls.grounding.AtomicGrounding;
2ronwalfimport org.mindswap.owls.grounding.JavaAtomicGrounding;
24daenzeroramaimport org.mindswap.owls.grounding.JavaParameter;
24daenzeroramaimport org.mindswap.owls.grounding.JavaVariable;
2ronwalfimport org.mindswap.owls.grounding.MessageMapList;
2ronwalfimport org.mindswap.owls.process.Input;
2ronwalfimport org.mindswap.owls.process.Output;
2ronwalfimport org.mindswap.owls.process.Parameter;
2ronwalfimport org.mindswap.owls.vocabulary.MoreGroundings;
2ronwalfimport org.mindswap.owls.vocabulary.OWLS;
2ronwalfimport org.mindswap.query.ValueMap;
28daenzeroramaimport org.mindswap.utils.ReflectionUtils;
27daenzeroramaimport org.mindswap.utils.URIUtils;
2ronwalf
2ronwalf/**
2ronwalf * A JavaAtomicGrounding grounds an OWL-S Service to a Java method invocation. The method call
2ronwalf * is specified by its method signature in an OWL-S Ontology. The driving parts are:
2ronwalf * <ul>
2ronwalf * <li>fully qualified class name</li>
2ronwalf * <li>method name</li>
2ronwalf * <li>a map of all input parameters (at the time only primitive datatypes and their adapter classes)</li>
2ronwalf * <li>an output type (at the time only primitive datatypes and their adapter classes)</li>
2ronwalf * </ul>
2ronwalf *
2ronwalf * @author Michael Daenzer, University of Z�rich
2ronwalf *
2ronwalf * @see <a href="http://www.ifi.unizh.ch/ddis/ont/owl_s/MoreGroundings.owl">Grounding Ontology</a>
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding
2ronwalf * @see org.mindswap.owls.vocabulary.NextOnt
2ronwalf * @see impl.owls.grounding.AtomicGroundingImpl
2ronwalf */
30daenzeroramapublic class JavaAtomicGroundingImpl extends AtomicGroundingImpl implements JavaAtomicGrounding {
2ronwalf /**
2ronwalf * @param ind
2ronwalf */
2ronwalf public JavaAtomicGroundingImpl(OWLIndividual ind) {
2ronwalf super(ind);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setClaz(java.lang.String)
2ronwalf */
2ronwalf public void setClaz(String claz) {
2ronwalf setProperty(MoreGroundings.javaClass, claz);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.JavaAtomicGrounding#getClaz()
2ronwalf */
2ronwalf public String getClaz() {
2ronwalf return getPropertyAsString(MoreGroundings.javaClass);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setMethod(java.lang.String)
2ronwalf */
2ronwalf public void setMethod(String method) {
2ronwalf setProperty(MoreGroundings.javaMethod, method);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.JavaAtomicGrounding#getMethod()
2ronwalf */
2ronwalf public String getMethod() {
2ronwalf return getPropertyAsString(MoreGroundings.javaMethod);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#getDescriptionURL()
2ronwalf */
2ronwalf public URL getDescriptionURL() {
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#addMessageMap(org.mindswap.owls.process.Parameter, java.lang.String, java.lang.String)
2ronwalf */
2ronwalf public void addMessageMap(Parameter owlsParameter,
2ronwalf String groundingParameter, String xsltTransformation) {
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#getInputMap()
2ronwalf */
2ronwalf public MessageMapList getInputMap() {
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#getOutputMap()
2ronwalf */
2ronwalf public MessageMapList getOutputMap() {
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.query.ValueMap)
2ronwalf */
2ronwalf public ValueMap invoke(ValueMap values) throws ExecutionException {
2ronwalf return invoke(values, OWLFactory.createKB());
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.query.ValueMap, org.mindswap.owl.OWLKnowledgeBase)
2ronwalf */
2ronwalf public ValueMap invoke(ValueMap values, OWLKnowledgeBase kb) throws ExecutionException {
2ronwalf Class claz = null;
2ronwalf Method method = null;
2ronwalf Class params[] = null;
2ronwalf Object paramValues[] = null;
2ronwalf ValueMap results = new ValueMap();
2ronwalf Object result = null;
2ronwalf
2ronwalf // get and check class
2ronwalf String strClaz = getClaz();
2ronwalf if ((strClaz == null) || (strClaz.equals("")))
2ronwalf throw new ExecutionException("JavaAtomicGrounding: No Java Class defined in Grounding!");
2ronwalf // get and check method
2ronwalf String strMethod = getMethod();
2ronwalf if ((strMethod == null) || (strMethod.equals("")))
2ronwalf throw new ExecutionException("JavaAtomicGrounding: No Java Method defined in Grounding!");
2ronwalf
2ronwalf // prepare parameters
30daenzerorama int paramSize = getInputParameters().size();
30daenzerorama params = new Class[paramSize];
30daenzerorama for (int i = 0; i < paramSize; i++)
30daenzerorama params[i] = getJavaParameterTypeAt(i);
2ronwalf
2ronwalf // get class and method reference
2ronwalf try {
2ronwalf claz = Class.forName(strClaz);
2ronwalf method = claz.getDeclaredMethod(strMethod, params);
2ronwalf } catch (ClassNotFoundException e){
2ronwalf throw new ExecutionException("JavaAtomicGrounding: Class " + strClaz + " defined in Grounding not found." );
2ronwalf } catch (NoSuchMethodException e) {
2ronwalf throw new ExecutionException("JavaAtomicGrounding: Method " + strMethod + " defined in Grounding not found." );
2ronwalf } catch (Exception e) {
2ronwalf throw new ExecutionException("JavaAtomicGrounding: " + e.getClass().toString() + " ocurred: " + e.getMessage());
30daenzerorama }
2ronwalf
2ronwalf // prepare inputs
30daenzerorama paramValues = new Object[paramSize];
2ronwalf for (int i = 0; i < params.length; i++) {
30daenzerorama
30daenzerorama OWLValue owlValue = values.getValue(getOWLSParameterAt(i));
2ronwalf if (owlValue.isDataValue())
28daenzerorama paramValues[i] = ReflectionUtils.getCastedObjectFromStringValue(((OWLDataValue) owlValue).getLexicalValue(), params[i]);
30daenzerorama else if (owlValue.isIndividual()) {
30daenzerorama paramValues[i] = getJavaParameterAt(i, (OWLIndividual) owlValue);
2ronwalf }
3daenzerorama //System.out.println( "Parameter " + i + " " + paramValues[i] );
2ronwalf }
2ronwalf
2ronwalf // invoke java method
2ronwalf try {
2ronwalf Object obj = claz.newInstance();
2ronwalf if (method.getReturnType().toString().equalsIgnoreCase("void"))
2ronwalf method.invoke(obj, paramValues);
2ronwalf else
2ronwalf result = method.invoke(obj, paramValues);
2ronwalf } catch (InvocationTargetException e) {
2ronwalf throw new ExecutionException("JavaAtomicGrounding: Error in executed method\n" + e.getTargetException().toString() + " ocurred: ");
2ronwalf } catch (Exception e) {
2ronwalf throw new ExecutionException("JavaAtomicGrounding: Error while executing method\n" + e.getClass().toString() + " ocurred: ");
2ronwalf }
2ronwalf
2ronwalf // set output
2ronwalf if (result != null) {
30daenzerorama Parameter param = getOWLSOutput();
2ronwalf if (param == null)
3daenzerorama throw new ExecutionException("JavaAtomicGrounding: Output in Grounding not specified although method provides a return value.");
2ronwalf if(param.getParamType().isDataType())
2ronwalf results.setValue(param, EntityFactory.createDataValue(result));
2ronwalf else
30daenzerorama results.setValue(param, transformResult(result));
2ronwalf }
2ronwalf
2ronwalf return results;
2ronwalf }
2ronwalf
31daenzerorama private OWLValue transformResult(Object result) {
30daenzerorama JavaVariable output = getOutput();
30daenzerorama if (output == null)
30daenzerorama return null;
30daenzerorama
30daenzerorama String transformerName = output.getTransformator().trim();
30daenzerorama if (transformerName != null && !transformerName.trim().equals("")) {
30daenzerorama try {
30daenzerorama Class transformerClass = Class.forName(transformerName);
31daenzerorama Constructor constructor = transformerClass.getConstructor(new Class[] {OWLKnowledgeBase.class});
31daenzerorama JavaClassTransformator transformer = (JavaClassTransformator) constructor.newInstance(new Object[] {getKB()});
30daenzerorama return transformer.transformToOWL(result);
30daenzerorama } catch (ClassNotFoundException e) {
30daenzerorama e.printStackTrace();
30daenzerorama } catch (InstantiationException e) {
30daenzerorama e.printStackTrace();
30daenzerorama } catch (IllegalAccessException e) {
30daenzerorama e.printStackTrace();
31daenzerorama } catch (IllegalArgumentException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (InvocationTargetException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (SecurityException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (NoSuchMethodException e) {
31daenzerorama e.printStackTrace();
30daenzerorama }
30daenzerorama }
30daenzerorama return null;
30daenzerorama }
30daenzerorama
30daenzerorama // returns the OWL-S Output Parameter
30daenzerorama private Parameter getOWLSOutput() {
2ronwalf OWLObject owlsParam = getProperty(MoreGroundings.javaOutput).getProperty(MoreGroundings.owlsParameter);
2ronwalf return (owlsParam == null) ? null : (Parameter) owlsParam.castTo(Parameter.class);
2ronwalf }
3daenzerorama
2ronwalf
30daenzerorama
2ronwalf // returns the n-th OWL-S Input Parameter
30daenzerorama private Parameter getOWLSParameterAt(int index) {
30daenzerorama JavaParameter param = getInputParamter(index);
30daenzerorama if (param == null)
30daenzerorama return null;
2ronwalf
30daenzerorama return param.getOWLSParameter();
2ronwalf }
2ronwalf
2ronwalf // returns the n-th Parameter of the Java Method specified in the OWL-S JavaAtomicProcessGrounding
30daenzerorama private Class getJavaParameterTypeAt(int index) {
30daenzerorama JavaParameter param = getInputParamter(index);
30daenzerorama if (param == null)
30daenzerorama return null;
2ronwalf
30daenzerorama String transformerName = param.getTransformator().trim();
30daenzerorama String javaType = param.getJavaType();
30daenzerorama if (transformerName != null && !transformerName.trim().equals("")) {
30daenzerorama try {
30daenzerorama Class transformerClass = Class.forName(transformerName);
31daenzerorama Constructor constructor = transformerClass.getConstructor(new Class[] {OWLKnowledgeBase.class});
31daenzerorama JavaClassTransformator transformer = (JavaClassTransformator) constructor.newInstance(new Object[] {getKB()});
30daenzerorama return transformer.getJavaClass();
30daenzerorama } catch (ClassNotFoundException e) {
30daenzerorama e.printStackTrace();
30daenzerorama } catch (InstantiationException e) {
30daenzerorama e.printStackTrace();
30daenzerorama } catch (IllegalAccessException e) {
30daenzerorama e.printStackTrace();
31daenzerorama } catch (SecurityException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (NoSuchMethodException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (IllegalArgumentException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (InvocationTargetException e) {
31daenzerorama e.printStackTrace();
30daenzerorama }
30daenzerorama
30daenzerorama return null;
30daenzerorama } else {
30daenzerorama return ReflectionUtils.getClassFromString(javaType);
30daenzerorama }
2ronwalf }
30daenzerorama
30daenzerorama // returns the n-th Parameter of the Java Method specified in the OWL-S JavaAtomicProcessGrounding
30daenzerorama private Object getJavaParameterAt(int index, OWLIndividual owlValue) {
30daenzerorama JavaParameter param = getInputParamter(index);
30daenzerorama if (param == null)
30daenzerorama return null;
30daenzerorama
30daenzerorama String transformerName = param.getTransformator().trim();
30daenzerorama String javaType = param.getJavaType();
30daenzerorama if (transformerName != null && !transformerName.trim().equals("")) {
30daenzerorama try {
30daenzerorama Class transformerClass = Class.forName(transformerName);
31daenzerorama Constructor constructor = transformerClass.getConstructor(new Class[] {OWLKnowledgeBase.class});
31daenzerorama JavaClassTransformator transformer = (JavaClassTransformator) constructor.newInstance(new Object[] {getKB()});
30daenzerorama return transformer.transformFromOWL(owlValue);
30daenzerorama } catch (ClassNotFoundException e) {
30daenzerorama e.printStackTrace();
30daenzerorama } catch (InstantiationException e) {
30daenzerorama e.printStackTrace();
30daenzerorama } catch (IllegalAccessException e) {
30daenzerorama e.printStackTrace();
31daenzerorama } catch (SecurityException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (NoSuchMethodException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (IllegalArgumentException e) {
31daenzerorama e.printStackTrace();
31daenzerorama } catch (InvocationTargetException e) {
31daenzerorama e.printStackTrace();
30daenzerorama }
30daenzerorama
30daenzerorama return null;
30daenzerorama } else {
30daenzerorama return ReflectionUtils.getClassFromString(javaType);
30daenzerorama }
30daenzerorama }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setOutputVar(java.lang.String, java.lang.String, org.mindswap.owls.process.Output)
2ronwalf */
30daenzerorama public void setOutput(String name, String type, Output owlsParameter) {
2ronwalf OWLIndividual ind = getOntology().createInstance(MoreGroundings.JavaVariable, URI.create(name));
2ronwalf ind.setProperty(MoreGroundings.javaType, type);
2ronwalf ind.setProperty(MoreGroundings.owlsParameter, owlsParameter);
2ronwalf setProperty(MoreGroundings.javaOutput, ind);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.JavaAtomicGrounding#setInputVar(java.lang.String, java.lang.String, int, org.mindswap.owls.process.Input)
2ronwalf */
24daenzerorama public void setInputParameter(String name, String type, int index, Input owlsParameter) {
27daenzerorama OWLIndividual ind = getOntology().createInstance(MoreGroundings.JavaParameter, URIUtils.createURI(name));
2ronwalf ind.setProperty(MoreGroundings.javaType, type);
2ronwalf ind.setProperty(MoreGroundings.owlsParameter, owlsParameter);
2ronwalf ind.setProperty(MoreGroundings.paramIndex, Integer.toString(index));
2ronwalf addProperty(MoreGroundings.hasJavaParameter, ind);
2ronwalf }
23daenzerorama
23daenzerorama @Override
23daenzerorama public String getGroundingType() {
23daenzerorama return AtomicGrounding.GROUNDING_JAVA;
23daenzerorama }
23daenzerorama
23daenzerorama @Override
23daenzerorama public String toString() {
23daenzerorama return getClaz() + "." + getMethod();
23daenzerorama }
24daenzerorama
24daenzerorama private void removeAll() {
24daenzerorama if (hasProperty(MoreGroundings.javaClass))
24daenzerorama removeProperties(MoreGroundings.javaClass);
24daenzerorama if (hasProperty(MoreGroundings.javaMethod))
24daenzerorama removeProperties(MoreGroundings.javaMethod);
24daenzerorama
24daenzerorama // TODO an rdf:type property of the related input stays persistent. why????
24daenzerorama if (hasProperty(MoreGroundings.hasJavaParameter)) {
24daenzerorama OWLIndividualList indList = getProperties(MoreGroundings.hasJavaParameter);
24daenzerorama for (int i = 0; i < indList.size(); i++) {
24daenzerorama OWLIndividual ind = indList.individualAt(i);
24daenzerorama if (ind.hasProperty(MoreGroundings.javaType))
24daenzerorama ind.removeProperties(MoreGroundings.javaType);
24daenzerorama if (ind.hasProperty(MoreGroundings.owlsParameter))
24daenzerorama ind.removeProperties(MoreGroundings.owlsParameter);
24daenzerorama if (ind.hasProperty(MoreGroundings.paramIndex))
24daenzerorama ind.removeProperties(MoreGroundings.paramIndex);
24daenzerorama removeProperty(MoreGroundings.hasJavaParameter, ind);
24daenzerorama ind.delete();
24daenzerorama }
24daenzerorama }
24daenzerorama
24daenzerorama if (hasProperty(MoreGroundings.javaOutput)) {
24daenzerorama OWLIndividual ind = getProperty(MoreGroundings.javaOutput);
24daenzerorama if (ind.hasProperty(MoreGroundings.javaType))
24daenzerorama ind.removeProperties(MoreGroundings.javaType);
24daenzerorama if (ind.hasProperty(MoreGroundings.owlsParameter))
24daenzerorama ind.removeProperties(MoreGroundings.owlsParameter);
24daenzerorama removeProperties(MoreGroundings.javaOutput);
24daenzerorama ind.delete();
24daenzerorama }
24daenzerorama
24daenzerorama if (hasProperty(MoreGroundings.owlsProcess))
24daenzerorama removeProperties(MoreGroundings.owlsProcess);
24daenzerorama if (hasProperty(OWLS.Grounding.owlsProcess))
24daenzerorama removeProperties(OWLS.Grounding.owlsProcess);
24daenzerorama }
24daenzerorama
24daenzerorama @Override
24daenzerorama public void delete() {
24daenzerorama removeAll();
24daenzerorama
24daenzerorama super.delete();
24daenzerorama }
24daenzerorama
24daenzerorama public JavaParameter getInputParamter(Input input) {
24daenzerorama OWLIndividualList list = getPropertiesAs(MoreGroundings.hasJavaParameter, JavaParameter.class);
24daenzerorama for (int i = 0; i < list.size(); i++) {
25daenzerorama OWLIndividual ind = list.individualAt(i).getProperty(MoreGroundings.owlsParameter);
25daenzerorama if (ind.getURI().equals(input.getURI()))
24daenzerorama return (JavaParameter) list.individualAt(i);
24daenzerorama }
24daenzerorama return null;
24daenzerorama }
30daenzerorama
30daenzerorama public JavaParameter getInputParamter(int index) {
30daenzerorama OWLIndividualList list = getPropertiesAs(MoreGroundings.hasJavaParameter, JavaParameter.class);
30daenzerorama for (int i = 0; i < list.size(); i++) {
30daenzerorama String curIndex = list.individualAt(i).getProperty(MoreGroundings.paramIndex).getLexicalValue();
30daenzerorama if (Integer.valueOf(curIndex).intValue() == index)
30daenzerorama return (JavaParameter) list.individualAt(i);
30daenzerorama }
30daenzerorama return null;
30daenzerorama }
24daenzerorama
30daenzerorama public JavaVariable getOutput() {
24daenzerorama return (JavaVariable) getPropertyAs(MoreGroundings.javaOutput, JavaVariable.class);
26daenzerorama }
26daenzerorama
26daenzerorama public OWLIndividualList getInputParameters() {
26daenzerorama OWLIndividualList list = getPropertiesAs(MoreGroundings.hasJavaParameter, JavaParameter.class);
26daenzerorama
26daenzerorama list.add("tempPlaceForSwap");
26daenzerorama // loop through every element
26daenzerorama for (int i = 0; i < list.size() - 2; i++) {
26daenzerorama int element = Integer.parseInt(list.individualAt(i).getProperty(
26daenzerorama MoreGroundings.paramIndex).getLexicalValue());
26daenzerorama int min = Integer.MAX_VALUE;
26daenzerorama int pos = i;
26daenzerorama // find the minimal element in the rest
26daenzerorama for (int j = i + 1; j < list.size() - 1; j++) {
26daenzerorama int temp = Integer.parseInt(list.individualAt(j).getProperty(
26daenzerorama MoreGroundings.paramIndex).getLexicalValue());
26daenzerorama if (temp < min) {
26daenzerorama min = temp;
26daenzerorama pos = j;
26daenzerorama }
26daenzerorama }
26daenzerorama // swap it to the front
26daenzerorama if (min < element) {
26daenzerorama list.set(list.size() -1, list.get(i));
26daenzerorama list.set(i, list.get(pos));
26daenzerorama list.set(pos, list.get(list.size() - 1));
26daenzerorama }
26daenzerorama }
26daenzerorama
26daenzerorama list.remove(list.size() - 1);
26daenzerorama return list;
30daenzerorama }
30daenzerorama
30daenzerorama public String getTransformator(JavaVariable variable) {
30daenzerorama return getPropertyAsString(MoreGroundings.transformatorClass);
30daenzerorama }
30daenzerorama
30daenzerorama public void setTransformator(String transformClass) {
30daenzerorama setProperty(MoreGroundings.transformatorClass, transformClass);
24daenzerorama }
2ronwalf}
2ronwalf
2ronwalf