src/core/net/sf/basedb/util/jep/ScoreFunction.java

Code
Comments
Other
Rev Date Author Line
2399 20 Jun 06 nicklas 1 /*
2399 20 Jun 06 nicklas 2   $Id$
2399 20 Jun 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
2399 20 Jun 06 nicklas 5
2399 20 Jun 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2399 20 Jun 06 nicklas 7   Available at http://base.thep.lu.se/
2399 20 Jun 06 nicklas 8
2399 20 Jun 06 nicklas 9   BASE is free software; you can redistribute it and/or
2399 20 Jun 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
2399 20 Jun 06 nicklas 12   of the License, or (at your option) any later version.
2399 20 Jun 06 nicklas 13
2399 20 Jun 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2399 20 Jun 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2399 20 Jun 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2399 20 Jun 06 nicklas 17   GNU General Public License for more details.
2399 20 Jun 06 nicklas 18
2399 20 Jun 06 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2399 20 Jun 06 nicklas 21 */
2399 20 Jun 06 nicklas 22 package net.sf.basedb.util.jep;
2399 20 Jun 06 nicklas 23
2399 20 Jun 06 nicklas 24 import net.sf.basedb.core.BaseException;
2399 20 Jun 06 nicklas 25 import net.sf.basedb.core.DbControl;
5319 20 Apr 10 nicklas 26 import net.sf.basedb.core.DynamicQuery;
2399 20 Jun 06 nicklas 27 import net.sf.basedb.core.ReporterList;
2399 20 Jun 06 nicklas 28 import net.sf.basedb.core.query.Dynamic;
2399 20 Jun 06 nicklas 29 import net.sf.basedb.core.query.Expression;
2399 20 Jun 06 nicklas 30 import net.sf.basedb.core.query.SqlResult;
2981 30 Nov 06 nicklas 31 import net.sf.basedb.util.BioAssaySetUtil;
2399 20 Jun 06 nicklas 32
2399 20 Jun 06 nicklas 33 import java.sql.SQLException;
2399 20 Jun 06 nicklas 34 import java.util.Stack;
2399 20 Jun 06 nicklas 35
2399 20 Jun 06 nicklas 36 import org.nfunk.jep.Node;
2399 20 Jun 06 nicklas 37 import org.nfunk.jep.ParseException;
2399 20 Jun 06 nicklas 38
2399 20 Jun 06 nicklas 39 /**
2399 20 Jun 06 nicklas 40   A JEP function class that adds a <code>score(reporterListId)</code> function to a 
2399 20 Jun 06 nicklas 41   JEP expression parser. The function will look up a reporters score in the
2399 20 Jun 06 nicklas 42   reporter list with the ID given as an argument to this function.
2399 20 Jun 06 nicklas 43   <p>
2399 20 Jun 06 nicklas 44   To be able to use this function it must be registered with the JEP
2399 20 Jun 06 nicklas 45   parser and, before the expression is evaluated, a {@link SqlResult} object
2399 20 Jun 06 nicklas 46   must be set.
2399 20 Jun 06 nicklas 47   
2399 20 Jun 06 nicklas 48   @author Nicklas
2399 20 Jun 06 nicklas 49   @version 2.0
2399 20 Jun 06 nicklas 50   @base.modified $Date$
2399 20 Jun 06 nicklas 51   @see Jep
5319 20 Apr 10 nicklas 52   @see BioAssaySetUtil#createJepExpression(DbControl, String, DynamicQuery)
2399 20 Jun 06 nicklas 53 */
2399 20 Jun 06 nicklas 54 public class ScoreFunction
2399 20 Jun 06 nicklas 55   implements JepExpressionFunction
2399 20 Jun 06 nicklas 56 {
2399 20 Jun 06 nicklas 57
2399 20 Jun 06 nicklas 58   private DbControl dc;
2399 20 Jun 06 nicklas 59   private int scoreIndex;
2399 20 Jun 06 nicklas 60   private SqlResult result;
2399 20 Jun 06 nicklas 61   private int numParameters;
2399 20 Jun 06 nicklas 62
2399 20 Jun 06 nicklas 63   /**
2399 20 Jun 06 nicklas 64     Create a new instance of this function. The new instance cannot be used
2399 20 Jun 06 nicklas 65     to dynamically evaluate expressions. It should only be used for converting
2399 20 Jun 06 nicklas 66     JEP formulas to {@link Expression}:s.
4034 05 Dec 07 martin 67      @param dc DbControl to use when this ScoreFunction accessing the database. 
2399 20 Jun 06 nicklas 68     @see Jep#formulaToExpression(String, JepFunction[])  
2399 20 Jun 06 nicklas 69   */
2399 20 Jun 06 nicklas 70   public ScoreFunction(DbControl dc)
2399 20 Jun 06 nicklas 71   {
2399 20 Jun 06 nicklas 72     this.dc = dc;
2399 20 Jun 06 nicklas 73   }
2399 20 Jun 06 nicklas 74   
2399 20 Jun 06 nicklas 75   /**
2399 20 Jun 06 nicklas 76     Create a new instance of this function which can be used
2399 20 Jun 06 nicklas 77     to dynamically evaluate expressions.
4034 05 Dec 07 martin 78      @param dc DbControl to use when this ScoreFunction accessing the database.
2399 20 Jun 06 nicklas 79     @param scoreIndex The column index in the SqlResult that holds the score value
2399 20 Jun 06 nicklas 80     @see #setSqlResult(SqlResult)
2399 20 Jun 06 nicklas 81   */
2399 20 Jun 06 nicklas 82   public ScoreFunction(DbControl dc, int scoreIndex)
2399 20 Jun 06 nicklas 83   {
2399 20 Jun 06 nicklas 84     this.dc = dc;
2399 20 Jun 06 nicklas 85     this.scoreIndex = scoreIndex;
2399 20 Jun 06 nicklas 86   }
2399 20 Jun 06 nicklas 87   
2399 20 Jun 06 nicklas 88   /*
2399 20 Jun 06 nicklas 89     From the JepFunction interface
2399 20 Jun 06 nicklas 90     -------------------------------------------
2399 20 Jun 06 nicklas 91   */
2399 20 Jun 06 nicklas 92   /**
2399 20 Jun 06 nicklas 93     @return The string "score"
2399 20 Jun 06 nicklas 94   */
6127 14 Sep 12 nicklas 95   @Override
2399 20 Jun 06 nicklas 96   public String getFunctionName()
2399 20 Jun 06 nicklas 97   {
2399 20 Jun 06 nicklas 98     return "score";
2399 20 Jun 06 nicklas 99   }
2399 20 Jun 06 nicklas 100   // -------------------------------------------
2399 20 Jun 06 nicklas 101   /*
2399 20 Jun 06 nicklas 102     From the JepExpressionFunction interface
2399 20 Jun 06 nicklas 103     -------------------------------------------
2399 20 Jun 06 nicklas 104   */
2399 20 Jun 06 nicklas 105   /**
2399 20 Jun 06 nicklas 106     Use the {@link Dynamic#score(ReporterList)} method to create an
2399 20 Jun 06 nicklas 107     expression referencing the score of a reporter in the reporter list.
2399 20 Jun 06 nicklas 108   */
6127 14 Sep 12 nicklas 109   @Override
2399 20 Jun 06 nicklas 110   public Expression toExpression(Node node)
2399 20 Jun 06 nicklas 111   {
2399 20 Jun 06 nicklas 112     int numChildren = node.jjtGetNumChildren();
2399 20 Jun 06 nicklas 113     if (numChildren != 1)
2399 20 Jun 06 nicklas 114     {
2399 20 Jun 06 nicklas 115       throw new BaseException("Invalid number of expressions for 'score' function: " + numChildren);
2399 20 Jun 06 nicklas 116     }
2399 20 Jun 06 nicklas 117     int reporterListId = Jep.nodeToInt(node.jjtGetChild(0));
2399 20 Jun 06 nicklas 118     ReporterList list = ReporterList.getById(dc, reporterListId);
2399 20 Jun 06 nicklas 119     return Dynamic.score(list);
2399 20 Jun 06 nicklas 120   }
2399 20 Jun 06 nicklas 121   // -------------------------------------------
2399 20 Jun 06 nicklas 122   /*
2399 20 Jun 06 nicklas 123     From the PostfixMathCommandI interface
2399 20 Jun 06 nicklas 124     -------------------------------------------
2399 20 Jun 06 nicklas 125   */
2399 20 Jun 06 nicklas 126   /**
2399 20 Jun 06 nicklas 127     @return Always 1
2399 20 Jun 06 nicklas 128   */
6127 14 Sep 12 nicklas 129   @Override
2399 20 Jun 06 nicklas 130   public int getNumberOfParameters()
2399 20 Jun 06 nicklas 131   {
2399 20 Jun 06 nicklas 132     return 1;
2399 20 Jun 06 nicklas 133   }
6127 14 Sep 12 nicklas 134   @Override
2399 20 Jun 06 nicklas 135   public void setCurNumberOfParameters(int n)
2399 20 Jun 06 nicklas 136   {
2399 20 Jun 06 nicklas 137     this.numParameters = n;
2399 20 Jun 06 nicklas 138   }
6127 14 Sep 12 nicklas 139   @Override
2474 31 Jul 06 nicklas 140   public boolean checkNumberOfParameters(int n)
2474 31 Jul 06 nicklas 141   {
2474 31 Jul 06 nicklas 142     return n == 1;
2474 31 Jul 06 nicklas 143   }
6127 14 Sep 12 nicklas 144   @Override
6875 20 Apr 15 nicklas 145   @SuppressWarnings({"unchecked", "rawtypes"})
2399 20 Jun 06 nicklas 146   public void run(Stack stack)
2399 20 Jun 06 nicklas 147     throws ParseException
2399 20 Jun 06 nicklas 148   {
2399 20 Jun 06 nicklas 149     if (stack == null || stack.empty()) 
2399 20 Jun 06 nicklas 150     {
2399 20 Jun 06 nicklas 151       throw new ParseException("Stack is empty");
2399 20 Jun 06 nicklas 152     }
2399 20 Jun 06 nicklas 153     Object reporterListId = stack.pop();
2399 20 Jun 06 nicklas 154     if (reporterListId instanceof Number)
2399 20 Jun 06 nicklas 155     {
2399 20 Jun 06 nicklas 156       stack.push(score());
2399 20 Jun 06 nicklas 157     }
2399 20 Jun 06 nicklas 158     else
2399 20 Jun 06 nicklas 159     {
2399 20 Jun 06 nicklas 160       throw new ParseException("Invalid parameter type: " + reporterListId + "; expected integer");
2399 20 Jun 06 nicklas 161     }
2399 20 Jun 06 nicklas 162   }
2399 20 Jun 06 nicklas 163   // -------------------------------------------
2399 20 Jun 06 nicklas 164
2399 20 Jun 06 nicklas 165   /**
2399 20 Jun 06 nicklas 166     Set a new {@link SqlResult} object that will be used the next time the
2399 20 Jun 06 nicklas 167     JEP expression is evaluated.
2399 20 Jun 06 nicklas 168     @param result The result object
2399 20 Jun 06 nicklas 169   */
2399 20 Jun 06 nicklas 170   public void setSqlResult(SqlResult result)
2399 20 Jun 06 nicklas 171   {
2399 20 Jun 06 nicklas 172     this.result = result;
2399 20 Jun 06 nicklas 173   }
2399 20 Jun 06 nicklas 174   /**
2399 20 Jun 06 nicklas 175     Get the score value from the current sql result.
4023 30 Nov 07 martin 176      @return a {@link java.lang.Object} 
4023 30 Nov 07 martin 177      @throws ParseException If no result has been specified or if getting the score result fails.
2399 20 Jun 06 nicklas 178   */
2399 20 Jun 06 nicklas 179   public Object score()
2399 20 Jun 06 nicklas 180     throws ParseException
2399 20 Jun 06 nicklas 181   {
2399 20 Jun 06 nicklas 182     if (result == null)
2399 20 Jun 06 nicklas 183     {
2399 20 Jun 06 nicklas 184       throw new ParseException("No result object has been specified for function score()");
2399 20 Jun 06 nicklas 185     }
2399 20 Jun 06 nicklas 186     try
2399 20 Jun 06 nicklas 187     {
2399 20 Jun 06 nicklas 188       return result.getObject(scoreIndex);
2399 20 Jun 06 nicklas 189     }
2399 20 Jun 06 nicklas 190     catch (SQLException ex)
2399 20 Jun 06 nicklas 191     {
2399 20 Jun 06 nicklas 192       throw new ParseException(ex.getMessage());
2399 20 Jun 06 nicklas 193     }
2399 20 Jun 06 nicklas 194   }
2399 20 Jun 06 nicklas 195 }