Java Code Examples for org.neo4j.graphdb.Relationship#getOtherNode()

The following examples show how to use org.neo4j.graphdb.Relationship#getOtherNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LabelPropagation.java    From Neo4jSNA with Apache License 2.0 6 votes vote down vote up
protected long getMostFrequentLabel(Node node) {
	Long2LongMap commMap = new Long2LongOpenHashMap();
       Iterable<Relationship> relationships = relType == null ? node.getRelationships() : node.getRelationships(relType);

       for (Relationship r : relationships) {
           Node other = r.getOtherNode(node);
		long otherCommunity = (long) other.getProperty(attName);
		// commMap.put(other.getId(), otherCommunity);	WRONG
		long count = commMap.getOrDefault(otherCommunity, 0L);
		commMap.put(otherCommunity, count+1);
	}

	long mostFrequentLabel = -1;
	long mostFrequentLabelCount = -1;
	for( Entry<Long, Long> e : commMap.entrySet() ) {
		if( e.getValue() > mostFrequentLabelCount ) {
			mostFrequentLabelCount = e.getValue();
			mostFrequentLabel = e.getKey();
		}
	}
	return mostFrequentLabel;
}
 
Example 2
Source File: Neo4jUtil.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isConnected(final Node source, final Node target, final RelationshipType relationshipType) {
	final int sourceDegree = source.getDegree(relationshipType, Direction.OUTGOING);
	final int targetDegree = target.getDegree(relationshipType, Direction.INCOMING);
	
	final Direction searchDirection;
	final Node searchSource;
	final Node searchTarget;
	if (sourceDegree <= targetDegree) {
		searchDirection = Direction.OUTGOING;
		searchSource = source;
		searchTarget = target;
	} else {
		searchDirection = Direction.INCOMING;
		searchSource = target;
		searchTarget = source;
	}
	
	final Iterator<Relationship> edges = searchSource.getRelationships(searchDirection, relationshipType).iterator();
	while (edges.hasNext()) {
		final Relationship edge = edges.next();
		final Node otherNode = edge.getOtherNode(searchSource);
		if (searchTarget.equals(otherNode)) {
			return true;
		}
	}

	return false;
}
 
Example 3
Source File: PageRank.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Node node) {
	double secondMember = 0.0;
	for( Relationship rin : node.getRelationships() ) {
		Node neigh = rin.getOtherNode(node);
		
		double neighRank = (double) neigh.getProperty(attName);
		secondMember += neighRank / neigh.getDegree();
	}
	
	secondMember *= this.dampingFactor;
	node.setProperty(attName, firstMember + secondMember);
}
 
Example 4
Source File: TestGCISubClassOf.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubclassBlankNodeFiller() {
  Relationship r = getOnlyElement(subclass.getRelationships(Direction.INCOMING, OwlRelationships.FILLER));
  Node blankNode1 = r.getOtherNode(subclass);
  assertThat(blankNode1.hasLabel(OwlLabels.OWL_ANONYMOUS), is(true));
  r = getOnlyElement(blankNode1.getRelationships(Direction.OUTGOING, OwlRelationships.RDFS_SUBCLASS_OF));
  Node blankNode2 = r.getOtherNode(blankNode1);
  assertThat(blankNode2.hasLabel(OwlLabels.OWL_ANONYMOUS), is(true));
  r = getOnlyElement(blankNode1.getRelationships(Direction.OUTGOING, p));
  assertThat(r.getOtherNode(blankNode1), is(superclass));
}
 
Example 5
Source File: GraphOwlVisitorTestBase.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
static Node getLabeledOtherNode(Node node, RelationshipType type, Label label) {
  Iterable<Relationship> relationships = node.getRelationships(type);
  for (Relationship relationship: relationships) {
    if (relationship.getOtherNode(node).hasLabel(label)) {
      return relationship.getOtherNode(node);
    }
  }
  return null;
}
 
Example 6
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Integer> getNeighborsIds(int nodeId)
{
    Set<Integer> neighbors = new HashSet<Integer>();
    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            Node n = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_ID, String.valueOf(nodeId)).iterator()
                .next();
            for (Relationship relationship : n.getRelationships(RelTypes.SIMILAR, Direction.OUTGOING))
            {
                Node neighbour = relationship.getOtherNode(n);
                String neighbourId = (String) neighbour.getProperty(NODE_ID);
                neighbors.add(Integer.valueOf(neighbourId));
            }
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to get neighbors ids", e);
        }
    }

    return neighbors;
}
 
Example 7
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Integer> getCommunitiesConnectedToNodeCommunities(int nodeCommunities)
{
    Set<Integer> communities = new HashSet<Integer>();
    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            ResourceIterable<Node> nodes = neo4jGraph.findNodesByLabelAndProperty(Neo4jGraphDatabase.NODE_LABEL,
                NODE_COMMUNITY, nodeCommunities);
            for (Node n : nodes)
            {
                for (Relationship r : n.getRelationships(RelTypes.SIMILAR, Direction.OUTGOING))
                {
                    Node neighbour = r.getOtherNode(n);
                    Integer community = (Integer) (neighbour.getProperty(COMMUNITY));
                    communities.add(community);
                }
            }
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to get communities connected to node communities", e);
        }
    }

    return communities;
}
 
Example 8
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public double getEdgesInsideCommunity(int nodeCommunity, int communityNodes)
{
    double edges = 0;
    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            ResourceIterable<Node> nodes = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_COMMUNITY,
                nodeCommunity);
            ResourceIterable<Node> comNodes = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, COMMUNITY,
                communityNodes);
            for (Node node : nodes)
            {
                Iterable<Relationship> relationships = node.getRelationships(RelTypes.SIMILAR, Direction.OUTGOING);
                for (Relationship r : relationships)
                {
                    Node neighbor = r.getOtherNode(node);
                    if (Iterables.contains(comNodes, neighbor))
                    {
                        edges++;
                    }
                }
            }
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to get edges inside community", e);
        }
    }

    return edges;
}
 
Example 9
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 4 votes vote down vote up
@Override
public Node getOtherVertexFromEdge(Relationship r, Node n)
{
    return r.getOtherNode(n);
}