001/* ComponentEntity that contains a list of provenance execution KeplerLSIDs.
002 * 
003 * Copyright (c) 2015 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2017-08-24 23:01:35 +0000 (Thu, 24 Aug 2017) $' 
008 * '$Revision: 34624 $'
009 * 
010 * Permission is hereby granted, without written agreement and without
011 * license or royalty fees, to use, copy, modify, and distribute this
012 * software and its documentation for any purpose, provided that the above
013 * copyright notice and the following two paragraphs appear in all copies
014 * of this software.
015 *
016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
020 * SUCH DAMAGE.
021 *
022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
027 * ENHANCEMENTS, OR MODIFICATIONS.
028 *
029 */
030package org.kepler.provenance.manager;
031
032import java.util.LinkedList;
033import java.util.List;
034
035import org.kepler.objectmanager.lsid.KeplerLSID;
036
037import ptolemy.data.expr.Parameter;
038import ptolemy.data.expr.StringParameter;
039import ptolemy.kernel.ComponentEntity;
040import ptolemy.kernel.ComponentPort;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045/** A ComponentEntity that contains a list of provenance execution
046 *  KeplerLSIDs. An instance of this class is used as the "save initiator"
047 *  in KAR files.
048 *  
049 *  @author Daniel Crawl
050 *  @version $Id: TransferComponent.java 34624 2017-08-24 23:01:35Z crawl $
051 */
052public class TransferComponent extends ComponentEntity<ComponentPort> {
053
054    /** Create a new TransferComponent.
055     * @param format The provenance format. 
056     */
057    public TransferComponent(String format) throws IllegalActionException, NameDuplicationException {
058        super();
059        _init();
060        _format = format;
061    }
062
063    /** Create a new TransferComponent in a container with a name. */
064    public TransferComponent(CompositeEntity container, String name)
065        throws NameDuplicationException, IllegalActionException {
066       super(container, name);
067       _init();
068    }
069    
070    /** Add KeplerLSIDs for provenance executions. This method removes
071     *  any existing KeplerLSIDs before adding the new ones.
072     */
073    public void addExecutions(List<KeplerLSID> executionLSIDs)
074        throws IllegalActionException {
075        
076        List<StringParameter> ids = new LinkedList<StringParameter>();
077        ids.addAll(runLSIDs.attributeList(StringParameter.class));
078        for(StringParameter id : ids) {
079            try {
080                id.setContainer(null);
081            } catch (IllegalActionException | NameDuplicationException e) {
082                throw new IllegalActionException(this, e, "Error removing " + id);
083            }
084        }
085        
086        int i = 0;
087        for(KeplerLSID lsid : executionLSIDs) {
088            String name = "id" + i;
089            StringParameter id;
090            try {
091                id = new StringParameter(runLSIDs, name);
092            } catch (NameDuplicationException e) {
093                throw new IllegalActionException(this, e, 
094                    "Error creating run id parameter.");
095            }
096            id.setToken(lsid.toString());            
097            i++;
098        }
099    }
100
101    /** Get the KeplerLSIDs for provenance executions. */
102    public List<KeplerLSID> getExecutionLSIDs() throws IllegalActionException {
103        
104        List<KeplerLSID> retval = new LinkedList<KeplerLSID>();
105        
106        for(StringParameter id : runLSIDs.attributeList(StringParameter.class)) {
107            String lsidStr = id.getExpression();
108            KeplerLSID lsid;
109            try {
110                lsid = new KeplerLSID(lsidStr);
111            } catch (Exception e) {
112                throw new IllegalActionException(this, e,
113                    "Error creating KeplerLSID from string " + lsidStr);
114            }
115            retval.add(lsid);
116        }
117        
118        return retval;
119    }
120    
121    /** Get the provenance format. */
122    public String getFormat() {
123        return _format;
124    }
125    
126    /** A parameter that contains a set of parameters, each of
127     *  which has an provenance execution KeplerLSID as the value. 
128     */
129    public Parameter runLSIDs;
130
131    ///////////////////////////////////////////////////////////////////
132    ////                         private methods                   ////
133
134    /** Initialize fields in the class. */
135    private void _init() throws IllegalActionException, NameDuplicationException {
136        runLSIDs = new Parameter(this, "runLSIDs");
137    }
138
139    /** The provenance format */
140    private String _format = "json";
141}