def test_get_vertex(): """Check if vertex can be retrieved from directed graph.""" g = DirectedGraph() g.add_vertex(vertex_props=None) v = g.get_vertex(vertex_id=0) assert v is not None assert v.id == 0
def test_get_vertex_ids_graph_with_one_vertex(): """Test whether method DirectedGraph.get_vertex_ids() works correctly for graph with vertex.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) list_ids = g.get_vertex_ids() assert len(list_ids) == 1 assert v0.id in list_ids
def test_get_vertices_graph_with_one_vertex(): """Test whether method DirectedGraph.get_vertices() works correctly.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) list_vertices = g.get_vertices() assert len(list_vertices) == 1 assert v0 in list_vertices
def test_get_vertex_ids(): g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) v1 = g.add_vertex(vertex_props=None) list_ids = g.get_vertex_ids() assert len(list_ids) == 2 assert v0.id in list_ids assert v1.id in list_ids
def test_get_vertices(): g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) v1 = g.add_vertex(vertex_props=None) list_vertices = g.get_vertices() assert len(list_vertices) == 2 assert v0 in list_vertices assert v1 in list_vertices
def test_get_vertex_ids(): """Test whether method DirectedGraph.get_vertex_ids() works correctly.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) v1 = g.add_vertex(vertex_props=None) list_ids = g.get_vertex_ids() assert len(list_ids) == 2 assert v0.id in list_ids assert v1.id in list_ids
def test_add_edge_between_nonexistent_vertices(): """Test whether method DirectedGraph.add_edge() works correctly.""" g = DirectedGraph() v0 = Vertex(0, {}) v1 = Vertex(1, {}) # neither vertex are put into the directed graph, so it is impossible # to add an edge to connect them. ATM the exception thrown by the # add_edge method is not very specific, it might need to be improved. with pytest.raises(Exception): g.add_edge(from_id=v0.id, to_id=v1.id)
def test_add_cycle_to_the_same_vertex(): """Test whether method DirectedGraph.add_edge() works correctly.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) # create edge from and to the same vertex g.add_edge(from_id=v0.id, to_id=v0.id) v0_neighbours = v0.get_neighbours() # only one neighbour assert len(v0_neighbours) == 1 # neighbour has id = 1 assert v0_neighbours[0].id == 0
def test_find_common_reachable_vertex_empty_graph(): """Test if method DirectedGraph.find_common_reachable_vertices() works correctly.""" list_vertices = DirectedGraph.find_common_reachable_vertices(input_vertices=None) assert list_vertices is None list_vertices = DirectedGraph.find_common_reachable_vertices(input_vertices=[]) assert list_vertices is None v0 = Vertex(0, {}) list_vertices = DirectedGraph.find_common_reachable_vertices(input_vertices=[v0]) assert list_vertices is not None assert list_vertices[0] == v0
def test_add_edge(): g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) v1 = g.add_vertex(vertex_props=None) g.add_edge(from_id=v0.id, to_id=v1.id) v0_neighbours = v0.get_neighbours() # only one neighbour assert len(v0_neighbours) == 1 # neighbour has id = 1 assert v0_neighbours[0].id == 1 v1_neighbours = v1.get_neighbours() # no neighbour of v1 as it is directed graph assert len(v1_neighbours) == 0
def __init__(self, graph_store, synonyms_store): """Initialize the analyzer and read known synonyms.""" # load graph from given data store self.g = DirectedGraph.read_from_json(graph_store) self.known_licenses = [ 'public domain', 'mit', 'bsd-new', 'bsd-simplified', 'apache 2.0', 'lgplv2.1', 'lgplv2.1+', 'lgplv3+', 'mpl 1.1', 'gplv2', 'gplv2+', 'gplv3+', 'affero gplv3', 'epl 1.0', 'epl 2.0', 'cddlv1.1+', 'mpl 2.0', 'w3c', 'bouncycastle', 'cc0v1.0', 'cc-by-3.0', 'edl', 'json', 'postgresql', 'apache 1.1', 'cpl 1.0', 'cpal 1.0', 'ISC', 'gwt', 'psfl' ] # IMPORTANT: Order matters in the following tuple self.license_type_tuple = ('P', 'WP', 'SP', 'NP') # read the json that contains known synonyms list_synonym_jsons = synonyms_store.list_files() for synonym_json in list_synonym_jsons: self.syn = synonyms_store.read_json_file(synonym_json) break # currently only one synonym json is supported # identify compatibility classes among the licenses in graph self.dict_compatibility_classes = {} self.dict_type_compatibility_classes = {} self._find_compatibility_classes() self._find_type_compatibility_classes()
def test_add_cycle(): """Test whether method DirectedGraph.add_edge() works correctly.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) v1 = g.add_vertex(vertex_props=None) g.add_edge(from_id=v0.id, to_id=v1.id) g.add_edge(from_id=v1.id, to_id=v0.id) v0_neighbours = v0.get_neighbours() # only one neighbour assert len(v0_neighbours) == 1 # neighbour has id = 1 assert v0_neighbours[0].id == 1 v1_neighbours = v1.get_neighbours() # no neighbour of v1 as it is directed graph assert len(v1_neighbours) == 1 # neighbour has id = 0 assert v1_neighbours[0].id == 0
def test_num_vertices_counter(): """Check the num_vertices counter.""" g = DirectedGraph() assert g.num_vertices == 0 g.add_vertex(vertex_props=None) assert g.num_vertices == 1 g.add_vertex(vertex_props=None) assert g.num_vertices == 2
def test_iterator(): """Test that the iterator over all vertexes works correctly.""" g = DirectedGraph() items = [item for item in g] assert not items v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'}) items = [item for item in g] assert len(items) == 1 assert items[0] == v0 v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'}) items = [item for item in g] assert len(items) == 2 assert v0 in items assert v1 in items v2 = g.add_vertex(vertex_props={}) items = [item for item in g] assert len(items) == 3 assert v0 in items assert v1 in items assert v2 in items
def test_find_vertex(): g = DirectedGraph() v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'}) v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'}) v = g.find_vertex(prop_name='license', prop_value='L1') assert v != v0 assert v == v1 v = g.find_vertex(prop_name='license', prop_value='L2') assert v is None
def test_find_vertex(): """Test if method DirectedGraph.find_vertex() works correctly.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'}) v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'}) v = g.find_vertex(prop_name='license', prop_value='L1') assert v != v0 assert v == v1 v = g.find_vertex(prop_name='license', prop_value='L2') assert v is None
def test_find_vertex_graph_with_one_vertex(): """Test if method DirectedGraph.find_vertex() works correctly.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'}) v = g.find_vertex(prop_name='license', prop_value='L0') assert v is not None assert v == v0 v = g.find_vertex(prop_name='license', prop_value='something_else') assert v is None with pytest.raises(Exception): v = g.find_vertex(prop_name='unknown_property', prop_value='L0')
def __init__(self, args): super(GraphSearchPolicy, self).__init__() self.args = args self.model = args.model self.relation_only = args.relation_only self.history_dim = args.history_dim self.history_num_layers = args.history_num_layers self.entity_dim = args.entity_dim self.relation_dim = args.relation_dim if self.relation_only: self.action_dim = args.relation_dim else: self.action_dim = args.entity_dim + args.relation_dim self.ff_dropout_rate = args.ff_dropout_rate self.rnn_dropout_rate = args.rnn_dropout_rate self.action_dropout_rate = args.action_dropout_rate self.num_rollouts = args.num_rollouts self.bandwidth = args.bandwidth self.num_rollout_steps = args.num_rollout_steps self.emb_dropout_rate = args.emb_dropout_rate self.beam_size = args.beam_size self.xavier_initialization = args.xavier_initialization self.relation_only_in_path = args.relation_only_in_path self.path = None # Directed Graph self.dg = DirectedGraph(args.data_dir) # Graph Transformer hyper params self.num_heads = 4 self.head_dim = args.entity_dim // self.num_heads self.hidden_dim = self.history_dim # Set policy network modules self.define_modules() self.initialize_modules() # Fact network modules self.fn = None self.fn_kg = None
def __init__(self, graph_store, synonyms_store): # load graph from given data store self.g = DirectedGraph.read_from_json(graph_store) self.known_licenses = [ 'public domain', 'mit', 'bsd-new', 'apache 2.0', 'lgplv2.1', 'lgplv2.1+', 'lgplv3+', 'mpl 1.1', 'gplv2', 'gplv2+', 'gplv3+', 'affero gplv3' ] # IMPORTANT: Order matters in the following tuple self.license_type_tuple = ('P', 'WP', 'SP', 'NP') # read the json that contains known synonyms list_synonym_jsons = synonyms_store.list_files() for synonym_json in list_synonym_jsons: self.syn = synonyms_store.read_json_file(synonym_json) break # currently only one synonym json is supported # identify compatibility classes among the licenses in graph self.dict_compatibility_classes = {} self.dict_type_compatibility_classes = {} self._find_compatibility_classes() self._find_type_compatibility_classes()
def test_get_vertices_empty_graph(): """Test whether method DirectedGraph.get_vertices() works correctly for empty graph.""" g = DirectedGraph() list_vertices = g.get_vertices() assert not list_vertices
def _create_graph(): g = DirectedGraph() pd = g.add_vertex(vertex_props={'license': 'public domain', 'type': 'P'}) mit = g.add_vertex(vertex_props={'license': 'mit', 'type': 'P'}) bsd = g.add_vertex(vertex_props={'license': 'bsd-new', 'type': 'P'}) apache = g.add_vertex(vertex_props={'license': 'apache 2.0', 'type': 'P'}) lgpl2 = g.add_vertex(vertex_props={'license': 'lgplv2.1', 'type': 'WP'}) lgpl22 = g.add_vertex(vertex_props={'license': 'lgplv2.1+', 'type': 'WP'}) lgpl3 = g.add_vertex(vertex_props={'license': 'lgplv3+', 'type': 'WP'}) mpl = g.add_vertex(vertex_props={'license': 'mpl 1.1', 'type': 'WP'}) gpl2 = g.add_vertex(vertex_props={'license': 'gplv2', 'type': 'SP'}) gpl22 = g.add_vertex(vertex_props={'license': 'gplv2+', 'type': 'SP'}) gpl3 = g.add_vertex(vertex_props={'license': 'gplv3+', 'type': 'SP'}) agpl3 = g.add_vertex(vertex_props={'license': 'agplv3', 'type': 'NP'}) g.add_edge(pd.id, mit.id) g.add_edge(mit.id, bsd.id) g.add_edge(bsd.id, apache.id) g.add_edge(bsd.id, mpl.id) g.add_edge(bsd.id, lgpl2.id) g.add_edge(bsd.id, lgpl22.id) g.add_edge(bsd.id, lgpl3.id) g.add_edge(apache.id, lgpl3.id) g.add_edge(lgpl22.id, lgpl2.id) g.add_edge(lgpl22.id, lgpl3.id) g.add_edge(lgpl2.id, gpl2.id) g.add_edge(lgpl2.id, gpl22.id) g.add_edge(lgpl22.id, gpl22.id) g.add_edge(lgpl3.id, gpl3.id) g.add_edge(gpl22.id, gpl2.id) g.add_edge(gpl22.id, gpl3.id) g.add_edge(gpl3.id, agpl3.id) return g
def compute_representative_license(self, input_licenses): """Compute representative license for given list of licenses. First, it tries to identify the input licenses by using known synonyms. If there exists at least one unknown license, this method gives up. It makes use of a very popular license graph available in [1]. After identifying license vertices in the graph, it tries to find the common reachable vertex from the input license vertices. If a common reachable vertex is not possible then there is a conflict and all pairs of conflicting licenses are identified by using the concept of compatibility classes. If a common reachable vertex is available, then its license becomes representative license. Note that we also try to find outlier licenses when representative license is available. [1] https://www.dwheeler.com/essays/floss-license-slide.html :param input_licenses: list of input licenses :return: representative license with supporting information """ output = { 'status': 'Failure', 'reason': 'Input is invalid', 'representative_license': None, 'unknown_licenses': [], 'conflict_licenses': [], 'outlier_licenses': [], 'synonyms': {} } if input_licenses is None: return output if len(input_licenses) == 0: return output # Find synonyms input_lic_synonyms = [self.find_synonym(y) for y in input_licenses] output['synonyms'] = dict(list(zip(input_licenses, input_lic_synonyms))) # Check if all input licenses are known if len(set(input_lic_synonyms) - set(self.known_licenses)) > 0: output['status'] = 'Unknown' output['reason'] = 'Some unknown licenses found' output['unknown_licenses'] = list( set(input_lic_synonyms) - set(self.known_licenses)) output['representative_license'] = None return output # Let's try to find a representative license # First, we need to find vertices for input licenses license_vertices = [] for lic in input_lic_synonyms: v = self.g.find_vertex(prop_name='license', prop_value=lic) assert v is not None license_vertices.append(v) assert len(license_vertices) == len(input_lic_synonyms) # Find common reachable vertices from input license vertices reachable_vertices = DirectedGraph.find_common_reachable_vertices(license_vertices) if len(reachable_vertices) == 0: # i.e. conflict output['status'] = 'Conflict' output['reason'] = 'Some licenses are in conflict' output['conflict_licenses'] = self._find_conflict_licenses(license_vertices) output['representative_license'] = None return output # Some representative license is possible :) # Check if one of the input licenses is the representative one common_destination = None license_vertex_ids = [x.id for x in license_vertices] for v in reachable_vertices: if v.id in license_vertex_ids: common_destination = v # TODO: should we break after this ? if common_destination is not None: output['status'] = 'Successful' output['reason'] = 'Representative license found' output['representative_license'] = \ common_destination.get_prop_value(prop_name='license') rep_lic_vertex = self.g.find_vertex('license', output['representative_license']) rep_lic_type = rep_lic_vertex.get_prop_value('type') output['outlier_licenses'] = self._find_outlier_licenses(license_vertices, rep_lic_type) return output # If one of the input licenses is NOT the representative one, then # let us pick up the least restrictive one for license_type in self.license_type_tuple: list_common_destinations = [ x for x in reachable_vertices if x.get_prop_value(prop_name='type') == license_type ] if len(list_common_destinations) > 0: output['status'] = 'Successful' output['reason'] = 'Representative license found' output['representative_license'] = \ list_common_destinations[0].get_prop_value(prop_name='license') rep_lic_vertex = self.g.find_vertex('license', output['representative_license']) rep_lic_type = rep_lic_vertex.get_prop_value('type') output['outlier_licenses'] = self._find_outlier_licenses(license_vertices, rep_lic_type) return output # We should have returned by now ! Returning from here is unexpected ! output['status'] = 'Failure' output['reason'] = 'Something unexpected happened!' output['representative_license'] = None return output
def test_get_vertex(): g = DirectedGraph() g.add_vertex(vertex_props=None) v = g.get_vertex(vertex_id=0) assert v is not None
def test_find_vertex_empty_graph(): """Test if method DirectedGraph.find_vertex() works correctly for empty graph.""" g = DirectedGraph() v = g.find_vertex(prop_name='license', prop_value='L1') assert v is None
def test_find_common_reachable_vertex(): g = DirectedGraph() v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'}) v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'}) g.add_edge(from_id=v0.id, to_id=v1.id) list_vertices = DirectedGraph.find_common_reachable_vertices( input_vertices=None) assert list_vertices is None list_vertices = DirectedGraph.find_common_reachable_vertices( input_vertices=[]) assert list_vertices is None list_vertices = DirectedGraph.find_common_reachable_vertices( input_vertices=[v0]) assert list_vertices is not None assert len(list_vertices) == 2 assert list_vertices[0] == v0 assert list_vertices[1] == v1 list_vertices = DirectedGraph.find_common_reachable_vertices( input_vertices=[v0, v1]) assert list_vertices is not None assert len(list_vertices) == 1 assert list_vertices[0] == v1 v2 = g.add_vertex(vertex_props={'license': 'L2', 'type': 'SP'}) g.add_edge(from_id=v2.id, to_id=v1.id) list_vertices = DirectedGraph.find_common_reachable_vertices( input_vertices=[v0, v2]) assert list_vertices is not None assert len(list_vertices) == 1 assert list_vertices[0] == v1
def test_get_nonexistent_vertex(): """Check if non existent vertex can not be retrieved from directed graph.""" g = DirectedGraph() g.add_vertex(vertex_props=None) v = g.get_vertex(vertex_id=42) assert v is None
def test_initial_state(): """Check the initial state of directed graph.""" g = DirectedGraph() assert g.num_vertices == 0
def test_add_vertex(): g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) assert v0.id >= 0
def test_add_vertex(): """Check that new vertex can be added to directed graph.""" g = DirectedGraph() v0 = g.add_vertex(vertex_props=None) assert v0.id >= 0