Package org.neo4j.graphdb

Examples of org.neo4j.graphdb.Transaction


    }
  }
 
 
  public void calculateCoAuthors(int transactionThreshhold){
    Transaction tx = graphDB.beginTx();
    try {
      int transactionCount = 0;
      for (Node author:graphDB.getAllNodes()){
        if (!author.hasProperty("name"))continue;
        HashMap<Node, Integer> coAuthors = new HashMap<Node, Integer>();
        for (Relationship rel: author.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
          Node paper = rel.getOtherNode(author);
          for (Relationship coAuthorRel: paper.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
            Node coAuthor = coAuthorRel.getOtherNode(paper);
            if (coAuthor.getId()==author.getId())continue;
 
            if (coAuthors .containsKey(coAuthor))
              coAuthors.put(coAuthor, coAuthors.get(coAuthor) + 1);
            else
              coAuthors.put(coAuthor, 1);
 
          }
        }

        for (Node coAuthor: coAuthors.keySet()){
          Relationship coAuthorRel = author.createRelationshipTo(coAuthor, DBRelationshipTypes.CO_AUTHOR_COUNT);
          coAuthorRel.setProperty(DBRelationshipProperties.CO_AUTHOR_COUNT,coAuthors.get(coAuthor));
         
          if (++transactionCount % transactionThreshhold == 0){
            tx.success();
            tx.finish();
            tx = graphDB.beginTx();
            IOHelper.log(transactionCount + " coAuthor relations have been inserted so far");
            System.out.println("current relation's count is " + coAuthors.get(coAuthor))
          }         
        }     
      }
      tx.success();
    }finally{
      tx.finish();
    }
  }
View Full Code Here


    }
  }
 
 
  public void calculateLikesToCiteAuthors(int transactionThreshhold){
    Transaction tx = graphDB.beginTx();
    try {
      int transactionCount = 0;
      for (Node author:graphDB.getAllNodes()){
        if (!author.hasProperty("name"))continue; // only want to work on authors
        HashMap<Node, Integer> citedAuthors = new HashMap<Node, Integer>();
        for (Relationship rel:author.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
          Node paper = rel.getOtherNode(author);
          if (paper.hasProperty("title")){// i found a paper
            //find cited authors
            for (Relationship citedPaperRel: paper.getRelationships(DBRelationshipTypes.CITES,Direction.OUTGOING)){
              Node citedPaper = citedPaperRel.getOtherNode(paper);
              if (citedPaper.hasProperty("title")){
                for (Relationship citedAuthorRel: citedPaper.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
                  Node citedAuthor = citedAuthorRel.getOtherNode(citedPaper);
                  if (citedAuthor.getId()==author.getId())continue;
                  if (citedAuthor.hasProperty("name")){
                    if (citedAuthors.containsKey(citedAuthor))
                      citedAuthors.put(citedAuthor, citedAuthors.get(citedAuthor) + 1);
                    else
                      citedAuthors.put(citedAuthor, 1);
                  }
                }
              }
            }
          }
        }
        for (Node citedAuthor: citedAuthors.keySet()){
          //TODO: exchange relationship types and properties
          Relationship coAuthorRel = author.createRelationshipTo(citedAuthor, DBRelationshipTypes.CITES_AUTHOR);
          coAuthorRel.setProperty(DBRelationshipProperties.CITATION_COUNT,citedAuthors.get(citedAuthor));
         
          if (++transactionCount % transactionThreshhold == 0){
            tx.success();
            tx.finish();
            tx = graphDB.beginTx();
            IOHelper.log(transactionCount + " citedAuthor relations have been inserted so far");
            System.out.println("current relation's count is " + citedAuthors.get(citedAuthor));     
          }         
        }     
      }
      tx.success();
    }finally{
      tx.finish();
    }
  } 
