2ronwalf//The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalfpackage impl.owls.grounding;
2ronwalf
2ronwalfimport java.net.URL;
2ronwalf
2ronwalfimport org.mindswap.exceptions.ExecutionException;
2ronwalfimport org.mindswap.owl.EntityFactory;
2ronwalfimport org.mindswap.owl.OWLFactory;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLKnowledgeBase;
2ronwalfimport org.mindswap.owls.OWLSFactory;
23daenzeroramaimport org.mindswap.owls.grounding.AtomicGrounding;
2ronwalfimport org.mindswap.owls.grounding.MessageMap;
2ronwalfimport org.mindswap.owls.grounding.MessageMapList;
2ronwalfimport org.mindswap.owls.grounding.UPnPAtomicGrounding;
2ronwalfimport org.mindswap.owls.process.Parameter;
2ronwalfimport org.mindswap.owls.vocabulary.FLAServiceOnt;
2ronwalfimport org.mindswap.query.ValueMap;
2ronwalfimport org.mindswap.utils.Utils;
2ronwalfimport org.mindswap.utils.XSLTEngine;
2ronwalfimport org.w3c.dom.Node;
2ronwalf
2ronwalfimport com.fujitsu.fla.upnp.Action;
2ronwalfimport com.fujitsu.fla.upnp.Argument;
2ronwalfimport com.fujitsu.fla.upnp.ArgumentList;
2ronwalfimport com.fujitsu.fla.upnp.ControlPoint;
2ronwalfimport com.fujitsu.fla.upnp.Device;
2ronwalfimport com.fujitsu.fla.upnp.Service;
2ronwalfimport com.fujitsu.fla.upnp.UPnPStatus;
2ronwalf
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic class UPnPAtomicGroundingImpl extends AtomicGroundingImpl implements UPnPAtomicGrounding {
2ronwalf public UPnPAtomicGroundingImpl(OWLIndividual ind) {
2ronwalf super(ind);
2ronwalf }
2ronwalf
2ronwalf public MessageMapList getInputMap() {
2ronwalf return OWLSFactory.createMessageMapList(getProperties(FLAServiceOnt.upnpInputMapping));
2ronwalf }
2ronwalf
2ronwalf public MessageMapList getOutputMap() {
2ronwalf return OWLSFactory.createMessageMapList(getProperties(FLAServiceOnt.upnpOutputMapping));
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.UPnPAtomicGrounding#setUPnPService(java.lang.String)
2ronwalf */
2ronwalf public void setUPnPService(String service) {
2ronwalf setProperty(FLAServiceOnt.upnpServiceID, service);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.UPnPAtomicGrounding#getUPnPService()
2ronwalf */
2ronwalf public String getUPnPService() {
2ronwalf return getPropertyAsString(FLAServiceOnt.upnpServiceID);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.UPnPAtomicGrounding#setUPnPAction(java.lang.String)
2ronwalf */
2ronwalf public void setUPnPAction(String action) {
2ronwalf setProperty(FLAServiceOnt.upnpCommand, action);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.UPnPAtomicGrounding#getUPnPAction()
2ronwalf */
2ronwalf public String getUPnPAction() {
2ronwalf return getPropertyAsString(FLAServiceOnt.upnpCommand);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.AtomicGrounding#getDescriptionURL()
2ronwalf */
2ronwalf public URL getDescriptionURL() {
2ronwalf try {
2ronwalf ControlPoint cp = new ControlPoint();
2ronwalf Device device = cp.getProxyDevice(getUPnPDescription());
2ronwalf String url = device.getPresentationURL();
2ronwalf cp.stop();
2ronwalf return new URL(url);
2ronwalf } catch(Exception e) {
2ronwalf return null;
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.UPnPAtomicGrounding#setUPnPDescription(java.lang.String)
2ronwalf */
2ronwalf public void setUPnPDescription(String description) {
2ronwalf setProperty(FLAServiceOnt.upnpDeviceURL, description);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.grounding.UPnPAtomicGrounding#getUPnPDescription()
2ronwalf */
2ronwalf public String getUPnPDescription() {
2ronwalf return getPropertyAsString(FLAServiceOnt.upnpDeviceURL);
2ronwalf }
2ronwalf
2ronwalf public ValueMap invoke(ValueMap values) {
2ronwalf return invoke(values, OWLFactory.createKB());
2ronwalf }
2ronwalf
2ronwalf public ValueMap invoke(ValueMap values, OWLKnowledgeBase kb) {
2ronwalf ControlPoint cp = new ControlPoint();
2ronwalf Device device = cp.getProxyDevice(getUPnPDescription());
2ronwalf Service service = device.getService(getUPnPService());
2ronwalf Action action = service.getAction(getUPnPAction());
2ronwalf
2ronwalf ValueMap results = new ValueMap();
2ronwalf
2ronwalf MessageMapList inputMapList = getInputMap();
2ronwalf ArgumentList inArguments = action.getInputArgumentList();
2ronwalf for(int i = 0; i < inArguments.size(); i++) {
2ronwalf Argument in = inArguments.getArgument(i);
2ronwalf MessageMap mp = inputMapList.getMessageMap(in.getName());
2ronwalf Parameter param = mp.getOWLSParameter();
2ronwalf
2ronwalf Object value = values.getValue(param);
2ronwalf Object inputValue = value;
2ronwalf
2ronwalf System.out.println("value = " + value);
2ronwalf if(mp.getTransformation() != null) {
2ronwalf value = XSLTEngine.transform(value.toString(), mp.getTransformation());
2ronwalf Node node = Utils.getAsNode(value.toString());
2ronwalf inputValue = node.getFirstChild().getNodeValue();
2ronwalf System.out.println("input value = " + value);
2ronwalf }
2ronwalf
2ronwalf action.setArgumentValue(mp.getGroundingParameter().toString(), inputValue.toString());
2ronwalf }
2ronwalf
2ronwalf boolean ctrlRes = action.postControlAction();
2ronwalf if (ctrlRes == false) {
2ronwalf UPnPStatus err = action.getControlStatus();
2ronwalf throw new ExecutionException(err.getDescription() + " (" + Integer.toString(err.getCode()) + ")");
2ronwalf }
2ronwalf
2ronwalf MessageMapList outputMapList = getOutputMap();
2ronwalf ArgumentList outArguments = action.getOutputArgumentList();
2ronwalf for(int i = 0; i < outArguments.size(); i++) {
2ronwalf Argument out = outArguments.getArgument(i);
2ronwalf MessageMap mp = outputMapList.getMessageMap(out.getName());
2ronwalf
2ronwalf if(mp == null) continue;
2ronwalf
2ronwalf Parameter param = mp.getOWLSParameter();
2ronwalf
2ronwalf Object outputValue = null;
2ronwalf
2ronwalf if(mp.getTransformation() == null)
2ronwalf outputValue = out.getValue();
2ronwalf else
2ronwalf outputValue = XSLTEngine.transform(out.getValue().toString(), mp.getTransformation());
2ronwalf
2ronwalf // FIXME UPnP
2ronwalf if(param.getParamType().isDataType())
2ronwalf results.setValue(param, EntityFactory.createDataValue( outputValue ) );
2ronwalf else
2ronwalf results.setValue(param, kb.parseLiteral( outputValue.toString() ));
2ronwalf }
2ronwalf
2ronwalf return results;
2ronwalf }
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, String groundingParameter, String xsltTransformation) {
2ronwalf MessageMap map = getOntology().createUPnPMessageMap();
2ronwalf
2ronwalf map.setOWLSParameter(owlsParameter);
2ronwalf map.setGroundingParameter(groundingParameter);
2ronwalf
2ronwalf if(xsltTransformation != null && xsltTransformation.length() > 0)
2ronwalf map.setTransformation(xsltTransformation);
2ronwalf
2ronwalf addMessageMap(map);
2ronwalf }
23daenzerorama
23daenzerorama @Override
23daenzerorama public String getGroundingType() {
23daenzerorama return AtomicGrounding.GROUNDING_UPNP;
23daenzerorama }
2ronwalf}