Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andreas pull request #8

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public IFeatureModel getFeatureModel() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return getIdentifier().equals(((AFeatureModelElement) o).getIdentifier());
return Objects.equals(attributeValues, ((AFeatureModelElement) o).attributeValues);
}

@Override
public int hashCode() {
return Objects.hash(getIdentifier());
return Objects.hash(attributeValues);
}
}
169 changes: 169 additions & 0 deletions src/main/java/de/featjar/feature/model/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package de.featjar.feature.model;

import de.featjar.formula.assignment.Assignment;

import java.util.*;
import java.util.stream.Collectors;

/**
* Implementation of a {@link IConfiguration configuration}.
*
* @author Andreas Gerasimow
*/
public class Configuration implements IConfiguration.IMutableConfiguration {

private final Map<String, ConfigurationEntry> configurationEntries;

/**
* Instantiates a configuration from a set of configuration entries.
* @param configurationEntries Configuration entries to add.
*/
public Configuration(Set<ConfigurationEntry> configurationEntries) {
this.configurationEntries = new HashMap<>(
configurationEntries.stream().collect(Collectors.toMap(ConfigurationEntry::getFeatureName, entry -> entry))
);
}

/**
* Instantiates a configuration from a feature model.
* All values in the configuration entries will be <code>null</code>.
* All features will be unselected in the configuration.
* @param featureModel The feature model whose features are to be contained in the configuration.
*/
public Configuration(IFeatureModel featureModel) {
this.configurationEntries = new HashMap<>(
featureModel.getFeatures().stream().collect(Collectors.toMap(
feature -> feature.getName().get(),
feature -> new ConfigurationEntry(feature.getName().get(), null, feature.getType(), ConfigurationEntry.SelectedStatus.UNSELECTED)))
);
}

/**
* Instantiates a configuration from an assignment.
* All features will be unselected in the configuration.
* @param assignment The assignment whose elements are to be contained in the configuration.
*/
public Configuration(Assignment assignment) {
this.configurationEntries = new HashMap<>();
assignment.getAll().forEach((key, value) -> {
configurationEntries.put(key, new ConfigurationEntry(
key,
value,
value.getClass(),
ConfigurationEntry.SelectedStatus.UNSELECTED
));
});
}

/**
* Instantiates an empty configuration.
*/
public Configuration() {
this.configurationEntries = new HashMap<>();
}

@Override
public Set<ConfigurationEntry> getConfigurationEntries() {
return new HashSet<>(configurationEntries.values());
}

@Override
public Set<ConfigurationEntry> getUnselectedConfigurationEntries() {
return configurationEntries
.values()
.stream()
.filter((entry) -> entry.getSelectedStatus() == ConfigurationEntry.SelectedStatus.UNSELECTED)
.collect(Collectors.toSet());
}

@Override
public Set<ConfigurationEntry> getSelectedConfigurationEntries() {
return configurationEntries
.values()
.stream()
.filter((entry) ->
entry.getSelectedStatus() == ConfigurationEntry.SelectedStatus.MANUALLY_SELECTED ||
entry.getSelectedStatus() == ConfigurationEntry.SelectedStatus.AUTOMATICALLY_SELECTED)
.collect(Collectors.toSet());
}

@Override
public Set<ConfigurationEntry> getManuallySelectedConfigurationEntries() {
return configurationEntries
.values()
.stream()
.filter((entry) -> entry.getSelectedStatus() == ConfigurationEntry.SelectedStatus.MANUALLY_SELECTED)
.collect(Collectors.toSet());
}

@Override
public Set<ConfigurationEntry> getAutomaticallySelectedConfigurationEntries() {
return configurationEntries
.values()
.stream()
.filter((entry) -> entry.getSelectedStatus() == ConfigurationEntry.SelectedStatus.AUTOMATICALLY_SELECTED)
.collect(Collectors.toSet());
}

// modifier methods
@Override
public ConfigurationEntry addEntry(ConfigurationEntry entry) {
configurationEntries.put(entry.getFeatureName(), entry);
return entry;
}

@Override
public boolean removeEntry(ConfigurationEntry entry) {
return configurationEntries.remove(entry.getFeatureName(), entry);
}

@Override
public boolean removeEntry(String featureName) {
return configurationEntries.remove(featureName) != null;
}

// other methods

@Override
public Assignment toAssignment() {
LinkedHashMap<String, Object> variableValuePairs = new LinkedHashMap<>();
for (ConfigurationEntry entry : configurationEntries.values()) {
variableValuePairs.put(entry.getFeatureName(), entry.getValue());
}
return new Assignment(variableValuePairs);
}

@Override
public boolean fitsToFeatureModel(IFeatureModel featureModel) {
if (featureModel.getFeatures().size() != configurationEntries.size()) {
return false;
}

return featureModel.getFeatures().stream().allMatch((feature) -> {
ConfigurationEntry entry = configurationEntries.get(feature.getName().get());
return entry != null && entry.getFeatureName().equals(feature.getName().get()) && entry.getType().equals(feature.getType());
});
}

// hashCode / toString / equals

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Configuration that = (Configuration) o;
return Objects.equals(configurationEntries, that.configurationEntries);
}

@Override
public int hashCode() {
return Objects.hashCode(configurationEntries);
}

