Skip to content

Commit

Permalink
Update Client Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganeshwara Hananda committed Jan 28, 2021
1 parent f31f269 commit 7831de4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
21 changes: 10 additions & 11 deletions GraknConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package grakn.console;

import grakn.client.Grakn;
import grakn.client.GraknClient;
import grakn.client.common.exception.GraknClientException;
import grakn.client.concept.answer.ConceptMap;
Expand Down Expand Up @@ -75,15 +74,15 @@ public GraknConsole(CommandLineOptions options, Printer printer) {

public void run() {
printer.info(COPYRIGHT);
try (Grakn.Client client = createGraknClient(options)) {
try (GraknClient client = createGraknClient(options)) {
runRepl(client);
} catch (GraknClientException e) {
printer.error(e.getMessage());
}
}

private Grakn.Client createGraknClient(CommandLineOptions options) {
Grakn.Client client;
private GraknClient createGraknClient(CommandLineOptions options) {
GraknClient client;
if (options.server() != null) {
client = GraknClient.core(options.server());
} else if (options.cluster() != null) {
Expand All @@ -94,7 +93,7 @@ private Grakn.Client createGraknClient(CommandLineOptions options) {
return client;
}

private void runRepl(Grakn.Client client) {
private void runRepl(GraknClient client) {
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.variable(LineReader.HISTORY_FILE, Paths.get(System.getProperty("user.home"), ".grakn-console-command-history").toAbsolutePath())
Expand Down Expand Up @@ -135,22 +134,22 @@ private void runRepl(Grakn.Client client) {
}
} else if (command.isTransaction()) {
String database = command.asTransaction().database();
Grakn.Session.Type sessionType = command.asTransaction().sessionType();
Grakn.Transaction.Type transactionType = command.asTransaction().transactionType();
GraknClient.Session.Type sessionType = command.asTransaction().sessionType();
GraknClient.Transaction.Type transactionType = command.asTransaction().transactionType();
boolean shouldExit = runTransactionRepl(client, database, sessionType, transactionType);
if (shouldExit) break;
}
}
}

private boolean runTransactionRepl(Grakn.Client client, String database, Grakn.Session.Type sessionType, Grakn.Transaction.Type transactionType) {
private boolean runTransactionRepl(GraknClient client, String database, GraknClient.Session.Type sessionType, GraknClient.Transaction.Type transactionType) {
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.variable(LineReader.HISTORY_FILE, Paths.get(System.getProperty("user.home"), ".grakn-console-transaction-history").toAbsolutePath())
.build();
String prompt = database + "::" + sessionType.name().toLowerCase() + "::" + transactionType.name().toLowerCase() + "> ";
try (Grakn.Session session = client.session(database, sessionType);
Grakn.Transaction tx = session.transaction(transactionType)) {
try (GraknClient.Session session = client.session(database, sessionType);
GraknClient.Transaction tx = session.transaction(transactionType)) {
while (true) {
TransactionReplCommand command;
try {
Expand Down Expand Up @@ -194,7 +193,7 @@ private boolean runTransactionRepl(Grakn.Client client, String database, Grakn.S
return false;
}

private void runQuery(Grakn.Transaction tx, String queryString) {
private void runQuery(GraknClient.Transaction tx, String queryString) {
List<GraqlQuery> queries;
try {
queries = Graql.parseQueries(queryString).collect(Collectors.toList());
Expand Down
16 changes: 8 additions & 8 deletions command/ReplCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package grakn.console;

import grakn.client.Grakn;
import grakn.client.GraknClient;
import grakn.common.collection.Pair;
import org.jline.reader.LineReader;

Expand Down Expand Up @@ -219,10 +219,10 @@ class Transaction implements ReplCommand {
private static String description = "Start a transaction to database <db> with schema or data session, with read or write transaction";

private final String database;
private final Grakn.Session.Type sessionType;
private final Grakn.Transaction.Type transactionType;
private final GraknClient.Session.Type sessionType;
private final GraknClient.Transaction.Type transactionType;

public Transaction(String database, Grakn.Session.Type sessionType, Grakn.Transaction.Type transactionType) {
public Transaction(String database, GraknClient.Session.Type sessionType, GraknClient.Transaction.Type transactionType) {
this.database = database;
this.sessionType = sessionType;
this.transactionType = transactionType;
Expand All @@ -232,11 +232,11 @@ public String database() {
return database;
}

public Grakn.Session.Type sessionType() {
public GraknClient.Session.Type sessionType() {
return sessionType;
}

public Grakn.Transaction.Type transactionType() {
public GraknClient.Transaction.Type transactionType() {
return transactionType;
}

Expand Down Expand Up @@ -286,8 +286,8 @@ static ReplCommand getCommand(LineReader reader, Printer printer, String prompt)
} else if (tokens.length == 4 && tokens[0].equals(Transaction.token) &&
(tokens[2].equals("schema") || tokens[2].equals("data") && (tokens[3].equals("read") || tokens[3].equals("write")))) {
String database = tokens[1];
Grakn.Session.Type sessionType = tokens[2].equals("schema") ? Grakn.Session.Type.SCHEMA : Grakn.Session.Type.DATA;
Grakn.Transaction.Type transactionType = tokens[3].equals("read") ? Grakn.Transaction.Type.READ : Grakn.Transaction.Type.WRITE;
GraknClient.Session.Type sessionType = tokens[2].equals("schema") ? GraknClient.Session.Type.SCHEMA : GraknClient.Session.Type.DATA;
GraknClient.Transaction.Type transactionType = tokens[3].equals("read") ? GraknClient.Transaction.Type.READ : GraknClient.Transaction.Type.WRITE;
command = new Transaction(database, sessionType, transactionType);
} else {
printer.error("Unrecognised command, please check help menu");
Expand Down
18 changes: 9 additions & 9 deletions common/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package grakn.console;

import grakn.client.Grakn;
import grakn.client.GraknClient;
import grakn.client.concept.Concept;
import grakn.client.concept.answer.ConceptMap;
import grakn.client.concept.answer.ConceptMapGroup;
Expand Down Expand Up @@ -55,11 +55,11 @@ public void error(String s) {
err.println(colorError(s));
}

public void conceptMap(ConceptMap conceptMap, Grakn.Transaction tx) {
public void conceptMap(ConceptMap conceptMap, GraknClient.Transaction tx) {
out.println(conceptMapDisplayString(conceptMap, tx));
}

public void conceptMapGroup(ConceptMapGroup answer, Grakn.Transaction tx) {
public void conceptMapGroup(ConceptMapGroup answer, GraknClient.Transaction tx) {
for (ConceptMap conceptMap : answer.conceptMaps()) {
out.println(conceptDisplayString(answer.owner(), tx) + " => " + conceptMapDisplayString(conceptMap, tx));
}
Expand All @@ -69,11 +69,11 @@ public void numeric(Numeric answer) {
out.println(answer.asNumber());
}

public void numericGroup(NumericGroup answer, Grakn.Transaction tx) {
public void numericGroup(NumericGroup answer, GraknClient.Transaction tx) {
out.println(conceptDisplayString(answer.owner(), tx) + " => " + answer.numeric().asNumber());
}

private String conceptMapDisplayString(ConceptMap conceptMap, Grakn.Transaction tx) {
private String conceptMapDisplayString(ConceptMap conceptMap, GraknClient.Transaction tx) {
StringBuilder sb = new StringBuilder();
sb.append("{ ");
for (Map.Entry<String, Concept> entry : conceptMap.map().entrySet()) {
Expand All @@ -88,7 +88,7 @@ private String conceptMapDisplayString(ConceptMap conceptMap, Grakn.Transaction
return sb.toString();
}

private String conceptDisplayString(Concept concept, Grakn.Transaction tx) {
private String conceptDisplayString(Concept concept, GraknClient.Transaction tx) {
StringBuilder sb = new StringBuilder();
if (concept instanceof Attribute<?>) {
sb.append(attributeDisplayString(concept.asThing().asAttribute()));
Expand All @@ -106,14 +106,14 @@ private String conceptDisplayString(Concept concept, Grakn.Transaction tx) {
return sb.toString();
}

private String isaDisplayString(Thing thing, Grakn.Transaction tx) {
private String isaDisplayString(Thing thing, GraknClient.Transaction tx) {
StringBuilder sb = new StringBuilder();
Type type = thing.asRemote(tx).getType();
sb.append(colorKeyword(GraqlToken.Constraint.ISA.toString())).append(" ").append(colorType(type.getLabel()));
return sb.toString();
}

private String relationDisplayString(Relation relation, Grakn.Transaction tx) {
private String relationDisplayString(Relation relation, GraknClient.Transaction tx) {
StringBuilder sb = new StringBuilder();
List<String> rolePlayerStrings = new ArrayList<>();
Map<? extends RoleType, ? extends List<? extends Thing>> rolePlayers = relation.asRemote(tx).getPlayersByRoleType();
Expand All @@ -137,7 +137,7 @@ private String iidDisplayString(Thing thing) {
return sb.toString();
}

private String typeDisplayString(Type type, Grakn.Transaction tx) {
private String typeDisplayString(Type type, GraknClient.Transaction tx) {
StringBuilder sb = new StringBuilder();

String label = type.isRoleType() ? type.asRoleType().getScopedLabel() : type.asThingType().getLabel();
Expand Down

0 comments on commit 7831de4

Please sign in to comment.