Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ logs/*
build/
resources/
temp/
src/main/java/
15 changes: 13 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ group="offernet"
version="0.0.3"

task wrapper(type: Wrapper) {
gradleVersion = '3.3'
gradleVersion = '4.6'
}

jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.enabled true
}
}

test { finalizedBy jacocoTestReport }

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}

dependencies {
compile 'junit:junit:4.12'
compile 'org.codehaus.groovy:groovy-all:2.4.1'
compile 'org.codehaus.groovy:groovy-all:2.4.11'
compile 'com.datastax.dse:dse-java-driver-graph:1.5.1'
compile 'org.apache.tinkerpop:gremlin-core:3.3.1'
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-log4j12:1.7.22'
compile 'com.typesafe.akka:akka-actor_2.12:2.5.11'
compile 'com.typesafe.akka:akka-actor-typed_2.12:2.5.11'
testCompile 'com.typesafe.akka:akka-testkit_2.12:2.5.11'
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Thu Jan 12 11:26:58 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip
6 changes: 3 additions & 3 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions network-backends/dse-docker/scripts/gremlin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

HOST=192.168.1.6
PORT=8182

/opt/dse/bin/dse gremlin-console $HOST:$PORTe
File renamed without changes.
9 changes: 8 additions & 1 deletion network-backends/dse-docker/scripts/start-docker.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/bin/sh

# deleting all containers (not very efficient, but needed in order to run a container with the same name)
docker stop $(docker ps -a -q)
docker ps -q -a | xargs docker rm

# start dse-server with graph enabled
docker run -e DS_LICENSE=accept --name my-dse -d store/datastax/dse-server:5.1.6 -g
docker run -e DS_LICENSE=accept -e LISTEN_ADDRESS=127.0.0.1 -e START_RPC=true --name dse -d -p 8182:8182 -p 9042:9042 store/datastax/dse-server:5.1.6 -g

# start dse-studio (not needed for tests)
docker run -e DS_LICENSE=accept --name dse-studio -d -p 9091:9091 --link dse datastax/dse-studio

# start dse-opscenter
# docker run -e DS_LICENSE=accept --name opscenter -d -p 8888:8888 datastax/dse-opscenter
8 changes: 4 additions & 4 deletions scripts/compileGroovy.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash

if [ ! -d src/java ]; then mkdir -p src/java; else rm -r src/java/*; fi;
cp src/groovy/* src/java/
cd src/java
if [ ! -d src/main/java ]; then mkdir -p src/main/java; else rm -r src/main/java/*; fi;
cp src/main/groovy/* src/main/java/
cd src/main/java

#remove all @Grab annotations from the file -- cannot be run by java
#sed -i '/@Grab/d' *.groovy

echo $GROOVY_HOME

#compile
groovyc -cp "~/.groovy/gradle/*" -encoding utf-8 *.groovy
groovyc -cp "/home/kabir/.gradle/*" -encoding utf-8 --indy *.groovy
rm *.groovy
88 changes: 45 additions & 43 deletions src/main/groovy/Agent.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@Grab(group='com.datastax.cassandra', module='dse-driver', version='1.1.1')
//@Grab(group='com.datastax.dse', module='dse-java-driver-graph', version='1.5.1')
//@Grab(group='log4j', module='log4j', version='1.2.17')
//@Grab(group='com.typesafe.akka', module='akka-actor_2.12', version='2.5.11')

package net.vveitas.offernet

Expand All @@ -19,29 +20,22 @@ import org.apache.log4j.PropertyConfigurator
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import akka.actor.UntypedActor;
import akka.actor.UntypedAbstractActor;
import akka.actor.Props;
import akka.japi.Creator;

public class Agent extends UntypedActor {
import java.util.UUID;

public class Agent extends UntypedAbstractActor {
private Vertex vertex;
private DseSession session;
private Logger logger;

public static Props props(DseSession session) {
static Props props(DseSession session, String agentId) {
return Props.create(new Creator<Agent>() {
@Override
public Agent create() throws Exception {
return new Agent(session);
}
});
}

public static Props props(Object vertexId, DseSession session) {
return Props.create(new Creator<Agent>() {
@Override
public Agent create() throws Exception {
return new Agent(vertexId, session);
return new Agent(session,agentId);
}
});
}
Expand Down Expand Up @@ -70,7 +64,17 @@ public class Agent extends UntypedActor {
}
}

public Agent(DseSession session) {
/**
* Agent constructor returning a new agent by creating a vertex in the graph
* if a vertex with the given UUID exists - connect this vertex to the newly created actor
* UUID is shared between graph identifier (agentId) and actor identifier (path)
* @param session the DSE graph session for communication with the graph
* @return Agent class instance;
* @author kabir@singularitynet.io
*/

