naas-abi 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. naas_abi/__init__.py +35 -0
  2. naas_abi/agents/AbiAgent.py +442 -0
  3. naas_abi/agents/AbiAgent_test.py +157 -0
  4. naas_abi/agents/EntitytoSPARQLAgent.py +952 -0
  5. naas_abi/agents/EntitytoSPARQLAgent_test.py +66 -0
  6. naas_abi/agents/KnowledgeGraphBuilderAgent.py +321 -0
  7. naas_abi/agents/KnowledgeGraphBuilderAgent_test.py +86 -0
  8. naas_abi/agents/OntologyEngineerAgent.py +115 -0
  9. naas_abi/agents/OntologyEngineerAgent_test.py +42 -0
  10. naas_abi/apps/oxigraph_admin/main.py +392 -0
  11. naas_abi/apps/oxigraph_admin/terminal_style.py +151 -0
  12. naas_abi/apps/sparql_terminal/main.py +68 -0
  13. naas_abi/apps/sparql_terminal/terminal_style.py +236 -0
  14. naas_abi/apps/terminal_agent/main.py +553 -0
  15. naas_abi/apps/terminal_agent/terminal_style.py +175 -0
  16. naas_abi/cli.py +714 -0
  17. naas_abi/mappings.py +83 -0
  18. naas_abi/models/airgap_gemma.py +220 -0
  19. naas_abi/models/airgap_qwen.py +24 -0
  20. naas_abi/models/default.py +23 -0
  21. naas_abi/models/gpt_4_1.py +25 -0
  22. naas_abi/pipelines/AIAgentOntologyGenerationPipeline.py +635 -0
  23. naas_abi/pipelines/AIAgentOntologyGenerationPipeline_test.py +133 -0
  24. naas_abi/pipelines/AddIndividualPipeline.py +215 -0
  25. naas_abi/pipelines/AddIndividualPipeline_test.py +66 -0
  26. naas_abi/pipelines/InsertDataSPARQLPipeline.py +197 -0
  27. naas_abi/pipelines/InsertDataSPARQLPipeline_test.py +96 -0
  28. naas_abi/pipelines/MergeIndividualsPipeline.py +245 -0
  29. naas_abi/pipelines/MergeIndividualsPipeline_test.py +98 -0
  30. naas_abi/pipelines/RemoveIndividualPipeline.py +166 -0
  31. naas_abi/pipelines/RemoveIndividualPipeline_test.py +58 -0
  32. naas_abi/pipelines/UpdateCommercialOrganizationPipeline.py +198 -0
  33. naas_abi/pipelines/UpdateDataPropertyPipeline.py +175 -0
  34. naas_abi/pipelines/UpdateLegalNamePipeline.py +107 -0
  35. naas_abi/pipelines/UpdateLinkedInPagePipeline.py +179 -0
  36. naas_abi/pipelines/UpdatePersonPipeline.py +184 -0
  37. naas_abi/pipelines/UpdateSkillPipeline.py +118 -0
  38. naas_abi/pipelines/UpdateTickerPipeline.py +104 -0
  39. naas_abi/pipelines/UpdateWebsitePipeline.py +106 -0
  40. naas_abi/triggers.py +131 -0
  41. naas_abi/workflows/AgentRecommendationWorkflow.py +321 -0
  42. naas_abi/workflows/AgentRecommendationWorkflow_test.py +160 -0
  43. naas_abi/workflows/ArtificialAnalysisWorkflow.py +337 -0
  44. naas_abi/workflows/ArtificialAnalysisWorkflow_test.py +57 -0
  45. naas_abi/workflows/ConvertOntologyGraphToYamlWorkflow.py +210 -0
  46. naas_abi/workflows/ConvertOntologyGraphToYamlWorkflow_test.py +78 -0
  47. naas_abi/workflows/CreateClassOntologyYamlWorkflow.py +208 -0
  48. naas_abi/workflows/CreateClassOntologyYamlWorkflow_test.py +65 -0
  49. naas_abi/workflows/CreateIndividualOntologyYamlWorkflow.py +183 -0
  50. naas_abi/workflows/CreateIndividualOntologyYamlWorkflow_test.py +86 -0
  51. naas_abi/workflows/ExportGraphInstancesToExcelWorkflow.py +450 -0
  52. naas_abi/workflows/ExportGraphInstancesToExcelWorkflow_test.py +33 -0
  53. naas_abi/workflows/GetObjectPropertiesFromClassWorkflow.py +385 -0
  54. naas_abi/workflows/GetObjectPropertiesFromClassWorkflow_test.py +57 -0
  55. naas_abi/workflows/GetSubjectGraphWorkflow.py +84 -0
  56. naas_abi/workflows/GetSubjectGraphWorkflow_test.py +71 -0
  57. naas_abi/workflows/SearchIndividualWorkflow.py +190 -0
  58. naas_abi/workflows/SearchIndividualWorkflow_test.py +98 -0
  59. naas_abi-1.0.0.dist-info/METADATA +9 -0
  60. naas_abi-1.0.0.dist-info/RECORD +62 -0
  61. naas_abi-1.0.0.dist-info/WHEEL +5 -0
  62. naas_abi-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,208 @@
