Skip to content

Commit e8446d2

Browse files
Integrate .NET driver examples into automated build process (#3293)
1 parent cebd8e3 commit e8446d2

7 files changed

Lines changed: 68 additions & 40 deletions

File tree

gremlin-dotnet/Examples/BasicGremlin/BasicGremlin.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,32 @@ under the License.
2323

2424
public class BasicGremlinExample
2525
{
26+
static readonly string ServerHost = Environment.GetEnvironmentVariable("GREMLIN_SERVER_HOST") ?? "localhost";
27+
static readonly int ServerPort = int.Parse(Environment.GetEnvironmentVariable("GREMLIN_SERVER_PORT") ?? "8182");
28+
static readonly string VertexLabel = Environment.GetEnvironmentVariable("VERTEX_LABEL") ?? "person";
29+
2630
static async Task Main()
2731
{
28-
var server = new GremlinServer("localhost", 8182);
32+
var server = new GremlinServer(ServerHost, ServerPort);
2933
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
3034
var g = Traversal().WithRemote(remoteConnection);
3135

3236
// Basic Gremlin: adding and retrieving data
33-
var v1 = g.AddV("person").Property("name", "marko").Next();
34-
var v2 = g.AddV("person").Property("name", "stephen").Next();
35-
var v3 = g.AddV("person").Property("name", "vadas").Next();
37+
var v1 = g.AddV(VertexLabel).Property("name", "marko").Next();
38+
var v2 = g.AddV(VertexLabel).Property("name", "stephen").Next();
39+
var v3 = g.AddV(VertexLabel).Property("name", "vadas").Next();
3640

3741
// Be sure to use a terminating step like Next() or Iterate() so that the traversal "executes"
3842
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
3943
g.V(v1).AddE("knows").To(v2).Property("weight", 0.75).Iterate();
4044
g.V(v1).AddE("knows").To(v3).Property("weight", 0.75).Iterate();
4145

4246
// Retrieve the data from the "marko" vertex
43-
var marko = await g.V().Has("person", "name", "marko").Values<string>("name").Promise(t => t.Next());
47+
var marko = await g.V().Has(VertexLabel, "name", "marko").Values<string>("name").Promise(t => t.Next());
4448
Console.WriteLine("name: " + marko);
4549

4650
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
47-
var peopleMarkoKnows = await g.V().Has("person", "name", "marko").Out("knows").Values<string>("name").Promise(t => t.ToList());
51+
var peopleMarkoKnows = await g.V().Has(VertexLabel, "name", "marko").Out("knows").Values<string>("name").Promise(t => t.ToList());
4852
foreach (var person in peopleMarkoKnows)
4953
{
5054
Console.WriteLine("marko knows " + person);

gremlin-dotnet/Examples/Connections/Connections.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ under the License.
2424

2525
public class ConnectionExample
2626
{
27+
static readonly string ServerHost = Environment.GetEnvironmentVariable("GREMLIN_SERVER_HOST") ?? "localhost";
28+
static readonly int ServerPort = int.Parse(Environment.GetEnvironmentVariable("GREMLIN_SERVER_PORT") ?? "8182");
29+
static readonly string VertexLabel = Environment.GetEnvironmentVariable("VERTEX_LABEL") ?? "connection";
30+
2731
static void Main()
2832
{
2933
WithRemote();
@@ -34,41 +38,38 @@ static void Main()
3438
// Connecting to the server
3539
static void WithRemote()
3640
{
37-
var server = new GremlinServer("localhost", 8182);
41+
var server = new GremlinServer(ServerHost, ServerPort);
3842
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
3943
var g = Traversal().WithRemote(remoteConnection);
4044

41-
// Drop existing vertices
42-
g.V().Drop().Iterate();
43-
4445
// Simple query to verify connection
45-
var v = g.AddV().Iterate();
46-
var count = g.V().Count().Next();
46+
var v = g.AddV(VertexLabel).Iterate();
47+
var count = g.V().HasLabel(VertexLabel).Count().Next();
4748
Console.WriteLine("Vertex count: " + count);
4849
}
4950

5051
// Connecting to the server with customized configurations
5152
static void WithConf()
5253
{
5354
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(
54-
new GremlinServer(hostname: "localhost", port: 8182, enableSsl: false, username: "", password: "")), "g");
55+
new GremlinServer(hostname: ServerHost, port: ServerPort, enableSsl: false, username: "", password: "")), "g");
5556
var g = Traversal().WithRemote(remoteConnection);
5657

57-
var v = g.AddV().Iterate();
58-
var count = g.V().Count().Next();
58+
var v = g.AddV(VertexLabel).Iterate();
59+
var count = g.V().HasLabel(VertexLabel).Count().Next();
5960
Console.WriteLine("Vertex count: " + count);
6061
}
6162

6263
// Specifying a serializer
6364
static void WithSerializer()
6465
{
65-
var server = new GremlinServer("localhost", 8182);
66+
var server = new GremlinServer(ServerHost, ServerPort);
6667
var client = new GremlinClient(server, new GraphSON3MessageSerializer());
6768
using var remoteConnection = new DriverRemoteConnection(client, "g");
6869
var g = Traversal().WithRemote(remoteConnection);
6970

70-
var v = g.AddV().Iterate();
71-
var count = g.V().Count().Next();
71+
var v = g.AddV(VertexLabel).Iterate();
72+
var count = g.V().HasLabel(VertexLabel).Count().Next();
7273
Console.WriteLine("Vertex count: " + count);
7374
}
7475
}

gremlin-dotnet/Examples/ModernTraversals/ModernTraversals.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ under the License.
2626

2727
public class ModernTraversalExample
2828
{
29+
static readonly string ServerHost = Environment.GetEnvironmentVariable("GREMLIN_SERVER_HOST") ?? "localhost";
30+
static readonly int ServerPort = int.Parse(Environment.GetEnvironmentVariable("GREMLIN_SERVER_PORT") ?? "8182");
31+
static readonly bool IsDocker = Environment.GetEnvironmentVariable("DOCKER_ENVIRONMENT") == "true";
32+
2933
static void Main()
3034
{
31-
var server = new GremlinServer("localhost", 8182);
32-
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
35+
var server = new GremlinServer(ServerHost, ServerPort);
36+
// Use gmodern in CI environment, default connection locally
37+
var traversalSource = IsDocker ? "gmodern" : "g";
38+
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), traversalSource);
3339
var g = Traversal().WithRemote(remoteConnection);
3440

3541
/*

gremlin-dotnet/docker-compose.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ services:
5353
- ../gremlin-tools/gremlin-socket-server/conf:/gremlin-dotnet/gremlin-socket-server/conf/
5454
environment:
5555
- DOCKER_ENVIRONMENT=true
56+
- GREMLIN_SERVER_HOST=gremlin-server-test-dotnet
57+
- GREMLIN_SERVER_PORT=45940
58+
- VERTEX_LABEL=dotnet-example
5659
working_dir: /gremlin-dotnet
5760
command: >
5861
bash -c "dotnet tool update -g dotnet-trx; dotnet test ./Gremlin.Net.sln -c Release --logger trx; /root/.dotnet/tools/trx;
59-
EXIT_CODE=$$?; chown -R `stat -c "%u:%g" .` .; exit $$EXIT_CODE"
62+
EXIT_CODE=$$?;
63+
echo 'Running examples...';
64+
dotnet run --project Examples/BasicGremlin/BasicGremlin.csproj;
65+
dotnet run --project Examples/Connections/Connections.csproj;
66+
dotnet run --project Examples/ModernTraversals/ModernTraversals.csproj;
67+
echo 'All examples completed successfully';
68+
chown -R `stat -c \"%u:%g\" .` .; exit $$EXIT_CODE"
6069
depends_on:
6170
gremlin-server-test-dotnet:
6271
condition: service_healthy

gremlin-examples/gremlin-dotnet/BasicGremlin/BasicGremlin.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,32 @@ under the License.
2323

2424
public class BasicGremlinExample
2525
{
26+
static readonly string ServerHost = "localhost";
27+
static readonly int ServerPort = 8182;
28+
static readonly string VertexLabel = "person";
29+
2630
static async Task Main()
2731
{
28-
var server = new GremlinServer("localhost", 8182);
32+
var server = new GremlinServer(ServerHost, ServerPort);
2933
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
3034
var g = Traversal().WithRemote(remoteConnection);
3135

3236
// Basic Gremlin: adding and retrieving data
33-
var v1 = g.AddV("person").Property("name", "marko").Next();
34-
var v2 = g.AddV("person").Property("name", "stephen").Next();
35-
var v3 = g.AddV("person").Property("name", "vadas").Next();
37+
var v1 = g.AddV(VertexLabel).Property("name", "marko").Next();
38+
var v2 = g.AddV(VertexLabel).Property("name", "stephen").Next();
39+
var v3 = g.AddV(VertexLabel).Property("name", "vadas").Next();
3640

3741
// Be sure to use a terminating step like Next() or Iterate() so that the traversal "executes"
3842
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
3943
g.V(v1).AddE("knows").To(v2).Property("weight", 0.75).Iterate();
4044
g.V(v1).AddE("knows").To(v3).Property("weight", 0.75).Iterate();
4145

4246
// Retrieve the data from the "marko" vertex
43-
var marko = await g.V().Has("person", "name", "marko").Values<string>("name").Promise(t => t.Next());
47+
var marko = await g.V().Has(VertexLabel, "name", "marko").Values<string>("name").Promise(t => t.Next());
4448
Console.WriteLine("name: " + marko);
4549

4650
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
47-
var peopleMarkoKnows = await g.V().Has("person", "name", "marko").Out("knows").Values<string>("name").Promise(t => t.ToList());
51+
var peopleMarkoKnows = await g.V().Has(VertexLabel, "name", "marko").Out("knows").Values<string>("name").Promise(t => t.ToList());
4852
foreach (var person in peopleMarkoKnows)
4953
{
5054
Console.WriteLine("marko knows " + person);

gremlin-examples/gremlin-dotnet/Connections/Connections.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ under the License.
2424

2525
public class ConnectionExample
2626
{
27+
static readonly string ServerHost = "localhost";
28+
static readonly int ServerPort = 8182;
29+
static readonly string VertexLabel = "connection";
30+
2731
static void Main()
2832
{
2933
WithRemote();
@@ -34,41 +38,38 @@ static void Main()
3438
// Connecting to the server
3539
static void WithRemote()
3640
{
37-
var server = new GremlinServer("localhost", 8182);
41+
var server = new GremlinServer(ServerHost, ServerPort);
3842
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
3943
var g = Traversal().WithRemote(remoteConnection);
4044

41-
// Drop existing vertices
42-
g.V().Drop().Iterate();
43-
4445
// Simple query to verify connection
45-
var v = g.AddV().Iterate();
46-
var count = g.V().Count().Next();
46+
var v = g.AddV(VertexLabel).Iterate();
47+
var count = g.V().HasLabel(VertexLabel).Count().Next();
4748
Console.WriteLine("Vertex count: " + count);
4849
}
4950

5051
// Connecting to the server with customized configurations
5152
static void WithConf()
5253
{
5354
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(
54-
new GremlinServer(hostname: "localhost", port: 8182, enableSsl: false, username: "", password: "")), "g");
55+
new GremlinServer(hostname: ServerHost, port: ServerPort, enableSsl: false, username: "", password: "")), "g");
5556
var g = Traversal().WithRemote(remoteConnection);
5657

57-
var v = g.AddV().Iterate();
58-
var count = g.V().Count().Next();
58+
var v = g.AddV(VertexLabel).Iterate();
59+
var count = g.V().HasLabel(VertexLabel).Count().Next();
5960
Console.WriteLine("Vertex count: " + count);
6061
}
6162

6263
// Specifying a serializer
6364
static void WithSerializer()
6465
{
65-
var server = new GremlinServer("localhost", 8182);
66+
var server = new GremlinServer(ServerHost, ServerPort);
6667
var client = new GremlinClient(server, new GraphSON3MessageSerializer());
6768
using var remoteConnection = new DriverRemoteConnection(client, "g");
6869
var g = Traversal().WithRemote(remoteConnection);
6970

70-
var v = g.AddV().Iterate();
71-
var count = g.V().Count().Next();
71+
var v = g.AddV(VertexLabel).Iterate();
72+
var count = g.V().HasLabel(VertexLabel).Count().Next();
7273
Console.WriteLine("Vertex count: " + count);
7374
}
7475
}

gremlin-examples/gremlin-dotnet/ModernTraversals/ModernTraversals.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ under the License.
2626

2727
public class ModernTraversalExample
2828
{
29+
static readonly string ServerHost = "localhost";
30+
static readonly int ServerPort = 8182;
31+
2932
static void Main()
3033
{
31-
var server = new GremlinServer("localhost", 8182);
34+
var server = new GremlinServer(ServerHost, ServerPort);
3235
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
3336
var g = Traversal().WithRemote(remoteConnection);
3437

0 commit comments

Comments
 (0)