コード例 #1
0
    def setUp(self):
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {
            'test_field': 12,
            'time': 'now'
        })
コード例 #2
0
    def setUp(self):
        self.graph = Graph()
        self.graph.add_vertex('test_label', {'test_field': 1})
        self.graph.add_vertex('test_label', {'test_field': 2})
        self.graph.add_vertex('test_label', {'test_field': 12, 'time': 'now'})

        self.graph2 = Graph()
        self.vertex1 = self.graph2.add_vertex('person', {'username': '******'}, current_id=10)
        self.vertex2 = self.graph2.add_vertex('listing', {'title': 'Skyzone1'}, current_id=20)
        self.vertex3 = self.graph2.add_vertex('listing', {'title': 'Skyzone2'}, current_id=30)
        self.vertex1.add_edge('personListing', self.vertex2)
        self.vertex1.add_edge('personListing', self.vertex3)
コード例 #3
0
    def setUp(self):
        """
        setUp is invoked before each test method
        """
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {
            'test_field': 12,
            'time': 'now'
        })
コード例 #4
0
    def create_graph_test_3v_2e():
        graph = Graph()
        vertex1 = graph.add_vertex('person', {'username': '******'})
        vertex2 = graph.add_vertex('listing', {'title': 'Skyzone1'})
        vertex3 = graph.add_vertex('listing', {'title': 'Skyzone2'})
        vertex1.add_edge('personListing', vertex2)
        vertex1.add_edge('personListing', vertex3)

        return graph
コード例 #5
0
    def test_pipeline_graph_vertex_chain_to_list(self):
        graph = Graph()
        graph.add_vertex('test_label', {'test_field': 1})
        graph.add_vertex('test_label', {'test_field': 2})

        pipeline_test = pipeline.Pipeline(
            graph.get_vertices_iterator(),
            [pipes.GraphVertexPipe(),
             pipes.ElementIdPipe()])

        self.assertEqual(pipeline_test.to_list(), [1, 2])
コード例 #6
0
ファイル: test_graph.py プロジェクト: markbetters/ozp-backend
    def test_graph_add_two_vertex(self):
        graph = Graph()
        self.assertEqual(str(graph), 'Graph(vertices: 0, edges: 0)')

        added_vertex = graph.add_vertex('test_label', {'test_field': 1})

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label')
        self.assertEqual(added_vertex.get_property('test_field'), 1)

        added_vertex = graph.add_vertex('test_label1', {'test_field': 2})

        self.assertEqual(str(graph), 'Graph(vertices: 2, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label1')
        self.assertEqual(added_vertex.get_property('test_field'), 2)
コード例 #7
0
ファイル: test_graph.py プロジェクト: markbetters/ozp-backend
    def test_graph_add_edit_one_vertex(self):
        graph = Graph()
        self.assertEqual(str(graph), 'Graph(vertices: 0, edges: 0)')

        added_vertex = graph.add_vertex('test_label', {'test_field': 1})

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label')
        self.assertEqual(added_vertex.get_property('test_field'), 1)

        added_vertex.set_property('test_field', 2)
        added_vertex = graph.get_vertex(1)
        self.assertEqual(added_vertex.get_property('test_field'), 2)

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
コード例 #8
0
    def test_pipeline_graph_vertex_while(self):
        graph = Graph()
        graph.add_vertex('test_label', {'test_field': 1})
        graph.add_vertex('test_label', {'test_field': 2})

        pipeline_test = pipeline.Pipeline(graph.get_vertices_iterator(),
                                          [pipes.GraphVertexPipe()])

        try:
            list_out = []
            while pipeline_test.has_next():
                current_object = pipeline_test.next()
                list_out.append(current_object.id)
        except recommend_utils.FastNoSuchElementException:
            # Ignore FastNoSuchElementException
            pass
        self.assertEqual(list_out, [1, 2])
コード例 #9
0
    def test_pipeline_graph_vertex_chain_dict_to_list(self):
        graph = Graph()
        graph.add_vertex('test_label', {'test_field': 8})
        graph.add_vertex('test_label', {'test_field': 10})
        graph.add_vertex('test_label', {'test_field': 12, 'time': 'now'})

        pipeline_test = pipeline.Pipeline(
            graph.get_vertices_iterator(),
            [pipes.GraphVertexPipe(),
             pipes.ElementPropertiesPipe()])
        output = [{
            'test_field': 8
        }, {
            'test_field': 10
        }, {
            'test_field': 12,
            'time': 'now'
        }]
        self.assertEqual(pipeline_test.to_list(), output)
