001/* Utilities for PROV. 002 * 003 * Copyright (c) 2015 The Regents of the University of California. 004 * All rights reserved. 005 * 006 * '$Author: crawl $' 007 * '$Date: 2017-08-12 00:49:39 +0000 (Sat, 12 Aug 2017) $' 008 * '$Revision: 34606 $' 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.prov; 031 032import java.io.File; 033import java.io.FileOutputStream; 034import java.io.IOException; 035import java.io.OutputStream; 036 037import org.kepler.provenance.ProvenanceRecorder; 038import org.kepler.provenance.QueryException; 039import org.kepler.provenance.Queryable; 040import org.kepler.provenance.RecordPlayer; 041import org.kepler.provenance.RecordingException; 042import org.openprovenance.prov.interop.InteropFramework.ProvFormat; 043 044/** Utilities for PROV. 045 * 046 * @author Daniel Crawl 047 * @version $Id: ProvUtilities.java 34606 2017-08-12 00:49:39Z crawl $ 048 */ 049public class ProvUtilities { 050 051 /** This class cannot be instantiated. */ 052 private ProvUtilities() {} 053 054 /** Write PROV to a file. 055 * @parm fileName the output file name. 056 * @param formatStr the output format type. 057 * @param execId the workflow execution id. 058 */ 059 public static void writeProv(String fileName, String formatStr, Integer execId) 060 throws RecordingException { 061 062 if(formatStr.toUpperCase().equals("jpg")) { 063 formatStr = "JPEG"; 064 } 065 ProvFormat format = ProvFormat.valueOf(formatStr.toUpperCase()); 066 if(format == null) { 067 throw new RecordingException("Unknown PROV format: " + formatStr); 068 } 069 writeProv(fileName, format, execId); 070 } 071 072 /** Write PROV to an output stream. 073 * @parm stream the output stream. 074 * @param formatStr the output format type. 075 * @param execId the workflow execution id. 076 */ 077 public static void writeProv(OutputStream stream, String formatStr, Integer execId) 078 throws RecordingException { 079 080 if(formatStr.toUpperCase().equals("JPG")) { 081 formatStr = "JPEG"; 082 } 083 ProvFormat format = ProvFormat.valueOf(formatStr.toUpperCase()); 084 if(format == null) { 085 throw new RecordingException("Unknown PROV format: " + formatStr); 086 } 087 writeProv(stream, format, execId); 088 } 089 090 /** Write PROV to a file. 091 * @parm fileName the output file name. 092 * @param formatStr the output format type. 093 * @param execId the workflow execution id. 094 */ 095 public static void writeProv(String fileName, ProvFormat format, Integer execId) 096 throws RecordingException { 097 098 try(OutputStream stream = new FileOutputStream(new File(fileName))) { 099 writeProv(stream, format, execId); 100 } catch (IOException e) { 101 throw new RecordingException("Error writing to " + fileName, e); 102 } 103 } 104 105 /** Write PROV to an output stream. 106 * @parm stream the output stream. 107 * @param formatStr the output format type. 108 * @param execId the workflow execution id. 109 */ 110 public static void writeProv(OutputStream stream, ProvFormat format, Integer execId) 111 throws RecordingException { 112 113 ProvRecording recording = new ProvRecording(); 114 recording.setOutputType(format); 115 116 try(Queryable query = ProvenanceRecorder.getDefaultQueryable(null);) { 117 recording.setOutput(stream); 118 119 RecordPlayer player = new RecordPlayer(query, recording); 120 player.play(execId); 121 122 } catch (IOException e) { 123 throw new RecordingException("Error closing provenance query interface.", e); 124 } catch(QueryException e) { 125 throw new RecordingException("Error querying provenance.", e); 126 } 127 } 128}