View Full Code Here

    }
  } 
 
 
  public void calculateTopKLikesToCiteAuthors(int transactionThreshhold, int k){
    Transaction tx = graphDB.beginTx();
    try {
      int transactionCount = 0;
      for (Node author:graphDB.getAllNodes()){
        if (!author.hasProperty("name"))continue; // only want to work on authors
        HashMap<Node, Integer> citedAuthors = new HashMap<Node, Integer>();
        for (Relationship rel:author.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
          Node paper = rel.getOtherNode(author);
          if (paper.hasProperty("title")){// i found a paper
            //find cited authors
            for (Relationship citedPaperRel: paper.getRelationships(DBRelationshipTypes.CITES,Direction.OUTGOING)){
              Node citedPaper = citedPaperRel.getOtherNode(paper);
              if (citedPaper.hasProperty("title")){
                for (Relationship citedAuthorRel: citedPaper.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
                  Node citedAuthor = citedAuthorRel.getOtherNode(citedPaper);
                  if (citedAuthor.getId()==author.getId())continue;
                  if (citedAuthor.hasProperty("name")){
                    if (citedAuthors.containsKey(citedAuthor))
                      citedAuthors.put(citedAuthor, citedAuthors.get(citedAuthor) + 1);
                    else
                      citedAuthors.put(citedAuthor, 1);
                  }
                }
              }
            }
          }
        }
       
        Algo<Node, Integer> a = new Algo<Node, Integer>();       
        TreeMap<Integer, Set<Node>> topkCitedAuthors = a.getTopkElements(citedAuthors, k);       
        int topkCnt=0;
       
        for (Integer citedAuthorCount: topkCitedAuthors.descendingKeySet()){
          for (Node citedAuthor: topkCitedAuthors.get(citedAuthorCount)){
            Relationship citedAuthorRel = author.createRelationshipTo(citedAuthor, DBRelationshipTypes.CITES_AUTHOR);
            citedAuthorRel.setProperty(DBRelationshipProperties.CITATION_COUNT,citedAuthorCount);
            if (++topkCnt>=k)break;
          }
          if (topkCnt>=k)break;         
       
        if (++transactionCount % transactionThreshhold == 0){
          tx.success();
          tx.finish();
          tx = graphDB.beginTx();
          IOHelper.log(transactionCount + " nodes have been equipped with citedAuthor relations so far")
        }         
      }
      tx.success();
    }finally{
      tx.finish();
    }
  } 
View Full Code Here

    setUriIndex();

    // build up new indices
    System.out.println("Building up indices");
   
    Transaction transaction = graphDB.beginTx();
    try {
      int nodeCounter = 0;
      for (Node n:graphDB.getAllNodes()) {
        // Resume indexing
        //if (n.hasProperty(DBNodeProperties.URI)) continue;
       
        if (NodeType.isPaperNode(n) && n.hasProperty(DBNodeProperties.PAGE_RANK_VALUE)){
          // add paper node to index
          searchIndex.add(n, "key", (String)n.getProperty("title"));
          searchIndex.add(n, "score", new ValueContext((Double)n.getProperty(DBNodeProperties.PAGE_RANK_VALUE)).indexNumeric());
          searchIndex.add(n, "type", "paper");
          registerURI(n, "paper:" + (String)n.getProperty("c_authors") + ";" + (String)n.getProperty("title") );
        } else if (NodeType.isAuthorNode(n) && n.hasProperty(DBNodeProperties.PAGE_RANK_VALUE)) {
          // add author node to index
          searchIndex.add(n, "key", (String)n.getProperty("name"));
          searchIndex.add(n, "score", new ValueContext((Double)n.getProperty(DBNodeProperties.PAGE_RANK_VALUE)).indexNumeric());
          searchIndex.add(n, "type", "author");
          registerURI(n, "author:" + (String)n.getProperty("name") );
        } else {
          System.out.println("Skipped node: "+ n.toString());
         
        }

        if ( nodeCounter++ % 10000 == 9999){
          // New transactioon and print statistics
          IOHelper.log((nodeCounter-1) + " nodes added to search index.");
          transaction.success();
          transaction.finish();
          transaction = graphDB.beginTx();
          // REMOVE THIS
          // break;
        }
      }
      transaction.success();
     
    } catch (Exception e) {
      System.out.println("ERROR ABORTING");
      System.out.println(e.getMessage());
      transaction.failure();
     
    } finally {
      transaction.finish();
      System.out.println("Finished indexing.");
    }

    return searchIndex;
  }
View Full Code Here

  private void deleteIndex(String idxName) {
  // Delete index if it does exist
    if (graphDB.index().existsForNodes(idxName)) {
      System.out.println("Deleting index:" + idxName);
      Transaction tx = graphDB.beginTx();
      graphDB.index().forNodes(idxName).delete();
      tx.success();
      tx.finish();
    }
  }