public Agent(DseSession session, String agentId) {

def start = System.currentTimeMillis();
def config = new ConfigSlurper().parse(new File('configs/log4j-properties.groovy').toURL())
PropertyConfigurator.configure(config.toProperties())
Expand All @@ -80,36 +84,32 @@ public class Agent extends UntypedActor {

Map params = new HashMap();
params.put("labelValue", "agent");

GraphResultSet rs = session.executeGraph(new SimpleGraphStatement("g.addV(label, labelValue)", params));
params.put("agentId",agentId);
params.put("agentIdLabel","agentId")

GraphResultSet rs = session.executeGraph(new SimpleGraphStatement(
"if (g.V().has(agentIdLabel,agentId).toList().size() == 0)\n"+
"g.addV(label, labelValue).property(agentIdLabel,agentId)\n"+
"else\n"+
"g.V().has(agentIdLabel,agentId)", params));
this.vertex = rs.one().asVertex();

logger.warn("Created a new {} with id {}", vertex.getLabel(), vertex.getId());
logger.warn("Created a new {} with id {} and agentId {}", vertex.getLabel(), vertex.getId(), vertex.getProperty("agentId").getValue());
logger.warn("Method {} took {} seconds to complete", Utils.getCurrentMethodName(), (System.currentTimeMillis()-start)/1000)
}

public Agent(Object vertexId, DseSession session) {
def start = System.currentTimeMillis();
def config = new ConfigSlurper().parse(new File('configs/log4j-properties.groovy').toURL())
PropertyConfigurator.configure(config.toProperties())
logger = LoggerFactory.getLogger('OfferNet.class');

this.session= session;

Map params = new HashMap();
params.put("vertexId",vertexId);

GraphResultSet rs = session.executeGraph(new SimpleGraphStatement("g.V(vertexId)", params));
this.vertex = rs.one().asVertex();

logger.warn("Instantiated an {} with existing vertex id {}", vertex.getLabel(), vertex.getId());
logger.warn("Method {} took {} seconds to complete", Utils.getCurrentMethodName(), (System.currentTimeMillis()-start)/1000)
/**
* returns the agentId property on the vertex, which is the unique id (is also the actor name in akka system)
* need to rename into something more intuitive -- agentId
*/
private String id() {
return vertex.getProperty("agentId").getValue().asString();
}

/*
* returns an id of an Agent vertex
/**
* returns the agentId property on the vertex, which is the unique id (is also the actor name in akka system)
*/
private id() {
private Object vertexId() {
return vertex.getId();
}

Expand Down Expand Up @@ -176,7 +176,7 @@ public class Agent extends UntypedActor {

Map params = new HashMap();
params.put("labelValue", "work");
params.put("agent", this.id());
params.put("agent", this.vertexId());
params.put("edgeLabel","owns");

logger.warn("Creating new work for agent {}", params.agent)
Expand Down Expand Up @@ -264,7 +264,7 @@ public class Agent extends UntypedActor {

logger.warn("Getting all works owned by agent {}",params.agent);

SimpleGraphStatement s = new SimpleGraphStatement("g.V(agent).out(edgeLabel)",params);
SimpleGraphStatement s = new SimpleGraphStatement("g.V().has('agentId',agent).out(edgeLabel)",params);

GraphResultSet rs = session.executeGraph(s);
List<Vertex> works = rs.all().collect {it.asVertex()};
Expand All @@ -281,12 +281,13 @@ public class Agent extends UntypedActor {

def start = System.currentTimeMillis();
Map params = new HashMap();
params.put("agentLabelName", this.id());
params.put("agentId", this.id());
params.put("agentIdLabel", "agentId");

logger.warn("Getting all items of agent {}", this.id())

SimpleGraphStatement s = new SimpleGraphStatement(
"g.V(agentLabelName).outE('owns').inV().outE().inV().has(label,'item')", params)
"g.V().has(agentIdLabel,agentId).outE('owns').inV().outE().inV().has(label,'item')", params)

GraphResultSet rs = session.executeGraph(s);
List items = rs.all().collect {it.asVertex() };
Expand Down Expand Up @@ -334,13 +335,14 @@ public class Agent extends UntypedActor {
private List<Vertex> itemsOfKnownAgents(Integer maxReachDistance) {
def start = System.currentTimeMillis()
Map params = new HashMap();
params.put("thisAgent", this.id());
params.put("thisAgentId", this.id());
params.put("agentIdLabel","agentId")
params.put("repeats", maxReachDistance);

logger.warn("Getting a list of all connected items of agent {} with loop {}", this.id(), maxReachDistance)

SimpleGraphStatement s = new SimpleGraphStatement(
"g.V(thisAgent).as('s').repeat("+
"g.V().has(agentIdLabel,thisAgentId).as('s').repeat("+
"both('knows').has(label,'agent')).times(repeats).emit().dedup().as('t')"+
".where('t',neq('s')).out('owns').out()",params);

Expand Down
33 changes: 32 additions & 1 deletion src/main/groovy/OfferNet.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ public class OfferNet implements AutoCloseable {
def start = System.currentTimeMillis()
cluster = DseCluster.builder().addContactPoint("192.168.1.6").build();
cluster.connect().executeGraph("system.graph('offernet').ifNotExists().create()");

cluster = DseCluster.builder()
.addContactPoint("192.168.1.6")
.withGraphOptions(new GraphOptions().setGraphName("offernet"))
.build();
session = cluster.connect();

session.executeGraph(new SimpleGraphStatement("schema.config().option('graph.schema_mode').set('Development')"))

logger.info("Created OfferNet instance with session {}", session);
logger.warn("Method {} took {} seconds to complete", Utils.getCurrentMethodName(), (System.currentTimeMillis()-start)/1000)

Expand Down Expand Up @@ -140,6 +142,35 @@ public class OfferNet implements AutoCloseable {
return agentIds;
}

private void createAgentNetworkWithChains(String[] args){
def numberOfAgents = args[1];
def numberOfRandomWorks = args[2];
def numberOfChains = args[3];
def lenghtOfChain = args[4];
List chains = [];
numberOfChains.times {
chains.add(Utils.createChain(lenghtOfChain));
}
createAgentNetwork(numberOfAgents,numberOfRandomWorks,chains);
}

private List createAgentNetwork(Integer numberOfAgents, Integer numberOfRandomWorks, ArrayList chains) {

def start = System.currentTimeMillis();
agentList = on.createAgentNetwork(numberOfAgents)
agentList.each {agent ->
agent.ownsWork()
}
on.addRandomWorksToAgents(numberOfRandomWorks)
chains.each {chain ->
on.addChainToNetwork(chain)
}
logger.warn("Created agentNetwork with {} agents, {} randomWorks and {} chains",numberOfAgents,numberOfRandomWorks,chains.size())
logger.warn("Method {} took {} seconds to complete", Utils.getCurrentMethodName(), (System.currentTimeMillis()-start)/1000)

return agentList;
}

public addRandomWorksToAgents(int numberOfWorks) {
def start=System.currentTimeMillis();
List agentIds = this.getIds('agent');
Expand Down
Loading