org.neo4j.graphdb.RelationshipType Java Examples
The following examples show how to use
org.neo4j.graphdb.RelationshipType.
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: TestSubClassOfExistential.java From SciGraph with Apache License 2.0 | 6 votes |
/** * See https://github.com/SciCrunch/SciGraph/wiki/MappingToOWL#subclassof-axioms * * Reduction step should give us a simple edge {sub p super} */ @Test public void testSubclass() { Node subclass = getNode("http://example.org/subclass"); Node superclass = getNode("http://example.org/superclass"); RelationshipType p = RelationshipType.withName("http://example.org/p"); Relationship relationship = getOnlyElement(GraphUtil.getRelationships(subclass, superclass, p)); assertThat("subclassOf relationship should start with the subclass.", relationship.getStartNode(), is(subclass)); assertThat("subclassOf relationship should end with the superclass.", relationship.getEndNode(), is(superclass)); assertThat("relationship has the correct iri", GraphUtil.getProperty(relationship, CommonProperties.IRI, String.class), is(Optional.of("http://example.org/p"))); assertThat("relationship is asserted", GraphUtil.getProperty(relationship, CommonProperties.CONVENIENCE, Boolean.class), is(Optional.of(true))); assertThat("owltype is added", GraphUtil.getProperty(relationship, CommonProperties.OWL_TYPE, String.class), is(Optional.of(OwlRelationships.RDFS_SUBCLASS_OF.name()))); }
Example #2
Source File: GraphTransactionalImpl.java From SciGraph with Apache License 2.0 | 6 votes |
@Override public Collection<Long> createRelationshipsPairwise(Collection<Long> nodeIds, RelationshipType type) { Set<Long> relationships = new HashSet<>(); for (Long start : nodeIds) { for (Long end : nodeIds) { if (start.equals(end)) { continue; } else { if (!getRelationship(end, start, type).isPresent()) { relationships.add(createRelationship(start, end, type)); } } } } return relationships; }
Example #3
Source File: MboxHandler.java From SnowGraph with Apache License 2.0 | 6 votes |
private void createUserNode(Node mailNode, String userName, String userAddress, boolean sender) { Node userNode; if (!mailUserMap.containsKey(userAddress)) { userNode = db.createNode(); userNode.addLabel(Label.label(MailListExtractor.MAILUSER)); userNode.setProperty(MailListExtractor.MAILUSER_MAIL, userAddress); mailUserMap.put(userAddress, userNode); } userNode = mailUserMap.get(userAddress); if (!mailUserNameMap.containsKey(userAddress)) mailUserNameMap.put(userAddress, new HashSet<>()); mailUserNameMap.get(userAddress).add(userName); if (sender) mailNode.createRelationshipTo(userNode, RelationshipType.withName(MailListExtractor.MAIL_SENDER)); else mailNode.createRelationshipTo(userNode, RelationshipType.withName(MailListExtractor.MAIL_RECEIVER)); }
Example #4
Source File: MailListExtractor.java From SnowGraph with Apache License 2.0 | 6 votes |
public void run(GraphDatabaseService db) { this.db = db; MboxHandler myHandler = new MboxHandler(); myHandler.setDb(db); MimeConfig config=new MimeConfig(); config.setMaxLineLen(-1); parser = new MimeStreamParser(config); parser.setContentHandler(myHandler); parse(new File(mboxPath)); try (Transaction tx = db.beginTx()) { for (String address : myHandler.getMailUserNameMap().keySet()) { Node node = myHandler.getMailUserMap().get(address); node.setProperty(MAILUSER_NAMES, String.join(", ", myHandler.getMailUserNameMap().get(address))); } tx.success(); } try (Transaction tx = db.beginTx()) { for (String mailId : myHandler.getMailReplyMap().keySet()) { Node mailNode = myHandler.getMailMap().get(mailId); Node replyNode = myHandler.getMailMap().get(myHandler.getMailReplyMap().get(mailId)); if (mailNode != null & replyNode != null) mailNode.createRelationshipTo(replyNode, RelationshipType.withName(MailListExtractor.MAIL_IN_REPLY_TO)); } tx.success(); } }
Example #5
Source File: AbstractEmbeddedNeo4jDatastore.java From extended-objects with Apache License 2.0 | 6 votes |
@Override public DatastoreMetadataFactory<NodeMetadata<EmbeddedLabel>, EmbeddedLabel, RelationshipMetadata<EmbeddedRelationshipType>, EmbeddedRelationshipType> getMetadataFactory() { return new AbstractNeo4jMetadataFactory<EmbeddedLabel, EmbeddedRelationshipType>() { protected EmbeddedLabel createLabel(String value) { return new EmbeddedLabel(value); } protected EmbeddedRelationshipType createRelationshipType(String name) { return new EmbeddedRelationshipType(RelationshipType.withName(name)); } @Override protected boolean isBatchableDefault() { return false; } }; }
Example #6
Source File: GraphApi.java From SciGraph with Apache License 2.0 | 6 votes |
public Graph getEdges(RelationshipType type, boolean entail, long skip, long limit) { String query = "MATCH path = (start)-[r:" + type.name() + (entail ? "!" : "") + "]->(end) " + " RETURN path " // TODO: This slows down the query dramatically. // + " ORDER BY ID(r) " + " SKIP " + skip + " LIMIT " + limit; Graph graph = new TinkerGraph(); TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil); Result result; try { result = cypherUtil.execute(query); while (result.hasNext()) { Map<String, Object> map = result.next(); Path path = (Path) map.get("path"); tgu.addPath(path); } } catch (ArrayIndexOutOfBoundsException e) { // Return and empty graph if the limit is too high... } return graph; }
Example #7
Source File: CliqueTest.java From SciGraph with Apache License 2.0 | 6 votes |
@Before public void setup() { clique11 = createNode("http://x.org/a"); clique12 = createNode("http://x.org/b"); clique13 = createNode("http://x.org/c"); clique21 = createNode("http://x.org/d"); clique22 = createNode("http://x.org/e"); Relationship r1 = clique11.createRelationshipTo(clique12, IS_EQUIVALENT); Relationship r2 = clique12.createRelationshipTo(clique13, IS_EQUIVALENT); Relationship r3 = clique21.createRelationshipTo(clique22, IS_EQUIVALENT); Relationship r4 = clique12.createRelationshipTo(clique22, RelationshipType.withName("hasPhenotype")); Relationship r5 = clique13.createRelationshipTo(clique21, RelationshipType.withName("hasPhenotype")); CliqueConfiguration cliqueConfiguration = new CliqueConfiguration(); Set<String> rel = new HashSet<String>(); rel.add(IS_EQUIVALENT.name()); cliqueConfiguration.setRelationships(rel); Set<String> forbidden = new HashSet<String>(); forbidden.add("anonymous"); cliqueConfiguration.setLeaderForbiddenLabels(forbidden); cliqueConfiguration.setLeaderAnnotation(leaderAnnotation); clique = new Clique(graphDb, cliqueConfiguration); }
Example #8
Source File: Clique.java From SciGraph with Apache License 2.0 | 6 votes |
@Inject public Clique(GraphDatabaseService graphDb, CliqueConfiguration cliqueConfiguration) { this.graphDb = graphDb; this.prefixLeaderPriority = cliqueConfiguration.getLeaderPriority(); this.leaderAnnotationProperty = cliqueConfiguration.getLeaderAnnotation(); Set<Label> tmpLabels = new HashSet<Label>(); for (String l : cliqueConfiguration.getLeaderForbiddenLabels()) { tmpLabels.add(Label.label(l)); } this.forbiddenLabels = tmpLabels; Set<RelationshipType> tmpRelationships = new HashSet<RelationshipType>(); for (String r : cliqueConfiguration.getRelationships()) { tmpRelationships.add(RelationshipType.withName(r)); } this.relationships = tmpRelationships; this.batchCommitSize = cliqueConfiguration.getBatchCommitSize(); }
Example #9
Source File: OwlPostprocessor.java From SciGraph with Apache License 2.0 | 6 votes |
long processCategory(Node root, RelationshipType type, Direction direction, String category) { long count = 0; int batchSize = 100_000; Label label = Label.label(category); Transaction tx = graphDb.beginTx(); for (Path position : graphDb.traversalDescription().uniqueness(Uniqueness.NODE_GLOBAL) .depthFirst().relationships(type, direction) .relationships(OwlRelationships.RDF_TYPE, Direction.INCOMING) .relationships(OwlRelationships.OWL_EQUIVALENT_CLASS, Direction.BOTH).traverse(root)) { Node end = position.endNode(); GraphUtil.addProperty(end, Concept.CATEGORY, category); end.addLabel(label); if (0 == ++count % batchSize) { logger.fine("Commiting " + count); tx.success(); tx.close(); tx = graphDb.beginTx(); } } tx.success(); tx.close(); return count; }
Example #10
Source File: Neo4jUtil.java From trainbenchmark with Eclipse Public License 1.0 | 6 votes |
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 #11
Source File: UniqueRelationshipFactory.java From neo4jena with Apache License 2.0 | 6 votes |
/** * 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 #12
Source File: TinkerGraphUtilTest.java From SciGraph with Apache License 2.0 | 6 votes |
@Test public void relationshipsAreTranslated() { TinkerGraphUtil tgu = new TinkerGraphUtil(curieUtil); Vertex u = tgu.addNode(node); Vertex v = tgu.addNode(otherNode); Relationship relationship = mock(Relationship.class); when(relationship.getEndNode()).thenReturn(node); when(relationship.getStartNode()).thenReturn(otherNode); when(relationship.getType()).thenReturn(RelationshipType.withName("foo")); when(relationship.getPropertyKeys()).thenReturn(newHashSet("bar")); when(relationship.getProperty("bar")).thenReturn("baz"); Edge edge = tgu.addEdge(relationship); assertThat(edge.getVertex(Direction.IN), is(u)); assertThat(edge.getVertex(Direction.OUT), is(v)); assertThat(edge.getLabel(), is("foo")); assertThat((String)edge.getProperty("bar"), is("baz")); Edge edge2 = tgu.addEdge(relationship); assertThat(edge, is(edge2)); }
Example #13
Source File: GraphBatchImpl.java From SciGraph with Apache License 2.0 | 6 votes |
@Override public Collection<Long> createRelationshipsPairwise(Collection<Long> nodes, RelationshipType type) { Set<Long> relationships = new HashSet<>(); for (Long start : nodes) { for (Long end : nodes) { if (start.equals(end)) { continue; } else { synchronized (graphLock) { if (!getRelationship(end, start, type).isPresent()) { relationships.add(createRelationship(start, end, type)); } } } } } return relationships; }
Example #14
Source File: TestPun.java From SciGraph with Apache License 2.0 | 6 votes |
@Test public void testPun() { Node i = getNode("http://example.org/i"); Node j = getNode("http://example.org/j"); Node k = getNode("http://example.org/k"); RelationshipType p = RelationshipType.withName("http://example.org/p"); Relationship relationship = getOnlyElement(GraphUtil.getRelationships(i, j, p)); assertThat("OPE edge should start with the subject.", relationship.getStartNode(), is(i)); assertThat("OPE edge should end with the target.", relationship.getEndNode(), is(j)); relationship = getOnlyElement(GraphUtil.getRelationships(i, k, OwlRelationships.RDFS_SUBCLASS_OF)); assertThat("Subclass edge should start with i.", relationship.getStartNode(), is(i)); assertThat("Subclass edge should end with k.", relationship.getEndNode(), is(k)); assertThat("i is both a class an a named individual" , i.getLabels(), is(IsIterableContainingInAnyOrder.containsInAnyOrder(OwlLabels.OWL_CLASS, OwlLabels.OWL_NAMED_INDIVIDUAL))); }
Example #15
Source File: RelationshipProcedure.java From neo4j-versioner-core with Apache License 2.0 | 5 votes |
@Procedure(value = "graph.versioner.relationship.delete", mode = Mode.WRITE) @Description("graph.versioner.relationship.delete(entityA, entityB, type, date) - Delete a custom type relationship from entitySource's current State to entityDestination for the specified date.") public Stream<BooleanOutput> relationshipDelete( @Name("entitySource") Node entitySource, @Name("entityDestination") Node entityDestination, @Name(value = "type") String type, @Name(value = "date", defaultValue = "null") LocalDateTime date) { isEntityOrThrowException(entitySource); isEntityOrThrowException(entityDestination); if (isSystemType(type)) { throw new VersionerCoreException("It's not possible to delete a System Relationship like " + type + "."); } Optional<Node> sourceCurrentState = createNewSourceState(entitySource, defaultToNow(date)); Optional<Node> destinationRNode = getRNode(entityDestination); Update updateProcedure = new UpdateBuilder().withLog(log).withDb(db).build().orElseThrow(() -> new VersionerCoreException("Unable to initialize update procedure")); if (sourceCurrentState.isPresent() && destinationRNode.isPresent()) { updateProcedure.update(entitySource, sourceCurrentState.get().getAllProperties(), "", null); getCurrentRelationship(entitySource).ifPresent(rel -> rel.getEndNode().getRelationships(RelationshipType.withName(type), Direction.OUTGOING).forEach(Relationship::delete)); return Stream.of(new BooleanOutput(Boolean.TRUE)); } else { return Stream.of(new BooleanOutput(Boolean.FALSE)); } }
Example #16
Source File: CypherUtilTest.java From SciGraph with Apache License 2.0 | 5 votes |
@Test public void multipleEntailmentRegex_types() { Set<RelationshipType> types = util.getEntailedRelationshipTypes(newHashSet("http://x.org/#foo", "http://x.org/#bar")); Collection<String> typeNames = transform(types, new Function<RelationshipType, String>() { @Override public String apply(RelationshipType arg0) { return arg0.name(); } }); assertThat( typeNames, containsInAnyOrder("http://x.org/#foo", "http://x.org/#bar", "http://x.org/#fizz", "http://x.org/#fizz_equiv", "http://x.org/#baz")); }
Example #17
Source File: RelationshipMapTest.java From SciGraph with Apache License 2.0 | 5 votes |
@Test public void test() { RelationshipMap map = new RelationshipMap(); assertThat(map.containsKey(0L, 1L, RelationshipType.withName("foo")), is(false)); map.put(0L, 1L, RelationshipType.withName("foo"), 1L); assertThat(map.containsKey(0L, 1L, RelationshipType.withName("foo")), is(true)); assertThat(map.get(0L, 1L, RelationshipType.withName("foo")), is(1L)); }
Example #18
Source File: GraphApi.java From SciGraph with Apache License 2.0 | 5 votes |
/*** * @return All the {@link RelationshipType}s in the graph. */ public Collection<RelationshipType> getAllRelationshipTypes() { Set<RelationshipType> relationships = new HashSet<>(); try (Transaction tx = graphDb.beginTx()) { relationships.addAll(newHashSet(graphDb.getAllRelationshipTypes())); tx.success(); } return relationships; }
Example #19
Source File: UniqueRelationshipFactory.java From neo4jena with Apache License 2.0 | 5 votes |
/** * Gets an existing relationship of specified type between subject and object node * or creates one. */ public Relationship getOrCreate(Node subject, RelationshipType type, Node object) { Relationship r = get(subject, type, object); if(r==null) r = create(subject, type, object); return r; }
Example #20
Source File: Diff.java From neo4j-versioner-core with Apache License 2.0 | 5 votes |
@Procedure(value = "graph.versioner.diff.from.previous", mode = DEFAULT) @Description("graph.versioner.diff.from.previous(state) - Get a list of differences that must be applied to the previous statusof the given one in order to become the given state") public Stream<DiffOutput> diffFromPrevious( @Name("state") Node state) { return Optional.ofNullable(state.getSingleRelationship(RelationshipType.withName(Utility.PREVIOUS_TYPE), Direction.OUTGOING)) .map(Relationship::getEndNode) .map(sFrom -> diffBetweenStates(sFrom, state)) .orElse(Stream.empty()); }
Example #21
Source File: EdgeLabelerTest.java From SciGraph with Apache License 2.0 | 5 votes |
@Before public void setup() { n1 = createNode("http://x.org/a"); n2 = createNode("http://x.org/b"); Node rel = createNode(relationshipType1); rel.setProperty(NodeProperties.LABEL, relationshipType1Label); createNode(relationshipType2); n1.createRelationshipTo(n2, RelationshipType.withName(relationshipType1)); n1.createRelationshipTo(n2, RelationshipType.withName(relationshipType2)); n1.createRelationshipTo(n2, RelationshipType.withName(relationshipType3)); edgeLabeler = new EdgeLabeler(graphDb); edgeLabeler.run(); }
Example #22
Source File: GraphTransactionalImpl.java From SciGraph with Apache License 2.0 | 5 votes |
@Override public Optional<Long> getRelationship(long start, long end, RelationshipType type) { if (relationshipMap.containsKey(start, end, type)) { return Optional.of(relationshipMap.get(start, end, type)); } else { return Optional.empty(); } }
Example #23
Source File: NeoUtils.java From metalcon with GNU General Public License v3.0 | 5 votes |
/** * find an outgoing relation from the user passed of the specified type * * @param user * source user node * @param relType * relationship type of the relation being searched * @return node targeted by the relation<br> * null - if there is no relation of the type specified */ public static Node getNextSingleNode(final Node user, RelationshipType relationshipType) { // find an outgoing relation of the type specified Relationship rel = null; try { rel = user.getSingleRelationship(relationshipType, Direction.OUTGOING); } catch (final NonWritableChannelException e) { // TODO: why is this here? Bug for read-only databases in previous // version? } return (rel == null) ? (null) : (rel.getEndNode()); }
Example #24
Source File: GraphUtil.java From SciGraph with Apache License 2.0 | 5 votes |
public static Iterable<Relationship> getRelationships(final Node a, final Node b, RelationshipType type, final boolean directed) { checkNotNull(a); checkNotNull(b); checkNotNull(type); return filter(a.getRelationships(type), new Predicate<Relationship>() { @Override public boolean apply(Relationship relationship) { return directed ? relationship.getEndNode().equals(b) || relationship.getStartNode().equals(b) : relationship.getEndNode().equals(b); } }); }
Example #25
Source File: OwlPostprocessor.java From SciGraph with Apache License 2.0 | 5 votes |
public void processSomeValuesFrom() { logger.info("Processing someValuesFrom classes"); try (Transaction tx = graphDb.beginTx()) { Result results = graphDb.execute("MATCH (n)-[relationship]->(svf:someValuesFrom)-[:property]->(p) " + "RETURN n, relationship, svf, p"); while (results.hasNext()) { Map<String, Object> result = results.next(); Node subject = (Node) result.get("n"); Relationship relationship = (Relationship) result.get("relationship"); Node svf = (Node) result.get("svf"); Node property = (Node) result.get("p"); for (Relationship r : svf.getRelationships(OwlRelationships.FILLER)) { Node object = r.getEndNode(); String relationshipName = GraphUtil.getProperty(property, CommonProperties.IRI, String.class).get(); RelationshipType type = RelationshipType.withName(relationshipName); String propertyUri = GraphUtil.getProperty(property, CommonProperties.IRI, String.class).get(); Relationship inferred = subject.createRelationshipTo(object, type); inferred.setProperty(CommonProperties.IRI, propertyUri); inferred.setProperty(CommonProperties.CONVENIENCE, true); inferred.setProperty(CommonProperties.OWL_TYPE, relationship.getType().name()); } } tx.success(); tx.close(); } }
Example #26
Source File: GraphBatchImpl.java From SciGraph with Apache License 2.0 | 5 votes |
@Override public long createRelationship(long start, long end, RelationshipType type) { synchronized (graphLock) { if (!relationshipMap.containsKey(start, end, type)) { long relationshipId = inserter.createRelationship(start, end, type, Collections.<String, Object>emptyMap()); relationshipMap.put(start, end, type, relationshipId); } } return relationshipMap.get(start, end, type); }
Example #27
Source File: GraphBatchImpl.java From SciGraph with Apache License 2.0 | 5 votes |
@Override public Optional<Long> getRelationship(long start, long end, RelationshipType type) { if (relationshipMap.containsKey(start, end, type)) { return Optional.of(relationshipMap.get(start, end, type)); } else { return Optional.empty(); } }
Example #28
Source File: GraphOwlVisitor.java From SciGraph with Apache License 2.0 | 5 votes |
long getObjectPropertyRelationship( OWLPropertyAssertionAxiom<OWLObjectPropertyExpression, OWLIndividual> axiom) { long subject = getOrCreateNode(getIri(axiom.getSubject())); String property = getIri(axiom.getProperty()); long object = getOrCreateNode(getIri(axiom.getObject())); RelationshipType type = RelationshipType.withName(property.toString()); long relationship = getOrCreateRelationship(subject, object, type); graph.setRelationshipProperty(relationship, CommonProperties.IRI, property.toString()); return relationship; }
Example #29
Source File: GraphOwlVisitorTestBase.java From SciGraph with Apache License 2.0 | 5 votes |
@Test public void nonLiteralAnnotationAssertionAxiom() { Node person = getNode(ROOT + "/Person"); Node fazz = getNode(ROOT + "/Fazz"); Relationship relationship = getOnlyElement(GraphUtil.getRelationships(person, fazz, RelationshipType.withName(ROOT + "/fizz"), true)); assertThat(GraphUtil.getProperty(relationship, CommonProperties.IRI, String.class).get(), is(ROOT + "/fizz")); }
Example #30
Source File: GraphOwlVisitor.java From SciGraph with Apache License 2.0 | 5 votes |
private Collection<Long> getOrCreateRelationshipPairwise(Collection<Long> nodeIds, RelationshipType type) { Collection<Long> relationships = graph.createRelationshipsPairwise(nodeIds, type); for (long relationship : relationships) { graph.addRelationshipProperty(relationship, OwlRelationships.RDFS_IS_DEFINED_BY.name(), definingOntology); } return relationships; }