############################################################################### ## ## ## ALEXANDRIA DIGITAL LIBRARY ## ## University of California at Santa Barbara ## ## ## ## ------------------------------------------------------------------------- ## ## ## ## Copyright (c) 2003 by the Regents of the University of California ## ## All rights reserved ## ## ## ## Redistribution and use in source and binary forms, with or without ## ## modification, are permitted provided that the following conditions are ## ## met: ## ## ## ## 1. Redistributions of source code must retain the above copyright ## ## notice, this list of conditions, and the following disclaimer. ## ## ## ## 2. Redistributions in binary form must reproduce the above copyright ## ## notice, this list of conditions, and the following disclaimer in ## ## the documentation and/or other materials provided with the ## ## distribution. ## ## ## ## 3. All advertising materials mentioning features or use of this ## ## software must display the following acknowledgement: This product ## ## includes software developed by the Alexandria Digital Library, ## ## University of California at Santa Barbara, and its contributors. ## ## ## ## 4. Neither the name of the University nor the names of its ## ## contributors may be used to endorse or promote products derived ## ## from this software without specific prior written permission. ## ## ## ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY ## ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ## ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE ## ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ## ## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ## ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ## ## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ## ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, ## ## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ## ## ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ## ## POSSIBILITY OF SUCH DAMAGE. ## ## ## ############################################################################### # $Header: /export/home/gjanee/bucket99/paradigms/RCS/Temporal_IntegerYear.py,v 1.2 2003/10/24 03:59:07 gjanee Exp $ # SYNOPSIS # # Temporal_IntegerYear (table, idColumn, yearColumn, cardinality) # # table # A table to query, e.g., "holding". # # idColumn # The table's identifier column (i.e., the column to be # selected), e.g., "holding_id". # # yearColumn # The table's year column, e.g., "year". # # cardinality # A Cardinality object representing the cardinality of # 'table' with respect to 'yearColumn'. # # DESCRIPTION # # Translates a temporal constraint to a pair of SQL numeric # comparisons. This paradigm assumes that a collection item's # temporal footprint is described by a single integer year. # Returned queries generally have the form: # # SELECT idColumn FROM table # WHERE yearColumn BETWEEN C.beginYear AND C.endYear # # where C.beginYear and C.endYear are the year components of the # limits of the constraint range, although other query forms are # possible. # # The semantics of the "contains" and "is-contained-in" operators # will generally be correct only if the cardinality is "1" or # "1?". The "0+" and "1+" cardinalities should be used with this # paradigm only if collection items truly have multiple, # equivalent temporal footprints. In particular, this paradigm # does not support temporal footprints that are unions of # multiple, arbitrary years. (In the latter case, the "contains" # operator *is* supported if, for each collection item, the item's # years are separated by at least one year, i.e., they never form # a contiguous multi-year range.) # # (Known bug: support for the "is-contained-in" operator *should # be* added by wrapping this paradigm in an # Adaptor_TemporalIsContainedInRewriter paradigm. But this # doesn't work because of the way this paradigm rounds dates to # years by truncation. Given a range of dates [B, E], the # Adaptor_TemporalIsContainedInRewriter paradigm defines exclusion # regions by subtracting (adding) a single day from B (E), but # usually year(B) = year(B - 1 day) and so year(B) ends up being # excluded.) # # Exceptions thrown: # # none # # AUTHOR # # Greg Janee # gjanee@alexandria.ucsb.edu # # HISTORY # # $Log: Temporal_IntegerYear.py,v $ # Revision 1.2 2003/10/24 03:59:07 gjanee # Minor documentation addition. # # Revision 1.1 2003/01/28 18:23:58 gjanee # Initial revision # import types import edu.ucsb.adl.middleware M = edu.ucsb.adl.middleware import UniversalTranslator UT = UniversalTranslator class Temporal_IntegerYear (UT.Paradigm): def __init__ (self, table, idColumn, yearColumn, cardinality): UT.assertType(table, types.StringType) UT.assertType(idColumn, types.StringType) UT.assertType(yearColumn, types.StringType) UT.assertType(cardinality, UT.Cardinality) self.table = table self.idColumn = idColumn self.yearColumn = yearColumn self.cardinality = cardinality def translateBucketAtomic (self, constraint, vocabularies): UT.assertType(constraint, M.Query.TemporalConstraint) assert constraint.getOperator() in UT.standardTemporalOperators,\ "unsupported operator: " + constraint.getOperator() table = UT.TableRef(self.table) begin = constraint.getBegin()/10000 end = constraint.getEnd()/10000 if begin == end: clause = UT.Expression([table, "." + self.yearColumn + " = " +\ str(begin)]) else: if constraint.getOperator() == "contains": return UT.constantFalseQuery(self.table, self.idColumn, self.cardinality) if begin == 0: if end == 9999: return UT.constantTrueQuery(self.table, self.idColumn, self.yearColumn, self.cardinality) clause = UT.Expression([table, "." + self.yearColumn +\ " <= " + str(end)]) elif end == 9999: clause = UT.Expression([table, "." + self.yearColumn +\ " >= " + str(begin)]) else: clause = UT.Expression([table, "." + self.yearColumn +\ " BETWEEN " + str(begin) + " AND " + str(end)]) return UT.Select( [UT.MainFrom(table, self.idColumn, self.cardinality)], clause)