001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
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.ecoinformatics.seek.garp;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.StringToken;
035import ptolemy.data.expr.FileParameter;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040
041/**
042 * <p>
043 * GARP is a computer program for predicting species locations based on various
044 * spatial data sets of environment variables and known species locations. GARP
045 * is an acronym for Genetic Algorithm for Rule Set Production. GARP was
046 * originally ceated by David Stockwell. The version in Kepler is based on
047 * 'Desktop GARP', http://www.lifemapper.org/desktopgarp/. The GarpPrediction
048 * actor predicts presence/absence data on a spatial grid based on the input
049 * RuleSet (calculated by the GarpAlgorithm actor) and the input set of
050 * environmental layers. The input layers are described in a summary xml file
051 * (*.dxl). The outputs are either an *.asc grid file or a *.bmp file. Either
052 * can be displayed as a bitmapped image with predicted presence/absence
053 * indicated by pixel values (e.g. color mapped when displayed).
054 * </p>
055 * <p>
056 * This is a JNI-based actor. It requires the following: linux: libgarp.so
057 * windows: garp.dll, libexpat.dll MacOSX - currently not available for the Mac
058 * (3/16/2006)
059 * </p>
060 * 
061 * @author Chad Berkeley, Dan Higgins, NCEAS, UC Santa Barbara
062 */
063public class GarpPrediction extends TypedAtomicActor {
064        // FileParameters
065        /**
066         * This is the file name of the file containing the RuleSet data. It is
067         * usually the output of a GarpAlgorithm actor.
068         */
069        public FileParameter ruleSetFilenameParameter = new FileParameter(this,
070                        "ruleSetFilenameParameter");
071        /**
072         * This is the file name of the *.dxl file used to summarize the set of
073         * spatial data files with environmental data for each pixel.
074         */
075        public FileParameter layersetFilenameParameter = new FileParameter(this,
076                        "layersetFilenameParameter");
077        /**
078         * This is the file name to be used for the output ASCII grid file.
079         */
080        public FileParameter outputASCIIParameter = new FileParameter(this,
081                        "outputASCIIParameter");
082        /**
083         * This is the file name to be used for the output BMP raster file.
084         */
085        public FileParameter outputBMPParameter = new FileParameter(this,
086                        "outputBMPParameter");
087
088        // input ports
089        /**
090         * This is the file name of the file containing the RuleSet data. It is
091         * usually the output of a GarpAlgorithm actor.
092         */
093        public TypedIOPort ruleSetFilename = new TypedIOPort(this,
094                        "ruleSetFilename", true, false);
095
096        /**
097         * This is the file name of the *.dxl file used to summarize the set of
098         * spatial data files with environmental data for each pixel.
099         */
100        public TypedIOPort layersetFilename = new TypedIOPort(this,
101                        "layersetFilename", true, false);
102
103        /**
104         * This is the file name to be used for the output ASCII grid file.
105         */
106        public TypedIOPort outputASCII = new TypedIOPort(this, "outputASCII", true,
107                        false);
108
109        /**
110         * This is the file name to be used for the output BMP raster file.
111         */
112        public TypedIOPort outputBMP = new TypedIOPort(this, "outputBMP", true,
113                        false);
114        // output ports
115        /**
116         * This is the file name of the output ASCII grid file. This port fires when
117         * the output predicted distribution grid has been created
118         */
119        public TypedIOPort outputASCIIFileName = new TypedIOPort(this,
120                        "outputASCIIFileName", false, true);
121        /**
122         * This is the file name of the output BMP raster file. This port fires when
123         * the output predicted distribution grid has been created. It contains the
124         * same information as the ASCII output port but in a more easily displayed
125         * raster format.
126         */
127        public TypedIOPort outputBMPFileName = new TypedIOPort(this,
128                        "outputBMPFileName", false, true);
129
130        /**
131         * GarpPrediction Actor
132         */
133        public GarpPrediction(CompositeEntity container, String name)
134                        throws NameDuplicationException, IllegalActionException {
135                super(container, name);
136                ruleSetFilename.setTypeEquals(BaseType.STRING);
137                layersetFilename.setTypeEquals(BaseType.STRING);
138                outputASCII.setTypeEquals(BaseType.STRING);
139                outputBMP.setTypeEquals(BaseType.STRING);
140                outputASCIIFileName.setTypeEquals(BaseType.STRING);
141                outputBMPFileName.setTypeEquals(BaseType.STRING);
142
143                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
144                                + "width=\"66\" height=\"42\" " + "style=\"fill:white\"/>\n"
145                                + "<text x=\"12\" y=\"16\" "
146                                + "style=\"font-size:14; fill:blue; font-family:SansSerif\">"
147                                + "GARP</text>\n" + "<text x=\"4\" y=\"34\" "
148                                + "style=\"font-size:12; fill:blue; font-family:SansSerif\">"
149                                + "Prediction</text>\n" + "</svg>\n");
150
151        }
152
153        /**
154   *
155   */
156        public void initialize() throws IllegalActionException {
157        }
158
159        /**
160   *
161   */
162        public boolean prefire() throws IllegalActionException {
163                return super.prefire();
164        }
165
166        /**
167   *
168   */
169        public void fire() throws IllegalActionException {
170                System.out.println("firing GarpPrediction");
171                super.fire();
172                String ruleSetFilenameStr = "";
173                String layersetFilenameStr = "";
174                String outputASCIIStr = "";
175                String outputBMPStr = "";
176
177                if (ruleSetFilename.numberOfSources() > 0) {
178                        if (!ruleSetFilename.hasToken(0))
179                                return;
180                        StringToken ruleSetFilenameToken = (StringToken) ruleSetFilename
181                                        .get(0);
182                        ruleSetFilenameStr = ruleSetFilenameToken.stringValue();
183                        ruleSetFilenameParameter.setExpression(ruleSetFilenameStr);
184                } else {
185                        ruleSetFilenameStr = ruleSetFilenameParameter.asFile().getPath();
186                }
187
188                if (layersetFilename.numberOfSources() > 0) {
189                        if (!layersetFilename.hasToken(0))
190                                return;
191                        StringToken layersetFilenameToken = (StringToken) layersetFilename
192                                        .get(0);
193                        layersetFilenameStr = layersetFilenameToken.stringValue();
194                        layersetFilenameParameter.setExpression(layersetFilenameStr);
195                } else {
196                        layersetFilenameStr = layersetFilenameParameter.asFile().getPath();
197                }
198
199                if (outputASCII.numberOfSources() > 0) {
200                        if (!outputASCII.hasToken(0))
201                                return;
202                        StringToken outputASCIIToken = (StringToken) outputASCII.get(0);
203                        outputASCIIStr = outputASCIIToken.stringValue();
204                        outputASCIIParameter.setExpression(outputASCIIStr);
205                } else {
206                        outputASCIIStr = outputASCIIParameter.asFile().getPath();
207                }
208
209                if (outputBMP.numberOfSources() > 0) {
210                        if (!outputBMP.hasToken(0))
211                                return;
212                        StringToken outputBMPToken = (StringToken) outputBMP.get(0);
213                        outputBMPStr = outputBMPToken.stringValue();
214                        outputBMPParameter.setExpression(outputBMPStr);
215                } else {
216                        outputBMPStr = outputBMPParameter.asFile().getPath();
217                }
218
219                System.out.println("Starting GarpPrediction JNI Code");
220                new GarpJniGlue().DoGarpPrediction(ruleSetFilenameStr,
221                                layersetFilenameStr, outputASCIIStr, outputBMPStr);
222
223                outputASCIIFileName.broadcast(new StringToken(outputASCIIStr));
224                outputBMPFileName.broadcast(new StringToken(outputBMPStr));
225
226                System.out.println("Finished with GarpPrediction JNI Code");
227        }
228}