minigraph-book 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- minigraph_book-0.1.0/LICENSE +21 -0
- minigraph_book-0.1.0/PKG-INFO +80 -0
- minigraph_book-0.1.0/README.md +54 -0
- minigraph_book-0.1.0/pyproject.toml +115 -0
- minigraph_book-0.1.0/pyproject.toml.orig +95 -0
- minigraph_book-0.1.0/src/minigraph/__init__.py +53 -0
- minigraph_book-0.1.0/src/minigraph/arguments.py +160 -0
- minigraph_book-0.1.0/src/minigraph/binary.py +394 -0
- minigraph_book-0.1.0/src/minigraph/changes.py +64 -0
- minigraph_book-0.1.0/src/minigraph/constraints.py +43 -0
- minigraph_book-0.1.0/src/minigraph/database.py +112 -0
- minigraph_book-0.1.0/src/minigraph/errors.py +186 -0
- minigraph_book-0.1.0/src/minigraph/execute.py +315 -0
- minigraph_book-0.1.0/src/minigraph/expressions.py +571 -0
- minigraph_book-0.1.0/src/minigraph/formats.py +194 -0
- minigraph_book-0.1.0/src/minigraph/framelog.py +170 -0
- minigraph_book-0.1.0/src/minigraph/framework.py +185 -0
- minigraph_book-0.1.0/src/minigraph/graph.py +733 -0
- minigraph_book-0.1.0/src/minigraph/index.py +265 -0
- minigraph_book-0.1.0/src/minigraph/lazystore.py +315 -0
- minigraph_book-0.1.0/src/minigraph/lexer.py +103 -0
- minigraph_book-0.1.0/src/minigraph/lru.py +51 -0
- minigraph_book-0.1.0/src/minigraph/model.py +216 -0
- minigraph_book-0.1.0/src/minigraph/parse.py +469 -0
- minigraph_book-0.1.0/src/minigraph/pattern.py +209 -0
- minigraph_book-0.1.0/src/minigraph/py.typed +0 -0
- minigraph_book-0.1.0/src/minigraph/semantics.py +314 -0
- minigraph_book-0.1.0/src/minigraph/snapshot.py +32 -0
- minigraph_book-0.1.0/src/minigraph/storage.py +84 -0
- minigraph_book-0.1.0/src/minigraph/store.py +149 -0
- minigraph_book-0.1.0/src/minigraph/syntax.py +116 -0
- minigraph_book-0.1.0/src/minigraph/traversal.py +273 -0
- minigraph_book-0.1.0/src/minigraph/unit.py +379 -0
- minigraph_book-0.1.0/src/minigraph/updates.py +226 -0
- minigraph_book-0.1.0/src/minigraph/values.py +180 -0
- minigraph_book-0.1.0/src/minigraph/view.py +47 -0
- minigraph_book-0.1.0/src/minigraph/vocabulary.py +31 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stefano Bragaglia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: minigraph-book
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small property-graph database in pure Python, built one subsystem at a time in an accompanying book
|
|
5
|
+
Keywords: graph,database,property-graph,cypher,transactions,indexes,education,learning
|
|
6
|
+
Author: Stefano Bragaglia
|
|
7
|
+
Author-email: Stefano Bragaglia <stefano-bragaglia@users.noreply.github.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Database
|
|
18
|
+
Classifier: Topic :: Education
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.14
|
|
21
|
+
Project-URL: Homepage, https://github.com/stefano-bragaglia/mini-graph-book
|
|
22
|
+
Project-URL: Documentation, https://github.com/stefano-bragaglia/mini-graph-book/blob/main/docs/minigraph-book/index.md
|
|
23
|
+
Project-URL: Source, https://github.com/stefano-bragaglia/mini-graph-book
|
|
24
|
+
Project-URL: Issues, https://github.com/stefano-bragaglia/mini-graph-book/issues
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
<p align="center"><img src="https://raw.githubusercontent.com/stefano-bragaglia/mini-graph-book/main/docs/minigraph-book/images/logo.jpg" alt="minigraph-book logo: a giraffe eating leaves from a tree whose branches form a graph" width="480"></p>
|
|
28
|
+
|
|
29
|
+
# minigraph-book
|
|
30
|
+
|
|
31
|
+
A property-graph database in pure Python, built from scratch in a book you can read chapter by chapter.
|
|
32
|
+
|
|
33
|
+
Nodes have several labels, **edges have several types**. The database has indexes, constraints, a unit of work, pluggable snapshot formats, a write-ahead log, a lazy on-disk store for graphs larger than memory, a Cypher-like query language, and an argumentation layer (statements, attacks, supports, precedence) with algorithms that you can register and switch.
|
|
34
|
+
|
|
35
|
+
## Read the book
|
|
36
|
+
|
|
37
|
+
Start at [docs/minigraph-book/index.md](docs/minigraph-book/index.md). Eighteen chapters, three appendices (testing, complete code, answers), exercises at the end of every chapter.
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
pip install minigraph-book
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Python 3.14 or later, no dependencies.
|
|
46
|
+
|
|
47
|
+
## Try it
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from minigraph import Graph, run
|
|
51
|
+
|
|
52
|
+
graph = Graph()
|
|
53
|
+
run(graph, "CREATE (:Person:Admin {name: 'Ada'})-[:KNOWS:COLLEAGUE]->(:Person {name: 'Bob'})")
|
|
54
|
+
print(run(graph, "MATCH (a:Person)-[:KNOWS]->(b) RETURN a.name, b.name"))
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
And a debate (Part VII of the book):
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from minigraph import ArgumentGraph
|
|
61
|
+
|
|
62
|
+
debate = ArgumentGraph()
|
|
63
|
+
claim = debate.state("The bridge is safe")
|
|
64
|
+
cracks = debate.state("The 2019 inspection found cracks")
|
|
65
|
+
debate.attack_with(cracks, claim, authority=1)
|
|
66
|
+
print(debate.solve("grounded").labellings[0])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Develop
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
uv sync
|
|
73
|
+
uv run ruff check . && uv run pyrefly check
|
|
74
|
+
uv run python -m unittest discover -s tests -t . -q
|
|
75
|
+
python tools/book.py all # re-verifies every listing in the book
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Licence
|
|
79
|
+
|
|
80
|
+
Code: MIT ([LICENSE](LICENSE)). Book text: CC BY 4.0 ([docs/minigraph-book/LICENSE](docs/minigraph-book/LICENSE)).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<p align="center"><img src="https://raw.githubusercontent.com/stefano-bragaglia/mini-graph-book/main/docs/minigraph-book/images/logo.jpg" alt="minigraph-book logo: a giraffe eating leaves from a tree whose branches form a graph" width="480"></p>
|
|
2
|
+
|
|
3
|
+
# minigraph-book
|
|
4
|
+
|
|
5
|
+
A property-graph database in pure Python, built from scratch in a book you can read chapter by chapter.
|
|
6
|
+
|
|
7
|
+
Nodes have several labels, **edges have several types**. The database has indexes, constraints, a unit of work, pluggable snapshot formats, a write-ahead log, a lazy on-disk store for graphs larger than memory, a Cypher-like query language, and an argumentation layer (statements, attacks, supports, precedence) with algorithms that you can register and switch.
|
|
8
|
+
|
|
9
|
+
## Read the book
|
|
10
|
+
|
|
11
|
+
Start at [docs/minigraph-book/index.md](docs/minigraph-book/index.md). Eighteen chapters, three appendices (testing, complete code, answers), exercises at the end of every chapter.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pip install minigraph-book
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Python 3.14 or later, no dependencies.
|
|
20
|
+
|
|
21
|
+
## Try it
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from minigraph import Graph, run
|
|
25
|
+
|
|
26
|
+
graph = Graph()
|
|
27
|
+
run(graph, "CREATE (:Person:Admin {name: 'Ada'})-[:KNOWS:COLLEAGUE]->(:Person {name: 'Bob'})")
|
|
28
|
+
print(run(graph, "MATCH (a:Person)-[:KNOWS]->(b) RETURN a.name, b.name"))
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
And a debate (Part VII of the book):
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from minigraph import ArgumentGraph
|
|
35
|
+
|
|
36
|
+
debate = ArgumentGraph()
|
|
37
|
+
claim = debate.state("The bridge is safe")
|
|
38
|
+
cracks = debate.state("The 2019 inspection found cracks")
|
|
39
|
+
debate.attack_with(cracks, claim, authority=1)
|
|
40
|
+
print(debate.solve("grounded").labellings[0])
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Develop
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
uv sync
|
|
47
|
+
uv run ruff check . && uv run pyrefly check
|
|
48
|
+
uv run python -m unittest discover -s tests -t . -q
|
|
49
|
+
python tools/book.py all # re-verifies every listing in the book
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Licence
|
|
53
|
+
|
|
54
|
+
Code: MIT ([LICENSE](LICENSE)). Book text: CC BY 4.0 ([docs/minigraph-book/LICENSE](docs/minigraph-book/LICENSE)).
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "minigraph-book"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A small property-graph database in pure Python, built one subsystem at a time in an accompanying book"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.14"
|
|
9
|
+
dependencies = []
|
|
10
|
+
keywords = [
|
|
11
|
+
"graph",
|
|
12
|
+
"database",
|
|
13
|
+
"property-graph",
|
|
14
|
+
"cypher",
|
|
15
|
+
"transactions",
|
|
16
|
+
"indexes",
|
|
17
|
+
"education",
|
|
18
|
+
"learning",
|
|
19
|
+
]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Development Status :: 3 - Alpha",
|
|
22
|
+
"Intended Audience :: Developers",
|
|
23
|
+
"Intended Audience :: Education",
|
|
24
|
+
"Operating System :: OS Independent",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
27
|
+
"Programming Language :: Python :: 3.14",
|
|
28
|
+
"Topic :: Database",
|
|
29
|
+
"Topic :: Education",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[[project.authors]]
|
|
34
|
+
name = "Stefano Bragaglia"
|
|
35
|
+
email = "stefano-bragaglia@users.noreply.github.com"
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/stefano-bragaglia/mini-graph-book"
|
|
39
|
+
Documentation = "https://github.com/stefano-bragaglia/mini-graph-book/blob/main/docs/minigraph-book/index.md"
|
|
40
|
+
Source = "https://github.com/stefano-bragaglia/mini-graph-book"
|
|
41
|
+
Issues = "https://github.com/stefano-bragaglia/mini-graph-book/issues"
|
|
42
|
+
|
|
43
|
+
[dependency-groups]
|
|
44
|
+
dev = [
|
|
45
|
+
"ruff>=0.15",
|
|
46
|
+
"pyrefly>=1.5",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[build-system]
|
|
50
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
51
|
+
build-backend = "uv_build"
|
|
52
|
+
|
|
53
|
+
[tool.uv.build-backend]
|
|
54
|
+
module-name = "minigraph"
|
|
55
|
+
|
|
56
|
+
[tool.ruff]
|
|
57
|
+
line-length = 120
|
|
58
|
+
target-version = "py314"
|
|
59
|
+
src = ["src"]
|
|
60
|
+
extend-exclude = [
|
|
61
|
+
".idea",
|
|
62
|
+
"build",
|
|
63
|
+
"tools",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[tool.ruff.lint]
|
|
67
|
+
select = ["ALL"]
|
|
68
|
+
ignore = [
|
|
69
|
+
"D105",
|
|
70
|
+
"D107",
|
|
71
|
+
"EM",
|
|
72
|
+
"TRY003",
|
|
73
|
+
"CPY",
|
|
74
|
+
"COM812",
|
|
75
|
+
"T201",
|
|
76
|
+
"S101",
|
|
77
|
+
"PLR2004",
|
|
78
|
+
"PLR0913",
|
|
79
|
+
"E501",
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint.isort]
|
|
83
|
+
force-single-line = true
|
|
84
|
+
|
|
85
|
+
[tool.ruff.lint.pydocstyle]
|
|
86
|
+
convention = "pep257"
|
|
87
|
+
|
|
88
|
+
[tool.ruff.lint.per-file-ignores]
|
|
89
|
+
"tests/**" = [
|
|
90
|
+
"D",
|
|
91
|
+
"S311",
|
|
92
|
+
]
|
|
93
|
+
"examples/**" = ["S311"]
|
|
94
|
+
|
|
95
|
+
[tool.ruff.lint.mccabe]
|
|
96
|
+
max-complexity = 5
|
|
97
|
+
|
|
98
|
+
[tool.ruff.format]
|
|
99
|
+
docstring-code-format = true
|
|
100
|
+
|
|
101
|
+
[tool.pyrefly]
|
|
102
|
+
preset = "strict"
|
|
103
|
+
project-includes = [
|
|
104
|
+
"src",
|
|
105
|
+
"examples",
|
|
106
|
+
"tests",
|
|
107
|
+
]
|
|
108
|
+
python-version = "3.14"
|
|
109
|
+
search-path = [
|
|
110
|
+
"src",
|
|
111
|
+
".",
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
[tool.pyrefly.errors]
|
|
115
|
+
unused-call-result = false
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "minigraph-book"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A small property-graph database in pure Python, built one subsystem at a time in an accompanying book"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Stefano Bragaglia", email = "stefano-bragaglia@users.noreply.github.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
requires-python = ">=3.14"
|
|
12
|
+
dependencies = []
|
|
13
|
+
keywords = ["graph", "database", "property-graph", "cypher", "transactions", "indexes", "education", "learning"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Intended Audience :: Education",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Topic :: Database",
|
|
23
|
+
"Topic :: Education",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/stefano-bragaglia/mini-graph-book"
|
|
29
|
+
Documentation = "https://github.com/stefano-bragaglia/mini-graph-book/blob/main/docs/minigraph-book/index.md"
|
|
30
|
+
Source = "https://github.com/stefano-bragaglia/mini-graph-book"
|
|
31
|
+
Issues = "https://github.com/stefano-bragaglia/mini-graph-book/issues"
|
|
32
|
+
|
|
33
|
+
[dependency-groups]
|
|
34
|
+
dev = ["ruff>=0.15", "pyrefly>=1.5"]
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
38
|
+
build-backend = "uv_build"
|
|
39
|
+
|
|
40
|
+
[tool.uv.build-backend]
|
|
41
|
+
module-name = "minigraph" # the distribution is "minigraph-book"; the import name stays "minigraph"
|
|
42
|
+
|
|
43
|
+
# ---------------------------------------------------------------------------
|
|
44
|
+
# Ruff — linter + formatter
|
|
45
|
+
# ---------------------------------------------------------------------------
|
|
46
|
+
[tool.ruff]
|
|
47
|
+
line-length = 120
|
|
48
|
+
target-version = "py314"
|
|
49
|
+
src = ["src"]
|
|
50
|
+
extend-exclude = [".idea", "build", "tools"]
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint]
|
|
53
|
+
select = ["ALL"]
|
|
54
|
+
ignore = [
|
|
55
|
+
"D105", # docstring in magic methods (dunders are not documented)
|
|
56
|
+
"D107", # docstring in __init__ (the class docstring covers it)
|
|
57
|
+
"EM", # exception message must not be a string literal
|
|
58
|
+
"TRY003", # long messages outside the exception class
|
|
59
|
+
"CPY", # copyright header
|
|
60
|
+
"COM812", # trailing comma (conflicts with the formatter)
|
|
61
|
+
"T201", # print
|
|
62
|
+
"S101", # assert
|
|
63
|
+
"PLR2004", # magic values in comparisons
|
|
64
|
+
"PLR0913", # too many arguments
|
|
65
|
+
"E501", # line too long (the formatter handles it)
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint.isort]
|
|
69
|
+
force-single-line = true
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint.pydocstyle]
|
|
72
|
+
convention = "pep257" # Standard reStructuredText-compatible docstring style
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint.per-file-ignores]
|
|
75
|
+
"tests/**" = ["D", "S311"]
|
|
76
|
+
"examples/**" = ["S311"] # seeded generators make benchmarks reproducible
|
|
77
|
+
|
|
78
|
+
[tool.ruff.lint.mccabe]
|
|
79
|
+
# Grade A complexity corresponds to a score of 1 to 5
|
|
80
|
+
max-complexity = 5
|
|
81
|
+
|
|
82
|
+
[tool.ruff.format]
|
|
83
|
+
docstring-code-format = true
|
|
84
|
+
|
|
85
|
+
# ---------------------------------------------------------------------------
|
|
86
|
+
# Pyrefly — type checker
|
|
87
|
+
# ---------------------------------------------------------------------------
|
|
88
|
+
[tool.pyrefly]
|
|
89
|
+
preset = "strict" # ~ basedpyright typeCheckingMode = "all"
|
|
90
|
+
project-includes = ["src", "examples", "tests"]
|
|
91
|
+
python-version = "3.14"
|
|
92
|
+
search-path = ["src", "."]
|
|
93
|
+
|
|
94
|
+
[tool.pyrefly.errors]
|
|
95
|
+
unused-call-result = false # ~ reportUnusedCallResult = false
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""minigraph: a small property-graph database, built one subsystem at a time in the accompanying book."""
|
|
2
|
+
|
|
3
|
+
from minigraph.arguments import ArgumentGraph
|
|
4
|
+
from minigraph.database import Database
|
|
5
|
+
from minigraph.errors import ConstraintError
|
|
6
|
+
from minigraph.errors import EdgeNotFoundError
|
|
7
|
+
from minigraph.errors import FormatError
|
|
8
|
+
from minigraph.errors import GraphError
|
|
9
|
+
from minigraph.errors import IndexExistsError
|
|
10
|
+
from minigraph.errors import LogCorruptionError
|
|
11
|
+
from minigraph.errors import MissingPropertyError
|
|
12
|
+
from minigraph.errors import NodeInUseError
|
|
13
|
+
from minigraph.errors import NodeNotFoundError
|
|
14
|
+
from minigraph.errors import NoSuchIndexError
|
|
15
|
+
from minigraph.errors import QueryError
|
|
16
|
+
from minigraph.errors import QuerySyntaxError
|
|
17
|
+
from minigraph.errors import UniquenessError
|
|
18
|
+
from minigraph.errors import ValidationError
|
|
19
|
+
from minigraph.execute import Result
|
|
20
|
+
from minigraph.execute import run
|
|
21
|
+
from minigraph.graph import Graph
|
|
22
|
+
from minigraph.model import Edge
|
|
23
|
+
from minigraph.model import Node
|
|
24
|
+
from minigraph.storage import load
|
|
25
|
+
from minigraph.storage import save
|
|
26
|
+
from minigraph.vocabulary import Dimension
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"ArgumentGraph",
|
|
30
|
+
"ConstraintError",
|
|
31
|
+
"Database",
|
|
32
|
+
"Dimension",
|
|
33
|
+
"Edge",
|
|
34
|
+
"EdgeNotFoundError",
|
|
35
|
+
"FormatError",
|
|
36
|
+
"Graph",
|
|
37
|
+
"GraphError",
|
|
38
|
+
"IndexExistsError",
|
|
39
|
+
"LogCorruptionError",
|
|
40
|
+
"MissingPropertyError",
|
|
41
|
+
"NoSuchIndexError",
|
|
42
|
+
"Node",
|
|
43
|
+
"NodeInUseError",
|
|
44
|
+
"NodeNotFoundError",
|
|
45
|
+
"QueryError",
|
|
46
|
+
"QuerySyntaxError",
|
|
47
|
+
"Result",
|
|
48
|
+
"UniquenessError",
|
|
49
|
+
"ValidationError",
|
|
50
|
+
"load",
|
|
51
|
+
"run",
|
|
52
|
+
"save",
|
|
53
|
+
]
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""Model a debate as a graph: statements, and the attacks and supports between them.
|
|
2
|
+
|
|
3
|
+
Everything is stored in an ordinary :class:`~minigraph.graph.Graph`, in the layout that
|
|
4
|
+
:mod:`minigraph.vocabulary` describes.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
from minigraph.errors import ArgumentError
|
|
10
|
+
from minigraph.errors import UnknownAlgorithmError
|
|
11
|
+
from minigraph.framework import Options
|
|
12
|
+
from minigraph.framework import project
|
|
13
|
+
from minigraph.graph import Graph
|
|
14
|
+
from minigraph.semantics import Gradual
|
|
15
|
+
from minigraph.semantics import Grounded
|
|
16
|
+
from minigraph.semantics import Preferred
|
|
17
|
+
from minigraph.vocabulary import ATTACK
|
|
18
|
+
from minigraph.vocabulary import DIMENSIONS
|
|
19
|
+
from minigraph.vocabulary import SOURCE_OF
|
|
20
|
+
from minigraph.vocabulary import STATEMENT
|
|
21
|
+
from minigraph.vocabulary import SUPPORT
|
|
22
|
+
from minigraph.vocabulary import TARGETS
|
|
23
|
+
from minigraph.vocabulary import Dimension
|
|
24
|
+
|
|
25
|
+
if TYPE_CHECKING:
|
|
26
|
+
from collections.abc import Sequence
|
|
27
|
+
|
|
28
|
+
from minigraph.model import NodeId
|
|
29
|
+
from minigraph.semantics import Algorithm
|
|
30
|
+
from minigraph.semantics import Outcome
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ArgumentGraph:
|
|
34
|
+
"""A graph of statements with attacks and supports, built with the vocabulary of a debate.
|
|
35
|
+
|
|
36
|
+
>>> debate = ArgumentGraph()
|
|
37
|
+
>>> rain = debate.state("It will rain")
|
|
38
|
+
>>> sun = debate.state("It will be sunny")
|
|
39
|
+
>>> debate.attack_with(sun, rain, authority=1)
|
|
40
|
+
3
|
|
41
|
+
>>> debate.graph.node_count(), debate.graph.edge_count()
|
|
42
|
+
(3, 2)
|
|
43
|
+
|
|
44
|
+
:param graph: the graph to keep the debate in; ``None`` means a new, empty one.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def __init__(self, graph: Graph | None = None) -> None:
|
|
48
|
+
self.graph = Graph() if graph is None else graph
|
|
49
|
+
self._algorithms: dict[str, Algorithm] = {
|
|
50
|
+
"grounded": Grounded(),
|
|
51
|
+
"preferred": Preferred(),
|
|
52
|
+
"gradual": Gradual(),
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
def state(self, text: str, base: float | None = None) -> NodeId:
|
|
56
|
+
"""Add a statement.
|
|
57
|
+
|
|
58
|
+
:param text: what the statement says.
|
|
59
|
+
:param base: how believable it is before anyone argues about it, from 0 to 1; ``None`` leaves it unset.
|
|
60
|
+
:return: the id of the new statement.
|
|
61
|
+
:raises ArgumentError: if ``base`` is not between 0 and 1.
|
|
62
|
+
:raises ValidationError: if the text is not a legal property value.
|
|
63
|
+
"""
|
|
64
|
+
props: dict[str, object] = {"text": text}
|
|
65
|
+
if base is not None:
|
|
66
|
+
if not 0.0 <= base <= 1.0:
|
|
67
|
+
message = f"base must be between 0 and 1, not {base}"
|
|
68
|
+
raise ArgumentError(message)
|
|
69
|
+
props["base"] = float(base)
|
|
70
|
+
return self.graph.create_node(STATEMENT, props).id
|
|
71
|
+
|
|
72
|
+
def attack_with(
|
|
73
|
+
self,
|
|
74
|
+
source: NodeId,
|
|
75
|
+
target: NodeId,
|
|
76
|
+
*,
|
|
77
|
+
authority: int | None = None,
|
|
78
|
+
specificity: int | None = None,
|
|
79
|
+
recency: int | None = None,
|
|
80
|
+
) -> NodeId:
|
|
81
|
+
"""Make a statement attack a statement, or another attack or support.
|
|
82
|
+
|
|
83
|
+
:param source: the statement that attacks.
|
|
84
|
+
:param target: what it attacks: a statement, or an attack or a support.
|
|
85
|
+
:param authority: how authoritative the attack's source is; lower wins; ``None`` ranks last.
|
|
86
|
+
:param specificity: how specific it is; lower wins; ``None`` ranks last.
|
|
87
|
+
:param recency: how recent it is; lower wins; ``None`` ranks last.
|
|
88
|
+
:return: the id of the new attack.
|
|
89
|
+
:raises ArgumentError: if ``source`` is not a statement, or ``target`` is not part of the debate.
|
|
90
|
+
"""
|
|
91
|
+
ranks = {Dimension.AUTHORITY: authority, Dimension.SPECIFICITY: specificity, Dimension.RECENCY: recency}
|
|
92
|
+
return self._relate(ATTACK, source, target, ranks)
|
|
93
|
+
|
|
94
|
+
def support_with(
|
|
95
|
+
self,
|
|
96
|
+
source: NodeId,
|
|
97
|
+
target: NodeId,
|
|
98
|
+
*,
|
|
99
|
+
authority: int | None = None,
|
|
100
|
+
specificity: int | None = None,
|
|
101
|
+
recency: int | None = None,
|
|
102
|
+
) -> NodeId:
|
|
103
|
+
"""Make a statement support a statement, or another attack or support.
|
|
104
|
+
|
|
105
|
+
The arguments are those of :meth:`attack_with`.
|
|
106
|
+
|
|
107
|
+
:return: the id of the new support.
|
|
108
|
+
:raises ArgumentError: if ``source`` is not a statement, or ``target`` is not part of the debate.
|
|
109
|
+
"""
|
|
110
|
+
ranks = {Dimension.AUTHORITY: authority, Dimension.SPECIFICITY: specificity, Dimension.RECENCY: recency}
|
|
111
|
+
return self._relate(SUPPORT, source, target, ranks)
|
|
112
|
+
|
|
113
|
+
def _relate(self, kind: str, source: NodeId, target: NodeId, ranks: dict[Dimension, int | None]) -> NodeId:
|
|
114
|
+
"""Create the relation node and its two edges, all or nothing."""
|
|
115
|
+
if STATEMENT not in self.graph.node(source).labels:
|
|
116
|
+
message = f"node {source} is not a statement, so it cannot be the source of a relation"
|
|
117
|
+
raise ArgumentError(message)
|
|
118
|
+
if not self.graph.node(target).labels & {STATEMENT, ATTACK, SUPPORT}:
|
|
119
|
+
message = f"node {target} is not a statement, an attack or a support"
|
|
120
|
+
raise ArgumentError(message)
|
|
121
|
+
props = {dimension.value: rank for dimension, rank in ranks.items() if rank is not None}
|
|
122
|
+
with self.graph.begin() as unit:
|
|
123
|
+
relation = unit.create_node(kind, props)
|
|
124
|
+
unit.create_edge(source, relation.id, SOURCE_OF)
|
|
125
|
+
unit.create_edge(relation.id, target, TARGETS)
|
|
126
|
+
return relation.id
|
|
127
|
+
|
|
128
|
+
def register(self, name: str, algorithm: Algorithm) -> None:
|
|
129
|
+
"""Make an algorithm available under a name.
|
|
130
|
+
|
|
131
|
+
:param name: the name to run it by.
|
|
132
|
+
:param algorithm: any object with a ``solve`` method that takes a framework and returns an outcome.
|
|
133
|
+
:raises ArgumentError: if the name is already taken.
|
|
134
|
+
"""
|
|
135
|
+
if name in self._algorithms:
|
|
136
|
+
message = f"an algorithm called {name!r} is already registered"
|
|
137
|
+
raise ArgumentError(message)
|
|
138
|
+
self._algorithms[name] = algorithm
|
|
139
|
+
|
|
140
|
+
def solve(
|
|
141
|
+
self,
|
|
142
|
+
name: str,
|
|
143
|
+
*,
|
|
144
|
+
supports: bool = True,
|
|
145
|
+
higher_order: bool = True,
|
|
146
|
+
precedence: Sequence[Dimension] = DIMENSIONS,
|
|
147
|
+
) -> Outcome:
|
|
148
|
+
"""Run a registered algorithm on the debate.
|
|
149
|
+
|
|
150
|
+
:param name: the algorithm to run.
|
|
151
|
+
:param supports: if ``False``, reason on attacks only.
|
|
152
|
+
:param higher_order: if ``False``, ignore the attacks and supports that aim at other attacks and supports.
|
|
153
|
+
:param precedence: the dimensions on which attacks can outrank one another, most important first; empty means none.
|
|
154
|
+
:return: what the algorithm found.
|
|
155
|
+
:raises UnknownAlgorithmError: if no algorithm has that name.
|
|
156
|
+
"""
|
|
157
|
+
algorithm = self._algorithms.get(name)
|
|
158
|
+
if algorithm is None:
|
|
159
|
+
raise UnknownAlgorithmError(name, sorted(self._algorithms))
|
|
160
|
+
return algorithm.solve(project(self.graph, Options(supports, higher_order, tuple(precedence))))
|