1
+ from dataclasses import dataclass
2
+ from enum import Enum
3
+ from typing import Annotated, Any, Union
4
+
5
+ from fastapi import APIRouter
6
+ from langchain_core.tools import BaseTool, StructuredTool
7
+ from naas_abi.utils.SPARQL import get_class_uri_from_individual_uri, results_to_list
8
+ from naas_abi.workflows.ConvertOntologyGraphToYamlWorkflow import (
9
+ ConvertOntologyGraphToYamlWorkflow,
10
+ ConvertOntologyGraphToYamlWorkflowConfiguration,
11
+ ConvertOntologyGraphToYamlWorkflowParameters,
12
+ )
13
+ from naas_abi_core import logger, services
14
+ from naas_abi_core.services.triple_store.TripleStorePorts import (
15
+ ITripleStoreService,
16
+ OntologyEvent,
17
+ )
18
+ from naas_abi_core.workflow import Workflow, WorkflowConfiguration
19
+ from naas_abi_core.workflow.workflow import WorkflowParameters
20
+ from pydantic import Field
21
+ from rdflib import OWL, RDF, RDFS, Graph, Literal, URIRef
22
+
23
+
24
+ @dataclass
25
+ class CreateClassOntologyYamlWorkflowConfiguration(WorkflowConfiguration):
26
+ """Configuration for CreateOntologyYAML workflow.
27
+
28
+ Attributes:
29
+ naas_integration_config (NaasIntegrationConfiguration): Configuration for the Naas integration
30
+ """
31
+
32
+ triple_store: ITripleStoreService
33
+ convert_ontology_graph_config: ConvertOntologyGraphToYamlWorkflowConfiguration
34
+
35
+
36
+ class CreateClassOntologyYamlWorkflowParameters(WorkflowParameters):
37
+ """Parameters for CreateOntologyYAML workflow execution.
38
+
39
+ Attributes:
40
+ ontology_name (str): The name of the ontology store to use
41
+ label (str): The label of the ontology
42
+ description (str): The description of the ontology
43
+ logo_url (str): The URL of the ontology logo
44
+ level (str): The level of the ontology (e.g., 'TOP_LEVEL', 'MID_LEVEL', 'DOMAIN', 'USE_CASE')
45
+ display_relations_names (bool): Whether to display relation names in the visualization
46
+ """
47
+
48
+ class_uri: Annotated[
49
+ str, Field(..., description="The URI of the class to convert to YAML")
50
+ ]
51
+
52
+
53
+ class CreateClassOntologyYamlWorkflow(Workflow):
54
+ """Workflow for converting ontology files to YAML and pushing them to a Naas workspace."""
55
+
56
+ __configuration: CreateClassOntologyYamlWorkflowConfiguration
57
+
58
+ def __init__(self, configuration: CreateClassOntologyYamlWorkflowConfiguration):
59
+ self.__configuration = configuration
60
+ self.__convert_ontology_graph_workflow = ConvertOntologyGraphToYamlWorkflow(
61
+ self.__configuration.convert_ontology_graph_config
62
+ )
63
+
64
+ def trigger(
65
+ self, event: OntologyEvent, triple: tuple[Any, Any, Any]
66
+ ) -> Union[str, None]:
67
+ s, p, o = triple
68
+ # logger.debug(f"==> Triggering Create Class Ontology YAML Workflow: {s} {p} {o}")
69
+ if (
70
+ str(event) != str(OntologyEvent.INSERT)
71
+ or not str(o).startswith("http")
72
+ or str(o) == "http://www.w3.org/2002/07/owl#NamedIndividual"
73
+ ):
74
+ # logger.debug(f"==> Skipping class ontology YAML creation for {s} {p} {o}")
75
+ return None
76
+
77
+ # Get class type from URI
78
+ class_uri = get_class_uri_from_individual_uri(s)
79
+ class_uri_triggers = [
80
+ "https://www.commoncoreontologies.org/ont00001262", # Person
81
+ "https://www.commoncoreontologies.org/ont00000443", # Commercial Organization
82
+ ]
83
+ if class_uri in class_uri_triggers:
84
+ logger.debug(
85
+ f"==> Creating class ontology YAML for {class_uri} ({s} {p} {o})"
86
+ )
87
+ return self.graph_to_yaml(
88
+ CreateClassOntologyYamlWorkflowParameters(class_uri=class_uri)
89
+ )
90
+ return None
91
+
92
+ def graph_to_yaml(
93
+ self, parameters: CreateClassOntologyYamlWorkflowParameters
94
+ ) -> str:
95
+ # Initialize graph
96
+ graph = Graph()
97
+ graph.bind("abi", "http://ontology.naas.ai/abi/")
98
+
99
+ # Get label and description from class uri
100
+ query = f"""
101
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
102
+ PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
103
+ SELECT ?label ?definition
104
+ WHERE {{
105
+ <{parameters.class_uri}> rdfs:label ?label .
106
+ <{parameters.class_uri}> skos:definition ?definition .
107
+ }}
108
+ """
109
+ results = services.triple_store_service.query(query)
110
+ result_list = results_to_list(results)
111
+ if result_list:
112
+ ontology_label = result_list[0]["label"]
113
+ ontology_description = result_list[0]["definition"]
114
+ else:
115
+ ontology_label = ""
116
+ ontology_description = ""
117
+
118
+ # Get triples from class uri
119
+ query = f"""
120
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
121
+
122
+ SELECT DISTINCT ?subject ?predicate ?object
123
+ WHERE {{
124
+ ?subject a <{parameters.class_uri}> .
125
+ ?subject ?predicate ?object .
126
+ }}
127
+ ORDER BY ?subject ?predicate
128
+ """
129
+ results = services.triple_store_service.query(query)
130
+ list_uri = []
131
+ # Add triples to graph
132
+ for row in results:
133
+ subject = URIRef(row.get("subject"))
134
+ predicate = URIRef(row.get("predicate"))
135
+ obj = row.get("object")
136
+
137
+ # Add triple to graph
138
+ if isinstance(obj, str) and obj.startswith("http://ontology.naas.ai/abi/"):
139
+ obj = URIRef(obj)
140
+ list_uri.append(obj)
141
+ else:
142
+ obj = Literal(obj)
143
+ graph.add((subject, predicate, obj))
144
+
145
+ # Get all object properties label and type
146
+ if len(list_uri) > 0:
147
+ # Filter only ABI URIs
148
+ uri_filter = (
149
+ "(" + " || ".join([f"?object = <{uri}>" for uri in list_uri]) + ")"
150
+ )
151
+ query = f"""
152
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
153
+
154
+ SELECT DISTINCT ?object ?label ?type
155
+ WHERE {{
156
+ ?object rdfs:label ?label .
157
+ ?object rdf:type ?type .
158
+ FILTER {uri_filter}
159
+ }}
160
+ ORDER BY ?object
161
+ """
162
+ results = services.triple_store_service.query(query)
163
+
164
+ # Add object properties to graph
165
+ for row in results:
166
+ graph.add(
167
+ (URIRef(row.get("object")), RDF.type, URIRef(row.get("type")))
168
+ )
169
+ graph.add((URIRef(row.get("object")), RDF.type, OWL.NamedIndividual))
170
+ graph.add(
171
+ (URIRef(row.get("object")), RDFS.label, Literal(row.get("label")))
172
+ )
173
+
174
+ # Convert graph to YAML & push to Naas workspace
175
+ ontology_id = self.__convert_ontology_graph_workflow.graph_to_yaml(
176
+ ConvertOntologyGraphToYamlWorkflowParameters(
177
+ graph=graph.serialize(format="turtle"),
178
+ label=ontology_label,
179
+ description=ontology_description,
180
+ )
181
+ )
182
+ return ontology_id
183
+
184
+ def as_tools(self) -> list[BaseTool]:
185
+ """Returns a list of LangChain tools for this workflow."""
186
+ return [
187
+ StructuredTool(
188
+ name="ontology_create_class_yaml",
189
+ description="Create an ontology class YAML and push it to Naas workspace.",
190
+ func=lambda **kwargs: self.graph_to_yaml(
191
+ CreateClassOntologyYamlWorkflowParameters(**kwargs)
192
+ ),
193
+ args_schema=CreateClassOntologyYamlWorkflowParameters,
194
+ )
195
+ ]
196
+
197
+ def as_api(
198
+ self,
199
+ router: APIRouter,
200
+ route_name: str = "",
201
+ name: str = "",
202
+ description: str = "",
203
+ description_stream: str = "",
204
+ tags: list[str | Enum] | None = None,
205
+ ) -> None:
206
+ if tags is None:
207
+ tags = []
208
+ return None
@@ -0,0 +1,65 @@
1
+ import pytest
2
+ from naas_abi import secret, services
3
+ from naas_abi.workflows.ConvertOntologyGraphToYamlWorkflow import (
4
+ ConvertOntologyGraphToYamlWorkflowConfiguration,
5
+ )
6
+ from naas_abi.workflows.CreateClassOntologyYamlWorkflow import (
7
+ CreateClassOntologyYamlWorkflow,
8
+ CreateClassOntologyYamlWorkflowConfiguration,
9
+ CreateClassOntologyYamlWorkflowParameters,
10
+ )
11
+ from naas_abi_marketplace.applications.naas.integrations.NaasIntegration import (
12
+ NaasIntegrationConfiguration,
13
+ )
14
+
15
+
16
+ @pytest.fixture
17
+ def workflow() -> CreateClassOntologyYamlWorkflow:
18
+ return CreateClassOntologyYamlWorkflow(
19
+ CreateClassOntologyYamlWorkflowConfiguration(
20
+ services.triple_store_service,
21
+ ConvertOntologyGraphToYamlWorkflowConfiguration(
22
+ NaasIntegrationConfiguration(api_key=secret.get("NAAS_API_KEY"))
23
+ ),
24
+ )
25
+ )
26
+
27
+
28
+ def test_create_class_ontology_yaml_workflow(workflow: CreateClassOntologyYamlWorkflow):
29
+ import time
30
+ from uuid import uuid4
31
+
32
+ from naas_abi import services
33
+ from rdflib import OWL, RDF, RDFS, Graph, Literal, Namespace, URIRef
34
+
35
+ ABI = Namespace("http://ontology.naas.ai/abi/")
36
+
37
+ graph = Graph()
38
+ uri = ABI[str(uuid4())]
39
+ graph.add(
40
+ (uri, RDF.type, URIRef("https://www.commoncoreontologies.org/ont00000443"))
41
+ )
42
+ graph.add((uri, RDF.type, OWL.NamedIndividual))
43
+ graph.add((uri, RDFS.label, Literal("Naas.ai")))
44
+ graph.add(
45
+ (
46
+ uri,
47
+ ABI.logo,
48
+ Literal(
49
+ "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ9gXMaBLQZ39W6Pk53PRuzFjUvv_6lLRWPoQ&s"
50
+ ),
51
+ )
52
+ )
53
+ services.triple_store_service.insert(graph)
54
+ time.sleep(3)
55
+
56
+ # Parameters
57
+ class_uri = "https://www.commoncoreontologies.org/ont00001262"
58
+
59
+ # Run workflow
60
+ ontology_id = workflow.graph_to_yaml(
61
+ CreateClassOntologyYamlWorkflowParameters(class_uri=class_uri)
62
+ )
63
+ assert ontology_id is not None, ontology_id
64
+ assert ontology_id != "", ontology_id
65
+ assert isinstance(ontology_id, str), ontology_id
@@ -0,0 +1,183 @@
1
+ from dataclasses import dataclass
2
+ from enum import Enum
3
+ from typing import Annotated, Any, Union
4
+
5
+ from fastapi import APIRouter
6
+ from langchain_core.tools import BaseTool, StructuredTool
7
+ from naas_abi import ABIModule
8
+ from naas_abi.workflows.ConvertOntologyGraphToYamlWorkflow import (
9
+ ConvertOntologyGraphToYamlWorkflow,
10
+ ConvertOntologyGraphToYamlWorkflowConfiguration,
11
+ ConvertOntologyGraphToYamlWorkflowParameters,
12
+ )
13
+ from naas_abi_core.services.triple_store.TripleStorePorts import (
14
+ ITripleStoreService,
15
+ OntologyEvent,
16
+ )
17
+ from naas_abi_core.utils.Graph import URI_REGEX
18
+ from naas_abi_core.utils.SPARQL import SPARQLUtils
19
+ from naas_abi_core.workflow import Workflow, WorkflowConfiguration
20
+ from naas_abi_core.workflow.workflow import WorkflowParameters
21
+ from pydantic import Field
22
+ from rdflib import RDFS, Graph, Literal, URIRef
23
+
24
+
25
+ @dataclass
26
+ class CreateIndividualOntologyYamlWorkflowConfiguration(WorkflowConfiguration):
27
+ """Configuration for CreateOntologyYAML workflow.
28
+
29
+ Attributes:
30
+ naas_integration_config (NaasIntegrationConfiguration): Configuration for the Naas integration
31
+ """
32
+
33
+ triple_store: ITripleStoreService
34
+ convert_ontology_graph_config: ConvertOntologyGraphToYamlWorkflowConfiguration
35
+
36
+
37
+ class CreateIndividualOntologyYamlWorkflowParameters(WorkflowParameters):
38
+ """Parameters for CreateOntologyYAML workflow execution.
39
+
40
+ Attributes:
41
+ ontology_name (str): The name of the ontology store to use
42
+ label (str): The label of the ontology
43
+ description (str): The description of the ontology
44
+ logo_url (str): The URL of the ontology logo
45
+ level (str): The level of the ontology (e.g., 'TOP_LEVEL', 'MID_LEVEL', 'DOMAIN', 'USE_CASE')
46
+ display_relations_names (bool): Whether to display relation names in the visualization
47
+ """
48
+
49
+ individual_uri: Annotated[
50
+ str,
51
+ Field(
52
+ ...,
53
+ description="The URI of the individual to convert to YAML",
54
+ pattern=URI_REGEX,
55
+ ),
56
+ ]
57
+ depth: Annotated[
58
+ int,
59
+ Field(
60
+ description="The depth of the subject graph to get. 1 means the individual and its direct properties, 2 means the individual and its direct properties and the properties of the properties, etc."
61
+ ),
62
+ ] = 2
63
+
64
+
65
+ class CreateIndividualOntologyYamlWorkflow(Workflow):
66
+ """Workflow for converting ontology files to YAML and pushing them to a Naas workspace."""
67
+
68
+ __configuration: CreateIndividualOntologyYamlWorkflowConfiguration
69
+ __sparql_utils: SPARQLUtils
70
+
71
+ def __init__(
72
+ self, configuration: CreateIndividualOntologyYamlWorkflowConfiguration
73
+ ):
74
+ self.__configuration = configuration
75
+ self.__convert_ontology_graph_workflow = ConvertOntologyGraphToYamlWorkflow(
76
+ self.__configuration.convert_ontology_graph_config
77
+ )
78
+ self.__sparql_utils: SPARQLUtils = SPARQLUtils(
79
+ ABIModule.get_instance().engine.services.triple_store
80
+ )
81
+
82
+ def trigger(
83
+ self, event: OntologyEvent, triple: tuple[Any, Any, Any]
84
+ ) -> Union[str, None]:
85
+ s, p, o = triple
86
+ if (
87
+ str(event) != str(OntologyEvent.INSERT)
88
+ or not str(s).startswith("http://ontology.naas.ai/abi/")
89
+ or not str(o).startswith("http://ontology.naas.ai/abi/")
90
+ ):
91
+ return None
92
+
93
+ # Get class type from URI
94
+ class_uri = self.__sparql_utils.get_class_uri_from_individual_uri(s)
95
+ class_uri_triggers = [
96
+ "https://www.commoncoreontologies.org/ont00001262", # Person
97
+ "https://www.commoncoreontologies.org/ont00000443", # Commercial Organization
98
+ ]
99
+ if class_uri in class_uri_triggers:
100
+ return self.graph_to_yaml(
101
+ CreateIndividualOntologyYamlWorkflowParameters(
102
+ individual_uri=s, depth=2
103
+ )
104
+ )
105
+ return None
106
+
107
+ def graph_to_yaml(
108
+ self, parameters: CreateIndividualOntologyYamlWorkflowParameters
109
+ ) -> Union[str, None]:
110
+ # Create individual graph
111
+ graph = self.__sparql_utils.get_subject_graph(
112
+ parameters.individual_uri, parameters.depth
113
+ )
114
+
115
+ # Get label from individual URI
116
+ ontology_id = None
117
+ ontology_label = ""
118
+ ontology_description = ""
119
+ ontology_logo_url = ""
120
+ new_ontology = True
121
+ for s, p, o in graph:
122
+ if s == URIRef(parameters.individual_uri) and p == RDFS.label:
123
+ ontology_label = str(o)
124
+ ontology_description = f"{ontology_label} Ontology"
125
+ if (
126
+ s == URIRef(parameters.individual_uri)
127
+ and str(p) == "http://ontology.naas.ai/abi/logo"
128
+ ):
129
+ ontology_logo_url = str(o)
130
+ if (
131
+ s == URIRef(parameters.individual_uri)
132
+ and str(p) == "http://ontology.naas.ai/abi/naas_ontology_id"
133
+ ):
134
+ ontology_id = str(o)
135
+ new_ontology = False
136
+
137
+ # Convert graph to YAML & push to Naas workspace
138
+ ontology_id = self.__convert_ontology_graph_workflow.graph_to_yaml(
139
+ ConvertOntologyGraphToYamlWorkflowParameters(
140
+ graph=graph.serialize(format="turtle"),
141
+ ontology_id=ontology_id,
142
+ label=ontology_label,
143
+ description=ontology_description,
144
+ logo_url=ontology_logo_url,
145
+ )
146
+ )
147
+ if new_ontology:
148
+ graph_insert = Graph()
149
+ graph_insert.add(
150
+ (
151
+ URIRef(parameters.individual_uri),
152
+ URIRef("http://ontology.naas.ai/abi/naas_ontology_id"),
153
+ Literal(ontology_id),
154
+ )
155
+ )
156
+ self.__configuration.triple_store.insert(graph_insert)
157
+ return ontology_id
158
+
159
+ def as_tools(self) -> list[BaseTool]:
160
+ """Returns a list of LangChain tools for this workflow."""
161
+ return [
162
+ StructuredTool(
163
+ name="create_individual_ontology_yaml",
164
+ description="Create or Update a YAML ontology from an individual/instance in triple store and push it to Naas workspace.",
165
+ func=lambda **kwargs: self.graph_to_yaml(
166
+ CreateIndividualOntologyYamlWorkflowParameters(**kwargs)
167
+ ),
168
+ args_schema=CreateIndividualOntologyYamlWorkflowParameters,
169
+ )
170
+ ]
171
+
172
+ def as_api(
173
+ self,
174
+ router: APIRouter,
175
+ route_name: str = "",
176
+ name: str = "",
177
+ description: str = "",
178
+ description_stream: str = "",
179
+ tags: list[str | Enum] | None = None,
180
+ ) -> None:
181
+ if tags is None:
182
+ tags = []
183
+ return None
@@ -0,0 +1,86 @@
1
+ import pytest
2
+ from naas_abi import secret, services
3
+ from naas_abi.workflows.ConvertOntologyGraphToYamlWorkflow import (
4
+ ConvertOntologyGraphToYamlWorkflowConfiguration,
5
+ )
6
+ from naas_abi.workflows.CreateIndividualOntologyYamlWorkflow import (
7
+ CreateIndividualOntologyYamlWorkflow,
8
+ CreateIndividualOntologyYamlWorkflowConfiguration,
9
+ CreateIndividualOntologyYamlWorkflowParameters,
10
+ )
11
+ from naas_abi_marketplace.applications.naas.integrations.NaasIntegration import (
12
+ NaasIntegration,
13
+ NaasIntegrationConfiguration,
14
+ )
15
+
16
+
17
+ @pytest.fixture
18
+ def workflow() -> CreateIndividualOntologyYamlWorkflow:
19
+ return CreateIndividualOntologyYamlWorkflow(
20
+ CreateIndividualOntologyYamlWorkflowConfiguration(
21
+ services.triple_store_service,
22
+ ConvertOntologyGraphToYamlWorkflowConfiguration(
23
+ NaasIntegrationConfiguration(api_key=secret.get("NAAS_API_KEY"))
24
+ ),
25
+ )
26
+ )
27
+
28
+
29
+ def test_create_individual_ontology_yaml_workflow(
30
+ workflow: CreateIndividualOntologyYamlWorkflow,
31
+ ):
32
+ import time
33
+ from uuid import uuid4
34
+
35
+ from naas_abi import config, logger, services
36
+ from naas_abi.utils.SPARQL import get_subject_graph
37
+ from rdflib import OWL, RDF, RDFS, Graph, Literal, Namespace, URIRef
38
+
39
+ ABI = Namespace("http://ontology.naas.ai/abi/")
40
+
41
+ graph = Graph()
42
+ uri = ABI[str(uuid4())]
43
+ graph.add(
44
+ (uri, RDF.type, URIRef("https://www.commoncoreontologies.org/ont00000443"))
45
+ )
46
+ graph.add((uri, RDF.type, OWL.NamedIndividual))
47
+ graph.add((uri, RDFS.label, Literal("Naas.ai")))
48
+ graph.add(
49
+ (
50
+ uri,
51
+ ABI.logo,
52
+ Literal(
53
+ "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ9gXMaBLQZ39W6Pk53PRuzFjUvv_6lLRWPoQ&s"
54
+ ),
55
+ )
56
+ )
57
+ services.triple_store_service.insert(graph)
58
+ time.sleep(3)
59
+
60
+ # Run workflow
61
+ ontology_id = workflow.graph_to_yaml(
62
+ CreateIndividualOntologyYamlWorkflowParameters(individual_uri=str(uri), depth=1)
63
+ )
64
+
65
+ # Check if ontology id is set
66
+ graph = get_subject_graph(str(uri), 1)
67
+ naas_ontology_id = list(
68
+ graph.triples(
69
+ (None, URIRef("http://ontology.naas.ai/abi/naas_ontology_id"), None)
70
+ )
71
+ )[0][2]
72
+ assert str(ontology_id) == str(naas_ontology_id), ontology_id
73
+
74
+ # Remove graph
75
+ services.triple_store_service.remove(graph)
76
+
77
+ # Remove ontology
78
+ if ontology_id:
79
+ naas_integration = NaasIntegration(
80
+ NaasIntegrationConfiguration(api_key=secret.get("NAAS_API_KEY"))
81
+ )
82
+ result = naas_integration.delete_ontology(
83
+ workspace_id=config.workspace_id, ontology_id=ontology_id
84
+ )
85
+ logger.info(f"Removed ontology: {result}")
86
+ assert result is not None, result