|
|
@@ -262,23 +262,6 @@ def tool_get_entities_by_type(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
return run_sparql(query)
|
|
|
|
|
|
|
|
|
-def tool_cycle_plants(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
- cycle_uri = input_data.get("cycle_uri")
|
|
|
- if not cycle_uri:
|
|
|
- raise ValueError("Missing 'cycle_uri' field")
|
|
|
- limit = int(input_data.get("limit", 50))
|
|
|
- limit = min(max(limit, 1), SPARQL_MAX_LIMIT)
|
|
|
- query = f"""
|
|
|
- SELECT ?plant ?plantLabel ?parent WHERE {{
|
|
|
- ?plant <{IN_CYCLE}> <{cycle_uri}> .
|
|
|
- OPTIONAL {{ ?plant rdfs:label ?plantLabel }}
|
|
|
- OPTIONAL {{ ?plant <{CLONE_OF}> ?parent }}
|
|
|
- }}
|
|
|
- LIMIT {limit}
|
|
|
- """
|
|
|
- return run_sparql(query)
|
|
|
-
|
|
|
-
|
|
|
def tool_get_predicates_for_subject(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
subject_uri = input_data.get("subject_uri")
|
|
|
if not subject_uri:
|
|
|
@@ -594,34 +577,6 @@ def tool_batch_insert(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
return {**result, "query": query}
|
|
|
|
|
|
|
|
|
-def tool_reassign_cycle(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
- subject = input_data.get("subject")
|
|
|
- new_cycle = input_data.get("new_cycle")
|
|
|
- old_cycle = input_data.get("old_cycle")
|
|
|
- graph = input_data.get("graph") or GRAPH_URI
|
|
|
-
|
|
|
- if not subject or not new_cycle:
|
|
|
- raise ValueError("Provide 'subject' and 'new_cycle' fields")
|
|
|
-
|
|
|
- if old_cycle:
|
|
|
- delete_clause = f"<{subject}> <{IN_CYCLE}> <{old_cycle}> ."
|
|
|
- where_clause = delete_clause
|
|
|
- update_query = f"""
|
|
|
- WITH <{graph}>
|
|
|
- DELETE {{ {delete_clause} }}
|
|
|
- INSERT {{ <{subject}> <{IN_CYCLE}> <{new_cycle}> . }}
|
|
|
- WHERE {{ {where_clause} }}
|
|
|
- """
|
|
|
- else:
|
|
|
- update_query = f"""
|
|
|
- WITH <{graph}>
|
|
|
- INSERT {{ <{subject}> <{IN_CYCLE}> <{new_cycle}> . }}
|
|
|
- WHERE {{ }}
|
|
|
- """
|
|
|
-
|
|
|
- return run_sparql_update(update_query)
|
|
|
-
|
|
|
-
|
|
|
def tool_insert_triple(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
subject = input_data.get("subject")
|
|
|
predicate = input_data.get("predicate")
|
|
|
@@ -659,40 +614,12 @@ def tool_insert_triple(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
return {**result, "query": update_query}
|
|
|
|
|
|
|
|
|
-def tool_load_examples(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
- if not ALLOW_EXAMPLE_LOAD:
|
|
|
- raise HTTPException(status_code=403, detail="Example loading is disabled")
|
|
|
-
|
|
|
- files = input_data.get("files") or []
|
|
|
- if isinstance(files, str):
|
|
|
- files = [files]
|
|
|
- if not files:
|
|
|
- files = [p.name for p in EXAMPLES_DIR.glob("*.ttl")]
|
|
|
-
|
|
|
- graph = input_data.get("graph") or EXAMPLE_GRAPH
|
|
|
- results = []
|
|
|
-
|
|
|
- for filename in files:
|
|
|
- file_path = (EXAMPLES_DIR / filename).resolve()
|
|
|
- if not file_path.exists():
|
|
|
- raise HTTPException(status_code=400, detail=f"Missing example file: {filename}")
|
|
|
- if EXAMPLES_DIR not in file_path.parents:
|
|
|
- raise HTTPException(status_code=400, detail="Invalid example file path")
|
|
|
- ttl_text = file_path.read_text(encoding="utf-8")
|
|
|
- update_query = ttl_to_sparql_insert(ttl_text, graph)
|
|
|
- run_sparql_update(update_query)
|
|
|
- results.append({"file": filename, "graph": graph})
|
|
|
-
|
|
|
- return {"loaded": results}
|
|
|
-
|
|
|
-
|
|
|
# --- TOOL REGISTRY ---
|
|
|
TOOLS = {
|
|
|
"sparql_query": tool_sparql_query,
|
|
|
"list_graphs": tool_list_graphs,
|
|
|
"search_label": tool_search_label,
|
|
|
"get_entities_by_type": tool_get_entities_by_type,
|
|
|
- "cycle_plants": tool_cycle_plants,
|
|
|
"get_predicates_for_subject": tool_get_predicates_for_subject,
|
|
|
"get_labels_for_subject": tool_get_labels_for_subject,
|
|
|
"traverse_property": tool_traverse_property,
|
|
|
@@ -704,9 +631,7 @@ TOOLS = {
|
|
|
"path_traverse": tool_path_traverse,
|
|
|
"property_usage_statistics": tool_property_usage_statistics,
|
|
|
"batch_insert": tool_batch_insert,
|
|
|
- "reassign_cycle": tool_reassign_cycle,
|
|
|
"insert_triple": tool_insert_triple,
|
|
|
- "load_examples": tool_load_examples,
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -767,7 +692,6 @@ TOOL_DOCS = {
|
|
|
"batch_insert": "Insert multiple triples or TTL at once with a single guarded update.",
|
|
|
"reassign_cycle": "Move a subject to another production cycle by updating its inCycle link.",
|
|
|
"insert_triple": "Insert a single triple (useful for debugging updates).",
|
|
|
- "load_examples": "Load Turtle examples from the local examples/ directory into a graph.",
|
|
|
}
|
|
|
|
|
|
|