def query(d): try: pattern = eval(d["query"]) for _tuple in pattern: assert isinstance(_tuple[0], str) assert isinstance(_tuple[1], list) for match in _tuple[1]: assert isinstance(match, int) and 0 <= match < len(pattern) except: return "Error: Query pattern is invalid" try: gf = lab.GraphFactory(eval("lab." + d["graphClass"])) except: return "Error: Invalid graph class or Graph Factory failed to init" try: graph = gf.from_list(adj_list, labels) except: return "Error: Graph Factor failed to create graph" try: return graph.query(pattern), pattern except: return "Error: Query Function had an error"
def test_largeGraph(self): # Only tests for memory limit. graph = lab.GraphFactory(lab.CompactGraph).from_list(self.adj_list) self.assertLessEqual( get_size(graph), 400000, "Instance of Compact Graph is too big, so not space efficient enough" ) self.assertIsInstance(graph, lab.CompactGraph)
def test_02(self): cutoffs = {0: self.dict_class, 1.0: self.dict_class} for cutoff, expected_class in cutoffs.items(): factory = lab.GraphFactory(cutoff) edges = [] nodes = ["123", "456", "789"] graph = factory.from_edges_and_nodes(edges, nodes) self.assertIsInstance(graph, expected_class) self.assertEqual({"123", "456", "789"}, graph.nodes()) self.assertEqual(set(), graph.neighbors("123"))
def test_04(self): cutoffs = { 0: self.matrix_class, 0.99: self.matrix_class, 1.0: self.dict_class } for cutoff, expected_class in cutoffs.items(): factory = lab.GraphFactory(cutoff) edges = [ ('boston', 'chicago', 91238.21), ('chicago', 'boston', 91238.21), ] nodes = ["boston", "chicago"] graph = factory.from_edges_and_nodes(edges, nodes) self.assertIsInstance(graph, expected_class) self.assertEqual({"boston", "chicago"}, graph.nodes())
def test_01(self): adj_list = [[5], [6], [5], [5], [5], [0, 2, 3, 4], [1]] labels = {i: "building" for i in range(5)} labels.update({5: "toy", 6: "candy"}) labels = { 0: "building", 1: "building", 2: "building", 3: "building", 4: "building", 5: "toy", 6: "candy" } factory = lab.GraphFactory(lab.SimpleGraph) graph = factory.from_list(adj_list, labels=labels) stations = {0: "South", 1: "South", 2: "North", 3: "South", 4: "South"} result = lab.allocate_teams(graph, 3, stations, ["toy", "candy"]) self.assertEqual({"toy": 1, "candy": 0}, result)
def test_01(self): cutoffs = { 0.1: self.matrix_class, 0.25: self.dict_class, 0.4: self.dict_class } for cutoff, expected_class in cutoffs.items(): factory = lab.GraphFactory(cutoff) edges = [("A", "B", 2), ("A", "C", 0), ("A", "D", 4)] nodes = ["A", "B", "C", "D"] graph = factory.from_edges_and_nodes(edges, nodes) self.assertIsInstance(graph, expected_class) self.assertEqual({"A", "B", "C", "D"}, graph.nodes(), msg=f"failed on {expected_class}") self.assertEqual({("B", 2), ("C", 0), ("D", 4)}, graph.neighbors("A"), msg=f"failed on {expected_class}")
def _test_result(self, k): adj_list = [[11], [11], [11], [11], [11], [10], [10, 11], [11], [10], [10], [5, 6, 8, 9], [0, 1, 2, 3, 4, 6, 7]] labels = {i: "building" for i in range(10)} labels.update({10: "puppy", 11: "candy"}) factory = lab.GraphFactory(lab.SimpleGraph) graph = factory.from_list(adj_list, labels=labels) stations = { 0: "Old Square", 1: "Old Square", 2: "Old Square", 3: "Old Square", 4: "Town Hall", 5: "Town Hall", 6: "Town Hall", 7: "South Station", 8: "Ice Rink", 9: "Ice Rink" } gifts = ["puppy", "candy"] return lab.allocate_teams(graph, k, stations, gifts)
def test_03(self): cutoffs = { 0: self.matrix_class, 0.8: self.matrix_class, 1.0: self.dict_class } for cutoff, expected_class in cutoffs.items(): factory = lab.GraphFactory(cutoff) edges = [ ('Daphne', 'Fred', 1), ('Daphne', 'Scooby', 5), ('Daphne', 'Shaggy', 7), ('Daphne', 'Velma', 3), ('Fred', 'Daphne', 1), ('Fred', 'Scooby', 5), ('Fred', 'Shaggy', 7), ('Fred', 'Velma', 9), ('Scooby', 'Daphne', 5), ('Scooby', 'Fred', 5), ('Scooby', 'Shaggy', 0), ('Scooby', 'Velma', 3), ('Shaggy', 'Daphne', 7), ('Shaggy', 'Fred', 7), ('Shaggy', 'Scooby', 0), ('Shaggy', 'Velma', 6), ('Velma', 'Daphne', 3), ('Velma', 'Fred', 9), ('Velma', 'Scooby', 3), ('Velma', 'Shaggy', 6), ] nodes = ["Shaggy", "Scooby", "Daphne", "Velma", "Fred"] graph = factory.from_edges_and_nodes(edges, nodes) expected_nodes = {"Shaggy", "Scooby", "Daphne", "Velma", "Fred"} expected_neighbors = {("Scooby", 0), ("Velma", 6), ("Daphne", 7), ("Fred", 7)} self.assertIsInstance(graph, expected_class) self.assertEqual(expected_nodes, graph.nodes()) self.assertEqual(expected_neighbors, graph.neighbors("Shaggy"))
def setUp(self): super().setUp() factory = lab.GraphFactory(lab.CompactGraph) self.graph1 = factory.from_list(self.adj_list, labels=self.labels) self.graph2 = factory.from_dict(self.adj_dict, labels=self.labels)
def setUp(self): super().setUp() factory = lab.GraphFactory(lab.CompactGraph) self.graph = factory.from_list(self.adj_list)