|
1
|
|
|
import networkx as nx |
|
2
|
|
|
|
|
3
|
|
|
import graphinate |
|
4
|
|
|
|
|
5
|
|
|
# import graphinate.modeling |
|
6
|
|
|
from graphinate import GraphModel |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def polygonal_graph_edges(edges_count: int): |
|
10
|
|
|
for i in range(1, edges_count): |
|
11
|
|
|
yield {'source': i, 'target': i + 1} |
|
12
|
|
|
yield {'source': edges_count, 'target': 1} |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def polygonal_graph_model(name: str, number_of_sides: int) -> graphinate.GraphModel: |
|
16
|
|
|
""" |
|
17
|
|
|
Create a polygonal graph model. |
|
18
|
|
|
|
|
19
|
|
|
Args: |
|
20
|
|
|
name (str): The Graph's name. |
|
21
|
|
|
number_of_sides (int): Number of sides in the polygon. |
|
22
|
|
|
|
|
23
|
|
|
Returns: |
|
24
|
|
|
GraphModel: A graph model representing a polygonal graph. |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
|
|
# Define GraphModel |
|
28
|
|
|
graph_model: GraphModel = graphinate.model(name) |
|
29
|
|
|
|
|
30
|
|
|
# Register edges supplier function |
|
31
|
|
|
@graph_model.edge() |
|
32
|
|
|
def edge(): |
|
33
|
|
|
yield from polygonal_graph_edges(number_of_sides) |
|
34
|
|
|
|
|
35
|
|
|
return graph_model |
|
36
|
|
|
|
|
37
|
|
|
# instantiated here to be used to cli serving |
|
38
|
|
|
model = polygonal_graph_model("Octagonal Graph", 8) |
|
39
|
|
|
|
|
40
|
|
|
if __name__ == '__main__': |
|
41
|
|
|
|
|
42
|
|
|
# 1. Define Graph Builder |
|
43
|
|
|
builder = graphinate.builders.NetworkxBuilder(model) |
|
44
|
|
|
|
|
45
|
|
|
# Then |
|
46
|
|
|
# 2. Build the Graph object |
|
47
|
|
|
graph: nx.Graph = builder.build() |
|
48
|
|
|
|
|
49
|
|
|
# Then |
|
50
|
|
|
# 3. Option A - Output to console |
|
51
|
|
|
print(graph) |
|
52
|
|
|
|
|
53
|
|
|
# Or |
|
54
|
|
|
# 3. Option B - Output as a plot |
|
55
|
|
|
graphinate.renderers.matplotlib.plot(graph) |
|
56
|
|
|
|
|
57
|
|
|
# Alternatively, |
|
58
|
|
|
# 4. Define a GraphQL Builder |
|
59
|
|
|
builder = graphinate.builders.GraphQLBuilder(model) |
|
60
|
|
|
|
|
61
|
|
|
schema = builder.build() |
|
62
|
|
|
|
|
63
|
|
|
graphinate.graphql.server(schema, port=9077) |
|
64
|
|
|
|