예제 #1
0
  @Test
  public void testRemoveReferenceNode() throws Exception {
    GraphDatabaseService db = new ImpermanentGraphDatabase();
    final GraphDatabaseShellServer server = new GraphDatabaseShellServer(db, false);
    ShellClient client = new SameJvmClient(server);

    Documenter doc = new Documenter("sample session", client);
    doc.add("pwd", "", "where are we?");
    doc.add("set name \"Jon\"", "", "On the current node, set the key \"name\" to value \"Jon\"");
    doc.add("start n=node(0) return n", "Jon", "send a cypher query");
    doc.add(
        "mkrel -c -d i -t LIKES --np \"{'app':'foobar'}\"",
        "",
        "make an incoming relationship of type LIKES, create the end node with the node properties specified.");
    doc.add("ls", "1", "where are we?");
    doc.add("cd 1", "", "change to the newly created node");
    doc.add("ls -avr", "LIKES", "list relationships, including relationshship id");

    doc.add(
        "mkrel -c -d i -t KNOWS --np \"{'name':'Bob'}\"",
        "",
        "create one more KNOWS relationship and the end node");
    doc.add("pwd", "0", "print current history stack");
    doc.add("ls -avr", "KNOWS", "verbose list relationships");
    doc.run();
    // TODO: implement support for removing root node and previous nodes in the history stack of PWD
    // client.getServer().interpretLine( "rmnode -f 0", client.session(), client.getOutput() );
    //        client.getServer().interpretLine( "cd", client.session(), client.getOutput() );
    //        client.getServer().interpretLine( "pwd", client.session(), client.getOutput() );
    server.shutdown();
    db.shutdown();
  }
예제 #2
0
  @Test
  public void testMatrix() throws Exception {
    GraphDatabaseService db =
        new ImpermanentGraphDatabase(
            loadStrictly(new File("src/test/resources/autoindex.properties")));
    final GraphDatabaseShellServer server = new GraphDatabaseShellServer(db, false);
    ShellClient client = new SameJvmClient(server);

    Documenter doc = new Documenter("a matrix example", client);
    doc.add("mkrel -t ROOT -c -v", "created", "create the Thomas Andersson node");
    doc.add("cd 1", "", "go to the new node");
    doc.add("set name \"Thomas Andersson\"", "", "set the name property");
    doc.add("mkrel -t KNOWS -cv", "", "create Thomas direct friends");
    doc.add("cd 2", "", "go to the new node");
    doc.add("set name \"Trinity\"", "", "set the name property");
    doc.add("cd ..", "", "go back in the history stack");
    doc.add("mkrel -t KNOWS -cv", "", "create Thomas direct friends");
    doc.add("cd 3", "", "go to the new node");
    doc.add("set name \"Morpheus\"", "", "set the name property");
    doc.add("mkrel -t KNOWS -n 2", "", "create relationship to Trinity");

    doc.add("ls -rv", "", "list the relationships of node 3");
    doc.add("cd -r 2", "", "change the current position to relationship #2");

    doc.add("set -t int age 3", "", "set the age property on the relationship");
    doc.add("cd ..", "", "back to Morpheus");
    doc.add("cd -r 3", "", "next relationsip");
    doc.add("set -t int age 90", "", "set the age property on the relationship");
    doc.add("cd start", "", "position to the start node of the current relationship");

    doc.add(
        "", "", "We're now standing on Morpheus node, so let's create the rest of the friends.");
    doc.add("mkrel -t KNOWS -c", "", "new node");
    doc.add("ls -r", "", "list relationships on the current node");
    doc.add("cd 4", "", "go to Cypher");
    doc.add("set name Cypher", "", "set the name");
    doc.add("mkrel -ct KNOWS", "", "create new node from Cypher");
    // TODO: how to list outgoing relationships?
    // doc.add( "ls -rd out", "", "list relationships" );
    doc.add("ls -r", "", "list relationships");
    doc.add("cd 5", "", "go to the Agent Smith node");
    doc.add("set name \"Agent Smith\"", "", "set the name");
    doc.add("mkrel -cvt CODED_BY", "", "outgoing relationship and new node");
    doc.add("cd 6", "", "go there");
    doc.add("set name \"The Architect\"", "", "set the name");
    doc.add("cd", "", "go to the first node in the history stack");

    doc.add("", "", "Now, let's ask some questions");
    doc.add(
        "start morpheus = node:node_auto_index(name='Morpheus') "
            + "match morpheus-[:KNOWS]-zionist "
            + "return zionist.name",
        "",
        "Morpheus' friends, looking up Morpheus by name in the Neo4j autoindex");
    doc.run();
    server.shutdown();
    db.shutdown();
  }
예제 #3
0
  protected static void printAndInterpretTemplateLines(
      Collection<String> templateLines,
      boolean forcePrintHitHeader,
      boolean newLineBetweenHits,
      NodeOrRelationship entity,
      GraphDatabaseShellServer server,
      Session session,
      Output out)
      throws ShellException, RemoteException {
    if (templateLines.isEmpty() || forcePrintHitHeader) {
      out.println(getDisplayName(server, session, entity, true));
    }

    if (!templateLines.isEmpty()) {
      Map<String, Object> data = new HashMap<String, Object>();
      data.put("i", entity.getId());
      for (String command : templateLines) {
        String line = TextUtil.templateString(command, data);
        server.interpretLine(session.getId(), line, out);
      }
    }
    if (newLineBetweenHits) {
      out.println();
    }
  }
예제 #4
0
 protected static NodeOrRelationship getThingById(GraphDatabaseShellServer server, TypedId typedId)
     throws ShellException {
   NodeOrRelationship result = null;
   if (typedId.isNode()) {
     try {
       result = NodeOrRelationship.wrap(server.getDb().getNodeById(typedId.getId()));
     } catch (NotFoundException e) {
       throw new ShellException("Node " + typedId.getId() + " not found");
     }
   } else {
     try {
       result = NodeOrRelationship.wrap(server.getDb().getRelationshipById(typedId.getId()));
     } catch (NotFoundException e) {
       throw new ShellException("Relationship " + typedId.getId() + " not found");
     }
   }
   return result;
 }
예제 #5
0
 /**
  * @param server the {@link GraphDatabaseShellServer} to get the current node/relationship from.
  * @param session the {@link Session} used by the client.
  * @return the current node/relationship the client stands on at the moment.
  * @throws ShellException if some error occured.
  */
 public static NodeOrRelationship getCurrent(GraphDatabaseShellServer server, Session session)
     throws ShellException {
   String currentThing = (String) session.get(CURRENT_KEY);
   NodeOrRelationship result = null;
   if (currentThing == null) {
     try {
       result = NodeOrRelationship.wrap(server.getDb().getReferenceNode());
     } catch (NotFoundException e) {
       throw new ShellException("Reference node not found");
     }
     setCurrent(session, result);
   } else {
     TypedId typedId = new TypedId(currentThing);
     result = getThingById(server, typedId);
   }
   return result;
 }
예제 #6
0
 @Override
 protected void unload(GraphDatabaseShellServer server) {
   server.shutdown();
 }