コード例 #10
0
ファイル: test_graph.py プロジェクト: markbetters/ozp-backend
    def test_graph_three_vertices_simple(self):
        graph = Graph()
        vertex1 = graph.add_vertex('person', {'username': '******'},
                                   current_id=10)
        vertex2 = graph.add_vertex('listing', {'title': 'Skyzone1'},
                                   current_id=20)
        vertex3 = graph.add_vertex('listing', {'title': 'Skyzone2'},
                                   current_id=30)
        vertex1.add_edge('personListing', vertex2)
        vertex1.add_edge('personListing', vertex3)
        vertex1.add_edge('testListing', vertex3)

        self.assertEqual(str(graph), 'Graph(vertices: 3, edges: 3)')
        # Check Vertex 1
        self.assertEqual(len(vertex1.get_in_edges('personListing')), 0)
        self.assertEqual(
            [edge.label for edge in vertex1.get_in_edges('personListing')], [])
        self.assertEqual([
            edge.in_vertex.id for edge in vertex1.get_in_edges('personListing')
        ], [])

        self.assertEqual(len(vertex1.get_out_edges('personListing')), 2)
        self.assertEqual(len(vertex1.get_out_edges()), 3)
        self.assertEqual(
            [edge.label for edge in vertex1.get_out_edges('personListing')],
            ['personListing', 'personListing'])
        self.assertEqual([
            edge.out_vertex.id
            for edge in vertex1.get_out_edges('personListing')
        ], [20, 30])

        # Check Vertex 2
        self.assertEqual(len(vertex2.get_in_edges('personListing')), 1)
        self.assertEqual(
            [edge.label for edge in vertex2.get_in_edges('personListing')],
            ['personListing'])
        self.assertEqual([
            edge.in_vertex.id for edge in vertex2.get_in_edges('personListing')
        ], [10])

        self.assertEqual(len(vertex2.get_out_edges('personListing')), 0)
        self.assertEqual(
            [edge.label for edge in vertex2.get_out_edges('personListing')],
            [])
        self.assertEqual([
            edge.out_vertex.id
            for edge in vertex2.get_out_edges('personListing')
        ], [])

        # Check Vertex 3
        self.assertEqual(len(vertex3.get_in_edges('personListing')), 1)
        self.assertEqual(
            [edge.label for edge in vertex3.get_in_edges('personListing')],
            ['personListing'])
        self.assertEqual([
            edge.in_vertex.id for edge in vertex3.get_in_edges('personListing')
        ], [10])

        self.assertEqual(len(vertex3.get_out_edges('personListing')), 0)
        self.assertEqual(
            [edge.label for edge in vertex3.get_out_edges('personListing')],
            [])
        self.assertEqual([
            edge.out_vertex.id
            for edge in vertex3.get_out_edges('personListing')
        ], [])
