Skip to content
Snippets Groups Projects
Commit 34944f9c authored by Jan Gruteser's avatar Jan Gruteser
Browse files

some simplifications and comments in ConfigfileEvaluator

parent 87f1a9d1
No related branches found
No related tags found
No related merge requests found
Pipeline #148471 passed
...@@ -8,9 +8,11 @@ import de.tla2b.util.DebugUtils; ...@@ -8,9 +8,11 @@ import de.tla2b.util.DebugUtils;
import tla2sany.semantic.*; import tla2sany.semantic.*;
import tlc2.tool.impl.ModelConfig; import tlc2.tool.impl.ModelConfig;
import tlc2.util.Vect; import tlc2.util.Vect;
import tlc2.value.impl.BoolValue;
import tlc2.value.impl.IntValue; import tlc2.value.impl.IntValue;
import tlc2.value.impl.ModelValue; import tlc2.value.impl.ModelValue;
import tlc2.value.impl.SetEnumValue; import tlc2.value.impl.SetEnumValue;
import tlc2.value.impl.StringValue;
import tlc2.value.impl.Value; import tlc2.value.impl.Value;
import java.util.*; import java.util.*;
...@@ -88,8 +90,8 @@ public class ConfigfileEvaluator { ...@@ -88,8 +90,8 @@ public class ConfigfileEvaluator {
private void evalInvariants() throws ConfigFileErrorException { private void evalInvariants() throws ConfigFileErrorException {
Vect<?> v = configAst.getInvariants(); Vect<?> v = configAst.getInvariants();
for (int i = 0; i < v.capacity(); i++) { for (int i = 0; i < v.size(); i++) {
if (v.elementAt(i) != null) { if (v.elementAt(i) instanceof String) {
String inv = (String) v.elementAt(i); String inv = (String) v.elementAt(i);
if (!definitions.containsKey(inv)) { if (!definitions.containsKey(inv)) {
throw new ConfigFileErrorException("Invalid invariant declaration. Module does not contain definition '" + inv + "'"); throw new ConfigFileErrorException("Invalid invariant declaration. Module does not contain definition '" + inv + "'");
...@@ -120,11 +122,11 @@ public class ConfigfileEvaluator { ...@@ -120,11 +122,11 @@ public class ConfigfileEvaluator {
throw new ConfigFileErrorException( throw new ConfigFileErrorException(
String.format( String.format(
"Invalid substitution for %s.%n Constant %s has %s arguments while %s has %s arguments.", "Invalid substitution for %s.%n Constant %s has %s arguments while %s has %s arguments.",
left, left, conNode.getArity(), right, left, left, conNode.getArity(), right, rightDefNode.getArity()));
rightDefNode.getArity()));
} }
if (conNode.getArity() > 0) { if (conNode.getArity() > 0) {
bConstantList.remove(conNode); // Why? bConstantList.remove(conNode); // Why?
// constants has arguments and is overridden by an operator
// to not get error message Constant 'Leader' must be overridden in the configuration file. // to not get error message Constant 'Leader' must be overridden in the configuration file.
// but we get other problem // but we get other problem
} }
...@@ -137,8 +139,7 @@ public class ConfigfileEvaluator { ...@@ -137,8 +139,7 @@ public class ConfigfileEvaluator {
throw new ConfigFileErrorException( throw new ConfigFileErrorException(
String.format( String.format(
"Invalid substitution for %s.%n Operator %s has %s arguments while %s has %s arguments.", "Invalid substitution for %s.%n Operator %s has %s arguments while %s has %s arguments.",
left, left, defNode.getArity(), right, left, left, defNode.getArity(), right, rightDefNode.getArity()));
rightDefNode.getArity()));
} }
DebugUtils.printMsg("Putting Definition into OPERATOR OverrideTable " + defNode.getName() + "/" + defNode.getArity()); DebugUtils.printMsg("Putting Definition into OPERATOR OverrideTable " + defNode.getName() + "/" + defNode.getArity());
...@@ -156,14 +157,12 @@ public class ConfigfileEvaluator { ...@@ -156,14 +157,12 @@ public class ConfigfileEvaluator {
// k = 1 or def = 1 // k = 1 or def = 1
for (int i = 0; i < configCons.size(); i++) { for (int i = 0; i < configCons.size(); i++) {
Vect<?> symbol = (Vect<?>) configCons.elementAt(i); Vect<?> symbol = (Vect<?>) configCons.elementAt(i);
String symbolName = symbol.elementAt(0).toString(); String symbolName = symbol.firstElement().toString();
Value symbolValue = (Value) symbol.elementAt(symbol.size() - 1); Value symbolValue = (Value) symbol.lastElement();
TLAType symbolType = conGetType(symbol.elementAt(symbol.size() - 1)); TLAType symbolType = conGetType(symbol.lastElement());
if (constants.containsKey(symbolName)) { if (constants.containsKey(symbolName)) {
OpDeclNode c = constants.get(symbolName); OpDeclNode c = constants.get(symbolName);
constantAssignments.put(c, new ValueObj(symbolValue, symbolType));
ValueObj valueObj = new ValueObj(symbolValue, symbolType);
constantAssignments.put(c, valueObj);
// if conValue is a model value and the name of the value is the // if conValue is a model value and the name of the value is the
// same as the name of constants, then the constant declaration // same as the name of constants, then the constant declaration
// in the resulting B machine disappears // in the resulting B machine disappears
...@@ -172,12 +171,11 @@ public class ConfigfileEvaluator { ...@@ -172,12 +171,11 @@ public class ConfigfileEvaluator {
} }
} else if (definitions.containsKey(symbolName)) { } else if (definitions.containsKey(symbolName)) {
OpDefNode def = definitions.get(symbolName); OpDefNode def = definitions.get(symbolName);
ValueObj valueObj = new ValueObj(symbolValue, symbolType); operatorAssignments.put(def, new ValueObj(symbolValue, symbolType));
operatorAssignments.put(def, valueObj);
if (symbolType instanceof SetType && ((SetType) symbolType).getSubType() instanceof EnumType) { if (symbolType instanceof SetType && ((SetType) symbolType).getSubType() instanceof EnumType) {
operatorModelvalues.add(def); operatorModelvalues.add(def);
} else if ((symbolType instanceof EnumType)) { } else if (symbolType instanceof EnumType) {
operatorModelvalues.add(def); operatorModelvalues.add(def);
} }
} else { } else {
...@@ -188,38 +186,35 @@ public class ConfigfileEvaluator { ...@@ -188,38 +186,35 @@ public class ConfigfileEvaluator {
} }
private void evalModConOrDefAssignments() throws ConfigFileErrorException { private void evalModConOrDefAssignments() throws ConfigFileErrorException {
// val = [Counter] 7 // TODO: seems like there are no tests for this
// example: val = [Counter] 7
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Hashtable<String, Vect<?>> configCons = configAst.getModConstants(); Map<String, Vect<?>> configCons = configAst.getModConstants();
Enumeration<String> moduleNames = configCons.keys(); for (Map.Entry<String, Vect<?>> entry : configCons.entrySet()) {
while (moduleNames.hasMoreElements()) { String moduleName = entry.getKey();
String moduleName = moduleNames.nextElement(); Vect<?> assignments = entry.getValue();
ModuleNode mNode = searchModule(moduleName); ModuleNode mNode = searchModule(moduleName);
Vect<?> assignments = configCons.get(moduleName);
for (int i = 0; i < assignments.size(); i++) { for (int i = 0; i < assignments.size(); i++) {
Vect<?> assigment = (Vect<?>) assignments.elementAt(i); Vect<?> assignment = (Vect<?>) assignments.elementAt(i);
OpDefOrDeclNode opDefOrDeclNode = searchDefinitionOrConstant(mNode, (String) assigment.elementAt(0)); OpDefOrDeclNode opDefOrDeclNode = searchDefinitionOrConstant(mNode, (String) assignment.firstElement());
String symbolName = opDefOrDeclNode.getName().toString();
Value symbolValue = (Value) assigment.elementAt(1); Value symbolValue = (Value) assignment.elementAt(1);
TLAType symbolType = conGetType(assigment.elementAt(1)); TLAType symbolType = conGetType(symbolValue);
// System.out.println(symbolName + " " + symbolValue+ " " + ValueObj valueObj = new ValueObj(symbolValue, symbolType);
// symbolType);
if (opDefOrDeclNode instanceof OpDeclNode) { if (opDefOrDeclNode instanceof OpDeclNode) {
// TODO test whether c is a extended constant // TODO test whether c is a extended constant
// Instanced constants have to overridden in the instance // Instanced constants must be overridden in the instance statement
// statement
OpDeclNode c = (OpDeclNode) opDefOrDeclNode; OpDeclNode c = (OpDeclNode) opDefOrDeclNode;
ValueObj valueObj = new ValueObj(symbolValue, symbolType);
constantAssignments.put(c, valueObj); constantAssignments.put(c, valueObj);
// if conValue is a model value and the name of value is the // if conValue is a model value and the name of value is the same as the name of constants,
// same as the name of constants, then the constant // then the constant declaration in the resulting B machine disappears
// declaration in the resulting B machine disappears String symbolName = opDefOrDeclNode.getName().toString();
if (symbolName.equals(symbolValue.toString())) { if (symbolName.equals(symbolValue.toString())) {
bConstantList.remove(c); bConstantList.remove(c);
} }
} else { } else {
OpDefNode def = (OpDefNode) opDefOrDeclNode; OpDefNode def = (OpDefNode) opDefOrDeclNode;
ValueObj valueObj = new ValueObj(symbolValue, symbolType);
operatorAssignments.put(def, valueObj); operatorAssignments.put(def, valueObj);
if (symbolType instanceof SetType) { if (symbolType instanceof SetType) {
...@@ -235,28 +230,25 @@ public class ConfigfileEvaluator { ...@@ -235,28 +230,25 @@ public class ConfigfileEvaluator {
} }
private void evalModConOrDefOverrides() throws ConfigFileErrorException { private void evalModConOrDefOverrides() throws ConfigFileErrorException {
// foo <- [Counter] bar or k <- [Counter] bar // TODO: seems like there are no tests for this
// example: foo <- [Counter] bar or k <- [Counter] bar
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Hashtable<String, Hashtable<String, String>> configCons = configAst.getModOverrides(); Map<String, Map<String, String>> configCons = configAst.getModOverrides();
Enumeration<String> moduleNames = configCons.keys(); for (Map.Entry<String, Map<String, String>> entry : configCons.entrySet()) {
while (moduleNames.hasMoreElements()) { String moduleName = entry.getKey();
String moduleName = moduleNames.nextElement(); Map<String, String> o = entry.getValue();
ModuleNode mNode = searchModule(moduleName);
Hashtable<String, String> o = configCons.get(moduleName);
for (Map.Entry<String, String> entry : o.entrySet()) { for (Map.Entry<String, String> entry1 : o.entrySet()) {
String left = entry.getKey(); String left = entry1.getKey();
String right = entry.getValue(); String right = entry1.getValue();
OpDefNode rightDefNode = definitions.get(right); OpDefNode rightDefNode = definitions.get(right);
if (rightDefNode == null) { if (rightDefNode == null) {
throw new ConfigFileErrorException( throw new ConfigFileErrorException(
"Invalid substitution for " + left "Invalid substitution for " + left + ".\n Module does not contain definition " + right + ".");
+ ".\n Module does not contain definition "
+ right + ".");
} }
OpDefOrDeclNode opDefOrDeclNode = searchDefinitionOrConstant(mNode, left);
OpDefOrDeclNode opDefOrDeclNode = searchDefinitionOrConstant(searchModule(moduleName), left);
if (opDefOrDeclNode instanceof OpDefNode) { if (opDefOrDeclNode instanceof OpDefNode) {
// an operator is overridden by another operator // an operator is overridden by another operator
OpDefNode defNode = (OpDefNode) opDefOrDeclNode; OpDefNode defNode = (OpDefNode) opDefOrDeclNode;
...@@ -264,8 +256,7 @@ public class ConfigfileEvaluator { ...@@ -264,8 +256,7 @@ public class ConfigfileEvaluator {
throw new ConfigFileErrorException( throw new ConfigFileErrorException(
String.format( String.format(
"Invalid substitution for %s.%n Operator %s has %s arguments while %s has %s arguments.", "Invalid substitution for %s.%n Operator %s has %s arguments while %s has %s arguments.",
left, left, defNode.getArity(), right, left, left, defNode.getArity(), right, rightDefNode.getArity()));
rightDefNode.getArity()));
} }
operatorOverrides.put(defNode, rightDefNode); operatorOverrides.put(defNode, rightDefNode);
} else { } else {
...@@ -290,8 +281,7 @@ public class ConfigfileEvaluator { ...@@ -290,8 +281,7 @@ public class ConfigfileEvaluator {
throw new ConfigFileErrorException( throw new ConfigFileErrorException(
String.format( String.format(
"Invalid substitution for %s.%n Constant %s has %s arguments while %s has %s arguments.", "Invalid substitution for %s.%n Constant %s has %s arguments while %s has %s arguments.",
left, left, conNode.getArity(), right, left, left, conNode.getArity(), right, rightDefNode.getArity()));
rightDefNode.getArity()));
} }
bConstantList.remove(conNode); bConstantList.remove(conNode);
constantOverrides.put(conNode, rightDefNode); constantOverrides.put(conNode, rightDefNode);
...@@ -301,19 +291,15 @@ public class ConfigfileEvaluator { ...@@ -301,19 +291,15 @@ public class ConfigfileEvaluator {
} }
public ModuleNode searchModule(String moduleName) throws ConfigFileErrorException { public ModuleNode searchModule(String moduleName) throws ConfigFileErrorException {
/* // TODO: never used in tests
* Search module in extended modules // Search module in extended modules
*/ for (ModuleNode m : moduleNode.getExtendedModuleSet()) {
HashSet<ModuleNode> extendedModules = moduleNode.getExtendedModuleSet(); if (m.getName().toString().equals(moduleName))
for (ModuleNode m : extendedModules) {
if (m.getName().toString().equals(moduleName)) {
return m; return m;
} }
}
/* // search module in instanced modules
* search module in instanced modules // TODO: maybe use InstanceNodes instead?
*/
OpDefNode[] defs = moduleNode.getOpDefs(); OpDefNode[] defs = moduleNode.getOpDefs();
for (int j = defs.length - 1; j > 0; j--) { for (int j = defs.length - 1; j > 0; j--) {
OpDefNode def = null; OpDefNode def = null;
...@@ -344,35 +330,34 @@ public class ConfigfileEvaluator { ...@@ -344,35 +330,34 @@ public class ConfigfileEvaluator {
private TLAType conGetType(Object o) throws ConfigFileErrorException { private TLAType conGetType(Object o) throws ConfigFileErrorException {
if (o instanceof IntValue) { if (o instanceof IntValue) {
// IntValue iv = (IntValue) o;
return IntType.getInstance(); return IntType.getInstance();
} else if (o.getClass().getName().equals("tlc2.value.impl.SetEnumValue")) { } else if (o instanceof SetEnumValue) {
SetEnumValue set = (SetEnumValue) o; SetEnumValue set = (SetEnumValue) o;
SetType t = new SetType(new UntypedType());
if (set.isEmpty()) { if (set.isEmpty()) {
throw new ConfigFileErrorException("empty set is not permitted!"); throw new ConfigFileErrorException("type error: empty set is not permitted!");
} }
TLAType elemType;
if (set.elems.elementAt(0).getClass().getName().equals("tlc2.value.impl.ModelValue")) { SetType t = new SetType(new UntypedType());
TLAType elemType;
if (set.elems.firstElement() instanceof ModelValue) {
EnumType e = new EnumType(new ArrayList<>()); EnumType e = new EnumType(new ArrayList<>());
for (int i = 0; i < set.size(); i++) { for (int i = 0; i < set.size(); i++) {
if (set.elems.elementAt(i).getClass().getName().equals("tlc2.value.impl.ModelValue")) { if (set.elems.elementAt(i) instanceof ModelValue) {
String mv = set.elems.elementAt(i).toString(); String mv = set.elems.elementAt(i).toString();
if (!enumeratedSet.contains(mv)) { if (!enumeratedSet.contains(mv)) {
enumeratedSet.add(mv); enumeratedSet.add(mv);
e.modelvalues.add(mv);
} else { } else {
e.modelvalues.add(mv);
EnumType e2 = enumeratedTypes.get(mv); EnumType e2 = enumeratedTypes.get(mv);
try { try {
// TODO: why unify e2 with itself?
e = e2.unify(e2); e = e2.unify(e2);
} catch (UnificationException exception) { } catch (UnificationException ignored) {
// unification EnumType <-> EnumType always succeeds
} }
} }
e.modelvalues.add(mv);
} else { } else {
throw new ConfigFileErrorException("Elements of the set must have the same type: " + o); throw new ConfigFileErrorException("type error: elements of the set must have the same type: " + o);
} }
} }
for (String s : e.modelvalues) { for (String s : e.modelvalues) {
...@@ -380,19 +365,18 @@ public class ConfigfileEvaluator { ...@@ -380,19 +365,18 @@ public class ConfigfileEvaluator {
} }
elemType = e; elemType = e;
} else { } else {
elemType = conGetType(set.elems.elementAt(0)); elemType = conGetType(set.elems.firstElement());
for (int i = 1; i < set.size(); i++) { for (int i = 1; i < set.size(); i++) {
elemType = conGetType(set.elems.elementAt(i)); elemType = conGetType(set.elems.elementAt(i));
// all Elements have the same Type? // all Elements have the same Type?
if (!t.getSubType().compare(elemType)) { if (!t.getSubType().compare(elemType)) {
throw new ConfigFileErrorException("Elements of the set must have the same type: " + o); throw new ConfigFileErrorException("type error: elements of the set must have the same type: " + o);
} }
} }
} }
t.setSubType(elemType); t.setSubType(elemType);
return t; return t;
} else if (o instanceof ModelValue) {
} else if (o.getClass().getName().equals("tlc2.value.impl.ModelValue")) {
String mv = ((ModelValue) o).toString(); String mv = ((ModelValue) o).toString();
if (!enumeratedSet.contains(mv)) { if (!enumeratedSet.contains(mv)) {
enumeratedSet.add(mv); enumeratedSet.add(mv);
...@@ -402,13 +386,12 @@ public class ConfigfileEvaluator { ...@@ -402,13 +386,12 @@ public class ConfigfileEvaluator {
} else { } else {
return enumeratedTypes.get(mv); return enumeratedTypes.get(mv);
} }
} else if (o instanceof StringValue) {
} else if (o.getClass().getName().equals("tlc2.value.impl.StringValue")) {
return StringType.getInstance(); return StringType.getInstance();
} else if (o.getClass().getName().equals("tlc2.value.impl.BoolValue")) { } else if (o instanceof BoolValue) {
return BoolType.getInstance(); return BoolType.getInstance();
} else { } else {
throw new ConfigFileErrorException("Unknown ConstantType: " + o + " " + o.getClass()); throw new ConfigFileErrorException("type error: unknown constant type: " + o + " " + o.getClass());
} }
} }
......
...@@ -4,8 +4,8 @@ import de.tla2b.types.TLAType; ...@@ -4,8 +4,8 @@ import de.tla2b.types.TLAType;
import tlc2.value.impl.Value; import tlc2.value.impl.Value;
public class ValueObj { public class ValueObj {
private Value value; private final Value value;
private TLAType type; private final TLAType type;
public ValueObj(Value value, TLAType t) { public ValueObj(Value value, TLAType t) {
this.value = value; this.value = value;
...@@ -16,16 +16,7 @@ public class ValueObj { ...@@ -16,16 +16,7 @@ public class ValueObj {
return value; return value;
} }
public void setValue(Value value) {
this.value = value;
}
public void setType(TLAType type) {
this.type = type;
}
public TLAType getType() { public TLAType getType() {
return type; return type;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment