def test_level_mark_basics(self): G = nx.DiGraph() G.add_nodes_from(range(8)) G.add_edges_from([(0, 1), (0, 4), (1, 2), (2, 3), (4, 5), (5, 2), (5, 6), (6, 7), (7, 3)]) ConstructionOrdering.markLevels(G) self.assertEqual(G.node[0][ConstructionOrdering.kAttrNameNodeLevel], 0) self.assertEqual(G.node[1][ConstructionOrdering.kAttrNameNodeLevel], 1) self.assertEqual(G.node[2][ConstructionOrdering.kAttrNameNodeLevel], 3) self.assertEqual(G.node[3][ConstructionOrdering.kAttrNameNodeLevel], 5) self.assertEqual(G.node[4][ConstructionOrdering.kAttrNameNodeLevel], 1) self.assertEqual(G.node[5][ConstructionOrdering.kAttrNameNodeLevel], 2) self.assertEqual(G.node[6][ConstructionOrdering.kAttrNameNodeLevel], 3) self.assertEqual(G.node[7][ConstructionOrdering.kAttrNameNodeLevel], 4) self.assertEqual( G.node[0][ConstructionOrdering.kAttrNameNodeAfterLength], 5) self.assertEqual( G.node[1][ConstructionOrdering.kAttrNameNodeAfterLength], 2) self.assertEqual( G.node[2][ConstructionOrdering.kAttrNameNodeAfterLength], 1) self.assertEqual( G.node[3][ConstructionOrdering.kAttrNameNodeAfterLength], 0) self.assertEqual( G.node[4][ConstructionOrdering.kAttrNameNodeAfterLength], 4) self.assertEqual( G.node[5][ConstructionOrdering.kAttrNameNodeAfterLength], 3) self.assertEqual( G.node[6][ConstructionOrdering.kAttrNameNodeAfterLength], 2) self.assertEqual( G.node[7][ConstructionOrdering.kAttrNameNodeAfterLength], 1) routes = ConstructionOrdering.getCriticalWays(G) self.assertEqual(routes, [[0, 4, 5, 6, 7, 3]])
def MapFusionToKB(kbTasks, fusionTasks): """Map kb and fusion tasks to each other""" res = {} if not kbTasks or not "tasks" in kbTasks or not fusionTasks: return res # Order tasks obtained from KB using graph structure tasksOrder = nx.DiGraph() mapNumber = 1 for task in kbTasks["tasks"]: tasksOrder.add_node(task[Task.__kTagName]) tasksOrder.node[task[Task.__kTagName]] = { "task": task, "mapNumber": mapNumber } mapNumber += 1 for task in kbTasks["tasks"]: if Task.__kTagAfterTask in task[Task.__kTagProperties] and len( task[Task.__kTagProperties][Task.__kTagAfterTask]) > 0: for t in task[Task.__kTagProperties][Task.__kTagAfterTask]: tasksOrder.add_edge(t, task[Task.__kTagName]) seen = [] # Go over task groups in fusion array for fgroup in fusionTasks.items(): # Tasks are under the non-empty array values if isinstance(fgroup[1], list) and len(fgroup[1]) > 0: baseItem = fgroup[1][0] ruleGroups = [] # Find rulegroups for fusion task group for g in Task.__kMappingRules: applicableRules = 0 for rule in g: if rule[Task.__kLookForKey] in baseItem: applicableRules += 1 if len(g) == applicableRules: ruleGroups.append(g) # Map tasks from fusion tasks group to tasks from kb for ruleGroup in ruleGroups: nodes = [ node for node in tasksOrder if len([ rule for rule in ruleGroup if rule[Task.__kKeyValue].lower() in node.lower() ]) ] if nodes: for item in fgroup[1]: nodeSet = [] for node in nodes: for rule in ruleGroup: if Task.__kKBToolKey in rule: if Task.__CheckKBTaskKeyForMatch( tasksOrder.node[node]["task"], rule[Task.__kKBToolKey], item[rule[Task.__kKeyValue]]): nodeSet.append(node) else: nodeSet.append(node) for node in nodeSet: if not node in res: res[node] = {"orderMark": 0, "tasks": []} res[node]["tasks"].append(item) # Mark graph levels from Optimization.Order.ConstructionOrdering import ConstructionOrdering ConstructionOrdering.markLevels(tasksOrder) for item in res.items(): item[1]["orderMark"] = tasksOrder.node[item[0]]["level"] return res