Skip to content

Commit a7ee607

Browse files
committed
Update documentation
1 parent 6922f5c commit a7ee607

35 files changed

+296
-165
lines changed

src/pyrdfrules/__init__.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,93 @@
33
# SPDX-License-Identifier: MIT
44

55
"""
6+
PyRDFRules is a Python wrapper for the RDFRules tool, providing an interface to interact with RDFRules for rule mining from RDF knowledge graphs.
67
7-
PyRDFRules is a Python library for working with RDFRules. It provides a simple interface for creating and managing RDFRules pipelines.
8+
Sample usage:
9+
```python
10+
import pyrdfrules.application
11+
from pyrdfrules.common.task.task import Task
12+
from pyrdfrules.config import Config
13+
from pyrdfrules.rdfrules.commondata import ConfidenceType, Constraint, RuleConsumer, RuleConsumerType, Threshold
14+
from pyrdfrules.rdfrules.jsonformats import PrefixFull
15+
from pyrdfrules.rdfrules.pipeline import ComputeConfidence, GetRules, GraphAwareRules, Index, LoadGraph, MergeDatasets, AddPrefixes, Mine, Pipeline, SortRuleset
816
17+
# Create an instance of the application.
18+
app = pyrdfrules.application.Application()
19+
20+
# Connect to an existing instance of RDFRules.
21+
rdfrules = app.start_remote(
22+
url = Url("http://example.com/api/"),
23+
config=Config(
24+
task_update_interval_ms=1000
25+
)
26+
)
27+
28+
# Create a pipeline, a sequence of steps to be executed.
29+
# You do not have to use fully qualified names for the classes, as they are imported in the example.
30+
pipeline = pyrdfrules.rdfrules.pipeline.Pipeline(
31+
tasks=[
32+
pyrdfrules.rdfrules.pipeline.LoadGraph(
33+
graphName = "<dbpedia>",
34+
path = "/dbpedia_yago/mappingbased_objects_sample.ttl"
35+
),
36+
pyrdfrules.rdfrules.pipeline.LoadGraph(
37+
graphName = "<yago>",
38+
path = "/dbpedia_yago/yagoFacts.tsv",
39+
settings = "tsvParsedUris"
40+
),
41+
pyrdfrules.rdfrules.pipeline.LoadGraph(
42+
graphName = "<dbpedia>",
43+
path = "/dbpedia_yago/yagoDBpediaInstances.tsv",
44+
settings = "tsvParsedUris"
45+
),
46+
pyrdfrules.rdfrules.pipeline.MergeDatasets(),
47+
pyrdfrules.rdfrules.jsonformats.AddPrefixes(
48+
prefixes=[
49+
pyrdfrules.rdfrules.jsonformats.PrefixFull(prefix="dbo", nameSpace="http://dbpedia.org/ontology/"),
50+
pyrdfrules.rdfrules.jsonformats.PrefixFull(prefix="dbr", nameSpace="http://dbpedia.org/resource/")
51+
]
52+
),
53+
pyrdfrules.rdfrules.pipeline.Index(train=[], test=[]),
54+
pyrdfrules.rdfrules.pipeline.Mine(
55+
thresholds=[
56+
pyrdfrules.rdfrules.commondata.Threshold(name="MinHeadSize", value=100),
57+
pyrdfrules.rdfrules.commondata.Threshold(name="MaxRuleLength", value=3),
58+
pyrdfrules.rdfrules.commondata.Threshold(name="Timeout", value=5),
59+
pyrdfrules.rdfrules.commondata.Threshold(name="MinHeadCoverage", value=0.01),
60+
],
61+
ruleConsumers=[
62+
pyrdfrules.rdfrules.commondata.RuleConsumer(
63+
name=pyrdfrules.rdfrules.commondata.RuleConsumerType.TOP_K,
64+
k=1000,
65+
allowOverflow=False
66+
)
67+
],
68+
patterns=[],
69+
constraints=[
70+
pyrdfrules.rdfrules.commondata.Constraint(name="WithoutConstants")
71+
],
72+
parallelism=0
73+
),
74+
pyrdfrules.rdfrules.pipeline.ComputeConfidence(confidenceType=ConfidenceType.PCA_CONFIDENCE, min=0.5, topk=50),
75+
pyrdfrules.rdfrules.pipeline.SortRuleset(by=[]),
76+
pyrdfrules.rdfrules.pipeline.GraphAwareRules(),
77+
pyrdfrules.rdfrules.pipeline.GetRules()
78+
]
79+
)
80+
81+
# Create a task, which represents the execution of the pipeline.
82+
task : Task = None
83+
84+
# Submit the task to the RDFRules engine.
85+
task = rdfrules.task.create_task(pipeline)
86+
87+
# Run the task step by step.
88+
for step in rdfrules.task.run_task(task):
89+
print(step)
90+
# You can access the result of the task using the task object, read the logs, or interrupt the task here.
91+
92+
# Access the result of the task.
93+
print(task.result)
94+
```
995
"""

src/pyrdfrules/api/task/exception/task_not_found_exception.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from pyrdfrules.common.exception.pyrdfrules_exception import PyRDFRulesException
22

33
class TaskNotFoundException(PyRDFRulesException):
4-
"""Thrown when a task is not found in the RDFRules instance.
4+
"""
5+
Thrown when a task is not found in the RDFRules instance.
56
"""
67

78
def __init__(self, task_id: str):

src/pyrdfrules/application.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from pyrdfrules.rdfrules.rdfrules import RDFRules
1010

1111
class Application(BaseModel):
12+
"""
13+
The Application class provides methods to start and stop local or remote instances of RDFRules.
14+
"""
1215

1316
__rdfrules: RDFRules|None = None
1417

src/pyrdfrules/common/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Common module for pyRDFRules.
3+
4+
This module contains the common classes and functions used by the other modules of pyRDFRules.
5+
6+
Contains:
7+
- task: Task class and related classes.
8+
- rule: Rule class and related classes.
9+
- result: Result class and related classes.
10+
- logging: Logging functions.
11+
- config: Configuration class.
12+
- utils: Utility functions.
13+
"""

