@Override public void run() { logger.info("Starting anonymous nodes tagger..."); int taggedNodes = 0; Transaction tx = graphDb.beginTx(); ResourceIterable<Node> allNodes = graphDb.getAllNodes(); for (Node n : allNodes) { if (n.hasProperty(anonymousProperty)) { n.addLabel(OwlLabels.OWL_ANONYMOUS); taggedNodes++; } if (taggedNodes % batchCommitSize == 0) { tx.success(); tx.close(); tx = graphDb.beginTx(); } } logger.info(taggedNodes + " nodes tagged."); tx.success(); tx.close(); }
@Override public void run() { logger.info("Starting all nodes labeling..."); int processedNodes = 0; Transaction tx = graphDb.beginTx(); ResourceIterable<Node> allNodes = graphDb.getAllNodes(); for (Node n : allNodes) { n.addLabel(label); if (processedNodes % batchCommitSize == 0) { tx.success(); tx.close(); tx = graphDb.beginTx(); } processedNodes++; } logger.info(processedNodes + " nodes labeled."); tx.success(); tx.close(); }
public static List<String> getAllNodes(final GraphDatabaseService graphDb, final String labelName) { List<String> nodeIds = new ArrayList<>(); Label label = DynamicLabel.label(labelName); ResourceIterable<Node> nodes; try (Transaction tx = graphDb.beginTx()) { nodes = GlobalGraphOperations.at(graphDb).getAllNodesWithLabel(label); for (Node node : nodes) { try { nodeIds.add(node.getProperty("node_id").toString()); } catch (Exception e) { LOGGER.warn("Can't find a given node... skipping"); } } tx.success(); } return nodeIds; }
@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 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 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 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; }
public List<Root> getAll() { List<Root> roots = new ArrayList<>(); ResourceIterable<Node> nodes = GlobalGraphOperations.at(database).getAllNodesWithLabel(NodeLabels.ROOT); for (Node node : nodes) { Root root = session.get(Root.class,node); roots.add(root); } return roots; }
public Iterable<Token> getAll() { List<Token> tokens = new ArrayList<>(); ResourceIterable<Node> nodes = GlobalGraphOperations.at(database).getAllNodesWithLabel(NodeLabels.TOKEN); for (Node node : nodes) { Token root = session.get(Token.class,node); tokens.add(root); } return tokens; }
public static ResourceIterable<Path> create(final List<Node> roots, final TraversalDescription td) { return new ResourceIterable<Path>() { @Override public ResourceIterator<Path> iterator() { return new MultiRootPathIterator(roots, td); } }; }
public static ResourceIterable<Node> create(final List<Node> roots, final TraversalDescription td) { return new ResourceIterable<Node>() { @Override public ResourceIterator<Node> iterator() { return new MultiRootNodeIterator(roots, td); } }; }
@Override public Set<Integer> getCommunitiesConnectedToNodeCommunities(int nodeCommunities) { Set<Integer> communities = new HashSet<Integer>(); try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> nodes = neo4jGraph.findNodesByLabelAndProperty(Neo4jGraphDatabase.NODE_LABEL, NODE_COMMUNITY, nodeCommunities); for (Node n : nodes) { for (Relationship r : n.getRelationships(RelTypes.SIMILAR, Direction.OUTGOING)) { Node neighbour = r.getOtherNode(n); Integer community = (Integer) (neighbour.getProperty(COMMUNITY)); communities.add(community); } } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get communities connected to node communities", e); } } return communities; }
@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; }
@Override public double getEdgesInsideCommunity(int nodeCommunity, int communityNodes) { double edges = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { ResourceIterable<Node> nodes = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, NODE_COMMUNITY, nodeCommunity); ResourceIterable<Node> comNodes = neo4jGraph.findNodesByLabelAndProperty(NODE_LABEL, COMMUNITY, communityNodes); for (Node node : nodes) { Iterable<Relationship> relationships = node.getRelationships(RelTypes.SIMILAR, Direction.OUTGOING); for (Relationship r : relationships) { Node neighbor = r.getOtherNode(node); if (Iterables.contains(comNodes, neighbor)) { edges++; } } } tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get edges inside community", e); } } return edges; }
@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; }
@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 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 void run() { logger.info("Starting edge labeling..."); Map<String, String> map = new HashMap<String, String>(); int processedRels = 0; Transaction tx = graphDb.beginTx(); ResourceIterable<Relationship> rels = graphDb.getAllRelationships(); for (Relationship rel : rels) { if (processedRels % batchCommitSize == 0) { tx.success(); tx.close(); tx = graphDb.beginTx(); } String relName = rel.getType().name(); if (map.containsKey(relName)) { rel.setProperty(edgeProperty, map.get(relName)); } else { String relLabel = relName; String query = "START n = node:node_auto_index(iri='" + relName + "') match (n) return n"; Result result = graphDb.execute(query); if (result.hasNext()) { Node n = (Node) result.next().get("n"); if (n.hasProperty(NodeProperties.LABEL)) { relLabel = GraphUtil.getProperties(n, NodeProperties.LABEL, String.class).iterator().next(); } } rel.setProperty(edgeProperty, relLabel); map.put(relName, relLabel); } processedRels++; } logger.info(processedRels + " relations labeled."); tx.success(); tx.close(); }
@Override public void run() { logger.info("Starting clique merge"); Transaction tx = graphDb.beginTx(); ResourceIterable<Node> allNodes = graphDb.getAllNodes(); int size = Iterators.size(allNodes.iterator()); tx.success(); tx.close(); logger.info(size + " nodes left to process"); tx = graphDb.beginTx(); TraversalDescription traversalDescription = graphDb.traversalDescription().breadthFirst().uniqueness(Uniqueness.NODE_GLOBAL); for (RelationshipType rel : relationships) { traversalDescription = traversalDescription.relationships(rel, Direction.BOTH); } Set<Long> processedNodes = new HashSet<Long>(); for (Node baseNode : allNodes) { size -= 1; if (size % 100000 == 0) { logger.info(size + " nodes left to process"); } if (size % batchCommitSize == 0) { logger.fine("Node batch commit"); tx.success(); tx.close(); tx = graphDb.beginTx(); } logger.fine("Processing Node - " + baseNode.getProperty(NodeProperties.IRI)); if (!processedNodes.contains(baseNode.getId())) { // Keep a list of equivalentNodes List<Node> clique = new ArrayList<Node>(); for (Node node : traversalDescription.traverse(baseNode).nodes()) { logger.fine("-- " + node.getProperty(NodeProperties.IRI)); clique.add(node); processedNodes.add(node.getId()); } logger.fine("clique size: " + clique.size()); if (clique.size() == 1) { Node defactoLeader = clique.get(0); markAsCliqueLeader(defactoLeader); } else { Node leader = electCliqueLeader(clique, prefixLeaderPriority); markAsCliqueLeader(leader); clique.remove(leader); // keep only the peasants moveEdgesToLeader(leader, clique, tx); ensureLabel(leader, clique); } } } tx.success(); tx.close(); }
public static ResourceIterable<Node> create(final Node root, final TraversalDescription td) { return create(Lists.newArrayList(root), td); }
/** * Iterates over all nodes in taxonomic hierarchy, but unsorted withing each branch. */ public static ResourceIterable<Node> allNodes(GraphDatabaseService db, @Nullable Node root, @Nullable Rank lowestRank, boolean inclProParte) { return MultiRootNodeIterator.create(findRoot(db, root), filterRank(inclProParte ? Traversals.SORTED_TREE : Traversals.SORTED_TREE_WITHOUT_PRO_PARTE, lowestRank)); }
/** * Iterates over all paths */ public static ResourceIterable<Path> allPath(GraphDatabaseService db, @Nullable Node root, @Nullable Rank lowestRank, boolean inclProParte) { return MultiRootPathIterator.create(findRoot(db, root), filterRank(inclProParte ? Traversals.SORTED_TREE : Traversals.SORTED_TREE_WITHOUT_PRO_PARTE, lowestRank)); }
/** * Iterates over all paths ending in an accepted node. */ public static ResourceIterable<Path> acceptedPath(GraphDatabaseService db, @Nullable Node root, @Nullable Rank lowestRank) { return MultiRootPathIterator.create(findRoot(db, root), filterRank(Traversals.SORTED_ACCEPTED_TREE, lowestRank)); }
public static Collection<CSEntity> getCSEntities(final GraphDatabaseService graphDB, final String prefixedResourceURI, final AttributePath commonPrefixedAttributePath, final ContentSchema prefixedContentSchema) throws DMPGraphException { final Map<Long, CSEntity> csEntities = new LinkedHashMap<>(); try (final Transaction tx = graphDB.beginTx()) { final Node resourceNode = getResourceNode(graphDB, prefixedResourceURI); // determine CS entity nodes final ResourceIterable<Node> csEntityNodes = graphDB.traversalDescription().breadthFirst() .evaluator(Evaluators.toDepth(commonPrefixedAttributePath.getAttributes().size())) .evaluator(new EntityEvaluator(commonPrefixedAttributePath.getAttributes())) .traverse(resourceNode).nodes(); if (csEntityNodes == null) { tx.success(); return null; } for (final Node node : csEntityNodes) { final CSEntity csEntity = new CSEntity(node.getId()); csEntities.put(csEntity.getNodeId(), csEntity); } final ArrayList<Node> csEntityNodesList = Lists.newArrayList(csEntityNodes); final Node[] csEntityNodesArray = new Node[csEntityNodesList.size()]; csEntityNodesList.toArray(csEntityNodesArray); if (prefixedContentSchema.getKeyAttributePaths() != null) { // determine key entities determineKeyEntities(graphDB, commonPrefixedAttributePath, prefixedContentSchema, csEntities, csEntityNodesArray); } if (prefixedContentSchema.getValueAttributePath() != null) { // determine value entities determineValueEntities(graphDB, commonPrefixedAttributePath, prefixedContentSchema, csEntities, csEntityNodesArray); } tx.success(); } catch (final Exception e) { final String message = "couldn't determine cs entities successfully"; GraphDBUtil.LOG.error(message, e); throw new DMPGraphException(message); } final Collection<CSEntity> csEntitiesCollection = csEntities.values(); // determine cs entity order determineCSEntityOrder(csEntitiesCollection); return csEntitiesCollection; }