Search in sources :

Example 46 with RelationshipType

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

the class TraversalDescriptionBuilder method describeExpander.

@SuppressWarnings("unchecked")
private TraversalDescription describeExpander(TraversalDescription result, Map<String, Object> description) {
    Object relationshipsDescription = description.get("relationships");
    if (relationshipsDescription != null) {
        Collection<Object> pairDescriptions;
        if (relationshipsDescription instanceof Collection) {
            pairDescriptions = (Collection<Object>) relationshipsDescription;
        } else {
            pairDescriptions = Arrays.asList(relationshipsDescription);
        }
        PathExpanderBuilder builder = createExpander(description);
        for (Object pairDescription : pairDescriptions) {
            Map map = (Map) pairDescription;
            String name = (String) map.get("type");
            RelationshipType type = RelationshipType.withName(name);
            String directionName = (String) map.get("direction");
            builder = directionName == null ? builder.add(type) : builder.add(type, stringToEnum(directionName, RelationshipDirection.class, true).internal);
        }
        PathExpander<Object> expander = builder.build();
        result = result.expand(expander);
    }
    return result;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Map(java.util.Map)

Example 47 with RelationshipType

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

the class TestNeo4j method testBasicNodeRelationships.

@Test
public void testBasicNodeRelationships() {
    Node firstNode = null;
    Node secondNode = null;
    Relationship rel = null;
    // 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 = null;
    // 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)

Example 48 with RelationshipType

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

the class TestNeo4jCacheAndPersistence method testLowGrabSize.

@Test
public void testLowGrabSize() {
    Map<String, String> config = new HashMap<String, String>();
    config.put("relationship_grab_size", "1");
    String storePath = getStorePath("neo2");
    deleteFileOrDirectory(storePath);
    EmbeddedGraphDatabase graphDb = new EmbeddedGraphDatabase(storePath, config);
    Transaction tx = graphDb.beginTx();
    Node node1 = graphDb.createNode();
    Node node2 = graphDb.createNode();
    node1.createRelationshipTo(node2, MyRelTypes.TEST);
    node2.createRelationshipTo(node1, MyRelTypes.TEST2);
    node1.createRelationshipTo(node2, MyRelTypes.TEST_TRAVERSAL);
    tx.success();
    tx.finish();
    tx = graphDb.beginTx();
    Set<Relationship> rels = new HashSet<Relationship>();
    RelationshipType[] types = new RelationshipType[] { MyRelTypes.TEST, MyRelTypes.TEST2, MyRelTypes.TEST_TRAVERSAL };
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node1.getRelationships(types)) {
        assertTrue(rels.add(rel));
    }
    assertEquals(3, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node1.getRelationships()) {
        assertTrue(rels.add(rel));
    }
    assertEquals(3, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node2.getRelationships(types)) {
        assertTrue(rels.add(rel));
    }
    assertEquals(3, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node2.getRelationships()) {
        assertTrue(rels.add(rel));
    }
    assertEquals(3, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node1.getRelationships(Direction.OUTGOING)) {
        assertTrue(rels.add(rel));
    }
    assertEquals(2, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node1.getRelationships(Direction.INCOMING)) {
        assertTrue(rels.add(rel));
    }
    assertEquals(1, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node2.getRelationships(Direction.OUTGOING)) {
        assertTrue(rels.add(rel));
    }
    assertEquals(1, rels.size());
    rels.clear();
    graphDb.getConfig().getGraphDbModule().getNodeManager().clearCache();
    for (Relationship rel : node2.getRelationships(Direction.INCOMING)) {
        assertTrue(rels.add(rel));
    }
    assertEquals(2, rels.size());
    tx.success();
    tx.finish();
    graphDb.shutdown();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j-mobile-android by neo4j-contrib.

the class BatchInserterImpl method getRelationshipById.

public SimpleRelationship getRelationshipById(long relId) {
    RelationshipRecord record = getRelationshipRecord(relId);
    RelationshipType type = new RelationshipTypeImpl(typeHolder.getName(record.getType()));
    return new SimpleRelationship(record.getId(), record.getFirstNode(), record.getSecondNode(), type);
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)

Example 50 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j-mobile-android by neo4j-contrib.

the class NodeImpl method getRelationships.

public Iterable<Relationship> getRelationships(NodeManager nodeManager, RelationshipType type, Direction dir) {
    RelationshipType[] types = new RelationshipType[] { type };
    DirectionWrapper direction = RelIdArray.wrap(dir);
    return new IntArrayIterator(getAllRelationshipsOfType(nodeManager, direction, types), this, direction, nodeManager, types, !hasMoreRelationshipsToLoad());
}
Also used : DirectionWrapper(org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper) RelationshipType(org.neo4j.graphdb.RelationshipType)

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