############################################################################### ## ## ## 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/Adaptor_Switch.py,v 1.1 2003/10/28 00:16:54 gjanee Exp $ # SYNOPSIS # # Adaptor_Switch (bucketParadigm, fieldParadigm) # Adaptor_Switch (bucketParadigm, fieldParadigms) # # bucketParadigm # The underlying paradigm used to translate bucket-level # constraints. # # fieldParadigm # A single underlying paradigm used to translate all # field-level constraints. # # -or- # # fieldParadigms # A dictionary that maps one or more field URIs (e.g., # "http://purl.org/dc/elements/1.1/creator") to underlying # paradigms that translate corresponding field-level # searches. # # DESCRIPTION # # An adaptor that supports both bucket-level and field-level # searching by directing bucket-level constraints to one # underlying paradigm and field-level constraints to another. # Thus this paradigm is useful when different query translation # strategies are desired for different levels of searches. # # If a dictionary is specified for the field paradigms, a # field-level constraint matching one of the URIs listed in # 'fieldParadigms' is passed through to the corresponding # underlying paradigm; if the paradigm does not support # field-level searching, it should treat the constraint as being # bucket-level. A field-level constraint not matching any listed # URI results in a query of the form # # SELECT id FROM table # WHERE 1 = 0 # # being returned. # # Exceptions thrown: # # none # # AUTHOR # # Greg Janee # gjanee@alexandria.ucsb.edu # # HISTORY # # $Log: Adaptor_Switch.py,v $ # Revision 1.1 2003/10/28 00:16:54 gjanee # Initial revision # import types import edu.ucsb.adl.middleware M = edu.ucsb.adl.middleware import UniversalTranslator UT = UniversalTranslator class Adaptor_Switch (UT.FieldSearchableParadigm): def __init__ (self, bucketParadigm, arg): UT.assertType(bucketParadigm, UT.Paradigm) UT.assertPolytype(arg, [UT.FieldSearchableParadigm, types.DictionaryType]) self.bucketParadigm = bucketParadigm if isinstance(arg, UT.FieldSearchableParadigm): self.singleFieldParadigm = 1 self.fieldParadigm = arg else: UT.assertDictionaryElementType(arg, types.StringType, UT.Paradigm) UT.assertDictionaryNonempty(arg) self.singleFieldParadigm = 0 self.paradigms = arg def translateBucketAtomic (self, constraint, vocabularies): return self.bucketParadigm.translateBucketAtomic(constraint, vocabularies) def translateBucketBoolean (self, operator, constraints, vocabularies): return self.bucketParadigm.translateBucketBoolean(operator, constraints, vocabularies) def translateFieldAtomic (self, constraint, vocabularies): UT.assertType(constraint, M.Query.SimpleConstraint) assert constraint.getField() != None, "no field constraint" if self.singleFieldParadigm: return self.fieldParadigm.translateFieldAtomic(constraint, vocabularies) else: if constraint.getField().uri in self.paradigms.keys(): paradigm = self.paradigms[constraint.getField().uri] if isinstance(paradigm, UT.FieldSearchableParadigm): return paradigm.translateFieldAtomic(constraint, vocabularies) else: return paradigm.translateBucketAtomic(constraint, vocabularies) else: paradigm = self.paradigms.values()[0] return UT.falsify(paradigm.translateBucketAtomic(constraint, vocabularies)) def translateFieldBoolean (self, operator, constraints, vocabularies): UT.assertBooleanOperator(operator) UT.assertType(constraints, types.ListType) UT.assertListElementType(constraints, M.Query.SimpleConstraint) UT.assertBooleanOperatorOperandConsistency(operator, constraints) UT.assertListElementCommonValue(constraints, lambda c: c.getBucket()) UT.assertListElementPredicateAll(constraints, lambda c: c.getField() != None) UT.assertListElementCommonValue(constraints, lambda c: c.getField().uri) if self.singleFieldParadigm: return self.fieldParadigm.translateFieldBoolean(operator, constraints, vocabularies) else: if constraints[0].getField().uri in self.paradigms.keys(): paradigm = self.paradigms[constraints[0].getField().uri] if isinstance(paradigm, UT.FieldSearchableParadigm): return paradigm.translateFieldBoolean(operator, constraints, vocabularies) else: return paradigm.translateBucketBoolean(operator, constraints, vocabularies) else: return None