Skip to content
Draft
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
@@ -0,0 +1,15 @@
package de.vill.model.building;

import de.vill.exception.ParseError;
import de.vill.model.*;
import de.vill.model.constraint.LiteralConstraint;

public abstract class AbstractUVLElementFactory {

public abstract Feature createFeature(String name);

public abstract <T> Attribute<T> createAttribute(String name, T value, Feature correspondingFeature);

public abstract GlobalAttribute createGlobalAttribute(String identifier, FeatureModel featureModel);

}
23 changes: 23 additions & 0 deletions src/main/java/de/vill/model/building/DefaultUVLElementFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.vill.model.building;

import de.vill.exception.ParseError;
import de.vill.model.*;
import de.vill.model.constraint.LiteralConstraint;

public class DefaultUVLElementFactory extends AbstractUVLElementFactory {

@Override
public Feature createFeature(String name) {
return new Feature(name);
}

@Override
public <T> Attribute<T> createAttribute(String name, T value, Feature correspondingFeature) {
return new Attribute<>(name, value, correspondingFeature);
}

@Override
public GlobalAttribute createGlobalAttribute(String identifier, FeatureModel featureModel) {
return new GlobalAttribute(identifier, featureModel);
}
}
42 changes: 26 additions & 16 deletions src/main/java/de/vill/model/building/FeatureModelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
*
*/
public class FeatureModelBuilder {

private FeatureModel fmInConstruction;

private AbstractUVLElementFactory elementFactory;

public FeatureModelBuilder() {
fmInConstruction = new FeatureModel();
this(new DefaultUVLElementFactory());
}

public FeatureModelBuilder(AbstractUVLElementFactory factory) {
this.elementFactory = factory;
this.fmInConstruction = new FeatureModel();
}

public FeatureModelBuilder(FeatureModel old) {
fmInConstruction = old;
}


public void addImport(Import importToAdd) {
fmInConstruction.getImports().add(importToAdd);
}
Expand All @@ -49,7 +53,7 @@ public boolean setRootFeature(Feature feature) {
}

public Feature addRootFeature(String featureName, FeatureType featureType, Cardinality cardinality) {
Feature rootFeature = new Feature(featureName);
Feature rootFeature = elementFactory.createFeature(featureName);
rootFeature.setFeatureType(featureType);
rootFeature.setCardinality(cardinality);

Expand All @@ -65,13 +69,14 @@ public Feature addFeature(String featureName, Group group) {
public Feature addFeature(String featureName, Group group, Import featureOrigin) {
return addFeature(featureName, group, featureOrigin, FeatureType.BOOL);
}
public Feature addFeature(String featureName, Group group, Import featureOrigin, FeatureType type) {

public Feature addFeature(String featureName, Group group, Import featureOrigin, FeatureType type) {
return addFeature(featureName, group, featureOrigin, type, Cardinality.getStandardFeatureCardinality());
}

public Feature addFeature(String featureName, Group group, Import featureOrigin, FeatureType type, Cardinality cardinality) {
Feature feature = new Feature(featureName);
public Feature addFeature(String featureName, Group group, Import featureOrigin, FeatureType type,
Cardinality cardinality) {
Feature feature = elementFactory.createFeature(featureName);
feature.setRelatedImport(featureOrigin);
feature.setFeatureType(type);
feature.setCardinality(cardinality);
Expand Down Expand Up @@ -99,9 +104,11 @@ public void addAttribute(Feature feature, Attribute<?> attribute) {

/**
* Renames a feature and propagates the change through the mdoel
*
* @param oldName current name of feature to be renamed in the model
* @param newName target name
* @return indicator whether oldName successfully changed in the feature model; fails if oldName is not present
* @return indicator whether oldName successfully changed in the feature model;
* fails if oldName is not present
*/
public boolean renameFeature(String oldName, String newName) {
Map<String, Feature> featureMap = fmInConstruction.getFeatureMap();
Expand Down Expand Up @@ -137,6 +144,7 @@ public void renameAttributeGlobally(String oldName, String newName) {
crawlConstraintsToRenameGlobalAttribute(constraint, oldName, newName);
}
}

private void crawlConstraintsToRenameGlobalAttribute(Constraint constraint, String attributeName, String replace) {
if (constraint instanceof ExpressionConstraint) {
for (Expression exp : ((ExpressionConstraint) constraint).getExpressionSubParts()) {
Expand All @@ -154,7 +162,8 @@ private void crawlExpressionsToRenameGlobalAttribute(Expression expression, Stri
AggregateFunctionExpression aggregateFunctionExpression = (AggregateFunctionExpression) expression;
if (aggregateFunctionExpression.getAttribute().getIdentifier().equals(attributeName)) {
aggregateFunctionExpression.getAttribute().renameGlobalAttribute(replace);
};
}
;
} else {
for (Expression exp : expression.getExpressionSubParts()) {
crawlExpressionsToRenameGlobalAttribute(exp, attributeName, replace);
Expand All @@ -163,15 +172,16 @@ private void crawlExpressionsToRenameGlobalAttribute(Expression expression, Stri
}

public void addConstraint(Constraint constraint) {
fmInConstruction.getOwnConstraints().add(0,constraint);
fmInConstruction.getOwnConstraints().add(0, constraint);
}

public void addConstraintAtPosition(Constraint constraint, int position) {
fmInConstruction.getOwnConstraints().add(position,constraint);
fmInConstruction.getOwnConstraints().add(position, constraint);
}

public boolean doesFeatureModelSatisfyLanguageLevels(Set<LanguageLevel> languageLevelsToSatisfy) {
return fmInConstruction.isExplicitLanguageLevels() && !fmInConstruction.getUsedLanguageLevels().equals(languageLevelsToSatisfy);
return fmInConstruction.isExplicitLanguageLevels()
&& !fmInConstruction.getUsedLanguageLevels().equals(languageLevelsToSatisfy);
}

public FeatureModel getFeatureModel() {
Expand All @@ -192,7 +202,7 @@ public LiteralConstraint createFeatureLiteral(String name) {
}

public GlobalAttribute createGlobalAttribute(String name) {
GlobalAttribute toCreate = new GlobalAttribute(name, fmInConstruction);
GlobalAttribute toCreate = elementFactory.createGlobalAttribute(name, fmInConstruction);
if (toCreate.getType() == null) {
System.err.println("Tried to reference " + name + " but attribute with that name does not exist");
return null;
Expand Down
Loading