예제 #1
0
 def handleModel (self, model, args = None):
     """Testerizer.handleModel
     """
     global __DEBUG__, __VERBOSE__
     if args is not None:
         if "-debug" in args:
             __DEBUG__ = True
         if "-verbose" in args:
             __VERBOSE__ = True
     despecializer = Despecializer.getModelHandler()()
     factory = despecializer.factory
     despecializedModel = despecializer.handleModel(model)
     productionMap = factory.externalizeProdMap(despecializedModel)
     if __DEBUG__:
         pprint.pprint(productionMap)
     startTok = GrammarUtils.findStartSymbol(model)
     print "Starting token:", startTok
     testerGrammarModel = TesterizerGrammar(productionMap)
     def myCostFunction (symbolList):
         cost = 0
         for symbol in symbolList:
             if productionMap.has_key(symbol):
                 cost += 11
             else:
                 cost += 10
         return cost
     nontermToTermsMap = testerGrammarModel.buildTerminalMapping(
         myCostFunction)
     if __DEBUG__ or __VERBOSE__:
         pprint.pprint(nontermToTermsMap)
     productionUseMap = {}
     productionAppMap = {}
     symbolTupleList = testerGrammarModel.spanProductions(
         startTok, productionUseMap = productionUseMap,
         productionAppMap = productionAppMap)
     index = 0
     if __DEBUG__ or __VERBOSE__:
         print "_" * 70
         for symbolTuple in symbolTupleList:
             print "%d. %s" % (index, symbolTuple)
             pprint.pprint(productionAppMap[symbolTuple])
             print "_" * 60
             index += 1
     if __DEBUG__:
         pprint.pprint(productionUseMap)
     if __VERBOSE__:
         print "_" * 70
     def nontermToTermMapFn (symbol):
         if nontermToTermsMap.has_key(symbol):
             return string.join(nontermToTermsMap[symbol])
         return symbol
     index = 0
     for symbolTuple in symbolTupleList:
         testString = ""
         testSymbolList = map(nontermToTermMapFn, symbolTuple)
         while (len(testSymbolList) > 0) and ('' == testSymbolList[0]):
             del testSymbolList[0]
         if len(testSymbolList) > 0:
             testString = "%s" % testSymbolList[0]
             for symbol in testSymbolList[1:]:
                 if len(symbol) > 0:
                     testString += " %s" % symbol
         if __VERBOSE__:
             print "%d." % (index,),
         print testString
         index += 1
     return None