@Override public Node getVertex(Integer i) { // note, this probably should be run in the context of an active transaction. return neo4jGraph.findNodesByLabelAndProperty(Neo4jGraphDatabase.NODE_LABEL, NODE_ID, i.toString()).iterator() .next(); }
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; }
@Override public int getCommunitySize(int community) { Set<Integer> nodeCommunities = new HashSet<Integer>(); try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> nodes = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, COMMUNITY, community); for (Node n : nodes) { Integer nodeCommunity = (Integer) (n.getProperty(COMMUNITY)); nodeCommunities.add(nodeCommunity); } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get community size", e); } } return nodeCommunities.size(); }
@Override public Map<Integer, List<Integer>> mapCommunities(int numberOfCommunities) { Map<Integer, List<Integer>> communities = new HashMap<Integer, List<Integer>>(); try (final Transaction tx = beginUnforcedTransaction()) { try { for (int i = 0; i < numberOfCommunities; i++) { ResourceIterable<Node> nodesIter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, COMMUNITY, i); List<Integer> nodes = new ArrayList<Integer>(); for (Node n : nodesIter) { String nodeIdString = (String) (n.getProperty(NODE_ID)); nodes.add(Integer.valueOf(nodeIdString)); } communities.put(i, nodes); } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to map communities", e); } } return communities; }
@Override public Set<Integer> getNodesFromCommunity(int community) { Set<Integer> nodes = new HashSet<Integer>(); try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> iter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, COMMUNITY, community); for (Node n : iter) { String nodeIdString = (String) (n.getProperty(NODE_ID)); nodes.add(Integer.valueOf(nodeIdString)); } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get nodes from community", e); } } return nodes; }
@Override public Set<Integer> getNodesFromNodeCommunity(int nodeCommunity) { Set<Integer> nodes = new HashSet<Integer>(); try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> iter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_COMMUNITY, nodeCommunity); for (Node n : iter) { String nodeIdString = (String) (n.getProperty(NODE_ID)); nodes.add(Integer.valueOf(nodeIdString)); } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get nodes from node community", e); } } return nodes; }
/** * Get or create the root of the change feed. * * @return root. */ private Node getOrCreateRoot() { Node root; try (Transaction tx = database.beginTx()) { root = getSingleOrNull(database.findNodesByLabelAndProperty(Labels._GA_ChangeFeed, MODULE_ID, moduleId)); if (root == null) { LOG.info("Creating the ChangeFeed Root for Module ID " + moduleId); root = database.createNode(Labels._GA_ChangeFeed); root.setProperty(MODULE_ID, moduleId); } tx.success(); } return root; }
private Node getTerm(Term term) { Node node = null; ResourceIterator<Node> it = this.graph.findNodesByLabelAndProperty( NodeType.TERM, "value", term.getIdentifier()).iterator(); if (it.hasNext()) { node = it.next(); } it.close(); return node; }
private Node getPredicate(Predicate predicate) { Node node = null; ResourceIterator<Node> it = this.graph.findNodesByLabelAndProperty( NodeType.PREDICATE, "value", predicate.getIdentifier()).iterator(); while (node == null && it.hasNext()) { Node tmp = it.next(); if (tmp.getProperty("arity").equals(predicate.getArity())) { node = tmp; } } it.close(); return node; }
private Node getNode(String keyValue, GraphDatabaseService db) { Node nodeStart = null; try(Transaction tx = db.beginTx()) { ResourceIterator<Node> results = db.findNodesByLabelAndProperty(DynamicLabel.label(label), propertyKey, keyValue).iterator(); if (results.hasNext()) { nodeStart = results.next(); patternCache.put(keyValue, nodeStart.getId()); inversePatternCache.put(nodeStart.getId(), keyValue); } tx.success(); tx.close(); } return nodeStart; }
@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; }
@Override public double getNodeCommunityWeight(int nodeCommunity) { double nodeCommunityWeight = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> iter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_COMMUNITY, nodeCommunity); if (Iterables.size(iter) > 1) { for (Node n : iter) { nodeCommunityWeight += getNodeOutDegree(n); } } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get node community weight", e); } } return nodeCommunityWeight; }
@Override public double getCommunityWeight(int community) { double communityWeight = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> iter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, COMMUNITY, community); if (Iterables.size(iter) > 1) { for (Node n : iter) { communityWeight += getNodeOutDegree(n); } } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get community weight", e); } } return communityWeight; }
private Long getNodeId(String keyValue, GraphDatabaseService db) { Long nodeId = null; try(Transaction tx = db.beginTx()) { ResourceIterator<Node> results = db.findNodesByLabelAndProperty(DynamicLabel.label(label), propertyKey, keyValue).iterator(); if (results.hasNext()) { Node nodeStart = results.next(); patternCache.put(keyValue, nodeStart.getId()); inversePatternCache.put(nodeStart.getId(), keyValue); nodeId = nodeStart.getId(); } tx.success(); tx.close(); } return nodeId; }
@Override public int getCommunityFromNode(int nodeId) { Integer community = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { Node node = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_ID, String.valueOf(nodeId)).iterator() .next(); community = (Integer) (node.getProperty(COMMUNITY)); tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get community from node", e); } } return community; }
@Override public double getNodeWeight(int nodeId) { double weight = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { Node n = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_ID, String.valueOf(nodeId)).iterator() .next(); weight = getNodeOutDegree(n); tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get node weight", e); } } return weight; }
@Override public void moveNode(int nodeCommunity, int toCommunity) { try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> fromIter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_COMMUNITY, nodeCommunity); for (Node node : fromIter) { node.setProperty(COMMUNITY, toCommunity); } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to move node", e); } } }
@Override public int getCommunity(int nodeCommunity) { Integer community = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { Node node = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_COMMUNITY, nodeCommunity).iterator() .next(); community = (Integer) (node.getProperty(COMMUNITY)); tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get community", e); } } return community; }
@Override public boolean nodeExists(int nodeId) { try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> nodesIter = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_ID, nodeId); if (nodesIter.iterator().hasNext()) { tx.success(); return true; } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to determine if node exists", e); } } return false; }
ResourceIterator<Node> results = db.findNodesByLabelAndProperty(dynamicLabel, propertyKey, keyValue).iterator(); if (results.hasNext()) { nodeStart = results.next();