View Full Code Here

 
  public boolean startCalculation(int transactionThreshhold){
    calculateBackLinks(transactionThreshhold);

    Transaction tx = graphDB.beginTx();
    int transactionCount = 0;
    try {
      for (Node paper:graphDB.getAllNodes()){
        if (paper.hasProperty(DBNodeProperties.PAPER_TITLE)){
          HashMap<Node, Integer> coCitationCount = new HashMap<Node, Integer>();
          Integer citationCount = (Integer)paper.getProperty(DBNodeProperties.PAPER_CITATION_COUNT);
          for (Relationship rel:paper.getRelationships(DBRelationshipTypes.CITES,Direction.INCOMING)){//get backlinks
            Node parentPaper = rel.getOtherNode(paper);
            for (Relationship coCiteRel:parentPaper.getRelationships(DBRelationshipTypes.CITES,Direction.OUTGOING)){
              Node coCitedPaper = coCiteRel.getOtherNode(parentPaper);
              if (coCitedPaper.getId()==paper.getId())continue;
              if (coCitationCount.containsKey(coCitedPaper)){
                Integer tmp = coCitationCount.get(coCitedPaper);
                coCitationCount.put(coCitedPaper, tmp + 1);
              }
              else{
                coCitationCount.put(coCitedPaper, 1);
              }
            }
          }
           
          for (Node coCitation:coCitationCount.keySet()){
            Relationship coRel = paper.createRelationshipTo(coCitation, DBRelationshipTypes.CO_CITATION_SCORE);
            Integer citationCount1 = (Integer)coCitation.getProperty(DBNodeProperties.PAPER_CITATION_COUNT);
            Integer cut = coCitationCount.get(coCitation);
            Double tanimotoScore = (cut*1.0) / (citationCount + citationCount1 - cut);
            coRel.setProperty(DBRelationshipProperties.CO_CITATION_SCORE, tanimotoScore);
            if (++transactionCount % transactionThreshhold == 0){
              tx.success();
              tx.finish();
              tx = graphDB.beginTx();
              IOHelper.log(transactionCount + " weighted coCitation relations have been inserted so far");
              System.out.println("current relations similartiy score is " +tanimotoScore);     
            }
          }
        }
      }
      tx.success();
    }finally{
      tx.finish();
    }
    return true;
  }
View Full Code Here

 
 
  public boolean startTopKCalculation(int transactionThreshhold,int k){
    calculateBackLinks(transactionThreshhold);

    Transaction tx = graphDB.beginTx();
    int transactionCount = 0;
    try {
      for (Node paper:graphDB.getAllNodes()){
        if (paper.hasProperty(DBNodeProperties.PAPER_TITLE)){
          HashMap<Node, Integer> coCitationCount = new HashMap<Node, Integer>();
          Integer citationCount = (Integer)paper.getProperty(DBNodeProperties.PAPER_CITATION_COUNT);
          for (Relationship rel:paper.getRelationships(DBRelationshipTypes.CITES,Direction.INCOMING)){//get backlinks
            Node parentPaper = rel.getOtherNode(paper);
            for (Relationship coCiteRel:parentPaper.getRelationships(DBRelationshipTypes.CITES,Direction.OUTGOING)){
              Node coCitedPaper = coCiteRel.getOtherNode(parentPaper);
              if (coCitedPaper.getId()==paper.getId())continue;
              if (coCitationCount.containsKey(coCitedPaper)){
                Integer tmp = coCitationCount.get(coCitedPaper);
                coCitationCount.put(coCitedPaper, tmp + 1);
              }
              else{
                coCitationCount.put(coCitedPaper, 1);
              }
            }
          }
   
         
          Algo<Node, Integer> a = new Algo<Node, Integer>();
         
          TreeMap<Integer, Set<Node>> topkCoCitation = a.getTopkElements(coCitationCount, k);       

          int topkCnt=0;

          for (Integer cut: topkCoCitation.descendingKeySet()){
            for (Node coCitation: topkCoCitation.get(cut)){
              Relationship coRel = paper.createRelationshipTo(coCitation, DBRelationshipTypes.CO_CITATION_SCORE);
              Integer citationCount1 = (Integer)coCitation.getProperty(DBNodeProperties.PAPER_CITATION_COUNT);
              Double tanimotoScore = (cut*1.0) / (citationCount + citationCount1 - cut);
              coRel.setProperty(DBRelationshipProperties.CO_CITATION_SCORE, tanimotoScore);
            }
          }
          if (++transactionCount % transactionThreshhold == 0){
            tx.success();
            tx.finish();
            tx = graphDB.beginTx();
            IOHelper.log(transactionCount + " papers have been equiped with coCitation relations so far");
          }

        }
      }
      tx.success();
    }finally{
      tx.finish();
    }
    return true;
  }
