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 * @actor.name GarpAlgorithm
043 * @actor.lsid urn:lsid:ecoinformatics.org:kepler.44.1
044 * @actor.ontology <owl:Ontology rdf:about="http://seek.ecoinformatics.org/annotations"/>
045 * @actor.ontology http://seek.ecoinformatics.org/ontology#
046 * @actor.annotation <rdf:type rdf:resource="http://seek.ecoinformatics.org/ontology#ArithmeticMathOperationActor"/>
047 * @actor.annotation <rdf:type rdf:resource="http://seek.ecoinformatics.org/ontology#ExternalExecutionEnvironmentActor"/>
048 *
049 * @port.name cellSetFileName
050 * @port.type STRING
051 * @port.name ruleSetFilename
052 * @port.type STRING
053 * @port.name ruleSetFilenameOutput
054 * @port.type STRING
055 *
056 * @depends.linux libgarp.so
057 * @depends.windows garp.dll
058 * @depends.windows libexpat.dll
059 * @depends.osx libgarp.jnilib
060 *
061 *
062 */
063
064/**
065 * <p>
066 * GARP is a computer program for predicting species locations based on various
067 * spatial data sets of environment variables and known species locations. GARP
068 * is an acronym for Genetic Algorithm for Rule Set Production. GARP was
069 * originally ceated by David Stockwell. The version in Kepler is based on
070 * 'Desktop GARP', http://www.lifemapper.org/desktopgarp/. The GarpAlgorithm
071 * actor takes information from the GarpPresampleLayers actor (A randomly
072 * generated set of spatial locations and associated environment data.) and
073 * calculates a 'RuleSet' using a genetic algorithm. The RuleSet can then be
074 * used to determine whether the environmental data for some location is similar
075 * enough to the environment at known location positions to predict
076 * presence/absence.
077 * </p>
078 * <p>
079 * This is a JNI-based actor. It requires the following: linux: libgarp.so
080 * windows: garp.dll, libexpat.dll MacOSX - currently not available for the Mac
081 * (3/16/2006)
082 * </p>
083 * 
084 * @author Chad Berkeley, Dan Higgins, NCEAS, UC Santa Barbara
085 */
086public class GarpAlgorithm extends TypedAtomicActor {
087        // FileParameters
088        /**
089         * This is the name of the file containing the cellSet information. This is
090         * usually the output of the GarpPresampleLayers actor. This filename can
091         * also be specified on an input port.
092         */
093        public FileParameter cellSetFileNameParameter = new FileParameter(this,
094                        "cellSetFileNameParameter");
095        /**
096         * This is the name to be given to the file containing the RuleSet
097         * information. This filename can also be specified on an input port.
098         */
099        public FileParameter ruleSetFilenameParameter = new FileParameter(this,
100                        "ruleSetFilenameParameter");
101
102        // input ports
103        /**
104         * This is the name of the file containing the cellSet information. This is
105         * usually the output of the GarpPresampleLayers actor.
106         */
107        public TypedIOPort cellSetFileName = new TypedIOPort(this,
108                        "cellSetFileName", true, false);
109        /**
110         * This is the name to be given to the file containing the RuleSet
111         * information.
112         */
113        public TypedIOPort ruleSetFilename = new TypedIOPort(this,
114                        "ruleSetFilename", true, false);
115        // output ports
116        /**
117         * This is the name of the file containing the RuleSet information. The port
118         * is only fired once the ruleset has been created. It is usually used as a
119         * trigger.
120         */
121        public TypedIOPort ruleSetFilenameOutput = new TypedIOPort(this,
122                        "ruleSetFilenameOutput", false, true);
123
124        /**
125         * GarpAlgorithm Actor
126         */
127        public GarpAlgorithm(CompositeEntity container, String name)
128                        throws NameDuplicationException, IllegalActionException {
129                super(container, name);
130                cellSetFileName.setTypeEquals(BaseType.STRING);
131                ruleSetFilename.setTypeEquals(BaseType.STRING);
132                ruleSetFilenameOutput.setTypeEquals(BaseType.STRING);
133
134                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
135                                + "width=\"66\" height=\"42\" " + "style=\"fill:white\"/>\n"
136                                + "<text x=\"12\" y=\"16\" "
137                                + "style=\"font-size:14; fill:blue; font-family:SansSerif\">"
138                                + "GARP</text>\n" + "<text x=\"5\" y=\"34\" "
139                                + "style=\"font-size:12; fill:blue; font-family:SansSerif\">"
140                                + "Algorithm</text>\n" + "</svg>\n");
141
142        }
143
144        /**
145   *
146   */
147        public void initialize() throws IllegalActionException {
148        }
149
150        /**
151   *
152   */
153        public boolean prefire() throws IllegalActionException {
154                return super.prefire();
155        }
156
157        /**
158   *
159   */
160        public void fire() throws IllegalActionException {
161                System.out.println("firing GarpAlgorithm");
162                super.fire();
163                String cellSetFileNameStr = "";
164                String ruleSetFilenameStr = "";
165
166                if (cellSetFileName.numberOfSources() > 0) {
167                        // get the cellset filename from the port
168                        if (!cellSetFileName.hasToken(0))
169                                return;
170                        StringToken cellsetfnToken = (StringToken) cellSetFileName.get(0);
171                        cellSetFileNameStr = cellsetfnToken.stringValue();
172                        cellSetFileNameParameter.setExpression(cellSetFileNameStr);
173                } else {
174                        cellSetFileNameStr = cellSetFileNameParameter.asFile().getPath();
175                }
176
177                if (ruleSetFilename.numberOfSources() > 0) {
178                        // get the ruleset filename from the port
179                        if (!ruleSetFilename.hasToken(0))
180                                return;
181                        StringToken rulesetfnToken = (StringToken) ruleSetFilename.get(0);
182                        ruleSetFilenameStr = rulesetfnToken.stringValue();
183                        ruleSetFilenameParameter.setExpression(ruleSetFilenameStr);
184                } else {
185                        ruleSetFilenameStr = ruleSetFilenameParameter.asFile().getPath();
186                }
187                // make the jni call to the c++ code
188                System.out.println("Starting GarpAlgorithm JNI code");
189                new GarpJniGlue().RunGarpAlgorithm(ruleSetFilenameStr,
190                                cellSetFileNameStr);
191
192                ruleSetFilenameOutput.broadcast(new StringToken(ruleSetFilenameStr));
193
194                System.out.println("Finished with GarpAlgorithm JNI code");
195        }
196}