コード例 #11
0
    def load_db_into_graph():
        """
        Load Django Database into graph

        Agency <--stewardedAgency--
        Agency <--agency--          Profile --bookmarked--> Listing --listingCategory--> Category
                                                                    --listingAgency--> Agency

        Steps:
            Load all Category
            Load all Agency
            Load all Listings
                Link to Agency
                Link to Category
            Load all Profiles
                Link to Agency
                Link bookmarked listings
        """
        graph = Graph()

        for category in models.Category.objects.all():
            data = {'title': category.title}
            added_vertex = graph.add_vertex('category',
                                            data,
                                            current_id='c-{}'.format(
                                                category.pk))

        for agency in models.Agency.objects.all():
            data = {'title': agency.title, 'short_name': agency.short_name}
            added_vertex = graph.add_vertex('agency',
                                            data,
                                            current_id='a-{}'.format(
                                                agency.pk))

        for listing in models.Listing.objects.all():
            data = {
                'title': listing.title,
                'description': listing.description,
                'is_private': listing.is_private,
                'security_marking': listing.security_marking,
                'is_enabled': listing.is_enabled,
                'is_deleted': listing.is_deleted,
                'is_featured': listing.is_featured,
                'approval_status': listing.approval_status
            }

            if listing.is_enabled and not listing.is_deleted and listing.approval_status == models.Listing.APPROVED:
                added_vertex = graph.add_vertex('listing',
                                                data,
                                                current_id='l-{}'.format(
                                                    listing.pk))

                # One Agency per listing
                current_agency = listing.agency
                added_vertex.add_edge(
                    'listingAgency',
                    graph.get_vertex('a-{}'.format(current_agency.pk)))

                # Many Categories per listing
                for current_category in listing.categories.all():
                    added_vertex.add_edge(
                        'listingCategory',
                        graph.get_vertex('c-{}'.format(current_category.pk)))

        for profile in models.Profile.objects.all():
            data = {
                'username': profile.user.username,
                'highest_role': profile.highest_role()
            }
            added_vertex = graph.add_vertex('profile',
                                            data,
                                            current_id='p-{}'.format(
                                                profile.pk))

            # Many Agencies per profile
            for current_agency in profile.organizations.all():
                added_vertex.add_edge(
                    'agency',
                    graph.get_vertex('a-{}'.format(current_agency.pk)))

            # Many stewardedAgency Agencies per profile
            for current_agency in profile.stewarded_organizations.all():
                added_vertex.add_edge(
                    'stewardedAgency',
                    graph.get_vertex('a-{}'.format(current_agency.pk)))

            for current_entry in models.ApplicationLibraryEntry.objects.filter(
                    owner=profile):
                current_listing = current_entry.listing
                data = {'folder_name': current_entry.folder}
                added_vertex.add_edge(
                    'bookmarked',
                    graph.get_vertex('l-{}'.format(current_listing.pk)), data)

        return graph
コード例 #12
0
    def load_sample_profile_listing_graph():
        graph = Graph()
        profile1 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-1')
        profile2 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-2')
        profile3 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-3')
        profile4 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-4')
        profile5 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-5')

        listing1 = graph.add_vertex('listing', {'title': 'listing1'},
                                    current_id='l-1')
        listing2 = graph.add_vertex('listing', {'title': 'listing2'},
                                    current_id='l-2')
        listing3 = graph.add_vertex('listing', {'title': 'listing3'},
                                    current_id='l-3')
        listing4 = graph.add_vertex('listing', {'title': 'listing4'},
                                    current_id='l-4')
        listing5 = graph.add_vertex('listing', {'title': 'listing5'},
                                    current_id='l-5')
        listing6 = graph.add_vertex('listing', {'title': 'listing6'},
                                    current_id='l-6')
        listing7 = graph.add_vertex('listing', {'title': 'listing7'},
                                    current_id='l-7')
        listing8 = graph.add_vertex('listing', {'title': 'listing8'},
                                    current_id='l-8')

        category1 = graph.add_vertex('category', {'title': 'category1'},
                                     current_id='c-1')
        category2 = graph.add_vertex('category', {'title': 'category2'},
                                     current_id='c-2')

        listing1.add_edge('listingCategory', category1)
        listing2.add_edge('listingCategory', category2)
        listing3.add_edge('listingCategory', category1)
        listing4.add_edge('listingCategory', category2)

        listing5.add_edge('listingCategory', category1)
        listing6.add_edge('listingCategory', category2)
        listing7.add_edge('listingCategory', category1)
        listing8.add_edge('listingCategory', category2)

        profile1.add_edge('bookmarked', listing1)
        profile1.add_edge('bookmarked', listing2)
        profile1.add_edge('bookmarked', listing3)

        profile2.add_edge('bookmarked', listing1)
        profile2.add_edge('bookmarked', listing4)
        profile2.add_edge('bookmarked', listing5)

        profile3.add_edge('bookmarked', listing1)
        profile3.add_edge('bookmarked', listing2)
        profile3.add_edge('bookmarked', listing5)
        profile3.add_edge('bookmarked', listing6)
        profile3.add_edge('bookmarked', listing8)

        profile4.add_edge('bookmarked', listing2)
        profile4.add_edge('bookmarked', listing7)

        profile5.add_edge('bookmarked', listing2)
        profile5.add_edge('bookmarked', listing3)

        return graph
コード例 #13
0
    def create_graph_template():
        graph = Graph()
        graph.add_vertex()

        return graph