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

The following examples show how to use org.neo4j.graphdb.Relationship#getStartNode() . 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: Traversals.java    From EvilCoder with MIT License 6 votes vote down vote up
public static List<Node> getParentsConnectedBy(Node node, String edgeType)
{
	List<Node> retval = new LinkedList<Node>();

	long nodeId = node.getId();

	Iterable<Relationship> rels = node.getRelationships();
	for (Relationship rel : rels)
	{
		if (!rel.getType().name().equals(edgeType))
			continue;
		Node parentNode = rel.getStartNode();
		if (parentNode.getId() == nodeId)
			continue;

		retval.add(parentNode);
	}
	return retval;
}
 
Example 2
Source File: Neo4jUtil.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
public static Iterable<Node> getAdjacentNodes(final Node sourceNode, final RelationshipType relationshipType, final Direction direction, final Label targetNodeLabel) {
	final Collection<Node> nodes = new ArrayList<>();
	
	final Iterable<Relationship> relationships = sourceNode.getRelationships(relationshipType, direction);
	for (final Relationship relationship : relationships) {
		final Node candidate;
		switch (direction) {
		case INCOMING:
			candidate = relationship.getStartNode();
			break;
		case OUTGOING:
			candidate = relationship.getEndNode();			
			break;
		default:
			throw new UnsupportedOperationException("Direction: " + direction + " not supported.");
		}
		if (!candidate.hasLabel(targetNodeLabel)) {
			continue;
		}
		nodes.add(candidate);
	}
	return nodes;
}
 
Example 3
Source File: Traversals.java    From EvilCoder with MIT License 5 votes vote down vote up
public static Node getStatementForASTNode(Node node)
{
	Node n = node;
	Node parent = node;

	while (true)
	{

		try
		{
			Object property = n.getProperty(NodeKeys.IS_CFG_NODE);
			return n;
		}
		catch (NotFoundException ex)
		{

		}

		Iterable<Relationship> rels = n
				.getRelationships(Direction.INCOMING);
		for (Relationship rel : rels)
		{
			parent = rel.getStartNode();
			break;
		}

		if (n == parent)
			return null;
		n = parent;
	}
}
 
Example 4
Source File: TransExtractor.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private void prepare() {
    List<String> entities = new ArrayList<>();
    List<String> relations = new ArrayList<>();
    List<Triple<String, String, String>> triples = new ArrayList<>();
    try (Transaction tx = db.beginTx()) {
        for (Node node : db.getAllNodes()) {
            if (!node.hasLabel(Label.label(JavaCodeExtractor.CLASS)) &&
                    !node.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) &&
                    !node.hasLabel(Label.label(JavaCodeExtractor.METHOD)) &&
                    !node.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
                continue;
            entities.add("" + node.getId());
        }

        for (Relationship rel : db.getAllRelationships()) {
            Node node1 = rel.getStartNode();
            if (!node1.hasLabel(Label.label(JavaCodeExtractor.CLASS)) &&
                    !node1.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) &&
                    !node1.hasLabel(Label.label(JavaCodeExtractor.METHOD)) &&
                    !node1.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
                continue;
            Node node2 = rel.getEndNode();
            if (!node2.hasLabel(Label.label(JavaCodeExtractor.CLASS)) &&
                    !node2.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) &&
                    !node2.hasLabel(Label.label(JavaCodeExtractor.METHOD)) &&
                    !node2.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
                continue;
            triples.add(new ImmutableTriple<>("" + node1.getId(), "" + node2.getId(), rel.getType().name()));
            if (!relations.contains(rel.getType().name()))
                relations.add(rel.getType().name());
        }
        tx.success();
    }
    transE.prepare(entities, relations, triples);
}
 
Example 5
Source File: DirectedModularity.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
@Override
public void compute(Relationship r) {
	Node n1 = r.getStartNode();
	Node n2 = r.getEndNode();
	
	if( n1.getProperty(attName) == n2.getProperty(attName) ) {
		double weight = r.hasProperty("weight") ? (double) r.getProperty("weight") : 1.0;
		eii += weight;
	}
}
 
Example 6
Source File: NodeTransformer.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public Concept apply(Node n) {
  try (Transaction tx = n.getGraphDatabase().beginTx()) {
    Concept concept = new Concept(n.getId());
    concept.setIri((String) n.getProperty(Concept.IRI, null));
    concept.setAnonymous(n.hasLabel(OwlLabels.OWL_ANONYMOUS));
    concept.setDeprecated(isDeprecated(n));

    for (String definition : GraphUtil.getProperties(n, Concept.DEFINITION, String.class)) {
      concept.addDefinition(definition);
    }
    for (String abbreviation : GraphUtil.getProperties(n, Concept.ABREVIATION, String.class)) {
      concept.addAbbreviation(abbreviation);
    }
    for (String acronym : GraphUtil.getProperties(n, Concept.ACRONYM, String.class)) {
      concept.addAcronym(acronym);
    }
    for (String category : GraphUtil.getProperties(n, Concept.CATEGORY, String.class)) {
      concept.addCategory(category);
    }
    for (String label : GraphUtil.getProperties(n, Concept.LABEL, String.class)) {
      concept.addLabel(label);
    }
    for (String synonym : GraphUtil.getProperties(n, Concept.SYNONYM, String.class)) {
      concept.addSynonym(synonym);
    }
    for (Label type : n.getLabels()) {
      concept.addType(type.name());
    }

    for (Relationship r: n.getRelationships(OwlRelationships.OWL_EQUIVALENT_CLASS)) {
      Node equivalence = r.getStartNode().equals(n) ? r.getEndNode() : r.getStartNode();
      concept.getEquivalentClasses().add((String)equivalence.getProperty(CommonProperties.IRI));
    }

    tx.success();
    return concept;
  }
}
 
