Skip to content

Commit f767522

Browse files
committed
Refactoring of HostModel complete, except that the implicit type graph
of the generated host graph may deviate; check!
1 parent 9982ee2 commit f767522

File tree

6 files changed

+252
-161
lines changed

6 files changed

+252
-161
lines changed

src/main/java/nl/utwente/groove/grammar/aspect/AspectElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public interface AspectElement extends Element, Fixable {
4444
/** If */
4545
default public AspectElement denormalise() {
4646
if (getGraph() instanceof NormalAspectGraph nag) {
47-
return nag.normalToSourceMap().get(this);
47+
return nag.normalToOriginalMap().get(this);
4848
} else {
4949
return this;
5050
}

src/main/java/nl/utwente/groove/grammar/aspect/NormalAspectGraph.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,96 +63,96 @@ public class NormalAspectGraph extends AspectGraph {
6363
/**
6464
* Creates the normalised version of a given aspect graph.
6565
* Normalisation occurs upon the call if {@link #setFixed()}.
66-
* @param source the (non-normalised) source of this normalised aspect graph
66+
* @param origin the (non-normalised) source of this normalised aspect graph
6767
*/
68-
public NormalAspectGraph(AspectGraph source) {
69-
super(source.getName(), source.getRole(), source.isSimple());
70-
var toNormalMap = this.sourceToNormalMap = new AspectGraphMorphism(this);
71-
source.cloneTo(toNormalMap);
72-
this.source = source;
73-
this.normalToSourceMap = initNormalToSourceMap();
68+
public NormalAspectGraph(AspectGraph origin) {
69+
super(origin.getName(), origin.getRole(), origin.isSimple());
70+
var toNormalMap = this.originToNormalMap = new AspectGraphMorphism(this);
71+
origin.cloneTo(toNormalMap);
72+
this.origin = origin;
73+
this.normalToOriginMap = initNormalToOriginalMap();
7474
}
7575

7676
@Override
7777
public boolean isNormal() {
7878
return true;
7979
}
8080

81-
/** Returns the (non-normalised) source of this normalised aspect graph. */
82-
public AspectGraph getOriginal() {
83-
return this.source;
81+
/** Returns the (non-normalised) origin of this normalised aspect graph. */
82+
public AspectGraph getOrigin() {
83+
return this.origin;
8484
}
8585

86-
/** Returns the (non-normalised) source of this normalised aspect graph. */
87-
public FormatErrorSet getOriginalErrors() {
88-
return getErrors().apply(this.normalToSourceMap);
86+
/** Returns the errors in this normal graph, transferred to the context of the origin. */
87+
public FormatErrorSet getOriginErrors() {
88+
return getErrors().apply(this.normalToOriginMap);
8989
}
9090

9191
/** The (non-normalised) source of this normalised aspect graph. */
92-
private final AspectGraph source;
92+
private final AspectGraph origin;
9393

9494
/** Returns the morphism from the source {@link AspectGraph} to this normalised {@link AspectGraph}.
9595
* Should only be called after the normalised graph has been fixed.
9696
*/
97-
public AspectGraphMorphism sourceToNormalMap() {
97+
public AspectGraphMorphism originToNormalMap() {
9898
assert isFixed();
99-
return this.sourceToNormalMap;
99+
return this.originToNormalMap;
100100
}
101101

102102
/** Morphism from the source {@link AspectGraph} to the normalised {@link AspectGraph}. */
103-
private final AspectGraphMorphism sourceToNormalMap;
103+
private final AspectGraphMorphism originToNormalMap;
104104

105-
/** Computes the inverse of {@link #sourceToNormalMap}. */
106-
private final Map<AspectElement,AspectElement> initNormalToSourceMap() {
105+
/** Computes the inverse of {@link #originToNormalMap}. */
106+
private final Map<AspectElement,AspectElement> initNormalToOriginalMap() {
107107
Map<AspectElement,AspectElement> result = new HashMap<>();
108-
var toNormalMap = this.sourceToNormalMap;
108+
var toNormalMap = this.originToNormalMap;
109109
toNormalMap.nodeMap().entrySet().forEach(e -> result.put(e.getValue(), e.getKey()));
110110
toNormalMap.edgeMap().entrySet().forEach(e -> result.put(e.getValue(), e.getKey()));
111111
return result;
112112
}
113113

114-
/** Adds an entry to the {@link #normalToSourceMap} for a new element in the
114+
/** Adds an entry to the {@link #normalToOriginMap} for a new element in the
115115
* normalised graph, based on an original element (also of this graph).
116116
*/
117117
private void addNormalToSource(AspectElement orig, AspectElement added) {
118-
var normalToSourceMap = this.normalToSourceMap;
118+
var normalToSourceMap = this.normalToOriginMap;
119119
normalToSourceMap.put(added, normalToSourceMap.get(orig));
120120
}
121121

122122
/** Replaces a given aspect edge in this normalised graph by another, in the
123-
* {@link #sourceToNormalMap} and {@link #normalToSourceMap}.
123+
* {@link #originToNormalMap} and {@link #normalToOriginMap}.
124124
* Does not remove or add either of the edges from or to the graph
125125
* @param orig the original edge
126126
* @param replacement the new edge
127127
*/
128128
private void replaceNormalisedEdge(AspectEdge orig, AspectEdge replacement) {
129-
var source = this.normalToSourceMap.remove(orig);
129+
var source = this.normalToOriginMap.remove(orig);
130130
if (source instanceof AspectEdge edge) {
131-
this.sourceToNormalMap.putEdge(edge, replacement);
131+
this.originToNormalMap.putEdge(edge, replacement);
132132
}
133133
}
134134

135135
/** Replaces a given aspect node in this normalised graph by another, in the
136-
* {@link #sourceToNormalMap} and {@link #normalToSourceMap}.
136+
* {@link #originToNormalMap} and {@link #normalToOriginMap}.
137137
* Does not remove or add either of the nodes from or to the graph
138138
* @param orig the original node
139139
* @param replacement the new node
140140
*/
141141
private void replaceNormalisedNode(AspectNode orig, AspectNode replacement) {
142-
var source = (AspectNode) this.normalToSourceMap.remove(orig);
143-
this.sourceToNormalMap.putNode(source, replacement);
142+
var source = (AspectNode) this.normalToOriginMap.remove(orig);
143+
this.originToNormalMap.putNode(source, replacement);
144144
}
145145

146146
/** Returns the morphism from this normalised graph to the source {@link AspectGraph}.
147147
* Should only be called after the normalised graph has been fixed.
148148
*/
149-
public Map<AspectElement,AspectElement> normalToSourceMap() {
149+
public Map<AspectElement,AspectElement> normalToOriginalMap() {
150150
assert isFixed();
151-
return this.normalToSourceMap;
151+
return this.normalToOriginMap;
152152
}
153153

154-
/** Inverse of the {@link #sourceToNormalMap} before actual normalisation. */
155-
private final Map<AspectElement,AspectElement> normalToSourceMap;
154+
/** Inverse of the {@link #originToNormalMap} before actual normalisation. */
155+
private final Map<AspectElement,AspectElement> normalToOriginMap;
156156

157157
private AspectNode addNormalisedNode(AspectElement orig) {
158158
AspectNode result = addNode();
@@ -331,7 +331,7 @@ private void doNormalise() {
331331
removeNode(node);
332332
}
333333
addErrors(errors);
334-
getErrors().apply(this.normalToSourceMap);
334+
getErrors().apply(this.normalToOriginMap);
335335
}
336336

337337
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package nl.utwente.groove.grammar.model;
2+
3+
import static nl.utwente.groove.grammar.model.ResourceKind.HOST;
4+
import static nl.utwente.groove.grammar.model.ResourceKind.PROPERTIES;
5+
import static nl.utwente.groove.grammar.model.ResourceKind.TYPE;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import org.eclipse.jdt.annotation.NonNull;
11+
12+
import nl.utwente.groove.grammar.QualName;
13+
import nl.utwente.groove.grammar.aspect.AspectGraph;
14+
import nl.utwente.groove.grammar.host.HostGraph;
15+
import nl.utwente.groove.util.parse.FormatError;
16+
import nl.utwente.groove.util.parse.FormatErrorSet;
17+
import nl.utwente.groove.util.parse.FormatException;
18+
19+
/** Class to store the models that are used to compose the type graph. */
20+
public class CompositeHostModel extends ResourceModel<HostGraph> {
21+
/**
22+
* Constructs a composite type model
23+
* @param grammar the underlying graph grammar
24+
*/
25+
CompositeHostModel(@NonNull GrammarModel grammar) {
26+
super(grammar, TYPE);
27+
setDependencies(PROPERTIES);
28+
}
29+
30+
@Override
31+
public @NonNull GrammarModel getGrammar() {
32+
var result = super.getGrammar();
33+
assert result != null;
34+
return result;
35+
}
36+
37+
@Override
38+
public Object getSource() {
39+
return null;
40+
}
41+
42+
@Override
43+
public String getName() {
44+
return "Composite host graph";
45+
}
46+
47+
/** Indicates if this model is composed of more than one underlying host model. */
48+
public boolean isMultiple() {
49+
return getGrammar().getActiveNames(HOST).size() > 1;
50+
}
51+
52+
@Override
53+
HostGraph compute() throws FormatException {
54+
var hostModelMap = computeHostModelMap();
55+
if (hostModelMap.isEmpty()) {
56+
throw new FormatException("No active start graph");
57+
} else {
58+
AspectGraph startGraph = AspectGraph.mergeGraphs(getGrammar().getActiveGraphs(HOST));
59+
return new HostModel(getGrammar(), startGraph).compute();
60+
}
61+
}
62+
63+
/**
64+
* Computes the mapping from names to active host models.
65+
* @throws FormatException if there are format errors in the active type models
66+
*/
67+
private Map<QualName,HostModel> computeHostModelMap() throws FormatException {
68+
FormatErrorSet errors = new FormatErrorSet();
69+
Map<QualName,HostModel> result = new HashMap<>();
70+
for (var activeHostName : getGrammar().getActiveNames(HOST)) {
71+
ResourceModel<?> hostModel = getGrammar().getResource(HOST, activeHostName);
72+
result.put(activeHostName, (HostModel) hostModel);
73+
for (FormatError error : hostModel.getErrors()) {
74+
errors
75+
.add("Error in host graph '%s': %s", activeHostName, error,
76+
hostModel.getSource());
77+
}
78+
}
79+
errors.throwException();
80+
return result;
81+
}
82+
83+
/** Fixed name for the composite host model. */
84+
static public final String NAME = "composite-host";
85+
}

src/main/java/nl/utwente/groove/grammar/model/HostModel.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import nl.utwente.groove.grammar.aspect.AspectGraph;
2424
import nl.utwente.groove.grammar.host.HostGraph;
2525
import nl.utwente.groove.grammar.host.ValueNode;
26-
import nl.utwente.groove.grammar.type.ImplicitTypeGraph;
2726
import nl.utwente.groove.grammar.type.TypeGraph;
2827
import nl.utwente.groove.grammar.type.TypeLabel;
2928
import nl.utwente.groove.util.Factory;
@@ -62,25 +61,14 @@ public HostModelMap getMap() {
6261
if (hasErrors()) {
6362
throw new IllegalStateException();
6463
}
65-
return this.mappedModel.get().map();
64+
return this.morphism.get().map();
6665
}
6766

6867
@Override
6968
public TypeGraph getTypeGraph() {
70-
return this.typeGraph.get();
69+
return this.morphism.get().getTypeGraph();
7170
}
7271

73-
private final Factory<TypeGraph> typeGraph = Factory.lazy(() -> {
74-
TypeGraph result;
75-
var grammar = getGrammar();
76-
if (grammar == null) {
77-
result = ImplicitTypeGraph.newInstance(getLabels());
78-
} else {
79-
result = grammar.getTypeGraph();
80-
}
81-
return result;
82-
});
83-
8472
@Override
8573
public TypeModelMap getTypeMap() {
8674
return this.typeMap.get();
@@ -93,10 +81,10 @@ private TypeModelMap createTypeMap() {
9381
synchronise();
9482
TypeModelMap result = null;
9583
if (getStatus() == Status.DONE) {
96-
var mappedModel = this.mappedModel.get();
97-
var hostModelMap = mappedModel.map();
84+
var morphism = this.morphism.get();
85+
var hostModelMap = morphism.map();
9886
// create the type map
99-
result = new TypeModelMap(mappedModel.getTypeGraph().getFactory());
87+
result = new TypeModelMap(morphism.getTypeGraph().getFactory());
10088
for (var ne : hostModelMap.nodeMap().entrySet()) {
10189
result.putNode(ne.getKey(), ne.getValue().getType());
10290
}
@@ -130,25 +118,18 @@ private Set<TypeLabel> createLabels() {
130118
void notifyWillRebuild() {
131119
super.notifyWillRebuild();
132120
this.labelSet.reset();
133-
this.mappedModel.reset();
121+
this.morphism.reset();
134122
this.typeMap.reset();
135123
}
136124

137125
@Override
138126
HostGraph compute() throws FormatException {
139127
getSource().getErrors().throwException();
140-
var result = this.mappedModel.get().target();
141-
result.getErrors().throwException();
142-
return result;
128+
var morphism = this.morphism.get();
129+
morphism.getErrors().throwException();
130+
return morphism.target();
143131
}
144132

145-
private final Factory<HostModelMorphism> mappedModel
133+
private final Factory<HostModelMorphism> morphism
146134
= Factory.lazy(() -> HostModelMorphism.instance(getGrammar(), getSource()));
147-
148-
/** Sets the debug mode, causing the normalised graphs to be shown in a dialog. */
149-
public static void setDebug(boolean debug) {
150-
HostModel.debug = debug;
151-
}
152-
153-
static boolean debug;
154135
}

0 commit comments

Comments
 (0)