def test_mg_inverse(self): generating_set2 = {1, 2, 3, 4, 5, 6, 7, 8} mg2 = Metagraph(generating_set2) mg2.add_edges_from([ Edge({1, 2}, {3, 4}), Edge({3, 4, 5}, {6, 8}), Edge({1}, {5}), Edge({6, 7}, {1}) ]) inverse = mg2.get_inverse() self.assertEqual(len(inverse.edges), 6) self.assertEqual(len(inverse.nodes), 6)
def test_edge_properties(self): source = {1} target = {7} metapaths = self.mg1.get_all_metapaths_from(source, target) redundant = self.mg1.is_redundant_edge(Edge({1}, {2, 3}), metapaths[0], source, target) self.assertEqual(redundant, False) edge_list = [Edge({1}, {2, 3})] is_cutset = self.mg1.is_cutset(edge_list, source, target) is_bridge = self.mg1.is_bridge(edge_list, source, target) self.assertEqual(is_cutset, True) self.assertEqual(is_bridge, True)
def test_mg_efm(self): generating_set2 = {1, 2, 3, 4, 5, 6, 7, 8} mg2 = Metagraph(generating_set2) mg2.add_edges_from([ Edge({1, 2}, {3, 4}), Edge({3, 4, 5}, {6, 8}), Edge({1}, {5}), Edge({6, 7}, {1}) ]) generator_subset = {2, 4, 7} efm = mg2.get_efm(generator_subset) self.assertEqual(len(efm.edges), 3) self.assertEqual(len(efm.nodes), 3)
def test_mg_projection(self): generating_set2 = {1, 2, 3, 4, 5, 6, 7, 8} mg2 = Metagraph(generating_set2) mg2.add_edges_from([ Edge({1}, {3, 4}), Edge({3}, {6}), Edge({2}, {5}), Edge({4, 5}, {7}), Edge({6, 7}, {8}) ]) generator_subset = {1, 2, 6, 7, 8} projection = mg2.get_projection(generator_subset) self.assertEqual(len(projection.edges), 4) self.assertEqual(len(projection.nodes), 7)
def test_conflict(self): variable_set = {'u1', 'u2', 'u3', 'u4', 'u5', 'u6', 'r1', 'r2', 'r3'} propositions_set = {'action=permit', 'action=deny'} cm = ConditionalMetagraph(variable_set, propositions_set) cm.add_edges_from([ Edge({'u1', 'u2', 'u3'}, {'r1', 'r2'}, attributes=['action=permit']), Edge({'u3', 'u4', 'u5'}, {'r2', 'r3'}, attributes=['action=deny']), Edge({'u2', 'u3', 'u5', 'u6'}, {'r1', 'r2'}, attributes=['action=permit']) ]) all_metapaths = cm.get_all_metapaths() for metapath in all_metapaths: if cm.has_redundancies(metapath): pass # print('redundancy detected: %s'%repr(metapath)) if cm.has_conflicts(metapath): pass # print('conflict detected: %s'%repr(metapath.edge_list)) return
def setUp(self): import numpy t = numpy.version self.generating_set1 = {1, 2, 3, 4, 5, 6, 7} self.mg1 = Metagraph(self.generating_set1) self.mg1.add_edges_from( [Edge({1}, {2, 3}), Edge({1, 4}, {5}), Edge({3}, {6, 7})]) self.variable_set = set(range(1, 8)) self.propositions_set = {'p1', 'p2'} self.cmg1 = ConditionalMetagraph(self.variable_set, self.propositions_set) self.cmg1.add_edges_from([ Edge({1, 2}, {3, 4}, attributes=['p1']), Edge({2}, {4, 6}, attributes=['p2']), Edge({3, 4}, {5}, attributes=['p1', 'p2']), Edge({4, 6}, {5, 7}, attributes=['p1']) ])
def test_from_textbook(self): generating_set1 = {1, 2, 3, 4, 5, 6, 7, 8, 9} mg1 = Metagraph(generating_set1) mg1.add_edges_from([ Edge({1}, {2, 3}), Edge({1, 4}, {5}), Edge({3}, {7}), Edge({5, 2}, {6}), Edge({6, 7}, {9}), Edge({3, 4, 8}, {9}), Edge({4, 8}, {1, 5}) ]) #a1=mg1.adjacency_matrix() #a_star1= mg1.get_closure() #incidence_matrix= mg1.incidence_matrix() metapaths1 = mg1.get_all_metapaths_from({1, 4}, {6}) # check metapath dominance if len(metapaths1) > 0: edge_dominant = mg1.is_edge_dominant_metapath(metapaths1[0]) input_dominant = mg1.is_input_dominant_metapath(metapaths1[0]) dominant = mg1.is_dominant_metapath(metapaths1[0]) self.assertEqual(edge_dominant, True) self.assertEqual(input_dominant, True) self.assertEqual(dominant, True) metapaths3 = mg1.get_all_metapaths_from({1, 4}, {5, 6}) redundant1 = mg1.is_redundant_edge(Edge({1, 4}, {5}), metapaths3[0], {1, 4}, {5, 6}) redundant2 = mg1.is_redundant_edge(Edge({4, 8}, {5, 6}), metapaths3[0], {4, 8}, {5, 6}) self.assertEqual(redundant1, False) self.assertEqual(redundant2, True) source = {4, 8} target = {7} edge_list1 = [Edge({4, 8}, {1})] edge_list2 = [Edge({1}, {2, 3})] is_cutset = mg1.is_cutset(edge_list1, source, target) is_bridge = mg1.is_bridge(edge_list2, source, target) self.assertEqual(is_cutset, False) self.assertEqual(is_bridge, True)
def test_course_dependencies(self): import os import csv course_details_lookup = dict() edge_list = [] generator_list = [] folder_path = os.path.join(os.getcwd(), 'test_data') file_name = 'courses.csv' with open(os.path.join(folder_path, file_name), 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='|') index = 0 for row in reader: if index == 0: # omit header index += 1 continue code = row[0] name = row[1] pre_requisites = MetagraphHelper().get_pre_requisites_list( row[2]) if code not in course_details_lookup: course_details_lookup[code] = dict() if 'name' not in course_details_lookup[code]: course_details_lookup[code]['name'] = name if 'pre-requisites' not in course_details_lookup[code]: course_details_lookup[code][ 'pre-requisites'] = pre_requisites index += 1 for code, details in course_details_lookup.iteritems(): if code not in generator_list: generator_list.append(code) for pre_requisite in course_details_lookup[code][ 'pre-requisites']: if isinstance(pre_requisite, list): edge_list.append(Edge(set(pre_requisite), set([code]))) for item in pre_requisite: if item not in generator_list: generator_list.append(item) else: edge_list.append( Edge(set([pre_requisite]), set([code]))) if pre_requisite not in generator_list: generator_list.append(pre_requisite) # create course metagraph mg = Metagraph(set(generator_list)) mg.add_edges_from(edge_list) # run simple test - what are the patways to course "APP MTH 3001"? target = {'APP MTH 3001'} source = set(generator_list).difference(target) metapaths = mg.get_all_metapaths_from(source, target) # remove duplicates filtered = [] for mp1 in metapaths: included = False for mp2 in metapaths: if mp1 != mp2: if MetagraphHelper().is_edge_list_included( mp1.edge_list, mp2.edge_list): included = True break if not included: filtered.append(mp1) # print output #index=1 #for mp in filtered: # print('metapath %s'%index) # for edge in mp.edge_list: # print(repr(edge)) # index+=1 self.assertEqual(len(filtered), 4)