001/*
002 * Copyright (c) 2017 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2017-08-29 15:27:08 -0700 (Tue, 29 Aug 2017) $' 
007 * '$Revision: 1392 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.webview.provenance;
031
032import java.util.Date;
033
034import org.kepler.objectmanager.lsid.KeplerLSID;
035import org.kepler.provenance.FireState;
036import org.kepler.provenance.ProvenanceRecorder;
037import org.kepler.provenance.RecordingException;
038import org.kepler.provenance.SimpleFiringRecording;
039import org.kepler.webview.server.WebViewableUtilities;
040
041import ptolemy.actor.Actor;
042import ptolemy.actor.FiringEvent;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.Nameable;
045import ptolemy.kernel.util.NamedObj;
046
047public class WebViewRecording extends SimpleFiringRecording<Integer> {
048
049    public WebViewRecording() throws RecordingException {
050        super();
051    }
052
053    @Override
054    /** Record an actor firing at a specific time. */
055    public void actorFire(FiringEvent event, Date timestamp) throws RecordingException {
056        
057        Actor actor = event.getActor();
058        FiringEvent.FiringEventType curEventType = event.getType();
059        FireState<Integer> fireState = _fireStateTable.get(actor);
060        synchronized (fireState) {
061            
062            // get the last type of firing start
063            FiringEvent.FiringEventType lastStartType = fireState.getLastStartFireType();
064            if (curEventType == FiringEvent.BEFORE_ITERATE ||
065                    (curEventType == FiringEvent.BEFORE_PREFIRE &&
066                    lastStartType != FiringEvent.BEFORE_ITERATE)) {
067
068                int firing = fireState.getNumberOfFirings() + 1;
069                fireState.fireStart(curEventType, firing);
070                
071                try {
072                    WebViewableUtilities.sendEvent(WebViewableUtilities.Event.FireStart,
073                            (NamedObj)actor, timestamp);
074                } catch (IllegalActionException e) {
075                    // TODO Auto-generated catch block
076                    e.printStackTrace();
077                }
078            }
079            // see if current firing is end of iteration:
080            else if (curEventType == FiringEvent.AFTER_ITERATE ||
081                    (curEventType == FiringEvent.AFTER_POSTFIRE &&
082                    lastStartType == FiringEvent.BEFORE_PREFIRE)) {
083                
084                if (curEventType == FiringEvent.AFTER_POSTFIRE) {
085                    fireState.fireStop(FiringEvent.AFTER_PREFIRE);
086                } else {
087                    fireState.fireStop(curEventType);
088                }
089                
090                try {
091                    WebViewableUtilities.sendEvent(WebViewableUtilities.Event.FireEnd,
092                            (NamedObj)actor, timestamp);
093                } catch (IllegalActionException e) {
094                    // TODO Auto-generated catch block
095                    e.printStackTrace();
096                }
097            }
098        }
099    }
100
101    /**
102     * An actor threw an exception.
103     * 
104     * @param source
105     * @param throwable
106     * @param executionLSID
107     * @throws RecordingException
108     */
109    @Override
110    public void executionError(Nameable source, Throwable throwable, KeplerLSID executionLSID)
111            throws RecordingException {
112        // TODO
113    }
114
115    /**
116     * Record the starting of workflow execution at a specific time.
117     * 
118     * @param executionLSID
119     * @throws RecordingException
120     */
121    @Override
122    public void executionStart(KeplerLSID executionLSID, Date timestamp) throws RecordingException {
123        try {
124            WebViewableUtilities.sendEvent(
125                    WebViewableUtilities.Event.WorkflowExecutionStart,
126                    _model, timestamp);
127        } catch (IllegalActionException e) {
128            // TODO Auto-generated catch block
129            e.printStackTrace();
130        }
131    }
132
133    /**
134     * Record the stopping of workflow execution.
135     * 
136     * @param executionLSID
137     * @throws RecordingException
138     */
139    @Override
140    public void executionStop(KeplerLSID executionLSID, Date timestamp) throws RecordingException {
141        try {
142            WebViewableUtilities.sendEvent(
143                    WebViewableUtilities.Event.WorkflowExecutionEnd,
144                    _model, timestamp);
145        } catch (IllegalActionException e) {
146            // TODO Auto-generated catch block
147            e.printStackTrace();
148        }
149    }
150
151    @Override
152    public void setContainer(ProvenanceRecorder container) {
153        super.setContainer(container);
154        if (_recorderContainer == null) {
155            _model = null;
156        } else {
157            _model = _recorderContainer.toplevel();
158        }
159    }
160
161    private NamedObj _model;
162}