Skip to content

Commit 5d73f92

Browse files
committed
Several changes to simplify changing identifiers programatically with the fm builder
1 parent b794dbc commit 5d73f92

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

src/main/java/de/vill/model/Attribute.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
public class Attribute<T> implements VariableReference {
2121

2222
private int line;
23-
private final String name;
24-
private final T value;
23+
private String name;
24+
private T value;
2525
private Feature feature;
2626

2727
/**
@@ -53,6 +53,8 @@ public T getValue() {
5353
return value;
5454
}
5555

56+
public void setValue(T value) { this.value = value; }
57+
5658
/**
5759
* Returns the name of the attribute.
5860
*
@@ -62,6 +64,9 @@ public String getName() {
6264
return name;
6365
}
6466

67+
public void setName(String name) { this.name = name; }
68+
69+
6570
/**
6671
* Returns the type of the attribute
6772
* @return Name of the attribute (never null)

src/main/java/de/vill/model/GlobalAttribute.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static AttributeType fromString(String name) {
3434
}
3535
}
3636

37-
private final String identifier;
37+
private String identifier;
3838
private AttributeType type;
3939
private final Set<Feature> featuresContainingAttribute;
4040

@@ -55,6 +55,8 @@ public GlobalAttribute(String identifier, AttributeType type) {
5555
featuresContainingAttribute = new HashSet<>();
5656
}
5757

58+
public void renameGlobalAttribute(String newIdentifier) { this.identifier = newIdentifier;}
59+
5860
public void addFeature(Feature feature) {
5961
featuresContainingAttribute.add(feature);
6062
}

src/main/java/de/vill/model/building/FeatureModelBuilder.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import de.vill.exception.ParseError;
44
import de.vill.model.*;
55
import de.vill.model.constraint.Constraint;
6+
import de.vill.model.constraint.ExpressionConstraint;
67
import de.vill.model.constraint.LiteralConstraint;
8+
import de.vill.model.expression.AggregateFunctionExpression;
9+
import de.vill.model.expression.Expression;
710

811
import java.util.List;
912
import java.util.Map;
@@ -102,7 +105,7 @@ public void addAttribute(Feature feature, Attribute<?> attribute) {
102105
*/
103106
public boolean renameFeature(String oldName, String newName) {
104107
Map<String, Feature> featureMap = fmInConstruction.getFeatureMap();
105-
if (featureMap.containsKey(oldName)) {
108+
if (!featureMap.containsKey(oldName)) {
106109
return false;
107110
}
108111
Feature featureToUpdate = featureMap.get(oldName);
@@ -116,6 +119,49 @@ public boolean renameFeature(String oldName, String newName) {
116119
return true;
117120
}
118121

122+
public void renameAttribute(Attribute<?> attribute, String newName) {
123+
String oldName = attribute.getName();
124+
attribute.setName(newName);
125+
attribute.getFeature().getAttributes().remove(oldName);
126+
attribute.getFeature().getAttributes().put(newName, attribute);
127+
}
128+
129+
public void renameAttributeGlobally(String oldName, String newName) {
130+
for (Feature feature : fmInConstruction.getFeatureMap().values()) {
131+
if (feature.getAttributes().containsKey(oldName)) {
132+
Attribute<?> attribute = feature.getAttributes().get(oldName);
133+
renameAttribute(attribute, newName);
134+
}
135+
}
136+
for (Constraint constraint : fmInConstruction.getConstraints()) {
137+
crawlConstraintsToRenameGlobalAttribute(constraint, oldName, newName);
138+
}
139+
}
140+
private void crawlConstraintsToRenameGlobalAttribute(Constraint constraint, String attributeName, String replace) {
141+
if (constraint instanceof ExpressionConstraint) {
142+
for (Expression exp : ((ExpressionConstraint) constraint).getExpressionSubParts()) {
143+
crawlExpressionsToRenameGlobalAttribute(exp, attributeName, replace);
144+
}
145+
} else {
146+
for (Constraint child : constraint.getConstraintSubParts()) {
147+
crawlConstraintsToRenameGlobalAttribute(child, attributeName, replace);
148+
}
149+
}
150+
}
151+
152+
private void crawlExpressionsToRenameGlobalAttribute(Expression expression, String attributeName, String replace) {
153+
if (expression instanceof AggregateFunctionExpression) {
154+
AggregateFunctionExpression aggregateFunctionExpression = (AggregateFunctionExpression) expression;
155+
if (aggregateFunctionExpression.getAttribute().getIdentifier().equals(attributeName)) {
156+
aggregateFunctionExpression.getAttribute().renameGlobalAttribute(replace);
157+
};
158+
} else {
159+
for (Expression exp : expression.getExpressionSubParts()) {
160+
crawlExpressionsToRenameGlobalAttribute(exp, attributeName, replace);
161+
}
162+
}
163+
}
164+
119165
public void addConstraint(Constraint constraint) {
120166
fmInConstruction.getOwnConstraints().add(0,constraint);
121167
}

0 commit comments

Comments
 (0)