src/pyrdfrules/common/base_actions.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/pyrdfrules/common/base_transformations.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/pyrdfrules/common/event/event_dispatcher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
class EventDispatcher():
2+
"""
3+
The EventDispatcher class manages event listeners and dispatches events to them.
4+
"""
25
def __init__(self):
36
self._listeners = []
47

58
def add_listener(self, callback: callable):
9+
"""Adds a listener to the event dispatcher."""
610
self._listeners.append(callback)
711

812
def remove_listener(self, callback: callable):
13+
"""Removes a listener from the event dispatcher."""
914
self._listeners.remove(callback)
1015

1116
def dispatch(self, *args, **kwargs):
17+
"""Dispatches an event to all listeners."""
1218
for listener in self._listeners:
1319
listener(*args, **kwargs)
1420

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Common exceptions for pyRDFRules.
3+
"""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
RDFRules file representations.
3+
4+
Read all files in the workspace:
5+
6+
```python
7+
rdfrules = app.start_remote(
8+
url = Url("...")
9+
)
10+
11+
rdfrules.workspace.get_files()
12+
```
13+
14+
Upload a file:
15+
16+
```python
17+
with open(path, "r") as file:
18+
rdfrules.workspace.upload_file("data/asset.txt", file)
19+
```
20+
21+
Delete a file:
22+
23+
```python
24+
rdfrules.workspace.delete_file("data/asset.txt")
25+
```
26+
"""

src/pyrdfrules/common/file/workspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Workspace():
88
"""
9-
Path in which RDFRules can find datasets and store results.
9+
The Workspace class provides methods to interact with the RDFRules workspace, including file management.
1010
"""
1111

1212
api: WorkspaceApi

src/pyrdfrules/common/file/workspace_tree.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from pyrdfrules.common.file.workspace_file import WorkspaceFile
77

88
class WorkspaceTree(BaseModel):
9+
"""
10+
The WorkspaceTree class represents the structure of the workspace directory, including files and subdirectories.
11+
"""
912

1013
root: WorkspaceDirectory|None = None
1114

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
HTTP module exposing interactions via the HTTP protocol.
3+
"""

src/pyrdfrules/common/http/url.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
from pydantic_core import Url
22

33
class Url(Url):
4+
"""Represents a URL. Wrapped so there is no need to explicitly import any Pydantic classes.
5+
6+
Args:
7+
Url (Url): Wrapper for a URL.
8+
"""
49
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Logging module exposing logging functionality.
3+
4+
Configurable logging is provided by the `Config` class. The `Logger` class provides the logging functionality. The `log` function provides a global logger instance. The `configure_logging` function configures the logging, and is called automatically upon application startup.
5+
6+
By default, the logging level is set to `logging.INFO`. This can be changed by setting the `log_level` attribute of the `Config` class.
7+
"""

src/pyrdfrules/common/logging/logger.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
from pyrdfrules.config import Config
44

5-
pyrdfrules_logger = None
5+
pyrdfrules_logger : logging.Logger = None
6+
"""Global instance of the logger.
7+
Empty by default.
8+
Obtain the logger by calling the `log` function from this package.
9+
"""
610

711
class Logger():
12+
"""
13+
The Logger class provides methods to configure and retrieve the logger for the application.
14+
"""
815

916
logger: logging.Logger = None
1017

src/pyrdfrules/common/predictontasks/predictiontasks_actions.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/pyrdfrules/common/predictontasks/predictiontasks_transformations.py

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Package for translating the resulting JSON into internal object representations.
3+
"""

src/pyrdfrules/common/result/result.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44

55

66
class Result():
7+
"""Class representing the result of a task.
8+
"""
79

810
ruleset: Ruleset = None
911
"""Ruleset generated by RDFRules.
1012
"""
1113

14+
data: dict
15+
"""Raw JSON response from RDFRules."""
16+
1217
def __init__(self, data: dict) -> None:
13-
self.id = id
18+
"""Creates a new Result object.
19+
20+
Args:
21+
data (dict): JSON response from RDFRules.
22+
"""
1423
self.data = data
1524

1625
self._parse_data()
1726

1827
pass
1928

2029
def _parse_data(self):
30+
"""Internal function translating the resulting JSON into internal object representations.
31+
"""
2132

2233
rules = []
2334

@@ -35,4 +46,6 @@ def _parse_data(self):
3546
self.ruleset = Ruleset(rules = rules)
3647

3748
def get_ruleset(self) -> Ruleset:
49+
"""Returns the ruleset generated by RDFRules.
50+
"""
3851
return self.ruleset
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Contains representations of a generated rule from RDFRules.
3+
"""

src/pyrdfrules/common/rule/resultrule.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import List
22
from pydantic import BaseModel
33

4-
from pyrdfrules.common.graph.graph import Graph
54
from pyrdfrules.common.rule.measure.measure import RuleMeasure
65
from pyrdfrules.common.rule.rule.body import RuleBody
76
from pyrdfrules.common.rule.rule.head import RuleHead
@@ -24,4 +23,10 @@ class ResultRule(BaseModel):
2423
"""
2524
Measures of the rule.
2625
"""
27-
measures: List[RuleMeasure]
26+
measures: List[RuleMeasure]
27+
28+
def as_test(self) -> str:
29+
return ''
30+
31+
def as_json(self) -> str:
32+
return ''

0 commit comments

Comments
 (0)