From 54f187641a1ff82c939302a889cdb444cd1e8d33 Mon Sep 17 00:00:00 2001 From: Karel Douda Date: Fri, 3 Jan 2025 22:28:47 +0100 Subject: [PATCH] Test rule parsing output --- src/pyrdfrules/common/result/__init__.py | 0 src/pyrdfrules/common/result/result.py | 40 +++++++++++++++++++ src/pyrdfrules/common/rule/__init__.py | 0 .../common/rule/{rule.py => resultrule.py} | 4 +- src/pyrdfrules/common/rule/rule/__init__.py | 0 src/pyrdfrules/common/rule/rule/body.py | 4 +- src/pyrdfrules/common/rule/rule/head.py | 12 +++++- src/pyrdfrules/common/rule/rule/object.py | 8 ++++ src/pyrdfrules/common/rule/rule/predicate.py | 11 +++++ src/pyrdfrules/common/rule/rule/subject.py | 8 ++++ src/pyrdfrules/common/rule/ruleset.py | 9 +++-- src/pyrdfrules/common/task/task_runner.py | 2 +- src/tests/test_parsing.py | 28 +++++++++++++ src/tests/test_pipeline.py | 2 + 14 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 src/pyrdfrules/common/result/__init__.py create mode 100644 src/pyrdfrules/common/result/result.py create mode 100644 src/pyrdfrules/common/rule/__init__.py rename src/pyrdfrules/common/rule/{rule.py => resultrule.py} (90%) create mode 100644 src/pyrdfrules/common/rule/rule/__init__.py create mode 100644 src/pyrdfrules/common/rule/rule/object.py create mode 100644 src/pyrdfrules/common/rule/rule/predicate.py create mode 100644 src/pyrdfrules/common/rule/rule/subject.py create mode 100644 src/tests/test_parsing.py diff --git a/src/pyrdfrules/common/result/__init__.py b/src/pyrdfrules/common/result/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/pyrdfrules/common/result/result.py b/src/pyrdfrules/common/result/result.py new file mode 100644 index 0000000..a776c69 --- /dev/null +++ b/src/pyrdfrules/common/result/result.py @@ -0,0 +1,40 @@ + +from pyrdfrules.common.rule.resultrule import ResultRule +from pyrdfrules.common.rule.rule.body import RuleBody +from pyrdfrules.common.rule.rule.head import RuleHead +from pyrdfrules.common.rule.ruleset import Ruleset + + +class Result(): + + ruleset: Ruleset = None + """Ruleset generated by RDFRules. + """ + + def __init__(self, data: dict) -> None: + self.id = id + self.data = data + + self._parse_data() + + pass + + def _parse_data(self): + + rules = [] + + for item in self.data: + match item: + case {'body': _, 'head': __, 'measures': ___}: + # Item is a rule + + rule = ResultRule.model_construct(item) + + print(rule) + + pass + case _: + pass + + def get_ruleset(self) -> Ruleset: + pass \ No newline at end of file diff --git a/src/pyrdfrules/common/rule/__init__.py b/src/pyrdfrules/common/rule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/pyrdfrules/common/rule/rule.py b/src/pyrdfrules/common/rule/resultrule.py similarity index 90% rename from src/pyrdfrules/common/rule/rule.py rename to src/pyrdfrules/common/rule/resultrule.py index 34447a6..ec46d3b 100644 --- a/src/pyrdfrules/common/rule/rule.py +++ b/src/pyrdfrules/common/rule/resultrule.py @@ -6,7 +6,7 @@ from pyrdfrules.common.rule.rule.body import RuleBody from pyrdfrules.common.rule.rule.head import RuleHead -class Rule(BaseModel): +class ResultRule(BaseModel): """ Rule mined by RDFRules. """ @@ -14,7 +14,7 @@ class Rule(BaseModel): """ Represents the list of the parts of the rule in its body. """ - body: RuleBody + body: List[RuleBody] """ Head of the rule. diff --git a/src/pyrdfrules/common/rule/rule/__init__.py b/src/pyrdfrules/common/rule/rule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/pyrdfrules/common/rule/rule/body.py b/src/pyrdfrules/common/rule/rule/body.py index 2092c6d..4d28636 100644 --- a/src/pyrdfrules/common/rule/rule/body.py +++ b/src/pyrdfrules/common/rule/rule/body.py @@ -2,6 +2,8 @@ from pydantic import BaseModel class RuleBody(BaseModel): + graphs: str + object: str - items: List[any] \ No newline at end of file + items: List[dict] \ No newline at end of file diff --git a/src/pyrdfrules/common/rule/rule/head.py b/src/pyrdfrules/common/rule/rule/head.py index ab99485..a596134 100644 --- a/src/pyrdfrules/common/rule/rule/head.py +++ b/src/pyrdfrules/common/rule/rule/head.py @@ -1,6 +1,16 @@ from typing import List from pydantic import BaseModel +from pyrdfrules.common.rule.rule.object import Object +from pyrdfrules.common.rule.rule.predicate import Predicate +from pyrdfrules.common.rule.rule.subject import Subject + class RuleHead(BaseModel): - item: any \ No newline at end of file + graphs: List[str] + + object: Object + + predicate: Predicate + + subject: Subject \ No newline at end of file diff --git a/src/pyrdfrules/common/rule/rule/object.py b/src/pyrdfrules/common/rule/rule/object.py new file mode 100644 index 0000000..4911e05 --- /dev/null +++ b/src/pyrdfrules/common/rule/rule/object.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel + + +class Object(BaseModel): + + type: str + + value: str \ No newline at end of file diff --git a/src/pyrdfrules/common/rule/rule/predicate.py b/src/pyrdfrules/common/rule/rule/predicate.py new file mode 100644 index 0000000..a166021 --- /dev/null +++ b/src/pyrdfrules/common/rule/rule/predicate.py @@ -0,0 +1,11 @@ +from typing import Optional +from pydantic import BaseModel + + +class Predicate(BaseModel): + + localName: str + + nameSpace: str + + prefix: Optional[str] \ No newline at end of file diff --git a/src/pyrdfrules/common/rule/rule/subject.py b/src/pyrdfrules/common/rule/rule/subject.py new file mode 100644 index 0000000..80997b6 --- /dev/null +++ b/src/pyrdfrules/common/rule/rule/subject.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel + + +class Subject(BaseModel): + + type: str + + value: str \ No newline at end of file diff --git a/src/pyrdfrules/common/rule/ruleset.py b/src/pyrdfrules/common/rule/ruleset.py index 18584fa..c11fbe3 100644 --- a/src/pyrdfrules/common/rule/ruleset.py +++ b/src/pyrdfrules/common/rule/ruleset.py @@ -1,7 +1,8 @@ from typing import List from pydantic import BaseModel -from pyrdfrules.common.rule.rule import Rule +from pyrdfrules.common.rule.resultrule import ResultRule + class Ruleset(BaseModel): """ @@ -14,7 +15,7 @@ class Ruleset(BaseModel): """ List of rules generated by RDFRules. """ - rules: List[Rule] + rules: List[ResultRule] """ True if the ruleset is complete. @@ -32,7 +33,7 @@ class Ruleset(BaseModel): """ __iterator_index: int = 0 - def get_rules(self) -> List[Rule]: + def get_rules(self) -> List[ResultRule]: # return all rules pass @@ -47,7 +48,7 @@ def __iter__(self): self.__iterator_index = 0 return self - def __next__(self) -> Rule | None: + def __next__(self) -> ResultRule | None: idx = self.__iterator_index + 1 if len(self.rules) < idx: diff --git a/src/pyrdfrules/common/task/task_runner.py b/src/pyrdfrules/common/task/task_runner.py index 3022a8f..47e427b 100644 --- a/src/pyrdfrules/common/task/task_runner.py +++ b/src/pyrdfrules/common/task/task_runner.py @@ -51,7 +51,7 @@ def create_task_from_string(self, task: str) -> Task: return task def run_task(self, task: Task) -> Generator[Task, None, None]: - """Runs the task to completion. + """Runs a task step. """ yield from self.task_updater.run(task) diff --git a/src/tests/test_parsing.py b/src/tests/test_parsing.py new file mode 100644 index 0000000..7192885 --- /dev/null +++ b/src/tests/test_parsing.py @@ -0,0 +1,28 @@ +import json +import logging +import unittest + +from pyrdfrules.common.result.result import Result + + + +class TestRuleParsing(unittest.TestCase): + + def test_rule_parsing(self): + """ + Tests parsing of a rule. + """ + + data = '[{"body": [{"graphs": [""], "object": {"type": "variable", "value": "?b"}, "predicate": {"localName": "subsequentWork", "nameSpace": "http://dbpedia.org/ontology/", "prefix": "dbo"}, "subject": {"type": "variable", "value": "?c"}}, {"graphs": [""], "object": {"type": "variable", "value": "?a"}, "predicate": {"localName": "artist", "nameSpace": "http://dbpedia.org/ontology/", "prefix": "dbo"}, "subject": {"type": "variable", "value": "?c"}}], "head": {"graphs": [""], "object": {"type": "variable", "value": "?b"}, "predicate": "", "subject": {"type": "variable", "value": "?a"}}, "measures": [{"name": "Support", "value": 845}, {"name": "HeadSupport", "value": 5803}, {"name": "PcaBodySize", "value": 871}, {"name": "PcaConfidence", "value": 0.9701492537313433}, {"name": "HeadSize", "value": 5803}, {"name": "HeadCoverage", "value": 0.14561433741168361}]}]' + + rules = json.loads(data) + + print(rules) + + result = Result(rules) + + print(rules) + + +if __name__ == '__main__': + unittest.main() diff --git a/src/tests/test_pipeline.py b/src/tests/test_pipeline.py index 4ec9116..8815152 100644 --- a/src/tests/test_pipeline.py +++ b/src/tests/test_pipeline.py @@ -158,6 +158,8 @@ def test_pipeline_execution(self): print(step) self.assertIsNotNone(step, "Should not be None") + print(task.result) + app.stop() if __name__ == '__main__':