001/*
002 * Copyright (c) 2012-2013 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * Permission is hereby granted, without written agreement and without
006 * license or royalty fees, to use, copy, modify, and distribute this
007 * software and its documentation for any purpose, provided that the above
008 * copyright notice and the following two paragraphs appear in all copies
009 * of this software.
010 *
011 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 * SUCH DAMAGE.
016 *
017 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 * ENHANCEMENTS, OR MODIFICATIONS.
023 *
024 */
025package org.kepler.hadoop.execution;
026
027import java.io.IOException;
028import java.util.LinkedList;
029import java.util.List;
030
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.io.Writable;
033import org.apache.hadoop.io.WritableComparable;
034import org.apache.log4j.Logger;
035import org.kepler.ddp.actor.pattern.stub.MatchInput;
036import org.kepler.hadoop.io.KeyValuePair;
037import org.kepler.hadoop.util.StubUtilities;
038
039import ptolemy.data.Token;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.KernelException;
042
043//////////////////////////////////////////////////////////////////////////
044////KeplerApp4Match
045
046/**
047* This class will parse match sub-workflow, execute it according to its input, and return execution output. 
048* and send it out through fire().
049* 
050* @author Jianwu Wang (jianwu@sdsc.edu)
051* @version $Id: KeplerApp4Match.java 33070 2014-11-12 23:21:09Z crawl $
052*/
053public class KeplerApp4Match extends KeplerAppBase {
054        
055        static Logger logger = Logger.getLogger(KeplerApp4Match.class.getName());
056
057        public KeplerApp4Match(Configuration hadoopConf) {
058        super(hadoopConf, "MatchInput", "MatchOutput");
059        }
060        
061        public List<KeyValuePair> executeWf(WritableComparable key, Writable value1, Writable value2)                                   
062            throws IOException {        
063                try {
064                
065                        if (_sourceActor != null)
066                        {
067                            ((MatchInput) _sourceActor).setInput(StubUtilities.convertToToken(key),
068                                    StubUtilities.convertToToken(value1),
069                                    StubUtilities.convertToToken(value2));                      
070                        }
071                        
072            if(_runWorkflowLifecyclePerInput) {
073                _manager.execute();
074            }
075
076            List<KeyValuePair> exeResult =  new LinkedList<KeyValuePair>();
077                        if (_sinkActor != null)
078                        {
079                                final List<Token> tokenList = _sinkActor.getOutput();
080//                      if (tokenList != null ) 
081//                              for (Token token : tokenList) {
082//                              System.out.println("token type before converting into list:" + token.getType());
083//                              System.out.println("token value before converting into list:" + token.toString());
084//                      }
085                                if(tokenList != null) {
086                                    StubUtilities.convertTokenToList(tokenList, exeResult);
087                                } else if(_keplerManagerException != null) {
088                                    throw _keplerManagerException;
089                                }
090                        }
091                        return exeResult;       
092                } catch (IllegalActionException e) {
093                        throw new RuntimeException("Error getting output for " + _sinkActor.getName() + ".", e);
094                } catch (KernelException e) {
095                    throw new RuntimeException("Error executing workflow.", e);
096        }
097        }
098}