Example 7
Source File: AbstractEmbeddedDBAccess.java    From jcypher with Apache License 2.0 5 votes vote down vote up
private RelationNodes init(Relationship relation) {
	RelationNodes ret = new RelationNodes();
	JsonObjectBuilder rel = Json.createObjectBuilder();
	rel.add("id", String.valueOf(relation.getId()));
	RelationshipType typ = relation.getType();
	if (typ != null)
		rel.add("type", typ.name());
	Node sn = relation.getStartNode();
	if (sn != null) {
		rel.add("startNode", String.valueOf(sn.getId()));
		ret.startNode = sn;
	}
	Node en = relation.getEndNode();
	if (en != null) {
		rel.add("endNode", String.valueOf(en.getId()));
		ret.endNode = en;
	}
	JsonObjectBuilder props = Json.createObjectBuilder();
	Iterator<String> pit = relation.getPropertyKeys().iterator();
	while (pit.hasNext()) {
		String pKey = pit.next();
		Object pval = relation.getProperty(pKey);
		writeLiteral(pKey, pval, props);
	}
	rel.add("properties", props);
	this.relationObject = rel;
	return ret;
}
 
Example 8
Source File: EmbeddedRelationship.java    From extended-objects with Apache License 2.0 4 votes vote down vote up
public EmbeddedRelationship(Relationship delegate) {
    super(delegate.getId(), delegate);
    this.startNode = new EmbeddedNode(delegate.getStartNode());
    this.endNode = new EmbeddedNode(delegate.getEndNode());
}
 
Example 9
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 4 votes vote down vote up
@Override
public Node getSrcVertexFromEdge(Relationship edge)
{
    return edge.getStartNode();
}
 
Example 10
Source File: ReadOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 4 votes vote down vote up
/**
 * update the ego network of a user
 * 
 * @param user
 *            user where changes have occurred
 */
private void updateEgoNetwork(final Node user) {
	Node followedReplica, followingUser, lastPosterReplica;
	Node prevReplica, nextReplica;

	// loop through users following
	for (Relationship relationship : user.getRelationships(
			SocialGraphRelationshipType.REPLICA, Direction.INCOMING)) {
		// load each replica and the user corresponding
		followedReplica = relationship.getStartNode();
		followingUser = NeoUtils.getPrevSingleNode(followedReplica,
				SocialGraphRelationshipType.FOLLOW);

		// bridge user node
		prevReplica = NeoUtils.getPrevSingleNode(followedReplica,
				SocialGraphRelationshipType.GRAPHITY);
		if (!prevReplica.equals(followingUser)) {
			followedReplica.getSingleRelationship(
					SocialGraphRelationshipType.GRAPHITY,
					Direction.INCOMING).delete();

			nextReplica = NeoUtils.getNextSingleNode(followedReplica,
					SocialGraphRelationshipType.GRAPHITY);
			if (nextReplica != null) {
				followedReplica.getSingleRelationship(
						SocialGraphRelationshipType.GRAPHITY,
						Direction.OUTGOING).delete();
				prevReplica.createRelationshipTo(nextReplica,
						SocialGraphRelationshipType.GRAPHITY);
			}
		}

		// insert user's replica at its new position
		lastPosterReplica = NeoUtils.getNextSingleNode(followingUser,
				SocialGraphRelationshipType.GRAPHITY);
		if (!lastPosterReplica.equals(followedReplica)) {
			followingUser.getSingleRelationship(
					SocialGraphRelationshipType.GRAPHITY,
					Direction.OUTGOING).delete();
			followingUser.createRelationshipTo(followedReplica,
					SocialGraphRelationshipType.GRAPHITY);
			followedReplica.createRelationshipTo(lastPosterReplica,
					SocialGraphRelationshipType.GRAPHITY);
		}
	}
}
 
Example 11
Source File: NeoUtils.java    From metalcon with GNU General Public License v3.0 3 votes vote down vote up
/**
 * find an incoming relation from the user passed of the specified type
 * 
 * @param user
 *            destination user node
 * @param relationshipType
 *            relationship type of the relation being searched
 * @return source node of the relation<br>
 *         null - if there is no relation of the type specified
 */
public static Node getPrevSingleNode(final Node user,
		RelationshipType relationshipType) {
	// find an incoming relation of the type specified
	final Relationship rel = user.getSingleRelationship(relationshipType,
			Direction.INCOMING);

	return (rel == null) ? (null) : (rel.getStartNode());
}