Skip to content

Commit

Permalink
Test rule parsing output
Browse files Browse the repository at this point in the history
  • Loading branch information
kdouda committed Jan 3, 2025
1 parent a0e3e38 commit 54f1876
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 9 deletions.
Empty file.
40 changes: 40 additions & 0 deletions src/pyrdfrules/common/result/result.py
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
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.
"""

"""
Represents the list of the parts of the rule in its body.
"""
body: RuleBody
body: List[RuleBody]

"""
Head of the rule.
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion src/pyrdfrules/common/rule/rule/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pydantic import BaseModel

class RuleBody(BaseModel):
graphs: str

object: str

items: List[any]
items: List[dict]
12 changes: 11 additions & 1 deletion src/pyrdfrules/common/rule/rule/head.py
Original file line number Diff line number Diff line change
@@ -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
graphs: List[str]

object: Object

predicate: Predicate

subject: Subject
8 changes: 8 additions & 0 deletions src/pyrdfrules/common/rule/rule/object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel


class Object(BaseModel):

type: str

value: str
11 changes: 11 additions & 0 deletions src/pyrdfrules/common/rule/rule/predicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import Optional
from pydantic import BaseModel


class Predicate(BaseModel):

localName: str

nameSpace: str

prefix: Optional[str]
8 changes: 8 additions & 0 deletions src/pyrdfrules/common/rule/rule/subject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel


class Subject(BaseModel):

type: str

value: str
9 changes: 5 additions & 4 deletions src/pyrdfrules/common/rule/ruleset.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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.
Expand All @@ -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

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/pyrdfrules/common/task/task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions src/tests/test_parsing.py
Original file line number Diff line number Diff line change
@@ -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": ["<dbpedia>"], "object": {"type": "variable", "value": "?b"}, "predicate": {"localName": "subsequentWork", "nameSpace": "http://dbpedia.org/ontology/", "prefix": "dbo"}, "subject": {"type": "variable", "value": "?c"}}, {"graphs": ["<dbpedia>"], "object": {"type": "variable", "value": "?a"}, "predicate": {"localName": "artist", "nameSpace": "http://dbpedia.org/ontology/", "prefix": "dbo"}, "subject": {"type": "variable", "value": "?c"}}], "head": {"graphs": ["<yago>"], "object": {"type": "variable", "value": "?b"}, "predicate": "<created>", "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()
2 changes: 2 additions & 0 deletions src/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 54f1876

Please sign in to comment.