Skip to content

Commit

Permalink
Merge branch 'master' of [email protected]:nl-utwente-groove/code.git
Browse files Browse the repository at this point in the history
  • Loading branch information
rensink committed Jul 23, 2024
2 parents fdb1c7f + 370c5ec commit 773a6c9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 62 deletions.
22 changes: 14 additions & 8 deletions src/main/java/nl/utwente/groove/control/CtrlLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public Program buildProgram(Collection<QualName> progNames) throws FormatExcepti
errors.throwException();
if (!result.hasMain()) {
// try to parse "any" for static semantic checks
Fragment main = addControl(QualName.name(DEFAULT_MAIN_NAME), getDefaultMain()).check()
Fragment main = addControl(QualName.name(DEFAULT_MAIN_NAME), getDefaultMain())
.check()
.toFragment();
result.add(main);
}
Expand Down Expand Up @@ -170,15 +171,15 @@ public Map<QualName,String> changePriority(Map<QualName,Integer> prioMap) {
CtrlTree tree = this.controlTreeMap.get(controlName);
assert tree != null : String.format("Parse tree of %s not found", controlName);
CtrlTree recipeTree = tree.getProcs(Kind.RECIPE).get(recipeName);
assert recipeTree != null : String.format("Recipe declaration of %s not found",
recipeName);
assert recipeTree != null : String
.format("Recipe declaration of %s not found", recipeName);
TokenRewriteStream rewriter = getRewriter(tree);
boolean changed = false;
if (recipeTree.getChildCount() == 3) {
// no explicit priority
if (newPriority != 0) {
rewriter.insertAfter(recipeTree.getChild(1).getToken(),
"priority " + newPriority);
rewriter
.insertAfter(recipeTree.getChild(1).getToken(), "priority " + newPriority);
changed = true;
}
} else {
Expand All @@ -198,7 +199,11 @@ public Map<QualName,String> changePriority(Map<QualName,Integer> prioMap) {

private TokenRewriteStream getRewriter(CtrlTree tree) {
CtrlLexer lexer = new CtrlLexer(null);
lexer.setCharStream(new ANTLRStringStream(tree.toInputString()));
var inputString = tree.toInputString();
lexer
.setCharStream(new ANTLRStringStream(inputString == null
? ""
: inputString));
TokenRewriteStream rewriter = new TokenRewriteStream(lexer);
rewriter.fill();
return rewriter;
Expand Down Expand Up @@ -241,8 +246,9 @@ public static void main(String[] args) {
Grammar grammar = Groove.loadGrammar(grammarName).toGrammar();
for (int i = 1; i < args.length; i++) {
String programName = CONTROL.stripExtension(args[1]);
System.out.printf("Control automaton for %s:%n%s", programName,
run(grammar, programName, new File(grammarName)));
System.out
.printf("Control automaton for %s:%n%s", programName,
run(grammar, programName, new File(grammarName)));
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nl/utwente/groove/grammar/CheckPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public String unparse(PolicyMap value) {
if (e.getValue() != ERROR) {
result.append(e.getKey());
result.append(ASSIGN_CHAR);
result.append(e.getValue());
result.append(e.getValue().getName());
result.append(' ');
}
}
Expand Down
75 changes: 73 additions & 2 deletions src/main/java/nl/utwente/groove/grammar/GrammarProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -341,8 +343,8 @@ public void setActiveNames(ResourceKind kind, List<QualName> names) {
* @return a (non-{@code null}, but possibly empty) set of active names
*/
public Set<QualName> getActiveNames(ResourceKind kind) {
assert kind != ResourceKind.RULE;
if (kind == ResourceKind.CONFIG || kind == ResourceKind.GROOVY) {
if (kind == ResourceKind.CONFIG || kind == ResourceKind.GROOVY
|| kind == ResourceKind.PROPERTIES || kind == ResourceKind.RULE) {
return Collections.emptySet();
}
List<QualName> names = parsePropertyOrDefault(resourceKeyMap.get(kind)).getQualNameList();
Expand Down Expand Up @@ -512,6 +514,75 @@ public GrammarProperties relabel(TypeLabel oldLabel, TypeLabel newLabel) {
: this;
}

/**
* Returns a clone of this properties object where all occurrences of a
* given collection of resource names are deleted.
* @param names the names to be deleted
* @return a clone of these properties, or the properties themselves if
* {@code names} did not occur
*/
public GrammarProperties deleteResources(ResourceKind kind, Collection<QualName> names) {
GrammarProperties result = clone();
boolean hasChanged = false;
var activeNames = new HashSet<>(getActiveNames(kind));
if (activeNames.removeAll(names)) {
hasChanged = true;
var orderedActiveNames = new ArrayList<>(activeNames);
orderedActiveNames.sort(null);
result.setActiveNames(kind, orderedActiveNames);
hasChanged = true;
}
// change the control labels
if (kind == ResourceKind.RULE) {
var actionPolicy = new PolicyMap();
actionPolicy.putAll(getRulePolicy());
var policyChanged = actionPolicy.keySet().removeAll(names);
if (policyChanged) {
result.setRulePolicy(actionPolicy);
hasChanged = true;
}
}
return hasChanged
? result
: this;
}

/**
* Returns a clone of this properties object where all occurrences of a
* given resource name are replaced by a new name.
* @param oldName the name to be replaced
* @param newName the new value for {@code oldName}
* @return a clone of these properties, or the properties themselves if
* {@code oldName} did not occur
*/
public GrammarProperties renameResource(ResourceKind kind, QualName oldName, QualName newName) {
GrammarProperties result = clone();
boolean hasChanged = false;
var activeNames = new HashSet<>(getActiveNames(kind));
if (activeNames.remove(oldName)) {
hasChanged = true;
activeNames.add(newName);
var orderedActiveNames = new ArrayList<>(activeNames);
orderedActiveNames.sort(null);
result.setActiveNames(kind, orderedActiveNames);
hasChanged = true;
}
// change the control labels
if (kind == ResourceKind.RULE) {
var actionPolicy = new PolicyMap();
actionPolicy.putAll(getRulePolicy());
var namePolicy = actionPolicy.remove(oldName);
if (namePolicy != null) {
actionPolicy.put(newName, namePolicy);
result.setRulePolicy(actionPolicy);
hasChanged = true;
}
}
return hasChanged
? result
: this;
}

/**
* Checks if the stored properties are valid in a given grammar.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/nl/utwente/groove/gui/tree/TypeTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ private void addRelatedTypes(Set<? extends TypeNode> typeNodes, TypedEntryNode t
Set<TypeNode> relatedTypes = map.get(type);
assert relatedTypes != null : String
.format("Node type '%s' does not occur in type graph '%s'", type, map.keySet());
for (TypeNode relType : relatedTypes) {
var orderedRelatedTypes = new TreeSet<>(relatedTypes);
for (TypeNode relType : orderedRelatedTypes) {
// test if the node type label exists in the partial type graph
if (typeNodes.contains(relType)) {
TypedEntryNode subTypeNode = new TypedEntryNode(this, relType, false);
Expand Down
63 changes: 13 additions & 50 deletions src/main/java/nl/utwente/groove/io/store/SystemStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/
package nl.utwente.groove.io.store;

import static nl.utwente.groove.grammar.model.ResourceKind.GROOVY;
import static nl.utwente.groove.grammar.model.ResourceKind.PROPERTIES;
import static nl.utwente.groove.grammar.model.ResourceKind.RULE;
import static nl.utwente.groove.io.FileType.GRAMMAR;
import static nl.utwente.groove.io.store.EditType.LAYOUT;

Expand Down Expand Up @@ -255,23 +253,17 @@ GraphBasedEdit doDeleteGraphs(ResourceKind kind,
Collection<QualName> names) throws IOException {
testInit();
List<AspectGraph> deletedGraphs = new ArrayList<>(names.size());
List<QualName> activeNames = getRecordedActiveNames(kind);
boolean activeChanged = false;
for (QualName name : names) {
AspectGraph graph = getGraphMap(kind).remove(name);
assert graph != null;
File oldFile = createFile(kind, name);
this.marshaller.deleteGraph(oldFile);
deleteEmptyDirectories(oldFile.getParentFile());
deletedGraphs.add(graph);
activeChanged |= activeNames.remove(name);
}
GrammarProperties oldProps = null;
GrammarProperties newProps = null;
if (activeChanged) {
oldProps = getProperties();
newProps = getProperties().clone();
newProps.setActiveNames(kind, activeNames);
}
GrammarProperties oldProps = getProperties();
GrammarProperties newProps = oldProps.deleteResources(kind, names);
if (newProps != oldProps) {
doPutProperties(newProps);
}
return new GraphBasedEdit(kind, EditType.DELETE, deletedGraphs,
Expand Down Expand Up @@ -355,24 +347,18 @@ public Map<QualName,String> deleteTexts(ResourceKind kind,
TextBasedEdit doDeleteTexts(ResourceKind kind, Collection<QualName> names) throws IOException {
testInit();
Map<QualName,String> oldTexts = new HashMap<>();
boolean activeChanged = false;
var activeNames = getRecordedActiveNames(kind);
for (QualName name : names) {
assert name != null;
String text = getTextMap(kind).remove(name);
if (text != null) {
oldTexts.put(name, text);
createFile(kind, name).delete();
activeChanged |= activeNames.remove(name);
}
}
// change the control-related system properties, if necessary
GrammarProperties oldProps = null;
GrammarProperties newProps = null;
if (activeChanged) {
oldProps = getProperties();
newProps = getProperties().clone();
newProps.setActiveNames(kind, activeNames);
GrammarProperties oldProps = getProperties();
GrammarProperties newProps = oldProps.deleteResources(kind, names);
if (newProps != oldProps) {
doPutProperties(newProps);
}
return new TextBasedEdit(kind, EditType.DELETE, oldTexts,
Expand Down Expand Up @@ -416,14 +402,9 @@ TextBasedEdit doRenameText(ResourceKind kind, QualName oldName,
assert previous == null;
newTexts.put(newName, text);
// check if this affects the system properties
GrammarProperties oldProps = null;
GrammarProperties newProps = null;
List<QualName> activeNames = getRecordedActiveNames(kind);
if (activeNames.remove(oldName)) {
oldProps = getProperties();
newProps = getProperties().clone();
activeNames.add(newName);
newProps.setActiveNames(kind, activeNames);
GrammarProperties oldProps = getProperties();
GrammarProperties newProps = oldProps.renameResource(kind, oldName, newName);
if (newProps != oldProps) {
doPutProperties(newProps);
}
return new TextBasedEdit(kind, EditType.RENAME, oldTexts, newTexts, oldProps, newProps);
Expand All @@ -447,14 +428,9 @@ GraphBasedEdit doRenameGraph(ResourceKind kind, QualName oldName,
this.marshaller.saveGraph(newGraph.toPlainGraph(), createFile(kind, newName));
deleteEmptyDirectories(oldFile.getParentFile());
// change the properties if there is a change in the enabled types
GrammarProperties oldProps = null;
GrammarProperties newProps = null;
List<QualName> activeNames = getRecordedActiveNames(kind);
if (activeNames.remove(oldName)) {
oldProps = getProperties();
newProps = oldProps.clone();
activeNames.add(newName);
newProps.setActiveNames(kind, activeNames);
GrammarProperties oldProps = getProperties();
GrammarProperties newProps = oldProps.renameResource(kind, oldName, newName);
if (newProps != oldProps) {
doPutProperties(newProps);
}
return new GraphBasedEdit(kind, EditType.RENAME, Collections.singleton(oldGraph),
Expand Down Expand Up @@ -731,19 +707,6 @@ public String toString() {
return getName() + " - " + location;
}

/**
* Returns a copy of the currently activated names of a given resource kind,
* as stored in the grammar properties.
*/
private List<QualName> getRecordedActiveNames(ResourceKind kind) {
List<QualName> result = new ArrayList<>();
if (kind != RULE && kind != GROOVY && kind != PROPERTIES) {
result.addAll(getProperties().getActiveNames(kind));
result.sort(null);
}
return result;
}

/**
* Collects all aspect graphs from the {@link #file} directory with a given
* extension, and a given role.
Expand Down

0 comments on commit 773a6c9

Please sign in to comment.