flamapy-dnnf 2.6.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.
- flamapy_dnnf-2.6.0/LICENSE +165 -0
- flamapy_dnnf-2.6.0/PKG-INFO +66 -0
- flamapy_dnnf-2.6.0/README.md +46 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/__init__.py +0 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/models/__init__.py +4 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/models/dnnf_model.py +46 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/operations/__init__.py +8 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/operations/d4_runner.py +22 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/operations/dnnf_configurations_number.py +29 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/operations/dnnf_satisfiable.py +24 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/transformations/__init__.py +4 -0
- flamapy_dnnf-2.6.0/flamapy/metamodels/dnnf_metamodel/transformations/fm_to_dnnf.py +66 -0
- flamapy_dnnf-2.6.0/flamapy_dnnf.egg-info/PKG-INFO +66 -0
- flamapy_dnnf-2.6.0/flamapy_dnnf.egg-info/SOURCES.txt +19 -0
- flamapy_dnnf-2.6.0/flamapy_dnnf.egg-info/dependency_links.txt +1 -0
- flamapy_dnnf-2.6.0/flamapy_dnnf.egg-info/requires.txt +9 -0
- flamapy_dnnf-2.6.0/flamapy_dnnf.egg-info/top_level.txt +1 -0
- flamapy_dnnf-2.6.0/pyproject.toml +75 -0
- flamapy_dnnf-2.6.0/setup.cfg +4 -0
- flamapy_dnnf-2.6.0/setup.py +3 -0
- flamapy_dnnf-2.6.0/tests/test_dnnf.py +84 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flamapy-dnnf
|
|
3
|
+
Version: 2.6.0
|
|
4
|
+
Summary: flamapy-dnnf is a plugin to flamapy providing exact analysis of feature models via d-DNNF knowledge compilation with the d4 compiler
|
|
5
|
+
Author-email: Flamapy <flamapy@us.es>
|
|
6
|
+
License-Expression: GPL-3.0-or-later
|
|
7
|
+
Project-URL: Homepage, https://github.com/flamapy/dnnf_metamodel
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: flamapy-fw~=2.6.0
|
|
12
|
+
Requires-Dist: flamapy-fm~=2.6.0
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest; extra == "dev"
|
|
15
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
16
|
+
Requires-Dist: prospector; extra == "dev"
|
|
17
|
+
Requires-Dist: mypy; extra == "dev"
|
|
18
|
+
Requires-Dist: coverage; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# flamapy-dnnf
|
|
22
|
+
|
|
23
|
+
`flamapy-dnnf` is a [flamapy](https://flamapy.github.io) plugin for **exact** analysis of feature
|
|
24
|
+
models via **d-DNNF knowledge compilation**, using the [d4](https://github.com/crillab/d4) compiler.
|
|
25
|
+
|
|
26
|
+
d-DNNF (deterministic Decomposable Negation Normal Form) is a highly succinct compilation target
|
|
27
|
+
that supports exact, polytime model counting. It complements the ecosystem:
|
|
28
|
+
|
|
29
|
+
- `flamapy-bdd` — OBDD; exact, but can blow up on large models.
|
|
30
|
+
- `flamapy-sdd` — SDD; exact, more succinct than OBDD, pure-Python (PySDD).
|
|
31
|
+
- `flamapy-sharpsat` — ApproxMC; **approximate** count for very large models.
|
|
32
|
+
- `flamapy-dnnf` — d4/d-DNNF; **exact** count that scales to large industrial models.
|
|
33
|
+
|
|
34
|
+
## Requirements
|
|
35
|
+
|
|
36
|
+
d4 is a native C++ binary with **no PyPI package**. Install it from
|
|
37
|
+
<https://github.com/crillab/d4> and either put it on your `PATH` as `d4` or point
|
|
38
|
+
`FLAMAPY_D4_PATH` at the binary.
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
pip install flamapy-dnnf
|
|
42
|
+
export FLAMAPY_D4_PATH=/path/to/d4 # or have `d4` on PATH
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Operations
|
|
46
|
+
|
|
47
|
+
| Operation | Class |
|
|
48
|
+
|---|---|
|
|
49
|
+
| Exact number of configurations | `DDNNFConfigurationsNumber` |
|
|
50
|
+
| Satisfiable (non-void) | `DDNNFSatisfiable` |
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from flamapy.metamodels.fm_metamodel.transformations import UVLReader
|
|
56
|
+
from flamapy.metamodels.dnnf_metamodel.transformations import FmToDDNNF
|
|
57
|
+
from flamapy.metamodels.dnnf_metamodel.operations import DDNNFConfigurationsNumber
|
|
58
|
+
|
|
59
|
+
fm = UVLReader('model.uvl').transform()
|
|
60
|
+
dnnf_model = FmToDDNNF(fm).transform() # add compile=True to also dump the d-DNNF file
|
|
61
|
+
count = DDNNFConfigurationsNumber().execute(dnnf_model).get_result()
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
GPL-3.0-or-later.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# flamapy-dnnf
|
|
2
|
+
|
|
3
|
+
`flamapy-dnnf` is a [flamapy](https://flamapy.github.io) plugin for **exact** analysis of feature
|
|
4
|
+
models via **d-DNNF knowledge compilation**, using the [d4](https://github.com/crillab/d4) compiler.
|
|
5
|
+
|
|
6
|
+
d-DNNF (deterministic Decomposable Negation Normal Form) is a highly succinct compilation target
|
|
7
|
+
that supports exact, polytime model counting. It complements the ecosystem:
|
|
8
|
+
|
|
9
|
+
- `flamapy-bdd` — OBDD; exact, but can blow up on large models.
|
|
10
|
+
- `flamapy-sdd` — SDD; exact, more succinct than OBDD, pure-Python (PySDD).
|
|
11
|
+
- `flamapy-sharpsat` — ApproxMC; **approximate** count for very large models.
|
|
12
|
+
- `flamapy-dnnf` — d4/d-DNNF; **exact** count that scales to large industrial models.
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
d4 is a native C++ binary with **no PyPI package**. Install it from
|
|
17
|
+
<https://github.com/crillab/d4> and either put it on your `PATH` as `d4` or point
|
|
18
|
+
`FLAMAPY_D4_PATH` at the binary.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install flamapy-dnnf
|
|
22
|
+
export FLAMAPY_D4_PATH=/path/to/d4 # or have `d4` on PATH
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Operations
|
|
26
|
+
|
|
27
|
+
| Operation | Class |
|
|
28
|
+
|---|---|
|
|
29
|
+
| Exact number of configurations | `DDNNFConfigurationsNumber` |
|
|
30
|
+
| Satisfiable (non-void) | `DDNNFSatisfiable` |
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from flamapy.metamodels.fm_metamodel.transformations import UVLReader
|
|
36
|
+
from flamapy.metamodels.dnnf_metamodel.transformations import FmToDDNNF
|
|
37
|
+
from flamapy.metamodels.dnnf_metamodel.operations import DDNNFConfigurationsNumber
|
|
38
|
+
|
|
39
|
+
fm = UVLReader('model.uvl').transform()
|
|
40
|
+
dnnf_model = FmToDDNNF(fm).transform() # add compile=True to also dump the d-DNNF file
|
|
41
|
+
count = DDNNFConfigurationsNumber().execute(dnnf_model).get_result()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
GPL-3.0-or-later.
|
|
File without changes
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from flamapy.core.models import VariabilityModel
|
|
6
|
+
from flamapy.core.exceptions import FlamaException
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def locate_d4() -> str:
|
|
10
|
+
"""Return the path to the d4 compiler binary.
|
|
11
|
+
|
|
12
|
+
Resolved from the ``FLAMAPY_D4_PATH`` environment variable, or ``d4`` on ``PATH``. d4 is a
|
|
13
|
+
native C++ binary (https://github.com/crillab/d4) with no PyPI distribution, so it must be
|
|
14
|
+
installed separately.
|
|
15
|
+
"""
|
|
16
|
+
candidate = os.environ.get('FLAMAPY_D4_PATH') or shutil.which('d4')
|
|
17
|
+
if not candidate or not os.path.exists(candidate):
|
|
18
|
+
raise FlamaException(
|
|
19
|
+
'The d4 compiler was not found. Install d4 (https://github.com/crillab/d4) and '
|
|
20
|
+
'expose it as "d4" on PATH or via the FLAMAPY_D4_PATH environment variable.'
|
|
21
|
+
)
|
|
22
|
+
return candidate
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class DDNNFModel(VariabilityModel):
|
|
26
|
+
"""A d-DNNF (deterministic Decomposable Negation Normal Form) view of a feature model.
|
|
27
|
+
|
|
28
|
+
The model keeps the CNF (as DIMACS) produced from the feature model; exact queries are
|
|
29
|
+
answered by the external d4 compiler, which compiles the CNF to d-DNNF internally. The
|
|
30
|
+
compiled d-DNNF artifact can optionally be dumped to ``nnf_path``.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
@staticmethod
|
|
34
|
+
def get_extension() -> str:
|
|
35
|
+
return 'dnnf'
|
|
36
|
+
|
|
37
|
+
def __init__(self) -> None:
|
|
38
|
+
self.dimacs_path: str = '' # path to the CNF in DIMACS format
|
|
39
|
+
self.nnf_path: Optional[str] = None # optional compiled d-DNNF artifact
|
|
40
|
+
self.var_count: int = 0
|
|
41
|
+
self.variables: dict[str, int] = {} # feature name -> DIMACS variable index
|
|
42
|
+
self.features: dict[int, str] = {} # DIMACS variable index -> feature name
|
|
43
|
+
self.original_model: Optional[VariabilityModel] = None
|
|
44
|
+
|
|
45
|
+
def feature_variables(self) -> list[int]:
|
|
46
|
+
return list(self.features.keys())
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import subprocess
|
|
3
|
+
|
|
4
|
+
from flamapy.core.exceptions import FlamaException
|
|
5
|
+
from flamapy.metamodels.dnnf_metamodel.models import locate_d4
|
|
6
|
+
|
|
7
|
+
# d4 prints the exact model count on a solution line, e.g. "s 1904"; the count is unbounded so
|
|
8
|
+
# it is parsed as an arbitrary-precision Python int.
|
|
9
|
+
_COUNT_LINE = re.compile(r'^s\s+(\d+)\s*$', re.MULTILINE)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def d4_model_count(dimacs_path: str) -> int:
|
|
13
|
+
"""Run d4 in exact model-counting mode over a DIMACS file and return the count."""
|
|
14
|
+
d4 = locate_d4()
|
|
15
|
+
completed = subprocess.run(
|
|
16
|
+
[d4, dimacs_path, '-mc'],
|
|
17
|
+
check=True, capture_output=True, text=True,
|
|
18
|
+
)
|
|
19
|
+
match = _COUNT_LINE.search(completed.stdout)
|
|
20
|
+
if match is None:
|
|
21
|
+
raise FlamaException(f'Could not parse a model count from d4 output:\n{completed.stdout}')
|
|
22
|
+
return int(match.group(1))
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from typing import cast
|
|
2
|
+
|
|
3
|
+
from flamapy.core.models import VariabilityModel
|
|
4
|
+
from flamapy.core.operations import ConfigurationsNumber
|
|
5
|
+
from flamapy.metamodels.dnnf_metamodel.models import DDNNFModel
|
|
6
|
+
from flamapy.metamodels.dnnf_metamodel.operations.d4_runner import d4_model_count
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DDNNFConfigurationsNumber(ConfigurationsNumber):
|
|
10
|
+
"""Exact number of valid configurations, computed by the d4 d-DNNF compiler.
|
|
11
|
+
|
|
12
|
+
d4 compiles the CNF to a d-DNNF and counts its models exactly. It scales to large models
|
|
13
|
+
where OBDD compilation (the bdd plugin) can blow up, and unlike the sharpsat plugin the
|
|
14
|
+
result is exact rather than approximate.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self) -> None:
|
|
18
|
+
self._result = 0
|
|
19
|
+
|
|
20
|
+
def get_configurations_number(self) -> int:
|
|
21
|
+
return self._result
|
|
22
|
+
|
|
23
|
+
def get_result(self) -> int:
|
|
24
|
+
return self._result
|
|
25
|
+
|
|
26
|
+
def execute(self, model: VariabilityModel) -> 'DDNNFConfigurationsNumber':
|
|
27
|
+
dnnf_model = cast(DDNNFModel, model)
|
|
28
|
+
self._result = d4_model_count(dnnf_model.dimacs_path)
|
|
29
|
+
return self
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from typing import cast
|
|
2
|
+
|
|
3
|
+
from flamapy.core.models import VariabilityModel
|
|
4
|
+
from flamapy.core.operations import Satisfiable
|
|
5
|
+
from flamapy.metamodels.dnnf_metamodel.models import DDNNFModel
|
|
6
|
+
from flamapy.metamodels.dnnf_metamodel.operations.d4_runner import d4_model_count
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DDNNFSatisfiable(Satisfiable):
|
|
10
|
+
"""Whether the feature model is satisfiable (non-void): its exact model count is non-zero."""
|
|
11
|
+
|
|
12
|
+
def __init__(self) -> None:
|
|
13
|
+
self._result = False
|
|
14
|
+
|
|
15
|
+
def is_satisfiable(self) -> bool:
|
|
16
|
+
return self._result
|
|
17
|
+
|
|
18
|
+
def get_result(self) -> bool:
|
|
19
|
+
return self._result
|
|
20
|
+
|
|
21
|
+
def execute(self, model: VariabilityModel) -> 'DDNNFSatisfiable':
|
|
22
|
+
dnnf_model = cast(DDNNFModel, model)
|
|
23
|
+
self._result = d4_model_count(dnnf_model.dimacs_path) > 0
|
|
24
|
+
return self
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import subprocess
|
|
3
|
+
import tempfile
|
|
4
|
+
|
|
5
|
+
from flamapy.core.transformations import ModelToModel
|
|
6
|
+
from flamapy.metamodels.fm_metamodel.models import FeatureModel, ClauseSet
|
|
7
|
+
from flamapy.metamodels.dnnf_metamodel.models import DDNNFModel, locate_d4
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FmToDDNNF(ModelToModel):
|
|
11
|
+
"""Transform a feature model into a d-DNNF-backed model.
|
|
12
|
+
|
|
13
|
+
Builds the CNF via the feature-model ``ClauseSet`` (no SAT-solver dependency), writes it as
|
|
14
|
+
DIMACS, and — when ``compile`` is set — invokes d4 to dump the compiled d-DNNF artifact.
|
|
15
|
+
Exact queries run d4 on the DIMACS.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def get_source_extension() -> str:
|
|
20
|
+
return 'fm'
|
|
21
|
+
|
|
22
|
+
@staticmethod
|
|
23
|
+
def get_destination_extension() -> str:
|
|
24
|
+
return 'dnnf'
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
source_model: FeatureModel,
|
|
29
|
+
cnf_method: str = 'distributive',
|
|
30
|
+
compile: bool = False,
|
|
31
|
+
) -> None:
|
|
32
|
+
self.source_model = source_model
|
|
33
|
+
self.cnf_method = cnf_method
|
|
34
|
+
self.compile = compile
|
|
35
|
+
self.destination_model = DDNNFModel()
|
|
36
|
+
|
|
37
|
+
def transform(self) -> DDNNFModel:
|
|
38
|
+
clause_set = ClauseSet.from_feature_model(self.source_model, cnf_method=self.cnf_method)
|
|
39
|
+
clauses = [list(clause) for clause in clause_set.clauses]
|
|
40
|
+
var_count = max((abs(literal) for clause in clauses for literal in clause), default=1)
|
|
41
|
+
|
|
42
|
+
dimacs_fd, dimacs_path = tempfile.mkstemp(suffix='.cnf', prefix='flamapy_dnnf_')
|
|
43
|
+
with os.fdopen(dimacs_fd, 'w') as dimacs_file:
|
|
44
|
+
dimacs_file.write(f'p cnf {var_count} {len(clauses)}\n')
|
|
45
|
+
for clause in clauses:
|
|
46
|
+
dimacs_file.write(' '.join(str(literal) for literal in clause) + ' 0\n')
|
|
47
|
+
|
|
48
|
+
model = self.destination_model
|
|
49
|
+
model.dimacs_path = dimacs_path
|
|
50
|
+
model.var_count = var_count
|
|
51
|
+
model.variables = dict(clause_set.variables)
|
|
52
|
+
model.features = dict(clause_set.features)
|
|
53
|
+
model.original_model = self.source_model
|
|
54
|
+
|
|
55
|
+
if self.compile:
|
|
56
|
+
nnf_fd, nnf_path = tempfile.mkstemp(suffix='.nnf', prefix='flamapy_dnnf_')
|
|
57
|
+
os.close(nnf_fd)
|
|
58
|
+
d4 = locate_d4()
|
|
59
|
+
# d4 compiles the CNF to a d-DNNF and dumps it to nnf_path.
|
|
60
|
+
subprocess.run(
|
|
61
|
+
[d4, dimacs_path, '-dDNNF', f'-out={nnf_path}'],
|
|
62
|
+
check=True, capture_output=True, text=True,
|
|
63
|
+
)
|
|
64
|
+
model.nnf_path = nnf_path
|
|
65
|
+
|
|
66
|
+
return model
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flamapy-dnnf
|
|
3
|
+
Version: 2.6.0
|
|
4
|
+
Summary: flamapy-dnnf is a plugin to flamapy providing exact analysis of feature models via d-DNNF knowledge compilation with the d4 compiler
|
|
5
|
+
Author-email: Flamapy <flamapy@us.es>
|
|
6
|
+
License-Expression: GPL-3.0-or-later
|
|
7
|
+
Project-URL: Homepage, https://github.com/flamapy/dnnf_metamodel
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: flamapy-fw~=2.6.0
|
|
12
|
+
Requires-Dist: flamapy-fm~=2.6.0
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest; extra == "dev"
|
|
15
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
16
|
+
Requires-Dist: prospector; extra == "dev"
|
|
17
|
+
Requires-Dist: mypy; extra == "dev"
|
|
18
|
+
Requires-Dist: coverage; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# flamapy-dnnf
|
|
22
|
+
|
|
23
|
+
`flamapy-dnnf` is a [flamapy](https://flamapy.github.io) plugin for **exact** analysis of feature
|
|
24
|
+
models via **d-DNNF knowledge compilation**, using the [d4](https://github.com/crillab/d4) compiler.
|
|
25
|
+
|
|
26
|
+
d-DNNF (deterministic Decomposable Negation Normal Form) is a highly succinct compilation target
|
|
27
|
+
that supports exact, polytime model counting. It complements the ecosystem:
|
|
28
|
+
|
|
29
|
+
- `flamapy-bdd` — OBDD; exact, but can blow up on large models.
|
|
30
|
+
- `flamapy-sdd` — SDD; exact, more succinct than OBDD, pure-Python (PySDD).
|
|
31
|
+
- `flamapy-sharpsat` — ApproxMC; **approximate** count for very large models.
|
|
32
|
+
- `flamapy-dnnf` — d4/d-DNNF; **exact** count that scales to large industrial models.
|
|
33
|
+
|
|
34
|
+
## Requirements
|
|
35
|
+
|
|
36
|
+
d4 is a native C++ binary with **no PyPI package**. Install it from
|
|
37
|
+
<https://github.com/crillab/d4> and either put it on your `PATH` as `d4` or point
|
|
38
|
+
`FLAMAPY_D4_PATH` at the binary.
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
pip install flamapy-dnnf
|
|
42
|
+
export FLAMAPY_D4_PATH=/path/to/d4 # or have `d4` on PATH
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Operations
|
|
46
|
+
|
|
47
|
+
| Operation | Class |
|
|
48
|
+
|---|---|
|
|
49
|
+
| Exact number of configurations | `DDNNFConfigurationsNumber` |
|
|
50
|
+
| Satisfiable (non-void) | `DDNNFSatisfiable` |
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from flamapy.metamodels.fm_metamodel.transformations import UVLReader
|
|
56
|
+
from flamapy.metamodels.dnnf_metamodel.transformations import FmToDDNNF
|
|
57
|
+
from flamapy.metamodels.dnnf_metamodel.operations import DDNNFConfigurationsNumber
|
|
58
|
+
|
|
59
|
+
fm = UVLReader('model.uvl').transform()
|
|
60
|
+
dnnf_model = FmToDDNNF(fm).transform() # add compile=True to also dump the d-DNNF file
|
|
61
|
+
count = DDNNFConfigurationsNumber().execute(dnnf_model).get_result()
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
GPL-3.0-or-later.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.py
|
|
5
|
+
flamapy/metamodels/dnnf_metamodel/__init__.py
|
|
6
|
+
flamapy/metamodels/dnnf_metamodel/models/__init__.py
|
|
7
|
+
flamapy/metamodels/dnnf_metamodel/models/dnnf_model.py
|
|
8
|
+
flamapy/metamodels/dnnf_metamodel/operations/__init__.py
|
|
9
|
+
flamapy/metamodels/dnnf_metamodel/operations/d4_runner.py
|
|
10
|
+
flamapy/metamodels/dnnf_metamodel/operations/dnnf_configurations_number.py
|
|
11
|
+
flamapy/metamodels/dnnf_metamodel/operations/dnnf_satisfiable.py
|
|
12
|
+
flamapy/metamodels/dnnf_metamodel/transformations/__init__.py
|
|
13
|
+
flamapy/metamodels/dnnf_metamodel/transformations/fm_to_dnnf.py
|
|
14
|
+
flamapy_dnnf.egg-info/PKG-INFO
|
|
15
|
+
flamapy_dnnf.egg-info/SOURCES.txt
|
|
16
|
+
flamapy_dnnf.egg-info/dependency_links.txt
|
|
17
|
+
flamapy_dnnf.egg-info/requires.txt
|
|
18
|
+
flamapy_dnnf.egg-info/top_level.txt
|
|
19
|
+
tests/test_dnnf.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
flamapy
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "flamapy-dnnf"
|
|
7
|
+
version = "2.6.0"
|
|
8
|
+
description = "flamapy-dnnf is a plugin to flamapy providing exact analysis of feature models via d-DNNF knowledge compilation with the d4 compiler"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "GPL-3.0-or-later"
|
|
11
|
+
authors = [{ name = "Flamapy", email = "flamapy@us.es" }]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"flamapy-fw~=2.6.0",
|
|
15
|
+
"flamapy-fm~=2.6.0",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.optional-dependencies]
|
|
19
|
+
dev = [
|
|
20
|
+
"pytest",
|
|
21
|
+
"pytest-mock",
|
|
22
|
+
"prospector",
|
|
23
|
+
"mypy",
|
|
24
|
+
"coverage",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/flamapy/dnnf_metamodel"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
include = ["flamapy.*"]
|
|
32
|
+
|
|
33
|
+
[tool.ruff]
|
|
34
|
+
line-length = 100
|
|
35
|
+
target-version = "py311"
|
|
36
|
+
|
|
37
|
+
[tool.ruff.lint]
|
|
38
|
+
select = ["E", "W", "F", "C", "PL", "RUF"]
|
|
39
|
+
ignore = [
|
|
40
|
+
"RUF012",
|
|
41
|
+
"RUF002",
|
|
42
|
+
"RUF001",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.ruff.lint.mccabe]
|
|
46
|
+
max-complexity = 10
|
|
47
|
+
|
|
48
|
+
[tool.ruff.lint.per-file-ignores]
|
|
49
|
+
"tests/*" = ["ALL"]
|
|
50
|
+
"build/*" = ["ALL"]
|
|
51
|
+
"resources/*" = ["ALL"]
|
|
52
|
+
".mypy_cache/*" = ["ALL"]
|
|
53
|
+
"venv/*" = ["ALL"]
|
|
54
|
+
".venv/*" = ["ALL"]
|
|
55
|
+
"env/*" = ["ALL"]
|
|
56
|
+
"flamapy_dnnf.egg-info/*" = ["ALL"]
|
|
57
|
+
"__pycache__/*" = ["ALL"]
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint.pycodestyle]
|
|
60
|
+
max-line-length = 100
|
|
61
|
+
|
|
62
|
+
[tool.mypy]
|
|
63
|
+
scripts_are_modules = true
|
|
64
|
+
show_traceback = true
|
|
65
|
+
check_untyped_defs = true
|
|
66
|
+
disallow_untyped_defs = true
|
|
67
|
+
disallow_any_generics = true
|
|
68
|
+
warn_no_return = true
|
|
69
|
+
strict_optional = true
|
|
70
|
+
no_implicit_optional = true
|
|
71
|
+
warn_redundant_casts = true
|
|
72
|
+
warn_unused_ignores = true
|
|
73
|
+
show_error_codes = true
|
|
74
|
+
ignore_missing_imports = true
|
|
75
|
+
warn_unreachable = true
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Tests for the d-DNNF (d4) metamodel.
|
|
2
|
+
|
|
3
|
+
d4 is a native binary with no PyPI distribution, so these tests are skipped unless d4 is
|
|
4
|
+
available (on PATH or via FLAMAPY_D4_PATH). When present, exact counts are checked against an
|
|
5
|
+
oracle brute-forced from fm_metamodel's solver-agnostic ClauseSet (no SAT plugin needed).
|
|
6
|
+
"""
|
|
7
|
+
import os
|
|
8
|
+
import shutil
|
|
9
|
+
import tempfile
|
|
10
|
+
from itertools import product
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
from flamapy.metamodels.fm_metamodel.transformations import UVLReader
|
|
15
|
+
from flamapy.metamodels.fm_metamodel.models import FeatureModel, ClauseSet
|
|
16
|
+
from flamapy.metamodels.dnnf_metamodel.transformations import FmToDDNNF
|
|
17
|
+
from flamapy.metamodels.dnnf_metamodel.operations import (
|
|
18
|
+
DDNNFConfigurationsNumber,
|
|
19
|
+
DDNNFSatisfiable,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _exact_count(fm: FeatureModel) -> int:
|
|
24
|
+
"""Exact configuration count by brute-forcing the ClauseSet CNF (no SAT backend)."""
|
|
25
|
+
clause_set = ClauseSet.from_feature_model(fm)
|
|
26
|
+
name_of = {vid: name for name, vid in clause_set.variables.items()}
|
|
27
|
+
var_ids = sorted({abs(lit) for clause in clause_set.clauses for lit in clause}
|
|
28
|
+
| set(clause_set.variables.values()))
|
|
29
|
+
configs = set()
|
|
30
|
+
for bits in product((False, True), repeat=len(var_ids)):
|
|
31
|
+
assignment = dict(zip(var_ids, bits))
|
|
32
|
+
if all(any((lit > 0) == assignment[abs(lit)] for lit in clause)
|
|
33
|
+
for clause in clause_set.clauses):
|
|
34
|
+
configs.add(frozenset(name_of[v] for v in var_ids if assignment[v] and v in name_of))
|
|
35
|
+
return len(configs)
|
|
36
|
+
|
|
37
|
+
_D4_AVAILABLE = bool(os.environ.get('FLAMAPY_D4_PATH') or shutil.which('d4'))
|
|
38
|
+
requires_d4 = pytest.mark.skipif(not _D4_AVAILABLE, reason='the d4 compiler is not installed')
|
|
39
|
+
|
|
40
|
+
_UVL = """features
|
|
41
|
+
Root {abstract}
|
|
42
|
+
mandatory
|
|
43
|
+
Base
|
|
44
|
+
optional
|
|
45
|
+
A
|
|
46
|
+
B
|
|
47
|
+
C
|
|
48
|
+
constraints
|
|
49
|
+
A => B
|
|
50
|
+
A | B
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _fm(uvl):
|
|
55
|
+
handle, path = tempfile.mkstemp(suffix='.uvl')
|
|
56
|
+
try:
|
|
57
|
+
with os.fdopen(handle, 'w') as file:
|
|
58
|
+
file.write(uvl)
|
|
59
|
+
return UVLReader(path).transform()
|
|
60
|
+
finally:
|
|
61
|
+
os.remove(path)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@requires_d4
|
|
65
|
+
def test_count_matches_sat():
|
|
66
|
+
fm = _fm(_UVL)
|
|
67
|
+
exact = _exact_count(fm)
|
|
68
|
+
dnnf = DDNNFConfigurationsNumber().execute(FmToDDNNF(fm).transform()).get_result()
|
|
69
|
+
assert dnnf == exact
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@requires_d4
|
|
73
|
+
def test_satisfiable():
|
|
74
|
+
assert DDNNFSatisfiable().execute(FmToDDNNF(_fm(_UVL)).transform()).get_result() is True
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_transformation_writes_dimacs():
|
|
78
|
+
"""The DIMACS export does not need d4 and can always be checked."""
|
|
79
|
+
model = FmToDDNNF(_fm(_UVL)).transform()
|
|
80
|
+
assert os.path.exists(model.dimacs_path)
|
|
81
|
+
with open(model.dimacs_path) as dimacs:
|
|
82
|
+
header = dimacs.readline()
|
|
83
|
+
assert header.startswith('p cnf ')
|
|
84
|
+
assert model.var_count >= 5 # Root, Base, A, B, C
|