forked from pippinlovesyou/graphista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
177 lines (147 loc) · 5.58 KB
/
console.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
"""
Console interface for IngestionEngine
"""
import os
import logging
from ingestion_engine.ingestion_engine import IngestionEngine
from graphrouter import Ontology
from graphrouter.core_ontology import create_core_ontology, extend_ontology
def setup_ontology():
"""Create ontology with proper type definitions"""
base_ontology = create_core_ontology()
# Add any additional custom types if needed (for now, an empty extension)
extensions = Ontology()
return extend_ontology(base_ontology, extensions)
def print_ontology(engine):
"""Display current ontology"""
print("\nNode Types:")
for label, details in engine.ontology.node_types.items():
print(f"\n{label}:")
print(" Properties:", details['properties'])
print(" Required:", details['required'])
print("\nEdge Types:")
for label, details in engine.ontology.edge_types.items():
print(f"\n{label}:")
print(" Properties:", details['properties'])
print(" Required:", details['required'])
def add_node_type(engine):
"""Add a new node type to ontology"""
label = input("Enter node type label: ")
properties = {}
while True:
prop = input("Enter property name (or 'done' to finish): ")
if prop.lower() == 'done':
break
prop_type = input(f"Enter type for {prop} (str/int/float/bool): ")
properties[prop] = prop_type
required = []
while True:
req = input("Enter required property name (or 'done' to finish): ")
if req.lower() == 'done':
break
if req in properties:
required.append(req)
else:
print("Property must be defined first!")
engine.ontology.add_node_type(label, properties, required)
print(f"Added node type: {label}")
def add_edge_type(engine):
"""Add a new edge type to ontology"""
label = input("Enter edge type label: ")
properties = {}
while True:
prop = input("Enter property name (or 'done' to finish): ")
if prop.lower() == 'done':
break
prop_type = input(f"Enter type for {prop} (str/int/float/bool): ")
properties[prop] = prop_type
required = []
while True:
req = input("Enter required property name (or 'done' to finish): ")
if req.lower() == 'done':
break
if req in properties:
required.append(req)
else:
print("Property must be defined first!")
engine.ontology.add_edge_type(label, properties, required)
print(f"Added edge type: {label}")
def main():
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Determine database file path
db_path = os.path.join(os.getcwd(), "test_graph.json")
print(f"Using local database file: {db_path}")
# Setup ontology
ontology = setup_ontology()
# Initialize the ingestion engine.
# Note: router_config is passed with a "db_path" key only;
# our engine will detect this and assume a local backend.
try:
engine = IngestionEngine(
router_config={"db_path": db_path},
default_ontology=ontology,
auto_extract_structured_data=True
)
except Exception as e:
print(f"Failed to initialize IngestionEngine: {e}")
return
while True:
print("\nIngestion Engine Test Console")
print("1. Upload File")
print("2. Search and Store")
print("3. Handle Webhook")
print("4. View Ontology")
print("5. Add Node Type")
print("6. Add Edge Type")
print("7. Exit")
choice = input("\nEnter your choice (1-7): ")
try:
if choice == "1":
file_path = input("Enter file path: ")
source_name = input("Enter data source name: ")
try:
node_id = engine.upload_file(file_path, source_name)
print(f"File uploaded successfully. Node ID: {node_id}")
except Exception as e:
logger.error(f"Error uploading file: {e}")
elif choice == "2":
query = input("Enter search query: ")
try:
engine.search_and_store_results(query)
print("Search results stored successfully")
except Exception as e:
logger.error(f"Error in search: {e}")
elif choice == "3":
try:
webhook_data = {
"event": input("Enter event type: "),
"payload": input("Enter payload: ")
}
source = input("Enter webhook source name: ")
engine.handle_webhook(webhook_data, source)
print("Webhook handled successfully")
except Exception as e:
logger.error(f"Error handling webhook: {e}")
elif choice == "4":
print_ontology(engine)
elif choice == "5":
add_node_type(engine)
elif choice == "6":
add_edge_type(engine)
elif choice == "7":
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
except Exception as e:
logger.error(f"Unexpected error: {e}")
if __name__ == "__main__":
try:
main()
finally:
# Ensure the database is saved when exiting
if 'engine' in globals() and hasattr(engine, 'db'):
engine.db.disconnect()