ontocast.tool¶
Tool package for OntoCast.
This package provides a collection of tools that support the OntoCast workflow, including document processing, ontology management, triple store operations, and LLM interactions.
The package includes: - LLMTool: Language model interaction and prompting - OntologyManager: Ontology loading and management - TripleStoreManager: Abstract interface for triple store operations - FusekiTripleStoreManager: Fuseki-specific triple store implementation (preferred) - Neo4jTripleStoreManager: Neo4j-specific triple store implementation - FilesystemTripleStoreManager: Filesystem-based triple store implementation - ConverterTool: Document format conversion utilities - ChunkerTool: Text chunking and segmentation
All tools inherit from the base Tool class and provide standardized interfaces for integration into the OntoCast workflow.
Example
from ontocast.tool import LLMTool, OntologyManager llm = LLMTool.create(provider="openai", model="gpt-4") om = OntologyManager()
ChunkerTool
¶
Bases: Tool
Tool for semantic chunking of documents.
Falls back to naive chunking if sentence-transformers is not available. Includes caching to avoid re-chunking the same text with the same parameters.
Source code in ontocast/tool/chunk/chunker.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
__call__(doc)
¶
Chunk the document using either semantic or naive chunking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
str
|
The document text to chunk. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of text chunks. |
Source code in ontocast/tool/chunk/chunker.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
__init__(chunk_config=None, cache=None, **kwargs)
¶
Initialize the ChunkerTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_config
|
ChunkConfig | None
|
Chunking configuration. If None, uses default ChunkConfig. |
None
|
cache
|
Cacher | None
|
Optional shared Cacher instance. If None, creates a new one. |
None
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ontocast/tool/chunk/chunker.py
ConverterTool
¶
Bases: Tool
Tool for converting documents to structured data.
This class provides functionality for converting various document formats into structured data that can be processed by the OntoCast system. It includes caching to avoid re-converting the same documents.
Attributes:
| Name | Type | Description |
|---|---|---|
supported_extensions |
set[str]
|
Set of supported file extensions. |
cache |
Any
|
Cacher instance for caching conversion results. |
Source code in ontocast/tool/converter.py
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 | |
__call__(file_input)
¶
Convert a document to structured data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_input
|
Union[bytes, str, Path]
|
The input file as either bytes, string, or pathlib.Path. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The converted document data. |
Source code in ontocast/tool/converter.py
__init__(cache=None, **kwargs)
¶
Initialize the converter tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
Cacher | None
|
Optional shared Cacher instance. If None, creates a new one. |
None
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ontocast/tool/converter.py
FilesystemTripleStoreManager
¶
Bases: TripleStoreManager
Filesystem-based implementation of triple store management.
This class provides a concrete implementation of triple store management using the local filesystem for storage. It reads and writes ontologies and facts as Turtle (.ttl) files in specified directories.
The manager supports: - Loading ontologies from a dedicated ontology directory - Storing ontologies with versioned filenames - Storing facts with customizable filenames based on specifications - Error handling for file operations
Attributes:
| Name | Type | Description |
|---|---|---|
working_directory |
Path | None
|
Path to the working directory for storing data. |
ontology_path |
Path | None
|
Optional path to the ontology directory for loading ontologies. |
Source code in ontocast/tool/triple_manager/filesystem_manager.py
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 | |
__init__(**kwargs)
¶
Initialize the filesystem triple store manager.
This method sets up the filesystem manager with the specified working and ontology directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Additional keyword arguments passed to the parent class. working_directory: Path to the working directory for storing data. ontology_path: Path to the ontology directory for loading ontologies. |
{}
|
Example
manager = FilesystemTripleStoreManager( ... working_directory="/path/to/work", ... ontology_path="/path/to/ontologies" ... )
Source code in ontocast/tool/triple_manager/filesystem_manager.py
clean(dataset=None)
async
¶
Clean/flush all data from the filesystem triple store.
This method deletes all Turtle (.ttl) files from both the working directory and the ontology directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
str | None
|
Optional dataset parameter (ignored for Filesystem, which doesn't support datasets). Included for interface compatibility. |
None
|
Raises:
| Type | Description |
|---|---|
Exception
|
If the cleanup operation fails. |
Source code in ontocast/tool/triple_manager/filesystem_manager.py
fetch_ontologies()
¶
Fetch all available ontologies from the filesystem.
This method scans the ontology directory for Turtle (.ttl) files and loads each one as an Ontology object. Files are processed in sorted order for consistent results.
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of all ontologies found in the ontology directory. |
Example
ontologies = manager.fetch_ontologies() for onto in ontologies: ... print(f"Loaded ontology: {onto.ontology_id}")
Source code in ontocast/tool/triple_manager/filesystem_manager.py
serialize_graph(graph, **kwargs)
¶
Store an RDF graph in the filesystem.
This method stores the given RDF graph as a Turtle file in the working directory. The filename is generated based on the graph_uri parameter or defaults to "current.ttl".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The RDF graph to store. |
required |
fname
|
str |
required |
Example
graph = RDFGraph() manager.serialize_graph(graph)
Creates: working_directory/current.ttl¶
manager.serialize_graph(graph, fname="facts_abc.ttl")
Source code in ontocast/tool/triple_manager/filesystem_manager.py
FusekiTripleStoreManager
¶
Bases: TripleStoreManagerWithAuth
Fuseki-based triple store manager.
This class provides a concrete implementation of triple store management using Apache Fuseki. It stores ontologies as named graphs using their URIs as graph names, and supports dataset creation and cleanup.
The manager uses Fuseki's REST API for all operations, including: - Dataset creation and management - Named graph operations for ontologies - SPARQL queries for ontology discovery - Graph-level data operations
Attributes:
| Name | Type | Description |
|---|---|---|
dataset |
str | None
|
The Fuseki dataset name to use for storage. |
clean |
None
|
Whether to clean the dataset on initialization. |
Source code in ontocast/tool/triple_manager/fuseki.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 | |
__init__(uri=None, auth=None, dataset=None, ontologies_dataset=None, **kwargs)
¶
Initialize the Fuseki triple store manager.
This method sets up the connection to Fuseki and creates the dataset if it doesn't exist. The dataset is NOT cleaned on initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Fuseki server URI (e.g., "http://localhost:3030"). |
None
|
|
auth
|
Authentication tuple (username, password) or string in "user/password" format. |
None
|
|
dataset
|
Dataset name to use for storage. |
None
|
|
ontologies_dataset
|
Dataset name for ontologies (defaults to separate dataset). |
None
|
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If dataset is not specified in URI or as argument. |
Example
manager = FusekiTripleStoreManager( ... uri="http://localhost:3030", ... dataset="test" ... )
To clean the dataset, use the clean() method explicitly:¶
await manager.clean()
Source code in ontocast/tool/triple_manager/fuseki.py
afetch_ontologies()
async
¶
Async version of fetch_ontologies.
This is the preferred method when running in an async context.
aserialize(o, **kwargs)
async
¶
Async version of serialize.
This is the preferred method when running in an async context.
aserialize_graph(graph, **kwargs)
async
¶
Async version of serialize_graph.
This is the preferred method when running in an async context.
Source code in ontocast/tool/triple_manager/fuseki.py
clean(dataset=None)
async
¶
Clean/flush data from Fuseki dataset(s).
This method removes all named graphs and clears the default graph from the specified dataset, or all datasets if no dataset is specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
str | None
|
Optional dataset name to clean. If None, cleans both the main dataset and the ontologies dataset. If specified, cleans only that dataset. |
None
|
from the specified dataset(s).
The method handles errors gracefully and logs the results of each cleanup operation.
Example
Clean all datasets¶
await manager.clean()
Clean specific dataset¶
await manager.clean(dataset="my_dataset")
Source code in ontocast/tool/triple_manager/fuseki.py
close()
async
¶
fetch_ontologies()
¶
Synchronous wrapper for fetch_ontologies.
For async usage, use afetch_ontologies() instead.
Source code in ontocast/tool/triple_manager/fuseki.py
init_dataset(dataset_name)
async
¶
Initialize a Fuseki dataset.
This method creates a new dataset in Fuseki if it doesn't already exist. It uses Fuseki's admin API to create the dataset with TDB2 storage.
Uses a temporary client to avoid event loop cleanup issues when called from different async contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
Name of the dataset to create. |
required |
Note
This method will not fail if the dataset already exists.
Source code in ontocast/tool/triple_manager/fuseki.py
serialize(o, **kwargs)
¶
Synchronous wrapper for serialize.
For async usage, use aserialize() instead.
serialize_graph(graph, **kwargs)
¶
Synchronous wrapper for serialize_graph.
For async usage, use aserialize_graph() instead.
Source code in ontocast/tool/triple_manager/fuseki.py
update_dataset(new_dataset)
async
¶
Update the dataset name for this manager.
This method allows changing the dataset without recreating the entire manager, which is useful for API requests that specify different datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_dataset
|
str
|
The new dataset name to use. |
required |
Source code in ontocast/tool/triple_manager/fuseki.py
LLMTool
¶
Bases: Tool
Tool for interacting with language models.
This class provides a unified interface for working with different language model providers (OpenAI, Ollama) through LangChain. It supports both synchronous and asynchronous operations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
LLMConfig
|
LLMConfig object containing all LLM settings. |
cache |
Any
|
Cacher instance for caching LLM responses. |
Source code in ontocast/tool/llm.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
llm
property
¶
Get the underlying language model instance.
Returns:
| Name | Type | Description |
|---|---|---|
BaseChatModel |
BaseChatModel
|
The configured language model. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the LLM has not been properly initialized. |
__call__(*args, **kwds)
async
¶
Call the language model directly (asynchronous).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to the LLM. |
()
|
**kwds
|
Any
|
Keyword arguments passed to the LLM. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The LLM's response. |
Source code in ontocast/tool/llm.py
__init__(cache=None, budget_tracker=None, **kwargs)
¶
Initialize the LLM tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
Cacher | None
|
Optional shared Cacher instance. If None, creates a new one. |
None
|
budget_tracker
|
Any
|
Optional budget tracker instance for usage statistics. |
None
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ontocast/tool/llm.py
acall(*args, **kwds)
async
¶
Call the language model directly (asynchronous).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to the LLM. |
()
|
**kwds
|
Any
|
Keyword arguments passed to the LLM. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The LLM's response. |
Source code in ontocast/tool/llm.py
acreate(config, cache=None, budget_tracker=None, **kwargs)
async
classmethod
¶
Create a new LLM tool instance asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LLMConfig
|
LLMConfig object containing LLM settings. |
required |
cache
|
Cacher | None
|
Optional shared Cacher instance. |
None
|
budget_tracker
|
Any
|
Optional budget tracker instance for usage statistics. |
None
|
**kwargs
|
Additional keyword arguments for initialization. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LLMTool |
A new instance of the LLM tool. |
Source code in ontocast/tool/llm.py
complete(prompt, **kwargs)
async
¶
Generate a completion for the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The input prompt for generation. |
required |
**kwargs
|
Additional keyword arguments for generation. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The generated completion. |
Source code in ontocast/tool/llm.py
create(config, cache=None, budget_tracker=None, **kwargs)
classmethod
¶
Create a new LLM tool instance synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LLMConfig
|
LLMConfig object containing LLM settings. |
required |
cache
|
Cacher | None
|
Optional shared Cacher instance. |
None
|
budget_tracker
|
Any
|
Optional budget tracker instance for usage statistics. |
None
|
**kwargs
|
Additional keyword arguments for initialization. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LLMTool |
A new instance of the LLM tool. |
Source code in ontocast/tool/llm.py
extract(prompt, output_schema, **kwargs)
async
¶
Extract structured data from the prompt according to a schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The input prompt for extraction. |
required |
output_schema
|
Type[T]
|
The Pydantic model class defining the output structure. |
required |
**kwargs
|
Additional keyword arguments for extraction. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The extracted data conforming to the output schema. |
Source code in ontocast/tool/llm.py
setup()
async
¶
Set up the language model based on the configured provider.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not supported. |
Source code in ontocast/tool/llm.py
Neo4jTripleStoreManager
¶
Bases: TripleStoreManagerWithAuth
Neo4j-based triple store manager using n10s (neosemantics) plugin.
This implementation handles RDF data more faithfully by using both the n10s property graph representation and raw RDF triple storage for accurate reconstruction. It provides comprehensive ontology management with namespace-based organization.
The manager uses Neo4j's n10s plugin for RDF operations, including: - RDF import and export via n10s - Ontology metadata storage and retrieval - Namespace-based ontology organization - Faithful RDF graph reconstruction
Attributes:
| Name | Type | Description |
|---|---|---|
_driver |
Any
|
Private Neo4j driver instance. |
Source code in ontocast/tool/triple_manager/neo4j.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
__init__(uri=None, auth=None, **kwargs)
¶
Initialize the Neo4j triple store manager.
This method sets up the connection to Neo4j, initializes the n10s plugin configuration, and creates necessary constraints and indexes. The database is NOT cleaned on initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Neo4j connection URI (e.g., "bolt://localhost:7687"). |
None
|
|
auth
|
Authentication tuple (username, password) or string in "user/password" format. |
None
|
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If the neo4j Python driver is not installed. |
Example
manager = Neo4jTripleStoreManager( ... uri="bolt://localhost:7687", ... auth="neo4j/password" ... )
To clean the database, use the clean() method explicitly:¶
await manager.clean()
Source code in ontocast/tool/triple_manager/neo4j.py
clean(dataset=None)
async
¶
Clean/flush all data from the Neo4j database.
This method deletes all nodes and relationships from the Neo4j database, effectively clearing all stored data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
str | None
|
Optional dataset parameter (ignored for Neo4j, which doesn't support datasets). Included for interface compatibility. |
None
|
Raises:
| Type | Description |
|---|---|
Exception
|
If the cleanup operation fails. |
Source code in ontocast/tool/triple_manager/neo4j.py
close()
¶
Close the Neo4j driver connection.
This method should be called when the manager is no longer needed to properly close the database connection and free resources.
Source code in ontocast/tool/triple_manager/neo4j.py
fetch_ontologies()
¶
Fetch ontologies from Neo4j with faithful RDF reconstruction.
This method retrieves all ontologies from Neo4j and reconstructs their RDF graphs faithfully. It uses a multi-step process:
- Identifies distinct ontologies by their namespace URIs
- Fetches all entities belonging to each ontology
- Reconstructs the RDF graph faithfully using stored triples when available
- Falls back to n10s property graph conversion when needed
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of all ontologies found in the database. |
Example
ontologies = manager.fetch_ontologies() for onto in ontologies: ... print(f"Found ontology: {onto.iri}")
Source code in ontocast/tool/triple_manager/neo4j.py
serialize(o, **kwargs)
¶
Serialize an Ontology or RDFGraph to Neo4j with both n10s and raw triple storage.
This method stores the given Ontology or RDFGraph in Neo4j using the n10s plugin for RDF import. The data is stored as RDF triples that can be faithfully reconstructed later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o
|
Ontology | RDFGraph
|
Ontology or RDFGraph object to store. |
required |
**kwargs
|
Additional keyword arguments (not used by Neo4j implementation). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
bool | None
|
The result summary from n10s import operation. |
Source code in ontocast/tool/triple_manager/neo4j.py
serialize_graph(graph, **kwargs)
¶
Serialize an RDF graph to Neo4j with both n10s and raw triple storage.
This method stores the given RDF graph in Neo4j using the n10s plugin for RDF import. The data is stored as RDF triples that can be faithfully reconstructed later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The RDF graph to store. |
required |
**kwargs
|
Additional parameters (not used by Neo4j implementation). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
bool | None
|
The result summary from n10s import operation. |
Source code in ontocast/tool/triple_manager/neo4j.py
OntologyManager
¶
Bases: Tool
Manager for handling multiple ontologies with version tracking.
This class provides functionality for managing a collection of ontologies, tracking version lineage using hash-based identifiers. For each IRI, it maintains a tree/graph of all versions identified by their hashes.
Attributes:
| Name | Type | Description |
|---|---|---|
ontology_versions |
dict[str, list[Ontology]]
|
Dictionary mapping IRI to list of all ontology versions (identified by hash). Each IRI can have multiple versions forming a lineage tree. |
Source code in ontocast/tool/ontology_manager.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
has_ontologies
property
¶
Check if there are any ontologies available.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if there are any ontologies, False otherwise. |
ontologies
property
¶
Get freshest terminal ontology for each IRI.
This property provides backward compatibility with code that expects a list of ontologies. Returns the freshest (most recently created) terminal version for each IRI.
The result is cached per IRI (as hashes) and updated incrementally when ontologies are added.
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of freshest terminal ontologies, one per IRI. |
__contains__(item)
¶
Check if an item (IRI or ontology_id) is in the ontology manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
The IRI or ontology_id to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the item exists in any version of any ontology. |
Source code in ontocast/tool/ontology_manager.py
__init__(**kwargs)
¶
Initialize the ontology manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ontocast/tool/ontology_manager.py
add_ontology(ontology)
¶
Add an ontology to the version tree for its IRI.
If an ontology with the same hash already exists, it is not added again. The ontology is added to the version tree for its IRI. Ensures that created_at is set if not already present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology
|
Ontology
|
The ontology to add. |
required |
Source code in ontocast/tool/ontology_manager.py
get_freshest_terminal_ontology(ontology_id=None)
¶
Get the freshest terminal ontology by ontology_id (backward compatibility wrapper).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_id
|
str | None
|
Optional ontology_id to filter by. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Ontology |
Ontology | None
|
The freshest terminal ontology, or None if no terminal ontologies exist. |
Source code in ontocast/tool/ontology_manager.py
get_freshest_terminal_ontology_by_iri(iri=None)
¶
Get the freshest terminal ontology based on created_at timestamp.
Returns the terminal ontology with the most recent created_at timestamp.
If multiple terminal ontologies exist, returns the one that was most recently
created. If no created_at is set, falls back to the first terminal ontology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iri
|
str | None
|
Optional IRI to filter by. If None, searches across all ontologies. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Ontology |
Ontology | None
|
The freshest terminal ontology, or None if no terminal ontologies exist. |
Source code in ontocast/tool/ontology_manager.py
get_lineage_graph(ontology_id)
¶
Get the lineage graph for a specific ontology_id (backward compatibility wrapper).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_id
|
str
|
The ontology_id to get the lineage graph for. |
required |
Returns:
| Type | Description |
|---|---|
|
networkx.DiGraph: The lineage graph for the ontology, or None if not found. |
Source code in ontocast/tool/ontology_manager.py
get_lineage_graph_by_iri(iri)
¶
Get the lineage graph for a specific IRI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iri
|
str
|
The IRI to get the lineage graph for. |
required |
Returns:
| Type | Description |
|---|---|
|
networkx.DiGraph: The lineage graph for the ontology, or None if not found. |
Source code in ontocast/tool/ontology_manager.py
get_ontology(ontology_id=None, ontology_iri=None, hash=None)
¶
Get an ontology by its IRI, ontology_id, or hash.
If hash is provided, returns the specific version. Otherwise, returns a terminal (most recent) version if multiple versions exist. IRI is preferred over ontology_id for lookup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_id
|
str | None
|
The short name of the ontology to retrieve (optional, for backward compatibility). |
None
|
ontology_iri
|
str | None
|
The IRI of the ontology to retrieve (preferred). |
None
|
hash
|
str | None
|
The hash of a specific version to retrieve (optional). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Ontology |
Ontology
|
The matching ontology if found, NULL_ONTOLOGY otherwise. |
Source code in ontocast/tool/ontology_manager.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
get_ontology_iris()
¶
Get a list of all ontology IRIs.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of ontology IRIs. |
get_ontology_names()
¶
Get a list of all ontology short names (backward compatibility wrapper).
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of unique ontology short names. |
Source code in ontocast/tool/ontology_manager.py
get_ontology_versions(ontology_id)
¶
Get all versions of an ontology by ontology_id (backward compatibility wrapper).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_id
|
str
|
The ontology_id to retrieve versions for. |
required |
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of all versions of the ontology. |
Source code in ontocast/tool/ontology_manager.py
get_ontology_versions_by_iri(iri)
¶
Get all versions of an ontology by IRI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iri
|
str
|
The IRI to retrieve versions for. |
required |
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of all versions of the ontology. |
Source code in ontocast/tool/ontology_manager.py
get_terminal_ontologies(ontology_id=None)
¶
Get terminal (leaf) ontologies by ontology_id (backward compatibility wrapper).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_id
|
str | None
|
Optional ontology_id to filter by. |
None
|
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of terminal ontologies. |
Source code in ontocast/tool/ontology_manager.py
get_terminal_ontologies_by_iri(iri=None)
¶
Get terminal (leaf) ontologies in the version graph.
Terminal ontologies are those that are not parents of any other ontology in the version tree. If iri is provided, returns terminals for that ontology only; otherwise returns terminals for all ontologies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iri
|
str | None
|
Optional IRI to filter by. |
None
|
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of terminal ontologies. |
Source code in ontocast/tool/ontology_manager.py
update_ontology(ontology_id, ontology_addendum)
¶
Update an existing ontology with additional triples.
Note: This method is deprecated. Use add_ontology() with a new version that has the current hash in parent_hashes instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_id
|
str
|
The short name of the ontology to update. |
required |
ontology_addendum
|
RDFGraph
|
The RDF graph containing additional triples to add. |
required |
Source code in ontocast/tool/ontology_manager.py
Tool
¶
Bases: BasePydanticModel
Base class for all OntoCast tools.
This class serves as the foundation for all tools in the OntoCast system. It provides common functionality and interface that all tools must implement. Tools should inherit from this class and implement their specific functionality.
Source code in ontocast/tool/onto.py
__init__(**kwargs)
¶
Initialize the tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword arguments passed to the parent class. |
{}
|
TripleStoreManager
¶
Bases: Tool
Base class for managing RDF triple stores.
This class defines the interface for triple store management operations, including fetching and storing ontologies and their graphs. All concrete triple store implementations should inherit from this class.
This is an abstract base class that must be implemented by specific triple store backends (e.g., Neo4j, Fuseki, Filesystem).
Source code in ontocast/tool/triple_manager/core.py
__init__(**kwargs)
¶
Initialize the triple store manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
clean(dataset=None)
abstractmethod
async
¶
Clean/flush data from the triple store.
This method removes data from the triple store. For Fuseki, the optional dataset parameter allows cleaning a specific dataset, or all datasets if None. For Neo4j and Filesystem, the dataset parameter is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
str | None
|
Optional dataset name to clean (Fuseki only). If None, cleans all data. For other stores, this parameter is ignored. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the triple store doesn't support cleaning. |
Source code in ontocast/tool/triple_manager/core.py
fetch_ontologies()
abstractmethod
¶
Fetch all available ontologies from the triple store.
This method should retrieve all ontologies stored in the triple store and return them as Ontology objects with their associated RDF graphs.
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of available ontologies with their graphs. |
Source code in ontocast/tool/triple_manager/core.py
serialize(o, **kwargs)
abstractmethod
¶
Store an RDF graph in the triple store.
This method should store the given RDF graph in the triple store. The implementation may choose how to organize the storage (e.g., as named graphs, in specific collections, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o
|
Ontology | RDFGraph
|
RDF graph or Ontology object to store. |
required |
**kwargs
|
Implementation-specific arguments (e.g., graph_uri for Fuseki). |
{}
|
Returns:
| Type | Description |
|---|---|
bool | None
|
bool | None: Implementation-specific return value (bool for Fuseki, summary for Neo4j, None for Filesystem). |
Source code in ontocast/tool/triple_manager/core.py
serialize_graph(graph, **kwargs)
abstractmethod
¶
Store an RDF graph in the triple store.
This method should store the given RDF graph in the triple store. The implementation may choose how to organize the storage (e.g., as named graphs, in specific collections, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The RDF graph to store. |
required |
**kwargs
|
Implementation-specific arguments (e.g., fname for filesystem, graph_uri for Fuseki). |
{}
|
Returns:
| Type | Description |
|---|---|
bool | None
|
bool | None: Implementation-specific return value (bool for Fuseki, summary for Neo4j, None for Filesystem). |