Skip to content

Commit 26a8244

Browse files
Introduce computed values (#207)
## What is the goal of this PR? We allow users to interact via Console with the new Value API introduced in typedb/typeql#260. This PR adds pretty printing for value concepts and updates required dependencies to support the change. ## What are the changes implemented in this PR? * Update to a client-java version that supports Values in queries and answers * Update the concept pretty-printer to allow printing Value concepts. * Update ConceptMap printing to first print concept variables (`$`) by alphabetical sorting of the variable name, and then value variables (`?`) by alphabetical sorting.
1 parent 131e527 commit 26a8244

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

common/Printer.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@
2828
import com.vaticle.typedb.client.api.concept.thing.Thing;
2929
import com.vaticle.typedb.client.api.concept.type.RoleType;
3030
import com.vaticle.typedb.client.api.concept.type.Type;
31+
import com.vaticle.typedb.client.api.concept.value.Value;
3132
import com.vaticle.typedb.client.api.database.Database;
3233
import com.vaticle.typeql.lang.common.TypeQLToken;
34+
import org.jline.utils.AttributedString;
35+
import org.jline.utils.AttributedStyle;
36+
3337
import java.io.PrintStream;
3438
import java.util.ArrayList;
3539
import java.util.Arrays;
40+
import java.util.Comparator;
3641
import java.util.List;
3742
import java.util.Map;
38-
import org.jline.utils.AttributedString;
39-
import org.jline.utils.AttributedStyle;
43+
4044
import static com.vaticle.typeql.lang.common.TypeQLToken.Constraint.ISA;
4145
import static java.util.stream.Collectors.joining;
4246

@@ -87,9 +91,16 @@ public void databaseReplica(Database.Replica replica) {
8791
}
8892

8993
private String conceptMapDisplayString(ConceptMap conceptMap, TypeDBTransaction tx) {
90-
String content = conceptMap.map().entrySet().stream().map(
91-
e -> TypeQLToken.Char.$ + e.getKey() + " " + conceptDisplayString(e.getValue(), tx) + ";"
92-
).collect(joining("\n"));
94+
Comparator<Map.Entry<String, Concept>> comparator = Comparator.comparing(e -> e.getValue().isValue());
95+
comparator = comparator.thenComparing(e -> e.getKey().toLowerCase());
96+
String content = conceptMap.map().entrySet().stream().sorted(comparator)
97+
.map(e -> {
98+
if (e.getValue().isValue()) {
99+
return TypeQLToken.Char.QUESTION_MARK + e.getKey() + " = " + conceptDisplayString(e.getValue().asValue(), tx) + ";";
100+
} else {
101+
return TypeQLToken.Char.$ + e.getKey() + " " + conceptDisplayString(e.getValue(), tx) + ";";
102+
}
103+
}).collect(joining("\n"));
93104
StringBuilder sb = new StringBuilder("{");
94105
if (content.lines().count() > 1) sb.append("\n").append(indent(content)).append("\n");
95106
else sb.append(" ").append(content).append(" ");
@@ -102,6 +113,8 @@ private static String indent(String string) {
102113
}
103114

104115
private String conceptDisplayString(Concept concept, TypeDBTransaction tx) {
116+
if (concept.isValue()) return valueDisplayString(concept.asValue());
117+
105118
StringBuilder sb = new StringBuilder();
106119
if (concept instanceof Attribute<?>) {
107120
sb.append(attributeDisplayString(concept.asThing().asAttribute()));
@@ -116,9 +129,14 @@ private String conceptDisplayString(Concept concept, TypeDBTransaction tx) {
116129
if (concept instanceof Thing) {
117130
sb.append(" ").append(isaDisplayString(concept.asThing()));
118131
}
132+
119133
return sb.toString();
120134
}
121135

136+
private String valueDisplayString(Value<?> value) {
137+
return com.vaticle.typeql.lang.common.util.Strings.valueToString(value.getValue());
138+
}
139+
122140
private String isaDisplayString(Thing thing) {
123141
StringBuilder sb = new StringBuilder();
124142
sb.append(colorKeyword(ISA.toString())).append(" ").append(colorType(thing.getType().getLabel().scopedName()));

dependencies/vaticle/repositories.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ def vaticle_dependencies():
2121
git_repository(
2222
name = "vaticle_dependencies",
2323
remote = "https://github.com/vaticle/dependencies",
24-
commit = "b968cab9c4d2f85d80a0069b8d15cce5afdd0da5", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_dependencies
24+
commit = "385716283e1e64245c3679a06054e271a0608ac1", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_dependencies
2525
)
2626

2727
def vaticle_typedb_common():
2828
git_repository(
2929
name = "vaticle_typedb_common",
3030
remote = "https://github.com/vaticle/typedb-common",
31-
tag = "2.17.0" # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_common
31+
commit = "9372dfb227d54c6eb631eed02e67f250e55e657e" # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_common
3232
)
3333

3434
def vaticle_typedb_client_java():
3535
git_repository(
3636
name = "vaticle_typedb_client_java",
3737
remote = "https://github.com/vaticle/typedb-client-java",
38-
tag = "2.17.1", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_client_java
38+
commit = "16c23649627001b44b39337f9d904619b4dac97c", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_client_java
3939
)

0 commit comments

Comments
 (0)