src/core/net/sf/basedb/util/jep/InReporterListFunction.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, Martin Svensson
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.Restriction;
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.util.Stack;
2399 20 Jun 06 nicklas 34
2399 20 Jun 06 nicklas 35 import org.nfunk.jep.Node;
2399 20 Jun 06 nicklas 36 import org.nfunk.jep.ParseException;
2399 20 Jun 06 nicklas 37
2399 20 Jun 06 nicklas 38 /**
2399 20 Jun 06 nicklas 39   A JEP function class that adds a <code>inList(reporterListId)</code> function to a 
2399 20 Jun 06 nicklas 40   JEP expression parser. The function will check if a reporter exists in the
2399 20 Jun 06 nicklas 41   reporter list with the ID given as an argument to this function.
2399 20 Jun 06 nicklas 42   <p>
2399 20 Jun 06 nicklas 43   To be able to use this function it must be registered with the JEP
2399 20 Jun 06 nicklas 44   parser and, before the expression is evaluated, a {@link SqlResult} object
2399 20 Jun 06 nicklas 45   must be set.
2399 20 Jun 06 nicklas 46   
2399 20 Jun 06 nicklas 47   @author Nicklas
2399 20 Jun 06 nicklas 48   @version 2.0
2399 20 Jun 06 nicklas 49   @base.modified $Date$
2399 20 Jun 06 nicklas 50   @see Jep
5319 20 Apr 10 nicklas 51   @see BioAssaySetUtil#createJepRestriction(DbControl, String, DynamicQuery)
2399 20 Jun 06 nicklas 52 */
2399 20 Jun 06 nicklas 53 public class InReporterListFunction
2399 20 Jun 06 nicklas 54   implements JepRestrictionFunction
2399 20 Jun 06 nicklas 55 {
2399 20 Jun 06 nicklas 56
2399 20 Jun 06 nicklas 57   private DbControl dc;
2399 20 Jun 06 nicklas 58   private int numParameters;
2399 20 Jun 06 nicklas 59
2399 20 Jun 06 nicklas 60   /**
2399 20 Jun 06 nicklas 61     Create a new instance of this function. The new instance cannot be used
2399 20 Jun 06 nicklas 62     to dynamically evaluate expressions. It should only be used for converting
2399 20 Jun 06 nicklas 63     JEP formulas to {@link Restriction}:s.
4034 05 Dec 07 martin 64      @param dc DbControl used to access database. 
2399 20 Jun 06 nicklas 65     @see Jep#formulaToRestriction(String, JepFunction[])  
2399 20 Jun 06 nicklas 66   */
2399 20 Jun 06 nicklas 67   public InReporterListFunction(DbControl dc)
2399 20 Jun 06 nicklas 68   {
2399 20 Jun 06 nicklas 69     this.dc = dc;
2399 20 Jun 06 nicklas 70   }
2399 20 Jun 06 nicklas 71   
2399 20 Jun 06 nicklas 72   /*
2399 20 Jun 06 nicklas 73     From the JepFunction interface
2399 20 Jun 06 nicklas 74     -------------------------------------------
2399 20 Jun 06 nicklas 75   */
2399 20 Jun 06 nicklas 76   /**
2399 20 Jun 06 nicklas 77     @return The string "inList"
2399 20 Jun 06 nicklas 78   */
6127 14 Sep 12 nicklas 79   @Override
2399 20 Jun 06 nicklas 80   public String getFunctionName()
2399 20 Jun 06 nicklas 81   {
2399 20 Jun 06 nicklas 82     return "inList";
2399 20 Jun 06 nicklas 83   }
2399 20 Jun 06 nicklas 84   // -------------------------------------------
2399 20 Jun 06 nicklas 85   /*
2399 20 Jun 06 nicklas 86     From the JepRestrictionFunction interface
2399 20 Jun 06 nicklas 87     -------------------------------------------
2399 20 Jun 06 nicklas 88   */
2399 20 Jun 06 nicklas 89   /**
2399 20 Jun 06 nicklas 90     Use the {@link Dynamic#isPartOf(ReporterList)} method to create a
2399 20 Jun 06 nicklas 91     restriction checking that a reporter is found in the reporter list.
2399 20 Jun 06 nicklas 92   */
6127 14 Sep 12 nicklas 93   @Override
2399 20 Jun 06 nicklas 94   public Restriction toRestriction(Node node)
2399 20 Jun 06 nicklas 95   {
2399 20 Jun 06 nicklas 96     int numChildren = node.jjtGetNumChildren();
2399 20 Jun 06 nicklas 97     if (numChildren != 1)
2399 20 Jun 06 nicklas 98     {
2399 20 Jun 06 nicklas 99       throw new BaseException("Invalid number of expressions for 'inList' function: " + numChildren);
2399 20 Jun 06 nicklas 100     }
2399 20 Jun 06 nicklas 101     int reporterListId = Jep.nodeToInt(node.jjtGetChild(0));
2399 20 Jun 06 nicklas 102     ReporterList list = ReporterList.getById(dc, reporterListId);
2399 20 Jun 06 nicklas 103     return Dynamic.isPartOf(list);
2399 20 Jun 06 nicklas 104   }
2399 20 Jun 06 nicklas 105   // -------------------------------------------
2399 20 Jun 06 nicklas 106   /*
2399 20 Jun 06 nicklas 107     From the PostfixMathCommandI interface
2399 20 Jun 06 nicklas 108     -------------------------------------------
2399 20 Jun 06 nicklas 109   */
2399 20 Jun 06 nicklas 110   /**
2399 20 Jun 06 nicklas 111     @return Always 1
2399 20 Jun 06 nicklas 112   */
6127 14 Sep 12 nicklas 113   @Override
2399 20 Jun 06 nicklas 114   public int getNumberOfParameters()
2399 20 Jun 06 nicklas 115   {
2399 20 Jun 06 nicklas 116     return 1;
2399 20 Jun 06 nicklas 117   }
6127 14 Sep 12 nicklas 118   @Override
2399 20 Jun 06 nicklas 119   public void setCurNumberOfParameters(int n)
2399 20 Jun 06 nicklas 120   {
2399 20 Jun 06 nicklas 121     this.numParameters = n;
2399 20 Jun 06 nicklas 122   }
6127 14 Sep 12 nicklas 123   @Override
2474 31 Jul 06 nicklas 124   public boolean checkNumberOfParameters(int n)
2474 31 Jul 06 nicklas 125   {
2474 31 Jul 06 nicklas 126     return n == 1;
2474 31 Jul 06 nicklas 127   }
6127 14 Sep 12 nicklas 128   @Override
6875 20 Apr 15 nicklas 129   @SuppressWarnings({"rawtypes"})
2399 20 Jun 06 nicklas 130   public void run(Stack stack)
2399 20 Jun 06 nicklas 131     throws ParseException
2399 20 Jun 06 nicklas 132   {
2399 20 Jun 06 nicklas 133     if (stack == null || stack.empty()) 
2399 20 Jun 06 nicklas 134     {
2399 20 Jun 06 nicklas 135       throw new ParseException("Stack is empty");
2399 20 Jun 06 nicklas 136     }
2399 20 Jun 06 nicklas 137     Object reporterListId = stack.pop();
5384 13 Aug 10 nicklas 138     throw new ParseException("'inList(" + reporterListId + ")' function doesn't support evaluation");
2399 20 Jun 06 nicklas 139   }
2399 20 Jun 06 nicklas 140   // -------------------------------------------
2399 20 Jun 06 nicklas 141
2399 20 Jun 06 nicklas 142 }