View Full Code Here

    }
    return true;
  }
 
  private boolean calculateBackLinks(int transactionThreshhold){
    Transaction tx = graphDB.beginTx();
    int transactionCount = 0;
    try {
      for (Node n:graphDB.getAllNodes()){
        if (n.hasProperty(DBNodeProperties.PAPER_TITLE)){
          transactionCount++;
          int citationCount = 0;
          for (Relationship rel:n.getRelationships(DBRelationshipTypes.CITES,Direction.INCOMING)){
            citationCount++;
          }
          n.setProperty(DBNodeProperties.PAPER_CITATION_COUNT, citationCount);
          if (++transactionCount % transactionThreshhold == 0){
            IOHelper.log(transactionCount + " nodes have citation Count Property");
            System.out.println("current paper has " +citationCount+ " citations");
            tx.success();
            tx.finish();
            tx = graphDB.beginTx();
          }

        }
      }
      tx.success();
    }finally{
      tx.finish();
    }
    return true;
  }
View Full Code Here

    }
    return true;
  }

  public boolean similarTopKAuthors(int transactionThreshhold, int k) {
    Transaction tx = graphDB.beginTx();
    try {
      int transactionCount = 0;
      for (Node author:graphDB.getAllNodes()){
        if (!author.hasProperty("name"))continue;
        HashMap<Node, Double> simAuthors = new HashMap<Node, Double>();

        for (Relationship rel:author.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
          Node paper = rel.getOtherNode(author);
          if (paper.hasProperty("title")){// i found a paper
            for (Relationship simPaperRel:paper.getRelationships(DBRelationshipTypes.CO_CITATION_SCORE, Direction.OUTGOING)){
              Node simPaper = simPaperRel.getEndNode();
              if (simPaperRel.hasProperty(DBRelationshipProperties.CO_CITATION_SCORE)){
                Double score = (Double)simPaperRel.getProperty(DBRelationshipProperties.CO_CITATION_SCORE);
                for (Relationship authorRel:simPaper.getRelationships(DBRelationshipTypes.WRITTEN_BY)){
                  Node simAuthor = authorRel.getOtherNode(simPaper);
                  if (simAuthor.getId()==author.getId())continue;
                  if (simAuthors.containsKey(simAuthor))
                    simAuthors.put(simAuthor, simAuthors.get(simAuthor) + score);
                  else
                    simAuthors.put(simAuthor, score);
                }
              }
            }
          }
        }
       
        //get top k as usual and enter to data base:       
        Algo<Node, Double> a = new Algo<Node, Double>();
        TreeMap<Double, Set<Node>> topkSimAuthors = a.getTopkElements(simAuthors, k);       
        int topkCnt=0;
        for (Double score: topkSimAuthors.descendingKeySet()){
          for (Node simAuthor: topkSimAuthors.get(score)){
            Relationship simAuthorRel = author.createRelationshipTo(simAuthor, DBRelationshipTypes.SIM_AUTHOR);
            simAuthorRel.setProperty(DBRelationshipProperties.SIM_AUTHOR_SCORE, score);
          }
        }
        if (++transactionCount % transactionThreshhold == 0){
          tx.success();
          tx.finish();
          tx = graphDB.beginTx();
          IOHelper.log(transactionCount + " papers have been equiped with similarAuthor relations so far");
        }
       
       
      }
      tx.success();
    }finally{
      tx.finish();
    }
    return true;
  }
View Full Code Here

      System.out.println("get author with uri: " + uri);
    } catch (Exception e) {
      System.out.println("URI INDEX ERROR. uri " + uri + " has more than one associated node.");
      return null;
    }
    Transaction tx = ContextHelper.getGraphDatabase(servletContext).beginTx();
    try {
      for (String key:action.getMetaData().keySet()){
        if (key.equals("uri"))continue;
        author.setProperty("metadata:"+key, action.getMetaData().get(key));
      }
      tx.success();
    }finally{
      tx.finish();
    }
   
    return new SetAuthorMetaDataActionHandlerResult(true);
  }
View Full Code Here

TOP

Related Classes of org.neo4j.graphdb.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.