Search in sources :

Example 86 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class DatabaseMetadataService method generateJsonRepresentation.

private String generateJsonRepresentation(Iterable<RelationshipType> relationshipTypes) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (RelationshipType rt : relationshipTypes) {
        sb.append("\"");
        sb.append(rt.name());
        sb.append("\",");
    }
    sb.append("]");
    return sb.toString().replaceAll(",]", "]");
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 87 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class DatabaseMetadataService method getRelationshipTypes.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getRelationshipTypes(@QueryParam("in_use") @DefaultValue("true") boolean inUse) {
    try {
        GraphDatabaseAPI db = database.getGraph();
        Iterable<RelationshipType> relationshipTypes = inUse ? db.getAllRelationshipTypesInUse() : db.getAllRelationshipTypes();
        return Response.ok().type(MediaType.APPLICATION_JSON).entity(generateJsonRepresentation(relationshipTypes)).build();
    } finally {
        representationWriteHandler.onRepresentationFinal();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) RelationshipType(org.neo4j.graphdb.RelationshipType) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 88 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class TestTransactionEvents method shouldProvideTheCorrectRelationshipData.

@Test
public void shouldProvideTheCorrectRelationshipData() {
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    // create a rel type so the next type id is non zero
    try (Transaction tx = db.beginTx()) {
        db.createNode().createRelationshipTo(db.createNode(), withName("TYPE"));
    }
    RelationshipType livesIn = withName("LIVES_IN");
    long relId;
    try (Transaction tx = db.beginTx()) {
        Node person = db.createNode(label("Person"));
        Node city = db.createNode(label("City"));
        Relationship rel = person.createRelationshipTo(city, livesIn);
        rel.setProperty("since", 2009);
        relId = rel.getId();
        tx.success();
    }
    final Set<String> changedRelationships = new HashSet<>();
    db.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Void>() {

        @Override
        public Void beforeCommit(TransactionData data) throws Exception {
            for (PropertyEntry<Relationship> entry : data.assignedRelationshipProperties()) {
                changedRelationships.add(entry.entity().getType().name());
            }
            return null;
        }
    });
    try (Transaction tx = db.beginTx()) {
        Relationship rel = db.getRelationshipById(relId);
        rel.setProperty("since", 2010);
        tx.success();
    }
    assertEquals(1, changedRelationships.size());
    assertTrue(livesIn + " not in " + changedRelationships.toString(), changedRelationships.contains(livesIn.name()));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Relationship(org.neo4j.graphdb.Relationship) TransactionData(org.neo4j.graphdb.event.TransactionData) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 89 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class TestRelationship method makeSureLazyLoadingRelationshipsWorksEvenIfOtherIteratorAlsoLoadsInTheSameIteration.

@Test
public void makeSureLazyLoadingRelationshipsWorksEvenIfOtherIteratorAlsoLoadsInTheSameIteration() {
    int numEdges = 100;
    /* create 256 nodes */
    GraphDatabaseService graphDB = getGraphDb();
    Node[] nodes = new Node[256];
    for (int num_nodes = 0; num_nodes < nodes.length; num_nodes += 1) {
        nodes[num_nodes] = graphDB.createNode();
    }
    newTransaction();
    /* create random outgoing relationships from node 5 */
    Node hub = nodes[4];
    int nextID = 7;
    RelationshipType outtie = withName("outtie");
    RelationshipType innie = withName("innie");
    for (int k = 0; k < numEdges; k++) {
        Node neighbor = nodes[nextID];
        nextID += 7;
        nextID &= 255;
        if (nextID == 0) {
            nextID = 1;
        }
        hub.createRelationshipTo(neighbor, outtie);
    }
    newTransaction();
    /* create random incoming relationships to node 5 */
    for (int k = 0; k < numEdges; k += 1) {
        Node neighbor = nodes[nextID];
        nextID += 7;
        nextID &= 255;
        if (nextID == 0) {
            nextID = 1;
        }
        neighbor.createRelationshipTo(hub, innie);
    }
    commit();
    newTransaction();
    hub = graphDB.getNodeById(hub.getId());
    int count = 0;
    for (Relationship ignore : hub.getRelationships()) {
        count += Iterables.count(hub.getRelationships());
    }
    assertEquals(40000, count);
    count = 0;
    for (@SuppressWarnings("unused") Relationship r1 : hub.getRelationships()) {
        count += Iterables.count(hub.getRelationships());
    }
    assertEquals(40000, count);
    commit();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Example 90 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class TestNeo4j method testBasicNodeRelationships.

@Test
public void testBasicNodeRelationships() {
    Node firstNode;
    Node secondNode;
    Relationship rel;
    // Create nodes and a relationship between them
    firstNode = getGraphDb().createNode();
    assertNotNull("Failure creating first node", firstNode);
    secondNode = getGraphDb().createNode();
    assertNotNull("Failure creating second node", secondNode);
    rel = firstNode.createRelationshipTo(secondNode, MyRelTypes.TEST);
    assertNotNull("Relationship is null", rel);
    RelationshipType relType = rel.getType();
    assertNotNull("Relationship's type is is null", relType);
    // Verify that the node reports that it has a relationship of
    // the type we created above
    assertTrue(firstNode.getRelationships(relType).iterator().hasNext());
    assertTrue(secondNode.getRelationships(relType).iterator().hasNext());
    Iterable<Relationship> allRels;
    // Verify that both nodes return the relationship we created above
    allRels = firstNode.getRelationships();
    assertTrue(this.objectExistsInIterable(rel, allRels));
    allRels = firstNode.getRelationships(relType);
    assertTrue(this.objectExistsInIterable(rel, allRels));
    allRels = secondNode.getRelationships();
    assertTrue(this.objectExistsInIterable(rel, allRels));
    allRels = secondNode.getRelationships(relType);
    assertTrue(this.objectExistsInIterable(rel, allRels));
    // Verify that the relationship reports that it is associated with
    // firstNode and secondNode
    Node[] relNodes = rel.getNodes();
    assertEquals("A relationship should always be connected to exactly " + "two nodes", relNodes.length, 2);
    assertTrue("Relationship says that it isn't connected to firstNode", this.objectExistsInArray(firstNode, relNodes));
    assertTrue("Relationship says that it isn't connected to secondNode", this.objectExistsInArray(secondNode, relNodes));
    assertTrue("The other node should be secondNode but it isn't", rel.getOtherNode(firstNode).equals(secondNode));
    assertTrue("The other node should be firstNode but it isn't", rel.getOtherNode(secondNode).equals(firstNode));
    rel.delete();
    secondNode.delete();
    firstNode.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)97 Node (org.neo4j.graphdb.Node)53 Test (org.junit.Test)45 Relationship (org.neo4j.graphdb.Relationship)38 Transaction (org.neo4j.graphdb.Transaction)18 Direction (org.neo4j.graphdb.Direction)15 Traverser (org.neo4j.graphdb.Traverser)10 NotFoundException (org.neo4j.graphdb.NotFoundException)9 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)7 Label (org.neo4j.graphdb.Label)7 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)6 Path (org.neo4j.graphdb.Path)6 StopEvaluator (org.neo4j.graphdb.StopEvaluator)6 TraversalPosition (org.neo4j.graphdb.TraversalPosition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)4