From 363c395bee02256341df15c7fdd3e29b446c74c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20A=2E=20Galindo?= Date: Mon, 27 May 2024 16:18:26 +0200 Subject: [PATCH] fix: updating operations to adhere to new name --- .../pysat_metamodel/operations/__init__.py | 18 +++--- .../operations/pysat_commonality.py | 4 +- ...at_products.py => pysat_configurations.py} | 12 ++-- ...mber.py => pysat_configurations_number.py} | 10 +-- .../pysat_false_optional_features.py | 8 +-- .../{pysat_valid.py => pysat_satisfiable.py} | 8 +-- .../pysat_satisfiable_configuration.py | 61 +++++++++++++++++++ .../operations/pysat_valid_configuration.py | 42 ------------- .../operations/pysat_valid_product.py | 47 -------------- setup.py | 6 +- tests/operations/operations_test.py | 14 ++--- 11 files changed, 100 insertions(+), 130 deletions(-) rename flamapy/metamodels/pysat_metamodel/operations/{pysat_products.py => pysat_configurations.py} (72%) rename flamapy/metamodels/pysat_metamodel/operations/{pysat_products_number.py => pysat_configurations_number.py} (70%) rename flamapy/metamodels/pysat_metamodel/operations/{pysat_valid.py => pysat_satisfiable.py} (78%) create mode 100644 flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable_configuration.py delete mode 100644 flamapy/metamodels/pysat_metamodel/operations/pysat_valid_configuration.py delete mode 100644 flamapy/metamodels/pysat_metamodel/operations/pysat_valid_product.py diff --git a/flamapy/metamodels/pysat_metamodel/operations/__init__.py b/flamapy/metamodels/pysat_metamodel/operations/__init__.py index 8198443..29c13a0 100644 --- a/flamapy/metamodels/pysat_metamodel/operations/__init__.py +++ b/flamapy/metamodels/pysat_metamodel/operations/__init__.py @@ -1,8 +1,7 @@ -from .pysat_valid import PySATValid -from .pysat_valid_configuration import PySATValidConfiguration -from .pysat_valid_product import PySATValidProduct -from .pysat_products import PySATProducts -from .pysat_products_number import PySATProductsNumber +from .pysat_satisfiable import PySATSatisfiable +from .pysat_satisfiable_configuration import PySATSatisfiableConfiguration +from .pysat_configurations import PySATConfigurations +from .pysat_configurations_number import PySATConfigurationsNumber from .pysat_commonality import PySATCommonality from .pysat_filter import PySATFilter from .pysat_core_features import PySATCoreFeatures @@ -12,11 +11,10 @@ __all__ = [ - 'PySATValid', - 'PySATValidConfiguration', - 'PySATValidProduct', - 'PySATProducts', - 'PySATProductsNumber', + 'PySATSatisfiable', + 'PySATSatisfiableConfiguration', + 'PySATConfigurations', + 'PySATConfigurationsNumber', 'PySATCommonality', 'PySATFilter', 'PySATCoreFeatures', diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_commonality.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_commonality.py index dda88dc..e8684f2 100644 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_commonality.py +++ b/flamapy/metamodels/pysat_metamodel/operations/pysat_commonality.py @@ -1,7 +1,7 @@ from flamapy.core.operations import Commonality from flamapy.core.models import VariabilityModel from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration -from flamapy.metamodels.pysat_metamodel.operations.pysat_products import PySATProducts +from .pysat_configurations import PySATConfigurations class PySATCommonality(Commonality): @@ -20,7 +20,7 @@ def get_result(self) -> float: return self.result def execute(self, model: VariabilityModel) -> 'PySATCommonality': - pysat_products_op = PySATProducts() + pysat_products_op = PySATConfigurations() pysat_products_op.execute(model) products = pysat_products_op.get_result() diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_products.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_configurations.py similarity index 72% rename from flamapy/metamodels/pysat_metamodel/operations/pysat_products.py rename to flamapy/metamodels/pysat_metamodel/operations/pysat_configurations.py index 3a5775b..5bc242a 100644 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_products.py +++ b/flamapy/metamodels/pysat_metamodel/operations/pysat_configurations.py @@ -2,31 +2,31 @@ from pysat.solvers import Solver -from flamapy.core.operations import Products +from flamapy.core.operations import Configurations from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel from flamapy.core.models import VariabilityModel -class PySATProducts(Products): +class PySATConfigurations(Configurations): def __init__(self) -> None: self.result: list[Configuration] = [] self.solver = Solver(name='glucose3') - def get_products(self) -> list[Configuration]: + def get_configurations(self) -> list[Configuration]: return self.get_result() def get_result(self) -> list[Configuration]: return self.result - def execute(self, model: VariabilityModel) -> 'PySATProducts': + def execute(self, model: VariabilityModel) -> 'PySATConfigurations': sat_model = cast(PySATModel, model) - self.result = products(self.solver, sat_model) + self.result = configurations(self.solver, sat_model) return self -def products(solver: Solver, model: PySATModel) -> list[Configuration]: +def configurations(solver: Solver, model: PySATModel) -> list[Configuration]: for clause in model.get_all_clauses(): solver.add_clause(clause) diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_products_number.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_configurations_number.py similarity index 70% rename from flamapy/metamodels/pysat_metamodel/operations/pysat_products_number.py rename to flamapy/metamodels/pysat_metamodel/operations/pysat_configurations_number.py index 8cd6384..d6137eb 100644 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_products_number.py +++ b/flamapy/metamodels/pysat_metamodel/operations/pysat_configurations_number.py @@ -2,24 +2,24 @@ from pysat.solvers import Solver -from flamapy.core.operations import ProductsNumber +from flamapy.core.operations import ConfigurationsNumber from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel from flamapy.core.models import VariabilityModel -class PySATProductsNumber(ProductsNumber): +class PySATConfigurationsNumber(ConfigurationsNumber): def __init__(self) -> None: self.products_number = 0 self.solver = Solver(name='glucose3') - def get_products_number(self) -> int: + def get_configurations_number(self) -> int: return self.products_number def get_result(self) -> int: - return self.get_products_number() + return self.get_configurations_number() - def execute(self, model: VariabilityModel) -> 'PySATProductsNumber': + def execute(self, model: VariabilityModel) -> 'PySATConfigurationsNumber': model = cast(PySATModel, model) for clause in model.get_all_clauses(): # AC es conjunto de conjuntos diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_false_optional_features.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_false_optional_features.py index d8f5524..423bc4d 100644 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_false_optional_features.py +++ b/flamapy/metamodels/pysat_metamodel/operations/pysat_false_optional_features.py @@ -6,7 +6,7 @@ from flamapy.core.operations import FalseOptionalFeatures from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel from flamapy.metamodels.fm_metamodel.models.feature_model import FeatureModel -from flamapy.core.models import VariabilityModel +from flamapy.core.models import VariabilityModel, VariabilityElement from flamapy.core.exceptions import FlamaException @@ -24,7 +24,7 @@ def execute(self, model: VariabilityModel) -> 'PySATFalseOptionalFeatures': self.result = self._get_false_optional_features(sat_model) return self - def get_false_optional_features(self) -> list[Any]: + def get_false_optional_features(self) -> list[VariabilityElement]: return self.get_result() def get_result(self) -> list[Any]: @@ -37,7 +37,7 @@ def _get_false_optional_features(self, sat_model: PySATModel) -> list[Any]: LOGGER.exception("The transformation didn't attach the source model, " "which is required for this operation.") - real_optional_features = [f.name for f in feature_model.get_features() + real_optional_features = [f for f in feature_model.get_features() if not f.is_root() and not f.is_mandatory()] result = [] @@ -52,6 +52,6 @@ def _get_false_optional_features(self, sat_model: PySATModel) -> list[Any]: assert variable is not None satisfiable = self.solver.solve(assumptions=[parent_variable, -variable]) if not satisfiable: - result.append(feature.name) + result.append(feature) self.solver.delete() return result diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_valid.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable.py similarity index 78% rename from flamapy/metamodels/pysat_metamodel/operations/pysat_valid.py rename to flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable.py index 8d69195..35c5be3 100644 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_valid.py +++ b/flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable.py @@ -2,25 +2,25 @@ from pysat.solvers import Solver -from flamapy.core.operations import Valid +from flamapy.core.operations import Satisfiable from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel from flamapy.core.models import VariabilityModel -class PySATValid(Valid): +class PySATSatisfiable(Satisfiable): def __init__(self) -> None: self.result = False self.solver = Solver(name='glucose3') - def is_valid(self) -> bool: + def is_satisfiable(self) -> bool: return self.get_result() def get_result(self) -> bool: return self.result - def execute(self, model: VariabilityModel) -> 'PySATValid': + def execute(self, model: VariabilityModel) -> 'PySATSatisfiable': sat_model = cast(PySATModel, model) self.result = valid(self.solver, sat_model) return self diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable_configuration.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable_configuration.py new file mode 100644 index 0000000..fc550a0 --- /dev/null +++ b/flamapy/metamodels/pysat_metamodel/operations/pysat_satisfiable_configuration.py @@ -0,0 +1,61 @@ +from typing import cast + +from pysat.solvers import Solver + +from flamapy.core.models import VariabilityModel +from flamapy.core.operations import SatisfiableConfiguration +from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration +from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel + + +class PySATSatisfiableConfiguration(SatisfiableConfiguration): + + def __init__(self) -> None: + self.result = False + self.configuration = Configuration(elements={}) + self.solver = Solver(name='glucose3') + self.is_full = False + + def is_satisfiable(self) -> bool: + return self.result + + def get_result(self) -> bool: + return self.is_satisfiable() + + def set_configuration(self, configuration: Configuration, is_full: bool) -> None: + self.configuration = configuration + self.is_full = is_full + + def execute(self, model: VariabilityModel) -> 'PySATSatisfiableConfiguration': + sat_model = cast(PySATModel, model) + + for clause in sat_model.get_all_clauses(): # AC es conjunto de conjuntos + self.solver.add_clause(clause) # añadimos la constraint + + if not self.is_full: + assumptions = [] + for feature, selected in self.configuration.elements.items(): + if selected: + assumptions.append(sat_model.variables[feature.name]) + else: + assumptions.append(-sat_model.variables[feature.name]) + else: + missing_features = [feature for feature in self.configuration.elements.keys() if feature.name not in sat_model.variables.keys()] + + if missing_features: + print("The features that are missing are:", [feature.name for feature in missing_features]) + print("The feature model contains the following features:", list(sat_model.variables.keys())) + self.result = False + return self + + print(self.configuration.elements.items()) + assumptions = [] + for feature in sat_model.features.values(): + if self.configuration.has(feature): + assumptions.append(sat_model.variables[feature]) + else: + assumptions.append(-sat_model.variables[feature]) + + self.result = self.solver.solve(assumptions=assumptions) + self.solver.delete() + return self diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_valid_configuration.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_valid_configuration.py deleted file mode 100644 index 26777c0..0000000 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_valid_configuration.py +++ /dev/null @@ -1,42 +0,0 @@ -from typing import cast - -from pysat.solvers import Solver - -from flamapy.core.models import VariabilityModel -from flamapy.core.operations import ValidConfiguration -from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration -from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel - - -class PySATValidConfiguration(ValidConfiguration): - - def __init__(self) -> None: - self.result = False - self.configuration = Configuration(elements={}) - self.solver = Solver(name='glucose3') - - def is_valid(self) -> bool: - return self.result - - def get_result(self) -> bool: - return self.is_valid() - - def set_configuration(self, configuration: Configuration) -> None: - self.configuration = configuration - - def execute(self, model: VariabilityModel) -> 'PySATValidConfiguration': - sat_model = cast(PySATModel, model) - - for clause in sat_model.get_all_clauses(): # AC es conjunto de conjuntos - self.solver.add_clause(clause) # añadimos la constraint - - assumptions = [] - for feature, selected in self.configuration.elements.items(): - if selected: - assumptions.append(sat_model.variables[feature]) - else: - assumptions.append(-sat_model.variables[feature]) - - self.result = self.solver.solve(assumptions=assumptions) - self.solver.delete() - return self diff --git a/flamapy/metamodels/pysat_metamodel/operations/pysat_valid_product.py b/flamapy/metamodels/pysat_metamodel/operations/pysat_valid_product.py deleted file mode 100644 index de4a54f..0000000 --- a/flamapy/metamodels/pysat_metamodel/operations/pysat_valid_product.py +++ /dev/null @@ -1,47 +0,0 @@ -from typing import cast - -from pysat.solvers import Solver - -from flamapy.core.models import VariabilityModel -from flamapy.core.operations import ValidProduct -from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration -from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel - - -class PySATValidProduct(ValidProduct): - - def __init__(self) -> None: - self.result = False - self.configuration = Configuration(elements={}) - self.solver = Solver(name='glucose3') - - def is_valid(self) -> bool: - return self.result - - def get_result(self) -> bool: - return self.is_valid() - - def set_configuration(self, configuration: Configuration) -> None: - self.configuration = configuration - - def execute(self, model: VariabilityModel) -> 'PySATValidProduct': - sat_model = cast(PySATModel, model) - - if any(feature not in sat_model.variables.keys() - for feature in self.configuration.elements.keys()): - self.result = False - return self - - for clause in sat_model.get_all_clauses(): # AC es conjunto de conjuntos - self.solver.add_clause(clause) # añadimos la constraint - - assumptions = [] - for feature in sat_model.features.values(): - if feature in self.configuration.elements and self.configuration.elements[feature]: - assumptions.append(sat_model.variables[feature]) - else: - assumptions.append(-sat_model.variables[feature]) - - self.result = self.solver.solve(assumptions=assumptions) - self.solver.delete() - return self diff --git a/setup.py b/setup.py index 027b7d1..c1259c2 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setuptools.setup( name="flamapy-sat", - version="1.6.0.dev0", + version="2.0.0.dev0", author="Flamapy", author_email="flamapy@us.es", description="flamapy-sat is a plugin to flamapy module", @@ -22,8 +22,8 @@ ], python_requires='>=3.9', install_requires=[ - 'flamapy~=1.6.0.dev0', - 'flamapy-fm~=1.6.0.dev0', + 'flamapy-fw~=2.0.0.dev0', + 'flamapy-fm~=2.0.0.dev0', 'python-sat>=0.1.7.dev6' ], extras_require={ diff --git a/tests/operations/operations_test.py b/tests/operations/operations_test.py index 3b4114f..e2c3992 100644 --- a/tests/operations/operations_test.py +++ b/tests/operations/operations_test.py @@ -11,15 +11,15 @@ from flamapy.metamodels.pysat_metamodel.operations.pysat_false_optional_features import ( PySATFalseOptionalFeatures, ) -from flamapy.metamodels.pysat_metamodel.operations.pysat_products import ( - PySATProducts, +from pysat_metamodel.flamapy.metamodels.pysat_metamodel.operations.pysat_configurations import ( + PySATConfigurations, ) -from flamapy.metamodels.pysat_metamodel.operations.pysat_products_number import ( - PySATProductsNumber, +from pysat_metamodel.flamapy.metamodels.pysat_metamodel.operations.pysat_configurations_number import ( + PySATConfigurationsNumber, ) -from flamapy.metamodels.pysat_metamodel.operations.pysat_valid import PySATValid -from flamapy.metamodels.pysat_metamodel.operations.pysat_valid_product import ( - PySATValidProduct, +from flamapy.metamodels.pysat_metamodel.operations.pysat_satisfiabjle import PySATSatisfiable +from flamapy.metamodels.pysat_metamodel.operations.pysat_satisfiable_configuraiton import ( + PySATSatisfiableConfiguration, )