-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command for creating arbitrary edges between nodes
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
ehri-cmdline/src/main/java/eu/ehri/project/commands/RelationAdd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package eu.ehri.project.commands; | ||
|
||
import com.tinkerpop.blueprints.TransactionalGraph; | ||
import com.tinkerpop.blueprints.Vertex; | ||
import com.tinkerpop.frames.FramedGraph; | ||
import eu.ehri.project.core.GraphManager; | ||
import eu.ehri.project.core.GraphManagerFactory; | ||
import eu.ehri.project.exceptions.*; | ||
import eu.ehri.project.models.utils.JavaHandlerUtils; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Option; | ||
|
||
/** | ||
* Add an arbitrary edge between two nodes. | ||
* | ||
*/ | ||
public class RelationAdd extends BaseCommand implements Command { | ||
|
||
final static String NAME = "add-rel"; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
*/ | ||
public RelationAdd() { | ||
} | ||
|
||
@Override | ||
protected void setCustomOptions() { | ||
options.addOption(new Option("u", "unique", false, | ||
"Ensure the out entity only has one relationship of this type")); | ||
options.addOption(new Option("s", "single", false, | ||
"Don't create this relationship if it already exists")); | ||
} | ||
|
||
@Override | ||
public String getHelp() { | ||
return "Usage: add-rel [OPTIONS] [-Pkey=value] <source> <rel-name> <target>"; | ||
} | ||
|
||
@Override | ||
public String getUsage() { | ||
return "Create a relationship between a source and a target"; | ||
} | ||
|
||
/** | ||
* Add a relationship between two nodes. | ||
* | ||
* @throws eu.ehri.project.exceptions.ItemNotFound | ||
*/ | ||
@Override | ||
public int execWithOptions(final FramedGraph<? extends TransactionalGraph> graph, | ||
CommandLine cmdLine) throws ItemNotFound { | ||
|
||
GraphManager manager = GraphManagerFactory.getInstance(graph); | ||
|
||
if (cmdLine.getArgList().size() < 3) | ||
throw new RuntimeException(getHelp()); | ||
|
||
String src = (String)cmdLine.getArgList().get(0); | ||
String label = (String)cmdLine.getArgList().get(1); | ||
String dst = (String)cmdLine.getArgList().get(2); | ||
|
||
Vertex source = manager.getVertex(src); | ||
Vertex target = manager.getVertex(dst); | ||
|
||
try { | ||
if (cmdLine.hasOption("unique")) { | ||
if (!JavaHandlerUtils.addUniqueRelationship(source, target, label)) { | ||
System.err.println("Relationship already exists"); | ||
} | ||
} else if (cmdLine.hasOption("single")) { | ||
if (!JavaHandlerUtils.addSingleRelationship(source, target, label)) { | ||
System.err.println("Relationship already exists"); | ||
} | ||
} else { | ||
source.addEdge(label, target); | ||
} | ||
graph.getBaseGraph().commit(); | ||
} catch (Exception e) { | ||
graph.getBaseGraph().rollback(); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
ehri-cmdline/src/test/java/eu/ehri/project/commands/RelationAddTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package eu.ehri.project.commands; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Maps; | ||
import com.tinkerpop.blueprints.Direction; | ||
import com.tinkerpop.blueprints.Vertex; | ||
import eu.ehri.project.models.EntityClass; | ||
import eu.ehri.project.test.AbstractFixtureTest; | ||
import eu.ehri.project.test.GraphTestBase; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.CommandLineParser; | ||
import org.apache.commons.cli.PosixParser; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* @author Mike Bryant (http://github.com/mikesname) | ||
*/ | ||
public class RelationAddTest extends GraphTestBase { | ||
|
||
private Vertex mike; | ||
private Vertex linda; | ||
private Vertex reto; | ||
private RelationAdd relationAdd; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
relationAdd = new RelationAdd(); | ||
mike = manager.createVertex("mike", EntityClass.USER_PROFILE, Maps.<String,Object>newHashMap()); | ||
reto = manager.createVertex("reto", EntityClass.USER_PROFILE, Maps.<String,Object>newHashMap()); | ||
linda = manager.createVertex("linda", EntityClass.USER_PROFILE, Maps.<String,Object>newHashMap()); | ||
} | ||
|
||
@Test | ||
public void testAddRelation() throws Exception { | ||
assertEquals(0L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
CommandLine commandLine = relationAdd.getCmdLine(new String[]{"mike", "knows", "linda"}); | ||
int retVal = relationAdd.execWithOptions(graph, commandLine); | ||
assertEquals(0, retVal); | ||
assertEquals(1L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
assertEquals(1L, Iterables.size(linda.getVertices(Direction.IN, "knows"))); | ||
|
||
relationAdd.execWithOptions(graph, commandLine); | ||
assertEquals(2L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
assertEquals(2L, Iterables.size(linda.getVertices(Direction.IN, "knows"))); | ||
} | ||
|
||
@Test | ||
public void testAddUniqueRelation() throws Exception { | ||
assertEquals(0L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
CommandLine commandLine = relationAdd.getCmdLine(new String[]{"-u", "mike", "knows", "linda"}); | ||
int retVal = relationAdd.execWithOptions(graph, commandLine); | ||
assertEquals(0, retVal); | ||
assertEquals(1L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
assertEquals(1L, Iterables.size(linda.getVertices(Direction.IN, "knows"))); | ||
|
||
relationAdd.execWithOptions(graph, commandLine); | ||
assertEquals(1L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
assertEquals(1L, Iterables.size(linda.getVertices(Direction.IN, "knows"))); | ||
} | ||
|
||
@Test | ||
public void testAddSingleRelation() throws Exception { | ||
assertEquals(0L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
CommandLine commandLine1 = relationAdd.getCmdLine(new String[]{"mike", "knows", "linda"}); | ||
int retVal = relationAdd.execWithOptions(graph, commandLine1); | ||
assertEquals(0, retVal); | ||
assertEquals(1L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
assertEquals(1L, Iterables.size(linda.getVertices(Direction.IN, "knows"))); | ||
|
||
CommandLine commandLine2 = relationAdd.getCmdLine(new String[]{"-s", "mike", "knows", "reto"}); | ||
relationAdd.execWithOptions(graph, commandLine2); | ||
assertEquals(1L, Iterables.size(mike.getVertices(Direction.OUT, "knows"))); | ||
assertEquals(0L, Iterables.size(linda.getVertices(Direction.IN, "knows"))); | ||
assertEquals(1L, Iterables.size(reto.getVertices(Direction.IN, "knows"))); | ||
} | ||
} |