org.neo4j.graphdb.ResourceIterable Java Examples

The following examples show how to use org.neo4j.graphdb.ResourceIterable. 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: AnonymousNodeTagger.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #2
Source File: AllNodesLabeler.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #4
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example #5
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #8
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #9
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #11
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #12
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #13
Source File: EdgeLabeler.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example #14
Source File: Clique.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@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();
}