Skip to content

Commit

Permalink
Minor improvements based on #gh 795
Browse files Browse the repository at this point in the history
  • Loading branch information
rensink committed Nov 18, 2024
1 parent 0be1bcd commit 8b4cdeb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
34 changes: 27 additions & 7 deletions src/main/java/nl/utwente/groove/grammar/type/TypeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,25 @@ public void test() throws FormatException {
}
if (hasCommonSubtype(edge1.source(), edge2.source())
&& hasCommonSubtype(edge1.target(), edge2.target())) {
errors
.add("Potential type confusion of %s-%ss: both source and target types have a common subtype",
edgeLabel.text(), edgeLabel.getRole() == FLAG
? "flag"
: "edge",
edge1, edge2);
var sourceSubtype = getCommonSubtypes(edge1.source(), edge2.source())
.stream()
.findFirst()
.get();
if (edgeLabel.hasRole(FLAG)) {
errors
.add("Potential type confusion: Declared flags %3$s:%1$s and %4$s:%1$s cause %2$s:%1$s to be ambiguously typed",
edgeLabel.text(), sourceSubtype, edge1.source(),
edge2.source(), edge1, edge2, edge1.getGraph(),
edge2.getGraph());
} else {
var targetSubtype = getCommonSubtypes(edge1.target(), edge2.target())
.stream()
.findFirst()
.get();
errors
.add("Potential type confusion: Declared edges %4$s and %5$s cause %2$s--%1$s->%3$s to be ambiguously typed",
edgeLabel.text(), sourceSubtype, targetSubtype, edge1, edge2);
}
}
}
}
Expand Down Expand Up @@ -1115,7 +1128,7 @@ private TypeLabel getActualType(Label type) {
return result;
}

/** Tests if two nodes have a common subtype. */
/** Tests if two nodes have a common (not necessarily proper) subtype. */
private boolean hasCommonSubtype(TypeNode node1, TypeNode node2) {
// check for common subtypes
if (node1.equals(node2)) {
Expand All @@ -1140,6 +1153,13 @@ private boolean hasCommonSubtype(TypeNode node1, TypeNode node2) {
return !Collections.disjoint(sub1, sub2);
}

/** Returns the set of all (not necessarily proper) subtypes. */
private Set<TypeNode> getCommonSubtypes(TypeNode node1, TypeNode node2) {
Set<TypeNode> result = new HashSet<>(getSubtypes(node1));
result.retainAll(getSubtypes(node2));
return result;
}

/** Returns the set of all type labels occurring in the type graph. */
public Set<TypeLabel> getLabels() {
testFixed(true);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/nl/utwente/groove/util/parse/FormatError.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import nl.utwente.groove.grammar.model.ControlModel;
import nl.utwente.groove.grammar.model.PrologModel;
import nl.utwente.groove.grammar.model.ResourceKind;
import nl.utwente.groove.grammar.type.TypeGraph;
import nl.utwente.groove.graph.Edge;
import nl.utwente.groove.graph.EdgeComparator;
import nl.utwente.groove.graph.Element;
Expand Down Expand Up @@ -79,6 +80,8 @@ private void addContext(Object par) {
c.forEach(this::addContext);
} else if (par instanceof FormatError e) {
e.getArguments().forEach(this::addContext);
this.resourceKind = e.getResourceKind();
e.getResourceNames().forEach(n -> addResource(getResourceKind(), n));
} else if (par instanceof Key k) {
this.key = k;
} else if (par instanceof GraphState s) {
Expand All @@ -96,6 +99,8 @@ private void addContext(Object par) {
this.elements.add(e);
} else if (par instanceof Integer i) {
this.numbers.add(i);
} else if (par instanceof TypeGraph tg) {
addResource(ResourceKind.TYPE, tg.getQualName());
} else if (par instanceof Rule r) {
addResource(ResourceKind.RULE, r.getQualName());
} else if (par instanceof Recipe r) {
Expand Down Expand Up @@ -282,7 +287,6 @@ public final SortedSet<QualName> getResourceNames() {
FormatError transfer(GraphMap map) {
var result = this;
if (!map.isEmpty()) {
result = new FormatError(toFormattableString());
Map<Object,Object> elementMap = new HashMap<>();
elementMap.putAll(map.nodeMap());
elementMap.putAll(map.edgeMap());
Expand Down Expand Up @@ -371,6 +375,7 @@ private FormatError clone(@Nullable Map<?,?> map) {
: arg;
result.addContext(newArg);
}
result.resourceKind = getResourceKind();
getResourceNames().forEach(n -> result.addResource(getResourceKind(), n));
return result;
}
Expand Down

0 comments on commit 8b4cdeb

Please sign in to comment.