Skip to content

Commit

Permalink
Improve BDD model
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhorcas committed May 15, 2024
1 parent 33609b6 commit 9c71a34
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 18 deletions.
18 changes: 11 additions & 7 deletions flamapy/metamodels/bdd_metamodel/models/bdd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ def __init__(self) -> None:
The BDD relies on a dddmp file that stores a feature model's BDD encoding (dddmp is the
format that the BDD library CUDD uses; check https://github.com/vscosta/cudd)
"""
self.bdd_file: str = ''
self._bdd_file: str = ''
self._temporal_bdd_file: bool = True
self._set_global_constants()

def set_bdd_file(self, dddmp_file: str) -> None:
self.bdd_file = dddmp_file
@property
def bdd_file(self) -> str:
return self._bdd_file

def get_bdd_file(self) -> str:
return self.bdd_file
@bdd_file.setter
def bdd_file(self, dddmp_file: str) -> None:
self._bdd_file = dddmp_file
self._temporal_bdd_file = False

def __del__(self) -> None:
if self.bdd_file is not None:
Path(self.bdd_file + '.dddmp').unlink()
if self._bdd_file is not None and self._temporal_bdd_file:
Path(self._bdd_file + '.dddmp').unlink()

def _set_global_constants(self) -> None:
"""Private auxiliary function that configures the following global constants.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def count(bdd_model: BDDModel, feature_assignment: list[str]) -> int:
:return: The number of valid configurations
"""
# Check bdd_file
bdd_file = bdd_model.check_file_existence(bdd_model.get_bdd_file(), 'dddmp')
bdd_file = bdd_model.check_file_existence(bdd_model.bdd_file, 'dddmp')

# Get all feature names
with open(bdd_file, "r", encoding='utf8') as file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def execute(self, model: VariabilityModel) -> 'BDDFeatureInclusionProbability':

def feature_inclusion_probability(bdd_model: BDDModel) -> dict[Any, float]:
# Check bdd_file
bdd_file = bdd_model.check_file_existence(bdd_model.get_bdd_file(), 'dddmp')
bdd_file = bdd_model.check_file_existence(bdd_model.bdd_file, 'dddmp')
feature_probabilities_process = bdd_model.run(BDDModel.FEATURE_PROBABILITIES,
bdd_file)
result = feature_probabilities_process.stdout.decode(locale.getdefaultlocale()[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def execute(self, model: VariabilityModel) -> 'BDDProductDistribution':

def product_distribution(bdd_model: BDDModel) -> list[int]:
# Check bdd_file
bdd_file = bdd_model.check_file_existence(bdd_model.get_bdd_file(), 'dddmp')
bdd_file = bdd_model.check_file_existence(bdd_model.bdd_file, 'dddmp')

product_distribution_process = bdd_model.run(BDDModel.PRODUCT_DISTRIBUTION,
bdd_file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def sample(bdd_model: BDDModel,
partial_configuration: Optional[Configuration] # pylint: disable=unused-argument
) -> list[Configuration]:
# BDDSampler requires the bdd_file with the '.dddmp' extension.
bdd_file = bdd_model.check_file_existence(bdd_model.get_bdd_file(), 'dddmp')
bdd_file = bdd_model.check_file_existence(bdd_model.bdd_file, 'dddmp')

# Run binary BDDSampler
parameters = ["-names"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def transform(self) -> BDDModel:
match = re.search(re_expression, self._path)
if match is not None:
file_name = match.group(1)
bdd_model.set_bdd_file(file_name)
bdd_model.bdd_file = file_name
return bdd_model
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, path: str, source_model: BDDModel) -> None:
self.source_model = source_model

def transform(self) -> str:
filename = f'{self.source_model.get_bdd_file()}.{DDDMPWriter.get_destination_extension()}'
filename = f'{self.source_model.bdd_file}.{DDDMPWriter.get_destination_extension()}'
shutil.copy(filename, self.path)
with open(self.path, 'r', encoding='utf8') as file:
content = file.read()
Expand Down
11 changes: 7 additions & 4 deletions flamapy/metamodels/bdd_metamodel/transformations/fm_to_bdd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
import tempfile

from flamapy.core.transformations import ModelToModel
from flamapy.metamodels.fm_metamodel.models import FeatureModel
Expand All @@ -21,8 +22,10 @@ def __init__(self, source_model: FeatureModel) -> None:
self.source_model = source_model

def transform(self) -> BDDModel:
splot_filepath = f'{self.source_model.root.name}.{SPLOTWriter.get_destination_extension()}'
SPLOTWriter(path=splot_filepath, source_model=self.source_model).transform()
bdd_model = SPLOTReader(splot_filepath).transform()
Path(splot_filepath).unlink()
with tempfile.NamedTemporaryFile(mode='w',
encoding='utf8',
suffix='.'+SPLOTWriter.get_destination_extension()
) as file:
SPLOTWriter(path=file.name, source_model=self.source_model).transform()
bdd_model = SPLOTReader(file.name).transform()
return bdd_model
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ def build_bdd(model_file: str, bdd_model: BDDModel) -> None:
filepath = file_name + ext
if os.path.exists(filepath):
Path(filepath).unlink()
bdd_model.set_bdd_file(file_name)
bdd_model.bdd_file = file_name
34 changes: 34 additions & 0 deletions tests/models/bdd_models/Pizzas.dddmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.ver DDDMP-3.0
.mode A
.varinfo 1
.dd Pizza.dddmp
.nnodes 18
.nvars 12
.nsuppvars 12
.varnames Pizza Topping Salami Ham Mozzarella Size Normal Big Dough Neapolitan Sicilian CheesyCrust
.suppvarnames Pizza Topping Salami Ham Mozzarella Size Normal Big Dough Neapolitan Sicilian CheesyCrust
.orderedvarnames Pizza Topping Salami Ham Mozzarella Size Normal Big Dough Neapolitan Sicilian CheesyCrust
.ids 0 1 2 3 4 5 6 7 8 9 10 11
.permids 0 1 2 3 4 5 6 7 8 9 10 11
.nroots 1
.rootids -18
.nodes
1 T 1 0 0
2 11 11 1 -1
3 10 10 1 2
4 10 10 2 1
5 9 9 3 4
6 8 8 5 1
7 7 7 1 6
8 10 10 1 -1
9 9 9 8 -8
10 8 8 9 1
11 7 7 10 1
12 6 6 7 11
13 5 5 12 1
14 4 4 13 1
15 3 3 13 14
16 2 2 13 15
17 1 1 16 1
18 0 0 17 1
.end

0 comments on commit 9c71a34

Please sign in to comment.