def test_dijkstra_from_b(self): mRouter = Router(1, self.AdjMatrix, self.DemandMatrix) start = time.time() mRouter.dijkstra() end = time.time() print(f"calculated Dijkstra in {end - start}") self.assertDictEqual(mRouter.shortest_paths, mRouter.shortest_paths)
def test_dijkstra2(self): CorrectOutput = { 0: ([[]], 0), 1: ([[1]], 4.0), 2: ([[1, 2]], 7.0), 3: ([[1, 3], [1, 2, 4, 3]], 10.0), 4: ([[1, 2, 4]], 8.0), 5: ([[1, 5]], 6.0) } mRouter = Router(0, self.AdjMatrix2, self.DemandMatrix) mRouter.dijkstra() self.assertDictEqual(mRouter.shortest_paths, CorrectOutput)
def test_dijkstra_unequal_bidirectional_weight(self): mRouter = Router(self.RouterID, self.AdjMatrixBi, self.DemandMatrix) start = time.time() mRouter.dijkstra() end = time.time() print(f"calculated Dijkstra in {end - start}") # print(mRouter.shortest_paths) self.assertDictEqual(mRouter.shortest_paths, { 0: ([[]], 0), 1: ([[1]], 1.0), 2: ([[2]], 2.0), 3: ([[2, 3]], 3.0) })
def test_2equal_links(self): AdjMatrix = np.array([[math.inf, 1, 1, math.inf], [1, math.inf, math.inf, 1], [1, math.inf, math.inf, 1], [math.inf, 1, 1, math.inf]]) mRouter = Router(0, AdjMatrix, self.DemandMatrix) mRouter.dijkstra() self.assertDictEqual( mRouter.shortest_paths, { 0: ([[]], 0), 1: ([[1]], 1.0), 2: ([[2]], 1.0), 3: ([[1, 3], [2, 3]], 2.0) })
def test_alternative(self): mRouter = Router(2, self.AdjMatrix2, self.DemandMatrix) alter = mRouter.restrainedDijkstra([[2, 1]]) mRouter.dijkstra() self.assertDictEqual( alter, { 0: ([[4, 5, 1, 0]], 12.0), 2: ([[]], 0), 1: ([[4, 5, 1]], 8.0), 3: ([[4, 3]], 3.0), 4: ([[4]], 1.0), 5: ([[4, 5]], 6.0) }) self.assertDictEqual(mRouter.getMinIncrements(alter), { 0: 5.0, 1: 5.0, 2: 0, 3: 0.0, 4: 0.0, 5: 1.0 })
def __init__(self, packageName, modObj=None, **kwargs): """ 模块的信息 __是受保护的内容 valid:是否有效 doc:注释信息 version:模块的信息 author:作者 rotuers:可调用对象->{方法,路由,解释,级别,系统} commandLines:支持的命令行参数版本 moduleObject:模块的实例 main_package:是否是主模块 :param packageName: 模块的名称 :return: """ try: package_root_modtype = macros.Macro('PACKAGEROOT_IMPORT') self.routers = dict() self.commandLines = dict() self.name = packageName self.main_package = False if self.name == 'Common': self.main_package = True if not modObj: # 载入packages下的模块 targetdir = macros.Macro('PACKAGEROOT') + os.sep + packageName if not os.path.exists(targetdir) or not os.path.isdir( targetdir): self.valid = False return modObj = getattr( __import__(package_root_modtype, globals(), locals(), [packageName]), packageName) # 导入模块 if not modObj: self.valid = False return self.valid = True self.fullName = '.'.join((package_root_modtype, packageName)) # 读取doc、version、author以及各个路由器的信息(只在公共接口中__all__)以及路由的命令行格式(如果有) if getattr(modObj, '__all__', None): # 合法的模块 for api in getattr(modObj, '__all__', None): fn = getattr(modObj, api) if callable(fn): # 是一个可调用对象 method = getattr(fn, '__method__', None) path = getattr(fn, '__route__', None) adminAcquire = getattr(fn, '__adminLevel__', None) if method and path: # 添加路由 # self.routers[fn] = {'method': method, 'route': path, 'doc':inspect.getdoc(fn), # 'level':'api' if path.startswith('/api') else 'user', # 'system':self.main_package # } # 可调用对象->{方法,路由,解释,级别,系统} # 读取命令行参数 if not asyncio.iscoroutinefunction( fn) and not inspect.isgeneratorfunction( fn): # 检查是否是异步函数 fn = asyncio.coroutine(fn) # 确定router级别 rt_level = 'user' if path.startswith('/api'): rt_level = 'api' elif path.startswith('/ws'): rt_level = 'websocket' rt = Router.Router( method=method, route=path, doc=inspect.getdoc(fn), level=rt_level, system=self.main_package, func=fn, acquireAdmin=adminAcquire) # 定义成Router 类 self.routers[rt.flagName] = rt if getattr(modObj, '__cmdLines__', None): self.commandLines = modObj.__cmdLines__ # 直接绑定为命令行 self.version = getattr(modObj, '__version__', '') self.author = getattr(modObj, '__author__', '') self.doc = getattr(modObj, '__doc__', '') self.moduleObject = modObj # 构建对象 Package.logger.info('模块对象‘%s’载入成功' % self.name) except Exception as e: # 处理错误 Package.logger.error('初始化模块对象出现问题:%s\n堆栈信息:%s\n' % (str(e.args), traceback.format_exc())) self.valid = False raise e
def test_getNeighbors(self): mRouter = Router(self.RouterID, self.AdjMatrix, self.DemandMatrix) self.assertListEqual(mRouter.neighbors, [1, 2])