public static int getDocumentSizeForFeature(GraphDatabaseService db, Long id) { int documentSize; String cacheKey = "DOCUMENT_SIZE_FEATURE_" + id; if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) { Node startNode = db.getNodeById(id); Iterator<Node> classes = db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.OUTGOING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(startNode) .nodes() .iterator(); documentSize = IteratorUtil.count(classes); vectorSpaceModelCache.put(cacheKey, documentSize); } else { documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey); } return documentSize; }
@Test public void shouldCorrectlyUpdateIndexesWhenChangingLabelsAndPropertyAtTheSameTime() throws Exception { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService(); Node myNode = createNode(beansAPI, map("name", "Hawking"), LABEL1, LABEL2); createIndex(beansAPI, LABEL1, "name"); createIndex(beansAPI, LABEL2, "name"); createIndex(beansAPI, LABEL3, "name"); // When try (Transaction tx = beansAPI.beginTx()) { myNode.removeLabel(LABEL1); myNode.addLabel(LABEL3); myNode.setProperty("name", "Einstein"); tx.success(); } // Then assertThat(myNode, inTx(beansAPI, hasProperty("name").withValue("Einstein"))); assertThat(labels(myNode), containsOnly(LABEL2, LABEL3)); assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), isEmpty()); assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Einstein", beansAPI), isEmpty()); assertThat(findNodesByLabelAndProperty(LABEL2, "name", "Hawking", beansAPI), isEmpty()); assertThat( findNodesByLabelAndProperty(LABEL2, "name", "Einstein", beansAPI), containsOnly(myNode)); assertThat(findNodesByLabelAndProperty(LABEL3, "name", "Hawking", beansAPI), isEmpty()); assertThat( findNodesByLabelAndProperty(LABEL3, "name", "Einstein", beansAPI), containsOnly(myNode)); }
public static Map<Long, Integer> getTermFrequencyMapForDocument( GraphDatabaseService db, Long classId) { Map<Long, Integer> termDocumentMatrix; String cacheKey = "TERM_DOCUMENT_FREQUENCY_" + classId; if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) { Node classNode = db.getNodeById(classId); termDocumentMatrix = new HashMap<>(); IteratorUtil.asCollection( db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.INCOMING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(classNode)) .stream() .forEach( p -> termDocumentMatrix.put( p.endNode().getId(), (Integer) p.lastRelationship().getProperty("matches"))); vectorSpaceModelCache.put(cacheKey, termDocumentMatrix); } else { termDocumentMatrix = (Map<Long, Integer>) vectorSpaceModelCache.getIfPresent(cacheKey); } return termDocumentMatrix; }
private static List<Double> getWeightVectorForClass( Map<String, List<LinkedHashMap<String, Object>>> documents, String key, List<Integer> featureIndexList, GraphDatabaseService db) { List<Double> weightVector; Transaction tx = db.beginTx(); // Get class id Long classId = db.findNodesByLabelAndProperty(DynamicLabel.label("Class"), "name", key) .iterator() .next() .getId(); // Get weight vector for class List<Long> longs = documents .get(key) .stream() .map(a -> ((Integer) a.get("feature")).longValue()) .collect(Collectors.toList()); weightVector = featureIndexList .stream() .map(i -> longs.contains(i.longValue()) ? tfidf(db, i.longValue(), classId) : 0.0) .collect(Collectors.toList()); tx.success(); tx.close(); return weightVector; }
@Test public void clearGraphWithoutRuntimeShouldDeleteBasedOnRelInclusionPolicy() { try (Transaction tx = database.beginTx()) { String cypher = "CREATE " + "(purple:Purple {name:'Purple'})<-[:REL]-(red1:Red {name:'Red'})-[:REL]->(black1:Black {name:'Black'})-[:REL]->(green:Green {name:'Green'})," + "(red2:Red {name:'Red'})-[:REL]->(black2:Black {name:'Black'}), (blue1:Blue)-[:REL2]->(blue2:Blue)"; populateDatabase(cypher); tx.success(); } InclusionPolicies inclusionPolicies = InclusionPolicies.all().with(new BlueNodeInclusionPolicy()).with(new Rel2InclusionPolicy()); try (Transaction tx = database.beginTx()) { clearGraph(database, inclusionPolicies); tx.success(); } try (Transaction tx = database.beginTx()) { assertEquals(6, count(at(database).getAllNodes())); assertEquals(4, count(at(database).getAllRelationships())); tx.success(); } }
public void rn() throws IOException { List<RoadLink> links = loadToDatabase("/Users/duduba/gj.mif", "/Users/duduba/gj.mid", false); GraphDatabaseService graphDb = neo.getDb(); Index<Node> nodeIndex = neo.getNodeIndex(); for (RoadLink link : links) { Transaction tx = graphDb.beginTx(); try { Node fromNode = nodeIndex.get("id", link.fromNode).getSingle(); if (fromNode == null) { fromNode = graphDb.createNode(); fromNode.setProperty("id", link.fromNode); nodeIndex.add(fromNode, "id", link.fromNode); } Node toNode = nodeIndex.get("id", link.toNode).getSingle(); if (toNode == null) { toNode = graphDb.createNode(); toNode.setProperty("id", link.toNode); nodeIndex.add(toNode, "id", link.toNode); } Relationship r = fromNode.createRelationshipTo(toNode, Neo.RelTypes.TO); r.setProperty("no", link.no); r.setProperty("name", link.name); tx.success(); } finally { tx.finish(); } } logger.debug("haha, it's ok!"); }
@Test public void listing3_2_create_single_user() { usersAndMovies.createSingleUser(); try (Transaction tx = graphDb.beginTx()) { assertNotNull(graphDb.getNodeById(0)); tx.success(); } }
@Test public void listing3_7_create_movies() { try (Transaction tx = graphDb.beginTx()) { assertNotNull(graphDb.getNodeById(3)); assertNotNull(graphDb.getNodeById(4)); assertNotNull(graphDb.getNodeById(5)); tx.success(); } }
@Test public void listing3_3_create_multiple_users() { usersAndMovies.createMultipleUsersInSingleTransaction(); try (Transaction tx = graphDb.beginTx()) { assertNotNull(graphDb.getNodeById(1)); assertNotNull(graphDb.getNodeById(2)); tx.success(); } }
private Node createNode( GraphDatabaseService beansAPI, Map<String, Object> properties, Label... labels) { try (Transaction tx = beansAPI.beginTx()) { Node node = beansAPI.createNode(labels); for (Map.Entry<String, Object> property : properties.entrySet()) node.setProperty(property.getKey(), property.getValue()); tx.success(); return node; } }
private void assertCanCreateAndFind( GraphDatabaseService db, Label label, String propertyKey, Object value) { Node created = createNode(db, map(propertyKey, value), label); try (Transaction tx = db.beginTx()) { Node found = db.findNode(label, propertyKey, value); assertThat(found, equalTo(created)); found.delete(); tx.success(); } }
@Test public void equalArraysWithDifferentTypeShouldBeEqual() { try (Transaction tx = database.beginTx()) { database.createNode().setProperty("number", new int[] {123, 124}); tx.success(); } String cypher = "CREATE (n {number:[123,124]})"; assertSameGraph(database, cypher); }
@Test public void listing3_11_node_labels_and_property() { try { usersAndMovies.addLabelToMovies(); } catch (Exception e) { // ignore if index already exist } try (Transaction tx = graphDb.beginTx()) { ResourceIterable<Node> movies = graphDb.findNodesByLabelAndProperty(DynamicLabel.label("MOVIES"), "name", "Fargo"); assertEquals(1, IteratorUtil.count(movies)); tx.success(); } }
private static List<LinkedHashMap<String, Object>> getFeaturesForClass( GraphDatabaseService db, Node classNode) { List<LinkedHashMap<String, Object>> patternIds = new ArrayList<>(); for (Path p : db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.INCOMING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(classNode)) { LinkedHashMap<String, Object> featureMap = new LinkedHashMap<>(); if (p.relationships().iterator().hasNext()) { featureMap.put("frequency", p.relationships().iterator().next().getProperty("matches")); } else { featureMap.put("frequency", 0); } featureMap.put("feature", ((Long) p.endNode().getId()).intValue()); patternIds.add(featureMap); } return patternIds; }
private void removeFromIndexes(Relationship relationship) { final IndexManager indexManager = delegate.index(); for (String indexName : indexManager.relationshipIndexNames()) { RelationshipIndex relationshipIndex = indexManager.forRelationships(indexName); if (relationshipIndex.isWriteable()) relationshipIndex.remove(relationship); } }
private void removeFromIndexes(Node node) { final IndexManager indexManager = delegate.index(); for (String indexName : indexManager.nodeIndexNames()) { Index<Node> nodeIndex = indexManager.forNodes(indexName); if (nodeIndex.isWriteable()) nodeIndex.remove(node); } }
private CypherResult doExecuteQuery(String query, boolean canProfile) { long time = System.currentTimeMillis(); Transaction tx = gdb.beginTx(); javax.transaction.Transaction resumeTx; try { resumeTx = suspendTx(query); ExecutionResult result = canProfile ? executionEngine.profile(query) : executionEngine.execute(query); final Collection<Map<String, Object>> data = IteratorUtil.asCollection(result); time = System.currentTimeMillis() - time; resumeTransaction(resumeTx); CypherResult cypherResult = new CypherResult( result.columns(), data, result.getQueryStatistics(), time, canProfile ? result.executionPlanDescription() : null, prettify(query)); tx.success(); return cypherResult; } finally { tx.close(); } }
@SuppressWarnings("unchecked") @Override public <T extends PropertyContainer> Index<T> getIndex(String indexName) { IndexManager indexManager = delegate.index(); if (indexManager.existsForNodes(indexName)) return (Index<T>) indexManager.forNodes(indexName); if (indexManager.existsForRelationships(indexName)) return (Index<T>) indexManager.forRelationships(indexName); throw new NoSuchIndexException(indexName); }
@Test public void deletedNewLabelShouldNotInfluenceEquality() { // bug test try (Transaction tx = database.beginTx()) { database.createNode(DynamicLabel.label("Accident")); database.createNode(DynamicLabel.label("RealLabel")); tx.success(); } try (Transaction tx = database.beginTx()) { database.getNodeById(0).delete(); tx.success(); } String cypher = "CREATE (n:RealLabel)"; assertSameGraph(database, cypher); }
@Test public void deletedNewPropsShouldNotInfluenceEquality() { // bug test try (Transaction tx = database.beginTx()) { database.createNode().setProperty("accident", "dummy"); database.createNode(); tx.success(); } try (Transaction tx = database.beginTx()) { database.getNodeById(0).delete(); tx.success(); } String cypher = "CREATE (n)"; assertSameGraph(database, cypher); }
@Test public void listing3_4_create_realationships_between_users() { usersAndMovies.createSimpleRelationshipsBetweenUsers(); try (Transaction tx = graphDb.beginTx()) { assertTrue(usersAndMovies.user1.hasRelationship(MyRelationshipTypes.IS_FRIEND_OF)); tx.success(); } }
@Test public void listing3_5_add_properties_to_user_nodes() { usersAndMovies.addPropertiesToUserNodes(); try (Transaction tx = graphDb.beginTx()) { assertTrue(usersAndMovies.user1.hasProperty("name")); assertEquals("John Johnson", usersAndMovies.user1.getProperty("name")); tx.success(); } }
@Test public void deletedRelationshipWithNewTypeShouldNotInfluenceEquality() { // bug test try (Transaction tx = database.beginTx()) { Node node1 = database.createNode(); Node node2 = database.createNode(); node1.createRelationshipTo(node2, DynamicRelationshipType.withName("ACCIDENT")); tx.success(); } try (Transaction tx = database.beginTx()) { GlobalGraphOperations.at(database).getAllRelationships().iterator().next().delete(); tx.success(); } String cypher = "CREATE (n), (m)"; assertSameGraph(database, cypher); }
@Override protected ConstraintDefinition obtainEntityInTransaction( GraphDatabaseService graphDatabaseService) { return graphDatabaseService .schema() .constraintFor(DynamicLabel.label("Label")) .assertPropertyIsUnique("property") .create(); }
@Test public void listing3_8_add_type_properties_to_all_nodes() { usersAndMovies.addTypePropertiesToNodes(); try (Transaction tx = graphDb.beginTx()) { assertEquals("User", usersAndMovies.user1.getProperty("type")); assertEquals("Movie", usersAndMovies.movie1.getProperty("type")); tx.success(); } }
@Test public void listing3_6_add_more_properties_to_user_nodes() { usersAndMovies.addMorePropertiesToUsers(); try (Transaction tx = graphDb.beginTx()) { assertTrue(usersAndMovies.user1.hasProperty("year_of_birth")); assertEquals(1982, usersAndMovies.user1.getProperty("year_of_birth")); tx.success(); } }
@Test public void listing3_10_node_labels() { usersAndMovies.addLabelToMovies(); try (Transaction tx = graphDb.beginTx()) { ResourceIterable<Node> movies = GlobalGraphOperations.at(graphDb).getAllNodesWithLabel(DynamicLabel.label("MOVIES")); assertEquals(3, IteratorUtil.count(movies)); tx.success(); } }
// TODO handle existing indexes @SuppressWarnings("unchecked") @Override public <T extends PropertyContainer> Index<T> createIndex(Class<T> type, String indexName, IndexType indexType) { Transaction tx = delegate.beginTx(); try { IndexManager indexManager = delegate.index(); if (isNode(type)) { if (indexManager.existsForNodes(indexName)) return (Index<T>) checkAndGetExistingIndex(indexName, indexType, indexManager.forNodes(indexName)); Index<Node> index = indexManager.forNodes(indexName, indexConfigFor(indexType)); return (Index<T>) index; } else { if (indexManager.existsForRelationships(indexName)) return (Index<T>) checkAndGetExistingIndex(indexName, indexType, indexManager.forRelationships(indexName)); return (Index<T>) indexManager.forRelationships(indexName, indexConfigFor(indexType)); } } finally { tx.success();tx.close(); } }
@Test public void listing3_9_add_properties_to_relationships() { usersAndMovies.addPropertiesToRelationships(); try (Transaction tx = graphDb.beginTx()) { Relationship hasSeen = usersAndMovies.user1.getSingleRelationship( MyRelationshipTypes.HAS_SEEN, Direction.OUTGOING); assertEquals(5, hasSeen.getProperty("stars")); tx.success(); } }
@Test public void createdNodeShouldShowUpInIndexQuery() throws Exception { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService(); createIndex(beansAPI, LABEL1, "name"); createNode(beansAPI, map("name", "Mattias"), LABEL1); // WHEN Transaction tx = beansAPI.beginTx(); long sizeBeforeDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias")); createNode(beansAPI, map("name", "Mattias"), LABEL1); long sizeAfterDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias")); tx.close(); // THEN assertThat(sizeBeforeDelete, equalTo(1l)); assertThat(sizeAfterDelete, equalTo(2l)); }