Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
3 changes: 3 additions & 0 deletions pytholog/fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ def __repr__ (self) :

def __lt__(self, other):
return self.lh.terms[self.lh.index] < other.lh.terms[other.lh.index]

def __eq__(self, other):
return self.fact == other.fact

30 changes: 29 additions & 1 deletion pytholog/knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ def __init__(self, name = None):
KnowledgeBase.__id += 1
self.name = name
self._cache = {}

def rem_kn(self, kn):
for i in kn:
i = Fact(i)
g = [Goal(Fact(r.to_string())) for r in i.rhs]
if i.lh.predicate in self.db:
self.db[i.lh.predicate]["facts"].pop(i)
self.db[i.lh.predicate]["terms"].pop(i.terms)
self.db[i.lh.predicate]["goals"].pop(g)

# delete it if it is now empty
if not len(self.db[i.lh.predicate]["facts"]) \
and not len(self.db[i.lh.predicate]["terms"]) \
and not len(self.db[i.lh.predicate]["goals"]):
del self.db[i.lh.predicate]

self.clear_cache()

## the main function that adds new entries or append existing ones
## it creates "facts", "goals" and "terms" buckets for each predicate
Expand All @@ -30,12 +47,17 @@ def add_kn(self, kn):
i = Fact(i)
## rhs are stored as Expr here we change class to Goal
g = [Goal(Fact(r.to_string())) for r in i.rhs]

if i.lh.predicate in self.db:
self.db[i.lh.predicate]["facts"].push(i)
self.db[i.lh.predicate]["terms"].push(i.terms)
self.db[i.lh.predicate]["goals"].push(g)
#self.db[i.lh.predicate]["terms"].append(i.terms)
# self.db[i.lh.predicate]["terms"].append(i.terms)
indx, look_up = term_checker(Expr(i.to_string()))
if look_up in self._cache:
del self._cache[look_up]
else:
# initialize the knowledge base
self.db[i.lh.predicate] = {}
self.db[i.lh.predicate]["facts"] = FactHeap()
self.db[i.lh.predicate]["facts"].push(i)
Expand All @@ -45,6 +67,9 @@ def add_kn(self, kn):
self.db[i.lh.predicate]["terms"].push(i.terms)
#self.db[i.lh.predicate]["goals"] = [g]
#self.db[i.lh.predicate]["terms"] = [i.terms]

def add(self, predicate, *values):
return self.add_kn([Fact(f"{predicate}({', '.join(str(x) for x in values)})")])

def __call__(self, args):
self.add_kn(args)
Expand All @@ -53,6 +78,9 @@ def __call__(self, args):
## it is only to be user intuitive readable method
def query(self, expr, cut = False, show_path = False):
return rule_query(self, expr, cut, show_path)

def q(self, predicate, *values):
return rule_query(self, Expr(f"{predicate}({', '.join(str(x) for x in values)})"))

def rule_search(self, expr):
if expr.predicate not in self.db:
Expand Down
6 changes: 6 additions & 0 deletions pytholog/pq.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def __init__(self):

def push(self, item):
insort(self._container, item) # in by sort

def pop(self, item):
if item in self._container:
self._container.remove(item)
else:
print("item not in container")

def __getitem__(self, item):
return self._container[item]
Expand Down