Java Code Examples for org.neo4j.graphdb.Node#getLabels()

The following examples show how to use org.neo4j.graphdb.Node#getLabels() . 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: UniqueRelationshipFactory.java    From neo4jena with Apache License 2.0 6 votes vote down vote up
/**
 * Finds relationship of given type between subject and object nodes.
 * 
 * @return Relationship if exists or null.
 */
public Relationship get(Node subject, RelationshipType type, Node object) {
	try {
		// FIXME Use relationship index instead of iterating over all relationships
		Iterable<Relationship> relations = subject.getRelationships(Direction.OUTGOING, type);
		for(Relationship relation: relations) {
			org.neo4j.graphdb.Node target = relation.getEndNode();
			// Match object with target node in the existing triple
			Iterable<Label> labels = object.getLabels();
			for(Label label:labels) {
				if(label.name().equals(NeoGraph.LABEL_LITERAL)) {
					// Match literal value of object and target in existing triple
					if(object.getProperty(NeoGraph.PROPERTY_VALUE).equals(target.getProperty(NeoGraph.PROPERTY_VALUE)))
						return relation;
					else return null;
				}
			}
			// Now match URI of object and target in existing triple
			// FIXME Blank Nodes?
			if(object.getProperty(NeoGraph.PROPERTY_URI).equals(target.getProperty(NeoGraph.PROPERTY_URI)))
				return relation;
		}
	} catch(RuntimeException exception) { }
	return null;
}
 
Example 2
Source File: GraphTransactionalImpl.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public void setLabel(long nodeId, Label label) {
  try (Transaction tx = graphDb.beginTx()) {
    Node node = graphDb.getNodeById(nodeId);
    for (Label currentLabel: node.getLabels()) {
      node.removeLabel(currentLabel);
    }
    node.addLabel(label);
    tx.success();
  }
}
 
Example 3
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 4
Source File: TinkerGraphUtil.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
Vertex addNode(Node node) {
  Vertex vertex = graph.getVertex(node.getId());
  if (null == vertex) {
    vertex = graph.addVertex(node.getId());
    copyProperties(node, vertex);
    Set<String> labels = new HashSet<>();
    for (Label label : node.getLabels()) {
      labels.add(label.name());
    }
    vertex.setProperty("types", labels);
  }
  return vertex;
}
 
Example 5
Source File: EmbeddedNode.java    From extended-objects with Apache License 2.0 5 votes vote down vote up
public EmbeddedNode(Node delegate) {
    super(delegate.getId(), delegate);
    this.labels = new HashSet<>();
    for (Label label : delegate.getLabels()) {
        labels.add(new EmbeddedLabel(label));
    }
}
 
Example 6
Source File: Neo4jHelper.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
private static String dumpNode(Node node) {
  try {
    ArrayList<String> labels = new ArrayList<>();
    for (Label label : node.getLabels()) {
      if (!"vertex".equals(label.name())) {
        labels.add(label.name());
      }
    }
    return String.format("(%d) [%s] %s ", node.getId(), String.join(", ", labels), dumpProps(node));
  } catch (Exception e) {
    return "(" + node.getId() + ") " + e.getMessage();
  }
}