Commit 0dfbd471 authored by Michael Hunger's avatar Michael Hunger
Browse files

Merge pull request #401 from peterneubauer/live-cypher

Live cypher console generation to the manual
parents 10111507 041f24e8
No related merge requests found
Showing with 121 additions and 2 deletions
+121 -2
......@@ -96,6 +96,12 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -32,7 +32,8 @@ import org.neo4j.visualization.asciidoc.AsciidocHelper
import org.neo4j.cypher.CuteGraphDatabaseService.gds2cuteGds
import org.neo4j.cypher.javacompat.GraphImpl
import org.neo4j.cypher.{CypherParser, ExecutionResult, ExecutionEngine}
import org.neo4j.test.{ImpermanentGraphDatabase, TestGraphDatabaseFactory, GraphDescription}
import org.neo4j.test.{ImpermanentGraphDatabase, TestGraphDatabaseFactory, GraphDescription, GeoffService}
import org.neo4j.test.GeoffService
abstract class DocumentingTestBase extends JUnitSuite {
......@@ -43,6 +44,7 @@ abstract class DocumentingTestBase extends JUnitSuite {
var nodeIndex: Index[Node] = null
var relIndex: Index[Relationship] = null
val properties: Map[String, Map[String, Any]] = Map()
var generateConsole: Boolean = true
def section: String
......@@ -75,6 +77,12 @@ abstract class DocumentingTestBase extends JUnitSuite {
}
writer.println()
writer.println()
if(generateConsole) {
writer.println(".Try this query live")
writer.println("[console]")
writer.println("----\n"+new GeoffService(db).toGeoff()+"\n"+query+"\n----")
writer.println()
}
writer.flush()
writer.close()
}
......
......@@ -30,7 +30,6 @@ import org.neo4j.cypher.CuteGraphDatabaseService.gds2cuteGds
class StartTest extends DocumentingTestBase {
def graphDescription = List("A KNOWS B", "A KNOWS C")
override def indexProps = List("name")
def section: String = "Start"
......@@ -72,6 +71,7 @@ class StartTest extends DocumentingTestBase {
}
@Test def nodes_by_index() {
generateConsole = false
testQuery(
title = "Node by index lookup",
text = "If the start point can be found by index lookups, it can be done like this: node:index-name(key = \"value\"). In this example, there exists a node index named 'nodes'.",
......@@ -81,6 +81,7 @@ class StartTest extends DocumentingTestBase {
}
@Test def relationships_by_index() {
generateConsole = false
db.inTx(()=>{
val r = db.getRelationshipById(0)
val property = "property"
......@@ -99,6 +100,7 @@ class StartTest extends DocumentingTestBase {
}
@Test def nodes_by_index_query() {
generateConsole = false
testQuery(
title = "Node by index query",
text = "If the start point can be found by index more complex lucene queries: node:index-name(\"query\")." +
......
......@@ -304,6 +304,12 @@ public class ComponentVersion extends Version
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
......
/**
* Copyright (c) 2002-2012 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.test;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.tooling.GlobalGraphOperations;
import com.google.gson.Gson;
/**
* @author mh
* @since 08.04.12
*/
public class GeoffService {
private final GraphDatabaseService gdb;
public GeoffService(GraphDatabaseService gdb) {
this.gdb = gdb;
}
private Map<String, Node> geoffNodeParams() {
Map<String, Node> result = new HashMap<String, Node>();
for (Node node : GlobalGraphOperations.at(gdb).getAllNodes()) {
result.put("(" + node.getId() + ")", node);
}
return result;
}
public String toGeoff() {
StringBuilder sb = new StringBuilder();
appendNodes(sb);
appendRelationships(sb);
return sb.toString();
}
private void appendRelationships(StringBuilder sb) {
for (Node node : GlobalGraphOperations.at(gdb).getAllNodes()) {
for (Relationship rel : node.getRelationships(Direction.OUTGOING)) {
formatNode(sb, rel.getStartNode());
sb.append("-[:").append(rel.getType().name()).append("]->");
formatNode(sb, rel.getEndNode());
formatProperties(sb, rel);
sb.append("\n");
}
}
}
private void appendNodes(StringBuilder sb) {
for (Node node : GlobalGraphOperations.at(gdb).getAllNodes()) {
formatNode(sb, node);
formatProperties(sb, node);
sb.append("\n");
}
}
private void formatNode(StringBuilder sb, Node n) {
sb.append("(").append(n.getId()).append(")");
}
private void formatProperties(StringBuilder sb, PropertyContainer pc) {
sb.append(" ");
sb.append(new Gson().toJson(toMap(pc)));
}
Map<String, Object> toMap(PropertyContainer pc) {
Map<String, Object> result = new TreeMap<String, Object>();
for (String prop : pc.getPropertyKeys()) {
result.put(prop, pc.getProperty(prop));
}
return result;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment