Skip to content

Commit

Permalink
feature: adding the metrics operation. fix: renaming for coherence wi…
Browse files Browse the repository at this point in the history
…th other solvers
  • Loading branch information
jagalindo committed Aug 23, 2023
1 parent 7996e2d commit 1408946
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 71 deletions.
10 changes: 2 additions & 8 deletions flamapy/metamodels/pysat_metamodel/models/pysat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ def get_extension() -> str:
return 'pysat'

def __init__(self) -> None:
# self.r_cnf = CNF() # ToDo: This should be avoid
# self.ctc_cnf = CNF() # ToDo: This should be avoid
self._cnf = CNF()
self.variables: dict[str, int] = {} # feature's name -> id
self.features: dict[int, str] = {} # id -> feature's name

self.original_model: VariabilityModel

def add_clause(self, clause: list[int]) -> None:
# self.ctc_cnf.append(clause)
self._cnf.append(clause)

def get_variable(self, key: str) -> int:
Expand All @@ -26,8 +24,4 @@ def get_variable(self, key: str) -> int:
return self.variables[key]

def get_all_clauses(self) -> CNF:
# clauses = CNF()
# clauses.extend(self.r_cnf.clauses)
# clauses.extend(self.ctc_cnf.clauses)
# return clauses
return self._cnf
46 changes: 23 additions & 23 deletions flamapy/metamodels/pysat_metamodel/operations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
from .glucose3_valid import Glucose3Valid
from .glucose3_valid_configuration import Glucose3ValidConfiguration
from .glucose3_valid_product import Glucose3ValidProduct
from .glucose3_products import Glucose3Products
from .glucose3_products_number import Glucose3ProductsNumber
from .glucose3_commonality import Glucose3Commonality
from .glucose3_filter import Glucose3Filter
from .glucose3_core_features import Glucose3CoreFeatures
from .glucose3_dead_features import Glucose3DeadFeatures
from .glucose3_false_optional_features import Glucose3FalseOptionalFeatures
from .glucose3_error_detection import Glucose3ErrorDetection

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_commonality import PySATCommonality
from .pysat_filter import PySATFilter
from .pysat_core_features import PySATCoreFeatures
from .pysat_dead_features import PySATDeadFeatures
from .pysat_false_optional_features import PySATFalseOptionalFeatures
from .pysat_error_detection import PySATErrorDetection
from .pysat_metrics import PySATMetrics

__all__ = [
'Glucose3Valid',
'Glucose3ValidConfiguration',
'Glucose3ValidProduct',
'Glucose3Products',
'Glucose3ProductsNumber',
'Glucose3Commonality',
'Glucose3Filter',
'Glucose3CoreFeatures',
'Glucose3DeadFeatures',
'Glucose3FalseOptionalFeatures',
'Glucose3ErrorDetection',
'PySATValid',
'PySATValidConfiguration',
'PySATValidProduct',
'PySATProducts',
'PySATProductsNumber',
'PySATCommonality',
'PySATFilter',
'PySATCoreFeatures',
'PySATDeadFeatures',
'PySATFalseOptionalFeatures',
'PySATErrorDetection',
]
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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.glucose3_products import Glucose3Products
from flamapy.metamodels.pysat_metamodel.operations.pysat_products import PySATProducts


class Glucose3Commonality(Commonality):
class PySATCommonality(Commonality):
def __init__(self) -> None:
self.commonality: float = 0
self.configuration = Configuration({})
Expand All @@ -18,8 +18,8 @@ def get_commonality(self) -> float:
def get_result(self) -> float:
return self.get_commonality()

def execute(self, model: VariabilityModel) -> 'Glucose3Commonality':
glucose3products = Glucose3Products()
def execute(self, model: VariabilityModel) -> 'PySATCommonality':
glucose3products = PySATProducts()
glucose3products.execute(model)

products = glucose3products.get_result()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from flamapy.core.models import VariabilityModel


class Glucose3CoreFeatures(CoreFeatures):
class PySATCoreFeatures(CoreFeatures):

def __init__(self) -> None:
self.core_features: list[list[Any]] = []
Expand All @@ -19,7 +19,7 @@ def get_core_features(self) -> list[list[Any]]:
def get_result(self) -> list[list[Any]]:
return self.get_core_features()

def execute(self, model: VariabilityModel) -> 'Glucose3CoreFeatures':
def execute(self, model: VariabilityModel) -> 'PySATCoreFeatures':
model = cast(PySATModel, model)
for clause in model.get_all_clauses(): # AC es conjunto de conjuntos
self.solver.add_clause(clause) # añadimos la constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from flamapy.core.models import VariabilityModel


class Glucose3DeadFeatures(DeadFeatures):
class PySATDeadFeatures(DeadFeatures):

def __init__(self) -> None:
self.dead_features: list[list[Any]] = []
Expand All @@ -19,7 +19,7 @@ def get_dead_features(self) -> list[list[Any]]:
def get_result(self) -> list[list[Any]]:
return self.get_dead_features()

def execute(self, model: VariabilityModel) -> 'Glucose3DeadFeatures':
def execute(self, model: VariabilityModel) -> 'PySATDeadFeatures':
model = cast(PySATModel, model)

for clause in model.get_all_clauses(): # AC es conjunto de conjuntos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

from flamapy.core.operations import ErrorDetection
from flamapy.metamodels.pysat_metamodel.models.pysat_model import PySATModel
from flamapy.metamodels.pysat_metamodel.operations.glucose3_dead_features import (
Glucose3DeadFeatures
from flamapy.metamodels.pysat_metamodel.operations.pysat_dead_features import (
PySATDeadFeatures
)
from flamapy.metamodels.pysat_metamodel.operations.glucose3_false_optional_features import (
Glucose3FalseOptionalFeatures
from flamapy.metamodels.pysat_metamodel.operations.pysat_false_optional_features import (
PySATFalseOptionalFeatures
)
from flamapy.core.models import VariabilityModel
from flamapy.core.exceptions import FlamaException
from flamapy.metamodels.fm_metamodel.models.feature_model import FeatureModel
from flamapy.metamodels.pysat_metamodel.operations.glucose3_valid import Glucose3Valid
from flamapy.metamodels.pysat_metamodel.operations.pysat_valid import PySATValid


class Glucose3ErrorDetection(ErrorDetection):
class PySATErrorDetection(ErrorDetection):

def __init__(self) -> None:
self.feature_model: Optional[FeatureModel] = None
Expand All @@ -26,25 +26,25 @@ def get_errors_messages(self) -> list[str]:
def get_result(self) -> list[str]:
return self.errors_messages

def execute(self, model: VariabilityModel) -> 'Glucose3ErrorDetection':
def execute(self, model: VariabilityModel) -> 'PySATErrorDetection':
if self.feature_model is None:
raise FlamaException('The feature model is not setted')

cast_model = cast(PySATModel, model)

# Valid feature model check
valid = Glucose3Valid().execute(cast_model).get_result()
valid = PySATValid().execute(cast_model).get_result()
if not valid:
self.errors_messages.append('The model is not valid (it is void), \
so it has not any product.')

# Dead features detection
dead_features = Glucose3DeadFeatures().execute(cast_model).get_result()
dead_features = PySATDeadFeatures().execute(cast_model).get_result()
if dead_features:
self.errors_messages.append(f'Dead features: {dead_features}')

# False optional detection
fof_op = Glucose3FalseOptionalFeatures()
fof_op = PySATFalseOptionalFeatures()
fof_op.feature_model = self.feature_model
false_optional_features = fof_op.execute(cast_model).get_result()
if false_optional_features:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Optional, cast
import logging
from typing import Any, cast

from pysat.solvers import Solver

Expand All @@ -8,21 +9,19 @@
from flamapy.core.models import VariabilityModel
from flamapy.core.exceptions import FlamaException

LOGGER = logging.getLogger('PySATFalseOptionalFeatures')

class Glucose3FalseOptionalFeatures(FalseOptionalFeatures):

class PySATFalseOptionalFeatures(FalseOptionalFeatures):

def __init__(self) -> None:
self.result: list[Any] = []
self.feature_model: Optional[FeatureModel] = None
self.solver = Solver(name='glucose3')

def execute(self, model: VariabilityModel) -> 'Glucose3FalseOptionalFeatures':
if self.feature_model is None:
raise FlamaException('The feature model is not setted')

def execute(self, model: VariabilityModel) -> 'PySATFalseOptionalFeatures':
model = cast(PySATModel, model)

self.result = self._get_false_optional_features(model, self.feature_model)
self.result = self._get_false_optional_features(model)
return self

def get_false_optional_features(self) -> list[list[Any]]:
Expand All @@ -31,8 +30,12 @@ def get_false_optional_features(self) -> list[list[Any]]:
def get_result(self) -> list[Any]:
return self.result

def _get_false_optional_features(self, sat_model: PySATModel,
feature_model: FeatureModel) -> list[Any]:
def _get_false_optional_features(self, sat_model: PySATModel) -> list[Any]:
try:
feature_model=cast(FeatureModel,sat_model.original_model)
except FlamaException:
LOGGER.exception("The transformation didn't attach the source model, which is required for this operation." )

real_optional_features = [f for f in feature_model.get_features()
if not f.is_root() and not f.is_mandatory()]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flamapy.core.models import VariabilityModel


class Glucose3Filter(Filter):
class PySATFilter(Filter):

def __init__(self) -> None:
self.filter_products: list[list[Any]] = []
Expand All @@ -24,7 +24,7 @@ def get_result(self) -> list[list[Any]]:
def set_configuration(self, configuration: Configuration) -> None:
self.configuration = configuration

def execute(self, model: VariabilityModel) -> 'Glucose3Filter':
def execute(self, model: VariabilityModel) -> 'PySATFilter':
model = cast(PySATModel, model)

for clause in model.get_all_clauses(): # AC es conjunto de conjuntos
Expand Down
Loading

0 comments on commit 1408946

Please sign in to comment.