/ src / python / txtai / graph / factory.py
factory.py
 1  """
 2  Factory module
 3  """
 4  
 5  from ..util import Resolver
 6  
 7  from .networkx import NetworkX
 8  from .rdbms import RDBMS
 9  
10  
11  class GraphFactory:
12      """
13      Methods to create graphs.
14      """
15  
16      @staticmethod
17      def create(config):
18          """
19          Create a Graph.
20  
21          Args:
22              config: graph configuration
23  
24          Returns:
25              Graph
26          """
27  
28          # Graph instance
29          graph = None
30          backend = config.get("backend", "networkx")
31  
32          # Create graph instance
33          if backend == "networkx":
34              graph = NetworkX(config)
35          elif backend == "rdbms":
36              graph = RDBMS(config)
37          else:
38              graph = GraphFactory.resolve(backend, config)
39  
40          # Store config back
41          config["backend"] = backend
42  
43          return graph
44  
45      @staticmethod
46      def resolve(backend, config):
47          """
48          Attempt to resolve a custom backend.
49  
50          Args:
51              backend: backend class
52              config: index configuration parameters
53  
54          Returns:
55              Graph
56          """
57  
58          try:
59              return Resolver()(backend)(config)
60          except Exception as e:
61              raise ImportError(f"Unable to resolve graph backend: '{backend}'") from e