@Override
public String toString() {
return "Configuration{" +
"configurationEntries=" + configurationEntries +
'}';
}
}
93 changes: 93 additions & 0 deletions src/main/java/de/featjar/feature/model/ConfigurationEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package de.featjar.feature.model;

import java.util.Objects;

/**
* An entry for a {@link Configuration configuration}.
*
* @author Andreas Gerasimow
*/
public class ConfigurationEntry {
public enum SelectedStatus {
UNSELECTED("unselected"),
MANUALLY_SELECTED("manually"),
AUTOMATICALLY_SELECTED("automatic");

private final String name;

SelectedStatus(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

private String featureName;
private Object value;
private Class<?> type;
private SelectedStatus selectedStatus;

public ConfigurationEntry(String featureName, Object value, Class<?> type, SelectedStatus selectedStatus) {
setFeatureName(featureName);
setValue(value);
setType(type);
setSelectedStatus(selectedStatus);
}

public String getFeatureName() {
return featureName;
}

public Object getValue() {
return value;
}

public Class<?> getType() {
return type;
}

public SelectedStatus getSelectedStatus() {
return selectedStatus;
}

public void setFeatureName(String featureName) {
this.featureName = featureName;
}

public void setValue(Object value) {
this.value = value;
}

public void setType(Class<?> type) {
this.type = type;
}

public void setSelectedStatus(SelectedStatus selectedStatus) {
this.selectedStatus = selectedStatus;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConfigurationEntry that = (ConfigurationEntry) o;
return Objects.equals(featureName, that.featureName) && Objects.equals(value, that.value) && Objects.equals(type, that.type) && selectedStatus == that.selectedStatus;
}

@Override
public int hashCode() {
return Objects.hash(featureName, value, type, selectedStatus);
}

@Override
public String toString() {
return "ConfigurationEntry{" +
"featureName='" + featureName + '\'' +
", value=" + value +
", type=" + type +
", selectedStatus=" + selectedStatus +
'}';
}
}
18 changes: 18 additions & 0 deletions src/main/java/de/featjar/feature/model/Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import de.featjar.feature.model.IConstraint.IMutableConstraint;
import de.featjar.formula.structure.IFormula;
import java.util.LinkedHashSet;
import java.util.Objects;

/**
* Implementation of a {@link IConstraint}.
*/
public class Constraint extends AFeatureModelElement implements IMutableConstraint {
protected IFormula formula;
protected final LinkedHashSet<IFeature> containedFeaturesCache = Sets.empty();
Expand Down Expand Up @@ -76,6 +80,20 @@ public void setFormula(IFormula formula) {
Constraint.this.formula = formula;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Constraint that = (Constraint) o;
return Objects.equals(formula, that.formula);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), formula);
}

@Override
public void setName(String name) {
attributeValues.put(Attributes.NAME, name);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/de/featjar/feature/model/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

import de.featjar.base.data.Result;
import de.featjar.feature.model.IFeature.IMutableFeature;
import java.util.Objects;

/**
* Implementation of a {@link IFeature}.
*/
public class Feature extends AFeatureModelElement implements IMutableFeature {
protected Class<?> type;

Expand Down Expand Up @@ -74,6 +78,20 @@ public void setName(String name) {
attributeValues.put(Attributes.NAME, name);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Feature feature = (Feature) o;
return Objects.equals(type, feature.type);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), type);
}

@Override
public void setDescription(String description) {
attributeValues.put(Attributes.DESCRIPTION, description);
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/de/featjar/feature/model/FeatureModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import java.util.Objects;
import java.util.Optional;

/**
* Implementation of a {@link IFeatureModel feature model}.
*/
public class FeatureModel implements IMutableFeatureModel, IMutatableAttributable {

protected final IIdentifier identifier;
Expand Down Expand Up @@ -163,12 +166,36 @@ public <S> S removeAttributeValue(Attribute<S> attribute) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return getIdentifier().equals(((FeatureModel) o).getIdentifier());
FeatureModel that = (FeatureModel) o;

if (featureTreeRoots.size() != that.featureTreeRoots.size()) return false;
for (int i = 0; i < featureTreeRoots.size(); i++) {
boolean eq = featureTreeRoots.get(i).equalsNode(that.featureTreeRoots.get(i));
if (!eq) return false;
}

List<IFeature> featuresList = new ArrayList<>(features.values());
List<IFeature> thatFeaturesList = new ArrayList<>(that.features.values());
if (thatFeaturesList.size() != featuresList.size()) return false;
for (int i = 0; i < featuresList.size(); i++) {
boolean eq = featuresList.get(i).equals(thatFeaturesList.get(i));
if (!eq) return false;
}

List<IConstraint> constraintsList = new ArrayList<>(constraints.values());
List<IConstraint> thatConstraintsList = new ArrayList<>(that.constraints.values());
if (thatConstraintsList.size() != constraintsList.size()) return false;
for (int i = 0; i < constraintsList.size(); i++) {
boolean eq = constraintsList.get(i).equals(thatConstraintsList.get(i));
if (!eq) return false;
}

return Objects.equals(attributeValues, that.attributeValues);
}

@Override
public int hashCode() {
return Objects.hash(getIdentifier());
return Objects.hash(featureTreeRoots, constraints.values(), attributeValues, features.values());
}

@Override
Expand Down
Loading