Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

purged UniqueString from the community modules #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions modules/tlc2/overrides/CSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import tlc2.value.impl.StringValue;
import tlc2.value.impl.TupleValue;
import tlc2.value.impl.Value;
import util.UniqueString;

public class CSV {

Expand All @@ -59,8 +58,8 @@ public static Value write(final StringValue template, final Value parameter, fin
}
final Object[] params = Arrays.asList(tv.getElems()).stream().map(v -> v.toString())
.toArray(size -> new Object[size]);
Files.write(Paths.get(absolutePath.val.toString()),
(String.format(template.val.toString(), params) + System.lineSeparator()).getBytes("UTF-8"),
Files.write(Paths.get(absolutePath.val),
(String.format(template.val, params) + System.lineSeparator()).getBytes("UTF-8"),
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
return BoolValue.ValTrue;
}
Expand All @@ -75,19 +74,19 @@ public static Value read(final Value columns, final StringValue delim, final Str
new String[] { "CSVRead", "sequence", Values.ppr(columns.toString()) });
}

final Path path = Paths.get(absolutePath.val.toString());
final Path path = Paths.get(absolutePath.val);
if (!path.toFile().exists()) {
// There are zero records in a file that doesn't exist.
return TupleValue.EmptyTuple;
}

final UniqueString[] names = Arrays.asList(tv.elems).stream().map(v -> ((StringValue) v).val)
.collect(Collectors.toList()).toArray(UniqueString[]::new);
final String[] names = Arrays.asList(tv.elems).stream().map(v -> ((StringValue) v).val)
.collect(Collectors.toList()).toArray(String[]::new);

final List<String> lines = Files.readAllLines(path);
final Value[] records = new Value[lines.size()];
for (int i = 0; i < lines.size(); i++) {
StringValue[] values = Arrays.asList(lines.get(i).split(delim.val.toString())).stream()
StringValue[] values = Arrays.asList(lines.get(i).split(delim.val)).stream()
.map(s -> new StringValue(s)).collect(Collectors.toList()).toArray(StringValue[]::new);
records[i] = new RecordValue(names, values, false);
}
Expand All @@ -98,7 +97,7 @@ public static Value read(final Value columns, final StringValue delim, final Str
@TLAPlusOperator(identifier = "CSVRecords", module = "CSV", minLevel = 1, warn = false)
public static Value records(final StringValue absolutePath)
throws IOException {
final Path path = Paths.get(absolutePath.val.toString());
final Path path = Paths.get(absolutePath.val);
if (!path.toFile().exists()) {
// There are zero records in a file that doesn't exist.
return IntValue.ValZero;
Expand Down
43 changes: 20 additions & 23 deletions modules/tlc2/overrides/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@
import tlc2.value.impl.StringValue;
import tlc2.value.impl.TupleValue;
import tlc2.value.impl.Value;
import util.UniqueString;

public class IOUtils {

@TLAPlusOperator(identifier = "IODeserialize", module = "IOUtils", warn = false)
public static final IValue ioDeserialize(final StringValue absolutePath, final BoolValue compress)
throws IOException {
final ValueInputStream vis = new ValueInputStream(new File(absolutePath.val.toString()), compress.val);
final ValueInputStream vis = new ValueInputStream(new File(absolutePath.val), compress.val);
try {
return vis.read(UniqueString.internTbl.toMap());
return vis.read();
} finally {
vis.close();
}
Expand All @@ -75,7 +74,7 @@ public static final IValue ioDeserialize(final StringValue absolutePath, final B
@TLAPlusOperator(identifier = "IOSerialize", module = "IOUtils", warn = false)
public static final IValue ioSerialize(final IValue value, final StringValue absolutePath, final BoolValue compress)
throws IOException {
final ValueOutputStream vos = new ValueOutputStream(new File(absolutePath.val.toString()), compress.val);
final ValueOutputStream vos = new ValueOutputStream(new File(absolutePath.val), compress.val);
try {
value.write(vos);
} finally {
Expand Down Expand Up @@ -110,7 +109,7 @@ public synchronized static Value textSerialize(final Tool tool, final ExprOrOpAr
}

final StringValue serializer = (StringValue) opts.apply(new StringValue("format"), EvalControl.Clear);
if("TXT".equals(serializer.getVal().toString())) {
if("TXT".equals(serializer.getVal())) {

final StringValue payload;
final StringValue filepath;
Expand All @@ -132,11 +131,11 @@ public synchronized static Value textSerialize(final Tool tool, final ExprOrOpAr

try {
Files.writeString(
Paths.get(filepath.getVal().toString()),
payload.getVal().toString(),
Charset.forName(charset.getVal().toString()),
Paths.get(filepath.getVal()),
payload.getVal(),
Charset.forName(charset.getVal()),
Arrays.asList(openOptions).stream()
.map(e -> StandardOpenOption.valueOf(e.getVal().toString()) )
.map(e -> StandardOpenOption.valueOf(e.getVal()) )
.toArray(size -> new StandardOpenOption[size]));

return new RecordValue(EXEC_NAMES, new Value[] { IntValue.ValZero, new StringValue(successmsg), new StringValue("") }, false);
Expand Down Expand Up @@ -173,7 +172,7 @@ public synchronized static Value textDeserialize(final Tool tool, final ExprOrOp
}

final StringValue serializer = (StringValue) opts.apply(new StringValue("format"), EvalControl.Clear);
if("TXT".equals(serializer.getVal().toString())) {
if("TXT".equals(serializer.getVal())) {

final StringValue filepath;
final StringValue charset;
Expand All @@ -186,9 +185,7 @@ public synchronized static Value textDeserialize(final Tool tool, final ExprOrOp
}

try {
final String result = Files.readString(
Paths.get(filepath.getVal().toString()),
Charset.forName(charset.getVal().toString()));
final String result = Files.readString(Paths.get(filepath.getVal()), Charset.forName(charset.getVal()));

return new RecordValue(EXEC_NAMES, new Value[] { IntValue.ValZero, new StringValue(result), new StringValue("") }, false);

Expand All @@ -206,12 +203,12 @@ public synchronized static Value textDeserialize(final Tool tool, final ExprOrOp
// process executes.
final Map<String, String> env = System.getenv();

final UniqueString[] names = new UniqueString[env.size()];
final String[] names = new String[env.size()];
final StringValue[] values = new StringValue[env.size()];

final List<Map.Entry<String, String>> entries = new ArrayList<>(env.entrySet());
for (int i = 0; i < entries.size(); i++) {
names[i] = UniqueString.of(entries.get(i).getKey());
names[i] = entries.get(i).getKey();
values[i] = new StringValue(entries.get(i).getValue());
}

Expand All @@ -223,7 +220,7 @@ public static Value atoi(final Value v) {
if (v instanceof StringValue) {
final StringValue sv = (StringValue) v;
try {
final int i = Integer.parseInt(sv.val.toString());
final int i = Integer.parseInt(sv.val);
return IntValue.gen(i);
} catch (Exception e) {
// "fall-through" to eval exception below.
Expand Down Expand Up @@ -350,9 +347,9 @@ private static Map<String, String> getEnv(final RecordValue environment) {
// Convert record of environment variables to what ProcessBuilder works with.
final Map<String, String> penv = new HashMap<>();
for (int i = 0; i < environment.size(); i++) {
final UniqueString name = environment.names[i];
final String name = environment.names[i];
final Value value = environment.values[i];
penv.put(name.toString(), value.toUnquotedString());
penv.put(name, value.toUnquotedString());
}
return penv;
}
Expand Down Expand Up @@ -398,11 +395,11 @@ private static String convert(IValue v) {
}
final StringValue sv = (StringValue) v;

return sv.val.toString();
return sv.val;
}

private static final UniqueString EXITVALUE = UniqueString.uniqueStringOf("exitValue");
private static final UniqueString STDOUT = UniqueString.uniqueStringOf("stdout");
private static final UniqueString STDERR = UniqueString.uniqueStringOf("stderr");
private static final UniqueString[] EXEC_NAMES = new UniqueString[] { EXITVALUE, STDOUT, STDERR };
private static final String EXITVALUE = ("exitValue");
private static final String STDOUT = ("stdout");
private static final String STDERR = ("stderr");
private static final String[] EXEC_NAMES = new String[] { EXITVALUE, STDOUT, STDERR };
}
27 changes: 13 additions & 14 deletions modules/tlc2/overrides/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import tlc2.value.impl.SubsetValue;
import tlc2.value.impl.TupleValue;
import tlc2.value.impl.Value;
import util.UniqueString;

/**
* Module overrides for operators to read and write JSON.
Expand Down Expand Up @@ -107,7 +106,7 @@ public static StringValue toJsonObject(final IValue value) throws IOException {
@TLAPlusOperator(identifier = "ndJsonDeserialize", module = "Json", warn = false)
public static IValue ndDeserialize(final StringValue path) throws IOException {
List<Value> values = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(new File(path.val.toString())))) {
try (BufferedReader reader = new BufferedReader(new FileReader(new File(path.val)))) {
String line = reader.readLine();
while (line != null) {
JsonElement node = JsonParser.parseString(line);
Expand All @@ -126,7 +125,7 @@ public static IValue ndDeserialize(final StringValue path) throws IOException {
*/
@TLAPlusOperator(identifier = "JsonDeserialize", module = "Json", warn = false)
public static IValue deserialize(final StringValue path) throws IOException {
JsonElement node = JsonParser.parseReader(new FileReader(new File(path.val.toString())));
JsonElement node = JsonParser.parseReader(new FileReader(new File(path.val)));
return getValue(node);
}

Expand All @@ -139,9 +138,9 @@ public static IValue deserialize(final StringValue path) throws IOException {
*/
@TLAPlusOperator(identifier = "ndJsonSerialize", module = "Json", warn = false)
public synchronized static BoolValue ndSerialize(final StringValue path, final TupleValue value) throws IOException {
File file = new File(path.val.toString());
File file = new File(path.val);
if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path.
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val)))) {
for (int i = 0; i < value.elems.length; i++) {
writer.write(getNode(value.elems[i]).toString() + "\n");
}
Expand All @@ -158,9 +157,9 @@ public synchronized static BoolValue ndSerialize(final StringValue path, final T
*/
@TLAPlusOperator(identifier = "JsonSerialize", module = "Json", warn = false)
public synchronized static BoolValue serialize(final StringValue path, final TupleValue value) throws IOException {
File file = new File(path.val.toString());
File file = new File(path.val);
if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path.
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val)))) {
writer.write("[\n");
for (int i = 0; i < value.elems.length; i++) {
writer.write(getNode(value.elems[i]).toString());
Expand All @@ -186,9 +185,9 @@ private static JsonElement getNode(IValue value) throws IOException {
} else if (value instanceof TupleValue) {
return getArrayNode((TupleValue) value);
} else if (value instanceof StringValue) {
return new JsonPrimitive(((StringValue) value).val.toString());
return new JsonPrimitive(((StringValue) value).val);
} else if (value instanceof ModelValue) {
return new JsonPrimitive(((ModelValue) value).val.toString());
return new JsonPrimitive(((ModelValue) value).val);
} else if (value instanceof IntValue) {
return new JsonPrimitive(((IntValue) value).val);
} else if (value instanceof BoolValue) {
Expand Down Expand Up @@ -272,7 +271,7 @@ private static JsonElement getObjectNode(FcnRcdValue value) throws IOException {
for (int i = 0; i < domain.length; i++) {
Value domainValue = domain[i];
if (domainValue instanceof StringValue) {
jsonObject.add(((StringValue) domainValue).val.toString(), getNode(value.values[i]));
jsonObject.add(((StringValue) domainValue).val, getNode(value.values[i]));
} else {
jsonObject.add(domainValue.toString(), getNode(value.values[i]));
}
Expand All @@ -289,7 +288,7 @@ private static JsonElement getObjectNode(FcnRcdValue value) throws IOException {
private static JsonElement getObjectNode(RecordValue value) throws IOException {
JsonObject jsonObject = new JsonObject();
for (int i = 0; i < value.names.length; i++) {
jsonObject.add(value.names[i].toString(), getNode(value.values[i]));
jsonObject.add(value.names[i], getNode(value.values[i]));
}
return jsonObject;
}
Expand Down Expand Up @@ -440,15 +439,15 @@ private static TupleValue getTupleValue(JsonElement node) throws IOException {
* @return the record value
*/
private static RecordValue getRecordValue(JsonElement node) throws IOException {
List<UniqueString> keys = new ArrayList<>();
List<String> keys = new ArrayList<>();
List<Value> values = new ArrayList<>();
Iterator<Map.Entry<String, JsonElement>> iterator = node.getAsJsonObject().entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
keys.add(UniqueString.uniqueStringOf(entry.getKey()));
keys.add(entry.getKey());
values.add(getValue(entry.getValue()));
}
return new RecordValue(keys.toArray(new UniqueString[keys.size()]), values.toArray(new Value[values.size()]),
return new RecordValue(keys.toArray(new String[keys.size()]), values.toArray(new Value[values.size()]),
false);
}

Expand Down
24 changes: 11 additions & 13 deletions modules/tlc2/overrides/SVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import tlc2.value.impl.TupleValue;
import tlc2.value.impl.Value;
import tlc2.value.impl.ValueVec;
import util.UniqueString;

public final class SVG {

Expand Down Expand Up @@ -78,8 +77,7 @@ public static Value SVGElemToString(Value elem) throws Exception {
RecordValue frv = (RecordValue) elem.toRcd();

// Get 'name'.
StringValue nameVal = (StringValue) frv.apply(new StringValue("name"), 0);
String name = nameVal.getVal().toString();
String name = ((StringValue) frv.apply(new StringValue("name"), 0)).getVal();

// Get 'attrs'. We convert it to 'RecordValue' type, which we expect should always be possible.
Value attrsVal = frv.apply(new StringValue("attrs"), 0);
Expand All @@ -88,11 +86,11 @@ public static Value SVGElemToString(Value elem) throws Exception {
throw new Exception("Was unable to convert element to a record: " + attrsVal.toString());
}
String attrStr = "";
for (UniqueString us : attrs.names) {
for (String us : attrs.names) {
attrStr += " ";
attrStr += us.toString().replaceAll("_", "-");
attrStr += us.replaceAll("_", "-");
attrStr += "=";
String v = ((StringValue) attrs.apply(new StringValue(us), 0)).getVal().toString();
String v = ((StringValue) attrs.apply(new StringValue(us), 0)).getVal();
// Quote all SVG attribute values. Technically, attribute values in HTML
// don't always need to be quoted, but we use quotes so we can handle all
// possible values. We single quote them to play nice with TLC string formatting.
Expand Down Expand Up @@ -132,7 +130,7 @@ public static Value SVGElemToString(Value elem) throws Exception {
// to be empty for all other element types, but since it's not rendered, we don't
// explicitly disallow it.
StringValue innerTextVal = (StringValue) frv.apply(new StringValue("innerText"), 0);
String innerText = innerTextVal.getVal().toString();
String innerText = innerTextVal.getVal();
// Make sure TLA+ tuples such as <<1,2,3>> get properly rendered.
innerText = innerText.replaceAll("<<", "&lt;&lt;").replaceAll(">>", "&gt;&gt;");

Expand All @@ -154,7 +152,7 @@ public static Value ringNetwork(IntValue cx, IntValue cy, IntValue r, IntValue n
// Polar to Cartesian coordinates offset by (cx,cy).
final int x = (int) (cx.val + r.val * Math.cos(angle));
final int y = (int) (cy.val + r.val * Math.sin(angle));
return new RecordValue(new UniqueString[] {UniqueString.of("x"), UniqueString.of("y")}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false);
return new RecordValue(new String[] {"x", "y"}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false);
}

@TLAPlusOperator(identifier = "NodesOfDirectedMultiGraph", module = "SVG", warn = false)
Expand Down Expand Up @@ -183,7 +181,7 @@ public static Value directedMultiGraph(final SetEnumValue nodes, final SetEnumVa

// Algorithm
final StringValue algo = (StringValue) opts.apply(new StringValue("algo"), EvalControl.Clear);
getAlgo(algo.val.toString(), opts).visit(layoutModel);
getAlgo(algo.val, opts).visit(layoutModel);

// Get the node's coordinates from the algorithm.
final Map <Value, Point> locations = layoutModel.getLocations();
Expand All @@ -196,7 +194,7 @@ public static Value directedMultiGraph(final SetEnumValue nodes, final SetEnumVa
private static Value point2Value(final Point p) {
final int x = ((Double) p.x).intValue();
final int y = ((Double) p.y).intValue();
return new RecordValue(new UniqueString[] { UniqueString.of("x"), UniqueString.of("y") },
return new RecordValue(new String[] { "x", "y" },
new Value[] { IntValue.gen(x), IntValue.gen(y) }, false);
}

Expand All @@ -207,12 +205,12 @@ private static LayoutAlgorithm<Value> getAlgo(final String algo, final RecordVal
switch (algo) {
case "Sugiyama":
// https://github.com/tomnelson/jungrapht-visualization/blob/afd155bf0246e5185f054ba1429bbcfbd429292a/jungrapht-layout/src/main/java/org/jungrapht/visualization/layout/algorithms/sugiyama/Layering.java#L4-L7
String l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val.toString();
String l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val;
return SugiyamaLayoutAlgorithm.<Value, Integer>edgeAwareBuilder()
.layering(Layering.valueOf(l)).vertexBoundsFunction(v -> Rectangle.of(-5, -5, nodeW.val, nodeH.val)).threaded(false)
.build();
case "Eiglsperger":
l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val.toString();
l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val;
return EiglspergerLayoutAlgorithm.<Value, Integer>edgeAwareBuilder()
.layering(Layering.valueOf(l)).vertexBoundsFunction(v -> Rectangle.of(-5, -5, nodeW.val, nodeH.val)).threaded(false)
.build();
Expand All @@ -233,6 +231,6 @@ public static Value pointOnLine(final RecordValue from, final RecordValue to, fi
int x = (int) (fx + ((tx - fx) / (idx.val * 1d)));
int y = (int) (fy + ((ty - fy) / (idx.val * 1d)));

return new RecordValue(new UniqueString[] {UniqueString.of("x"), UniqueString.of("y")}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false);
return new RecordValue(new String[] {"x", "y"}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false);
}
}