diff --git a/WikiRating/WebContent/WEB-INF/classes/log4j.properties b/WikiRating/WebContent/WEB-INF/classes/log4j.properties index b288681..0ba0cde 100644 --- a/WikiRating/WebContent/WEB-INF/classes/log4j.properties +++ b/WikiRating/WebContent/WEB-INF/classes/log4j.properties @@ -1,9 +1,16 @@ # Root logger option log4j.rootLogger=DEBUG, stdout, file # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n +# Redirect log messages to a log file, support file rolling. +log4j.appender.file=org.apache.log4j.RollingFileAppender +log4j.appender.file.File=/home/aronzx/Desktop/loggings +log4j.appender.file.MaxFileSize=5MB +log4j.appender.file.MaxBackupIndex=10 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/WikiRating/src/main/java/computations/BadgeGenerator.java b/WikiRating/src/main/java/computations/BadgeGenerator.java index 057df18..081b081 100644 --- a/WikiRating/src/main/java/computations/BadgeGenerator.java +++ b/WikiRating/src/main/java/computations/BadgeGenerator.java @@ -1,187 +1,188 @@ package main.java.computations; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; -import main.java.utilities.Logs; +import main.java.utilities.Loggings; import main.java.utilities.PropertiesAccess; /** * This class will calculate the Badges that will be assigned * to the Pages based on the analysis of Page Rating distribution */ public class BadgeGenerator { - Class className=BadgeGenerator.class; + static Class className=BadgeGenerator.class; //These variables will store the computed cutoffs for the various badges static double platinumBadgeRatingCutoff; static double goldBadgeRatingCutoff; static double silverBadgeRatingCutoff; static double bronzeBadgeRatingCutoff; static double stoneBadgeRatingCutoff; /** * This ENUM has the percentile ranges for various badges */ public enum Badges { PLATINUM(1), GOLD(2), SILVER(3), BRONZE(4), STONE(5), PLATINUM_BADGE_START_PERCENTILE(80), GOLD_BADGE_START_PERCENTILE(60), SILVER_BADGE_START_PERCENTILE(40), BRONZE_BADGE_START_PERCENTILE(20), STONE_BADGE_START_PERCENTILE(0); private int value; Badges(int value) { this.value = value; } public int getValue() { return value; } } /** * This class will store our PageObjects to insert into ArrayList for * percentile calculations */ public class PageRatingData{ int pid; double pageRating; String pageName; public PageRatingData(int pid,double pageRating,String pageName) { this.pid=pid; this.pageRating=pageRating; this.pageName=pageName; } } /** * This is the custom comparator to sort the pageList in the ascending * order of PageRatings */ class PageRatingComparator implements Comparator{ @Override public int compare(PageRatingData pageRating1, PageRatingData pageRating2) { if(pageRating1.pageRating>pageRating2.pageRating) return 1; else return -1; } } /** * This method will assign badges based on the percentile */ public void generateBadges(){ ArrayList pageList=new ArrayList(); OrientGraph graph = Connections.getInstance().getDbGraph(); Vertex currentPageNode=null; int badgeNumber=4; int currentPageID=0,noOfPages=0; double currentPageRating=0,maxPageRating=0; String currentPageName=""; for(Vertex pageNode:graph.getVertices("@class","Page")){ currentPageID=pageNode.getProperty("pid"); currentPageRating=pageNode.getProperty("PageRating"); currentPageName=pageNode.getProperty("title"); pageList.add(new PageRatingData(currentPageID, currentPageRating,currentPageName)); } Collections.sort(pageList,new PageRatingComparator()); calculateBadgeCutoff(pageList); noOfPages=pageList.size(); int noOfPagesCounter=0; for(PageRatingData currentPage:pageList){ badgeNumber=getBadgeNumber(currentPage.pageRating); - Logs.getLogs(className).info(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeNumber); - System.out.println(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeNumber); + + Loggings.getLogs(className).info(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeNumber); currentPageNode=graph.getVertices("pid",currentPage.pid).iterator().next(); currentPageNode.setProperty("badgeNumber",badgeNumber); graph.commit(); noOfPagesCounter++; if(noOfPagesCounter==noOfPages) maxPageRating=currentPage.pageRating; } //Adding the max value to the Preferences for later access PropertiesAccess.putParameter("maxRating", maxPageRating); graph.shutdown(); } /** * This method will calculate the cutoff for the various badges * @param pageList The ArrayList containing Page Objects */ public static void calculateBadgeCutoff(ArrayList pageList){ int noOfPages=pageList.size(); int platinumPageIndex; int goldPageIndex; int silverPageIndex; int bronzePageIndex; int stonePageIndex; //Storing index where the cutoff of badges start to get the respective cutoffs platinumPageIndex=(int)(noOfPages*(Badges.PLATINUM_BADGE_START_PERCENTILE.value/100.00)); goldPageIndex=(int)(noOfPages*(Badges.GOLD_BADGE_START_PERCENTILE.value/100.00)); silverPageIndex=(int)(noOfPages*(Badges.SILVER_BADGE_START_PERCENTILE.value/100.00)); bronzePageIndex=(int)(noOfPages*(Badges.BRONZE_BADGE_START_PERCENTILE.value/100.00)); stonePageIndex=(int)(noOfPages*(Badges.STONE_BADGE_START_PERCENTILE.value/100.00)); //Storing cutoffs platinumBadgeRatingCutoff=pageList.get(platinumPageIndex).pageRating; goldBadgeRatingCutoff=pageList.get(goldPageIndex).pageRating; silverBadgeRatingCutoff=pageList.get(silverPageIndex).pageRating; bronzeBadgeRatingCutoff=pageList.get(bronzePageIndex).pageRating; stoneBadgeRatingCutoff=pageList.get(stonePageIndex).pageRating; - System.out.println("Index "+platinumPageIndex+"marks platinum cutoff -------"+platinumBadgeRatingCutoff); - System.out.println("Index "+goldPageIndex+"marks gold cutoff------"+goldBadgeRatingCutoff); - System.out.println("Index "+silverPageIndex+"marks silver cutoff------"+silverBadgeRatingCutoff); - System.out.println("Index "+bronzePageIndex+"marks bronze cutoff------"+bronzeBadgeRatingCutoff); - System.out.println("Index "+stonePageIndex+"marks stone cutoff------"+stoneBadgeRatingCutoff); - + + Loggings.getLogs(className).info("Index "+platinumPageIndex+"marks platinum cutoff -------"+platinumBadgeRatingCutoff); + Loggings.getLogs(className).info("Index "+goldPageIndex+"marks gold cutoff------"+goldBadgeRatingCutoff); + Loggings.getLogs(className).info("Index "+silverPageIndex+"marks silver cutoff------"+silverBadgeRatingCutoff); + Loggings.getLogs(className).info("Index "+bronzePageIndex+"marks bronze cutoff------"+bronzeBadgeRatingCutoff); + Loggings.getLogs(className).info("Index "+stonePageIndex+"marks stone cutoff------"+stoneBadgeRatingCutoff); + } /** * This method will pick the badge according to the passed pageRating * @param pageRating PageRating of the page under consideration * @return The name of the Badge earned */ public static int getBadgeNumber(double pageRating){ if(pageRating>=platinumBadgeRatingCutoff) return Badges.PLATINUM.value; else if(pageRating>=goldBadgeRatingCutoff) return Badges.GOLD.value; else if(pageRating>=silverBadgeRatingCutoff) return Badges.SILVER.value; else if(pageRating>=bronzeBadgeRatingCutoff) return Badges.BRONZE.value; else return Badges.STONE.value; } } diff --git a/WikiRating/src/main/java/computations/Contribution.java b/WikiRating/src/main/java/computations/Contribution.java index d3f5623..c16885c 100644 --- a/WikiRating/src/main/java/computations/Contribution.java +++ b/WikiRating/src/main/java/computations/Contribution.java @@ -1,84 +1,87 @@ package main.java.computations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; +import main.java.utilities.Loggings; /** * This class will calculate the total size of all the edits done on the single page. - * + * */ public class Contribution { + static Class className=Contribution.class; public static HashMap getPageEdits() { /** * This method is used to calculate the total edits on a page and make a * HashMap linking pid(PageID) and corresponding edits on the Page - * + * * @return This returns a HashMap containing all the pid along * with the total number of edits on that page. */ OrientGraph graph = Connections.getInstance().getDbGraph(); HashMap totalPageEdits = new HashMap(); int pageEdits = 0; Vertex revisionNode = null, nextRevisionNode = null; boolean loopCounter = false; // Iterating over pages to calculate their total edits for (Vertex pageNode : graph.getVertices("@class", "Page")) { // To check for any null links if (pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().hasNext() == false) continue; else loopCounter = true; try { pageEdits = 0; revisionNode = pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next() .getVertex(Direction.IN); // Getting the latest version // to iterate on the // revisons // Finding out the total edits by traversing all the revisions for a particular page. - + while (loopCounter) { if (revisionNode.getEdges(Direction.OUT, "@class", "PreviousRevision").iterator().hasNext()) { nextRevisionNode = revisionNode.getEdges(Direction.OUT, "@class", "PreviousRevision").iterator() .next().getVertex(Direction.IN); pageEdits += Math.abs( (int) nextRevisionNode.getProperty("size") - (int) revisionNode.getProperty("size")); revisionNode = revisionNode.getEdges(Direction.OUT, "@class", "PreviousRevision").iterator() .next().getVertex(Direction.IN); } else { pageEdits += (int) revisionNode.getProperty("size"); loopCounter = false; } } totalPageEdits.put((Integer) pageNode.getProperty("pid"), pageEdits); } catch (Exception e) { e.printStackTrace(); } } // Iterating through the Map to print the results for total edits on the pages. - + Iterator it = totalPageEdits.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); - System.out.println(graph.getVertices("pid", pair.getKey()).iterator().next().getProperty("title") + Loggings.getLogs(className).info(graph.getVertices("pid", pair.getKey()).iterator().next().getProperty("title") + " has got insertions= " + pair.getValue()); + } graph.shutdown(); return totalPageEdits; } } diff --git a/WikiRating/src/main/java/computations/CreditSystem.java b/WikiRating/src/main/java/computations/CreditSystem.java index 7af4999..13dbefe 100644 --- a/WikiRating/src/main/java/computations/CreditSystem.java +++ b/WikiRating/src/main/java/computations/CreditSystem.java @@ -1,201 +1,203 @@ package main.java.computations; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.json.JSONArray; import org.json.JSONObject; import com.orientechnologies.orient.core.Orient; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; +import main.java.utilities.Loggings; /** * This class is used to return User Contributions for the passed pages collectively or individually. * Only major contributions done by registered users are considered. */ @Path("credit") @Produces("application/json") public class CreditSystem { - + static Class className=CreditSystem.class; + @GET @Path("users") @Produces("application/json") - + /** * This is the chief method that will return a JSON string having all the contributions * @param pageList List having all the Page Titles * @param join If set true collective user contributions for all the pages will be returned * @return JSON string having all the contributions and current version sizes. */ public static String generateCredits(@QueryParam("titles") List pageList,@QueryParam("join") boolean join){ - + OrientGraph graph = Connections.getInstance().getDbGraph(); - - + + if(!join){ //This part will be executed when individual contributions are asked for - + Vertex pageNode=null; - + JSONArray responseJson=new JSONArray(); - + for(int i=0;i userContributions=getUserContributions(graph,pageNode); - + JSONArray authorList=new JSONArray(); JSONObject pageNodeObject=new JSONObject(); - + Iterator it = userContributions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); JSONObject authorNode=new JSONObject(); authorNode.put("username", (String)pair.getKey()); authorNode.put("bytes_changed", (int)pair.getValue()); authorList.put(authorNode); - + } - - + + pageNodeObject.put("title",pageList.get(i) ); pageNodeObject.put("authors", authorList); pageNodeObject.put("bytes", pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN).getProperty("size")); - + responseJson.put(pageNodeObject); - + } - + JSONObject creditObject=new JSONObject(); creditObject.put("credits", responseJson); graph.shutdown(); return creditObject.toString(); - + } - - + + else{ //This part will be executed when collective contributions are asked for - + Vertex pageNode=null; int contributionSize=0;int totalBytes=0; String username=""; HashMap totalUserContributions=new HashMap(); for(int i=0;i userContributions=getUserContributions(graph,pageNode); - + Iterator it = userContributions.entrySet().iterator(); while (it.hasNext()) { - + Map.Entry pair = (Map.Entry) it.next(); - + username=(String)pair.getKey(); contributionSize=(int)pair.getValue(); - + if(totalUserContributions.containsKey(username)){ totalUserContributions.put(username, totalUserContributions.get(username)+contributionSize); } else{ totalUserContributions.put(username, contributionSize); } - + } totalBytes+=(int)pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN).getProperty("size"); } - + JSONArray authorList=new JSONArray(); Iterator it = totalUserContributions.entrySet().iterator(); while (it.hasNext()) { - + Map.Entry pair = (Map.Entry) it.next(); JSONObject authorNode=new JSONObject(); authorNode.put("username", (String)pair.getKey()); authorNode.put("bytes_changed", (int)pair.getValue()); authorList.put(authorNode); - + } - + JSONObject authorListObject=new JSONObject(); authorListObject.put("totalBytes", totalBytes); authorListObject.put("authors", authorList); - + graph.shutdown(); - + return authorListObject.toString(); - + } - - + + } - + /** * This method returns a HashMap filled with the user contributions of the passed pageNode * @param graph OrientGraph * @param pageNode The Page Node of whose contribution you want * @return HashMap having username as key and contributions as corresponding value */ public static HashMap getUserContributions(OrientGraph graph,Vertex pageNode){ Vertex revisionNode=null,userNode=null; revisionNode=pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); Edge contribute=null; int contributionSize=0; String username=""; HashMap userContributions=new HashMap(); try{ while(true){ - + if(revisionNode.getEdges(Direction.IN, "@class", "Contribute").iterator().hasNext()){ - + contribute=revisionNode.getEdges(Direction.IN, "@class", "Contribute").iterator().next(); userNode=contribute.getVertex(Direction.OUT); - + //Get the required bytes and the corresponding username contributionSize=contribute.getProperty("contributionSize"); - username=userNode.getProperty("username"); - + username=userNode.getProperty("username"); + //Putting the data into the HashMap if(userContributions.containsKey(username)){ - + userContributions.put(username, userContributions.get(username)+contributionSize); - + } else{ - + userContributions.put(username, contributionSize); - + } } - + if((int)revisionNode.getProperty("parentid")==0) break; - + revisionNode=graph.getVertices("revid", (int)revisionNode.getProperty("parentid")).iterator().next(); - + } }catch(Exception e){ e.printStackTrace(); } - - System.out.println(pageNode.getProperty("title")); + + Loggings.getLogs(className).info(pageNode.getProperty("title")); Iterator it = userContributions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); - System.out.println(pair.getKey()+" "+pair.getValue()); + Loggings.getLogs(className).info(pair.getKey()+" "+pair.getValue()); } - + return userContributions; } } diff --git a/WikiRating/src/main/java/computations/NormalisedVotes.java b/WikiRating/src/main/java/computations/NormalisedVotes.java index 74a3821..cf99a82 100644 --- a/WikiRating/src/main/java/computations/NormalisedVotes.java +++ b/WikiRating/src/main/java/computations/NormalisedVotes.java @@ -1,164 +1,165 @@ package main.java.computations; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientVertex; import main.java.utilities.Connections; +import main.java.utilities.Loggings; import main.java.utilities.PropertiesAccess; -/** +/** * This class will calculate the Normalised Votes * of all the revisions and hence the page by using the given - * recursive formula that takes keeps scaling the votes on the + * recursive formula that takes keeps scaling the votes on the * previous versions with the new ones - * + * */ public class NormalisedVotes { - + static Class className=NormalisedVotes.class; //To check for cases where latest version is voted on without any change - - static boolean latestVoteCheck=true; + + static boolean latestVoteCheck=true; final static double PHI_POWER_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("PHI_POWER_PARAMETER")); - + /** *This method will calculate the Normalised Votes of all the pages in on the platform *along with their respective revisions. * @return void */ public static void calculatePageVotes(){ - + OrientGraph graph = Connections.getInstance().getDbGraph(); double currentPageVote=0; Vertex revisionNode=null; for (Vertex pageNode : graph.getVertices("@class","Page")) { latestVoteCheck=true; try{ revisionNode = pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); currentPageVote=recursiveVotes(graph,(int)revisionNode.getProperty("revid")); pageNode.setProperty("currentPageVote",currentPageVote); graph.commit(); }catch(Exception e){e.printStackTrace();} } - + getTotalVotes(graph); graph.shutdown(); } - + /** * This method will calculate and store the Normalised votes for all the revisions of a particular page * and then return the final Normalised vote for the page itself * @param graph OrientGraph object - * @param revid Revision Id of the latest version connected to the Page + * @param revid Revision Id of the latest version connected to the Page * @return final vote of the latest version is computed and returned */ public static double recursiveVotes(OrientGraph graph,int revid){ - + double lastVote=0,phi=0,normalVote=0,currVote=0; Vertex revisionNode=graph.getVertices("revid", revid).iterator().next(); - + if(latestVoteCheck==false&&(double)revisionNode.getProperty("previousVote")!=-1){ - System.out.println(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+revisionNode.getProperty("previousVote")); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+revisionNode.getProperty("previousVote")); return (double)revisionNode.getProperty("previousVote"); } - + latestVoteCheck=false; if((int)revisionNode.getProperty("parentid")==0){ lastVote=simpleVote(graph,revid); revisionNode.setProperty("previousVote",lastVote); graph.commit(); - System.out.println(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+lastVote); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+lastVote); return lastVote; } - - + + else{ phi=getPhi(graph,revid); currVote=simpleVote(graph,revid); normalVote=((simpleVote(graph,revid)+phi*recursiveVotes(graph,(int)revisionNode.getProperty("parentid")))/(phi+1)); revisionNode.setProperty("previousVote",normalVote); graph.commit(); - System.out.println(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+normalVote); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+normalVote); return normalVote; } - + } - - /**This method will calculate the weighted average of votes of the current Revision Node - * + + /**This method will calculate the weighted average of votes of the current Revision Node + * * @param graph OrientGraph object * @param revid Revision Id for the revision node under the calculation - * @return The calculated Simple weighted average. + * @return The calculated Simple weighted average. */ - - + + public static double simpleVote(OrientGraph graph,int revid){ double denominator=0,numerator=0,simpleVote=0; Vertex userNode=null; Vertex revisionNode=graph.getVertices("revid",revid).iterator().next(); for(Edge reviewEdge:revisionNode.getEdges(Direction.IN,"@class","Review")){ //userNode=reviewEdge.getVertex(Direction.OUT); numerator+=(double)reviewEdge.getProperty("voteCredibility")*(double)reviewEdge.getProperty("vote"); denominator+=(double)reviewEdge.getProperty("vote"); } if(denominator==0)denominator=1; simpleVote=numerator/denominator; return simpleVote; } - - + + /** * This will calculate the parameter phi to scale the votes of the previous versions * @param graph OrientGraph object * @param revid Revision Id for the revision node under the calculation * @return The parameter phi */ public static double getPhi(OrientGraph graph,int revid){ - + double phi=0; double sizePrev=0,newEdits=0,currSize=0; Vertex revisionNode=graph.getVertices("revid",revid).iterator().next(); Vertex parentNode =graph.getVertices("revid",(int)revisionNode.getProperty("parentid")).iterator().next(); sizePrev=(int)parentNode.getProperty("size"); currSize=(int)revisionNode.getProperty("size"); newEdits=Math.abs(sizePrev-currSize); if(sizePrev==0)sizePrev=1; phi=Math.pow(Math.E,-1*(Math.pow(newEdits/sizePrev, PHI_POWER_PARAMETER))); return phi; } - + /** * This method will compute the no of Votes given to a particular page * for all the pages * @param graph OrientGraph */ public static void getTotalVotes(OrientGraph graph){ - + long totalVotes=0; OrientVertex revisionNode=null; for (Vertex pageNode : graph.getVertices("@class","Page")) { totalVotes=0; revisionNode=(OrientVertex)pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); - + while((int)revisionNode.getProperty("parentid")!=0){ totalVotes+=revisionNode.countEdges(Direction.IN, "@class","Review"); revisionNode=(OrientVertex) graph.getVertices("revid", (int)revisionNode.getProperty("parentid")).iterator().next(); - System.out.println(revisionNode.getProperty("revid")); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")); } - + totalVotes+=revisionNode.countEdges(Direction.IN, "@class","Review"); - System.out.println(pageNode.getProperty("title")+" "+totalVotes); - + Loggings.getLogs(className).info(pageNode.getProperty("title")+" "+totalVotes); + //Adding the totalVotes into the DB for faster retrieval pageNode.setProperty("totalVotes", totalVotes); graph.commit(); } - + } - - + + } diff --git a/WikiRating/src/main/java/computations/PageRating.java b/WikiRating/src/main/java/computations/PageRating.java index b293074..bf6bffc 100644 --- a/WikiRating/src/main/java/computations/PageRating.java +++ b/WikiRating/src/main/java/computations/PageRating.java @@ -1,39 +1,39 @@ package main.java.computations; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; -import main.java.utilities.Logs; +import main.java.utilities.Loggings; import main.java.utilities.PropertiesAccess; /** * This class will compute the final Page rating combining all the parameter in the Master Equation */ public class PageRating{ final static double PAGERANK_IMPORTANCE_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("PAGERANK_IMPORTANCE_PARAMETER")); /** * This method combines all the parameters are calculate and store the final Page Rating of the Page. * @return void */ public static void computePageRatings(){ double currentPageReliability=0,pageRank=0,currentPageVote=0,pageRating=0; OrientGraph graph = Connections.getInstance().getDbGraph(); for(Vertex pageNode:graph.getVertices("@class","Page")){ try{ currentPageReliability=pageNode.getProperty("currentPageReliability"); pageRank=pageNode.getProperty("Pagerank"); currentPageVote=pageNode.getProperty("currentPageVote"); pageRating=((currentPageReliability*currentPageVote)+(PAGERANK_IMPORTANCE_PARAMETER*pageRank)); pageNode.setProperty("PageRating", pageRating); System.out.println(pageNode.getProperty("title")+" ======has rating==== "+pageRating); }catch(Exception e){e.printStackTrace();} } graph.commit(); graph.shutdown(); } } diff --git a/WikiRating/src/main/java/computations/Pagerank.java b/WikiRating/src/main/java/computations/Pagerank.java index 15fe252..2d9a2fd 100644 --- a/WikiRating/src/main/java/computations/Pagerank.java +++ b/WikiRating/src/main/java/computations/Pagerank.java @@ -1,114 +1,116 @@ package main.java.computations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientVertex; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; +import main.java.utilities.Loggings; + /** * This class will calculate the Pagerank (Cite Index) of all the Pages on the platform */ public class Pagerank { - - + static Class className=Pagerank.class; + /** * This method prints all the edges * linking to a page for all the pages on the platform. * @return void */ public static void edgeList(){ - + OrientGraph graph = Connections.getInstance().getDbGraph(); for (Vertex pageNode :graph.getVertices("@class","Page")) { OrientVertex oPageNode=(OrientVertex)pageNode; long backLinks=oPageNode.countEdges(Direction.IN, "@class","Backlink"); - System.out.println("======= "+pageNode.getProperty("name").toString()+"========"+backLinks); + Loggings.getLogs(className).info("======= "+pageNode.getProperty("name").toString()+"========"+backLinks); for(Edge e:oPageNode.getEdges(Direction.IN,"@class","Backlink")){ - System.out.println(e.getVertex(Direction.OUT).getProperty("title")); + Loggings.getLogs(className).info(e.getVertex(Direction.OUT).getProperty("title")); } - + } - + } - - - + + + /** * This method will calculate and store the Pageranks of all the pages * on the platform. * @return void */ public static void pageRankCompute(){ - + OrientGraph graph = Connections.getInstance().getDbGraph(); HashMap pageRankMap=new HashMap(); long maxLink=0,currLink=0; double tempSum=0;double finalPageRank; - + //Iterating for the calculation of number of backLinks and the maximum of them. for (Vertex pageNode :graph.getVertices("@class","Page")) { OrientVertex oPageNode=(OrientVertex)pageNode; currLink=oPageNode.countEdges(Direction.IN, "@class","Backlink"); pageRankMap.put((Integer)oPageNode.getProperty("pid"),(double)currLink); maxLink=(maxLink getRevisionList(OrientGraph graph){ ArrayList revisionList=new ArrayList(); for(Vertex revisionNode:graph.getVertices("@class","Revision")){ revisionList.add((Integer) revisionNode.getProperty("revid")); } return revisionList; } /** - * This method will generate all the votes by choosing random users and then voting + * This method will generate all the votes by choosing random users and then voting * randomly for arbitrary pages */ public static void generateVotes(){ int noOfVotes=0; int revisionToVote=0; int revid=0,currVote=0; Vertex revisionNode=null; OrientGraph graph = Connections.getInstance().getDbGraph(); ArrayList revisionList=getRevisionList(graph); for (Vertex userNode : graph.getVertices("@class", "User")) { int[] voteTrack=new int[revisionList.size()]; - + //HashMap voteTrack=new HashMap(); if(getRandom(1,10)>4){ - + //To get how many votes a user will vote for noOfVotes=(int)getRandom(1,50); - + //Loop to cast votes on the behalf of User for(int i=0;i<=noOfVotes;i++){ revisionToVote=getRandom(0, revisionList.size()-1); revid=(int) revisionList.get(revisionToVote); if(voteTrack[revisionToVote]!=1){//To avoid voting for a same version revisionNode=graph.getVertices("revid",revid).iterator().next(); if(((int)revisionNode.getProperty("userid"))!=((int)userNode.getProperty("userid"))){ voteTrack[revisionToVote]=1; currVote=getRandom(1, 10); - System.out.println(userNode.getProperty("username")+" gives "+currVote+" to "+revisionNode.getProperty("Page")+" == "+revid); + Loggings.getLogs(className).info(userNode.getProperty("username")+" gives "+currVote+" to "+revisionNode.getProperty("Page")+" == "+revid); Edge review = graph.addEdge("review", userNode, revisionNode, "Review"); review.setProperty("vote", currVote/10.0); review.setProperty("voteCredibility",userNode.getProperty("credibility")); } //graph.commit(); } } graph.commit(); } } //graph.commit(); graph.shutdown(); } } diff --git a/WikiRating/src/main/java/computations/Reliability.java b/WikiRating/src/main/java/computations/Reliability.java index ccabadb..4b2faa2 100644 --- a/WikiRating/src/main/java/computations/Reliability.java +++ b/WikiRating/src/main/java/computations/Reliability.java @@ -1,135 +1,137 @@ package main.java.computations; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; +import main.java.utilities.Loggings; + /** * This class will calculate the reliability of the vote given by the users. * The structure and the methods of this class are very similar to class NormalisedVotes.java */ public class Reliability { - + static Class className=Reliability.class; //To check for cases where latest version is voted on without any change - static boolean latestVoteCheck=true; + static boolean latestVoteCheck=true; final static double PHI_POWER_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("PHI_POWER_PARAMETER")); /** *This method will calculate the reliability of the votes given by the user *to the versions. * @return void */ public static void calculateReliability(){ - + OrientGraph graph = Connections.getInstance().getDbGraph(); double currentPageReliability=0; Vertex revisionNode=null; double maxPageReliability=-1; for (Vertex pageNode : graph.getVertices("@class","Page")) { try{ - + revisionNode = pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); currentPageReliability=recursiveReliability(graph,(int)revisionNode.getProperty("revid")); - + if(maxPageReliability<=currentPageReliability){ maxPageReliability=currentPageReliability; } - + pageNode.setProperty("currentPageReliability",currentPageReliability); graph.commit(); }catch(Exception e){e.printStackTrace();} } //graph.commit(); PropertiesAccess.putParameter("maxPageReliability", maxPageReliability); graph.shutdown(); } - + /** * This method will calculate and store the reliability * of votes for all the revisions of a particular page * and then return the final reliability of vote for the page itself * @param graph OrientGraph object - * @param revid Revision Id of the latest version connected to the Page + * @param revid Revision Id of the latest version connected to the Page * @return final reliability of the latest version is computed and returned */ public static double recursiveReliability(OrientGraph graph,int revid){ - + double lastReliability=0,phi=0,normalReliability=0,currReliability=0; Vertex revisionNode=graph.getVertices("revid", revid).iterator().next(); - + if(latestVoteCheck==false&&(double)revisionNode.getProperty("previousReliability")!=-1){ - System.out.println(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+revisionNode.getProperty("previousReliability")); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+revisionNode.getProperty("previousReliability")); return (double)revisionNode.getProperty("previousReliability"); } - - + + latestVoteCheck=false; if((int)revisionNode.getProperty("parentid")==0){ lastReliability=simpleReliability(graph,revid); revisionNode.setProperty("previousReliability",lastReliability); graph.commit(); - System.out.println(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+lastReliability); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+lastReliability); return lastReliability; } - + else{ phi=getPhi(graph,revid); currReliability=simpleReliability(graph,revid); normalReliability=((simpleReliability(graph,revid)+phi*recursiveReliability(graph,(int)graph.getVertices("revid", revid).iterator().next().getProperty("parentid")))/(phi+1)); revisionNode.setProperty("previousReliability",normalReliability); graph.commit(); - System.out.println(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+normalReliability); + Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+normalReliability); return normalReliability; } - + } - + /** - * This method will calculate the average of reliabilities of the current Revision Node - * + * This method will calculate the average of reliabilities of the current Revision Node + * * @param graph OrientGraph object * @param revid Revision Id for the revision node under the calculation - * @return The calculated Simple weighted average. + * @return The calculated Simple weighted average. */ public static double simpleReliability(OrientGraph graph,int revid){ - + double numerator=0,simpleVote=0,globalVote=0,userVote=0; - + Vertex revisionNode=graph.getVertices("revid",revid).iterator().next(); for(Edge reviewEdge:revisionNode.getEdges(Direction.IN,"@class","Review")){ userVote=reviewEdge.getProperty("vote"); globalVote=revisionNode.getProperty("previousVote"); numerator+=(double)reviewEdge.getProperty("voteCredibility")*(1-Math.abs(userVote-globalVote)); - + } - + simpleVote=numerator; return simpleVote; } - - + + /** * This will calculate the parameter phi to scale the reliabilities of the previous versions * @param graph * @param revid * @return The parameter phi */ public static double getPhi(OrientGraph graph,int revid){ - + double phi=0; double sizePrev=0,newEdits=0,currSize=0; Vertex revisionNode=graph.getVertices("revid",revid).iterator().next(); Vertex parentNode =graph.getVertices("revid",(int)revisionNode.getProperty("parentid")).iterator().next(); sizePrev=(int)parentNode.getProperty("size"); currSize=(int)revisionNode.getProperty("size"); newEdits=Math.abs(sizePrev-currSize); if(sizePrev==0)sizePrev=1; phi=Math.pow(Math.E,-1*(Math.pow(newEdits/sizePrev, PHI_POWER_PARAMETER))); return phi; } - + } diff --git a/WikiRating/src/main/java/computations/UserCredibility.java b/WikiRating/src/main/java/computations/UserCredibility.java index 7f06cb0..f9e6655 100644 --- a/WikiRating/src/main/java/computations/UserCredibility.java +++ b/WikiRating/src/main/java/computations/UserCredibility.java @@ -1,114 +1,116 @@ package main.java.computations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; +import main.java.utilities.Loggings; + /** - * This class will deal with the calculations of User Credibility + * This class will deal with the calculations of User Credibility */ public class UserCredibility { - + static Class className=CreditSystem.class; final static double USER_CONTRI_IMPORTANCE_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("USER_CONTRI_IMPORTANCE_PARAMETER")); final static double USER_VOTE_IMPORTANCE_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("USER_VOTE_IMPORTANCE_PARAMETER")); - + /** - *This method will compute the credibility for all the Users + *This method will compute the credibility for all the Users */ - + public static void getUserCredibility(){ OrientGraph graph = Connections.getInstance().getDbGraph(); double alpha=0,relativeUserContribution=0,voteDeviation=0,credibility=0; HashMap pageEditMap=Contribution.getPageEdits(); //To iterate over all the Users for getting their respective Credibility try{ - for(Vertex userNode:graph.getVertices("@class", "User")){ + for(Vertex userNode:graph.getVertices("@class", "User")){ relativeUserContribution=getRelativeUserContribution(userNode,graph,pageEditMap); voteDeviation=getVoteDeviation(userNode,graph); alpha=(USER_CONTRI_IMPORTANCE_PARAMETER*relativeUserContribution+USER_VOTE_IMPORTANCE_PARAMETER*voteDeviation)/(USER_CONTRI_IMPORTANCE_PARAMETER+USER_VOTE_IMPORTANCE_PARAMETER); credibility=alpha; userNode.setProperty("credibility",credibility); - System.out.println(userNode.getProperty("username")+" has "+credibility); + Loggings.getLogs(className).info(userNode.getProperty("username")+" has "+credibility); graph.commit(); } }catch(Exception e){e.printStackTrace();} //graph.commit(); graph.shutdown(); - + } /** * This method calculates the parameter 'a'(relativeUserContribution) for credibility calculation * @param userNode The Vertex of the User class whose credibility is being calculated * @param graph OrientGraph object * @param pageEditMap HashMap containing all the edits and their corresponding pid * @return The value of parameter 'a' */ public static double getRelativeUserContribution(Vertex userNode,OrientGraph graph,HashMap pageEditMap){ - HashMap userPageContributions=new HashMap(); - int contpid=0,countContribution=0; + HashMap userPageContributions=new HashMap(); + int contpid=0,countContribution=0; double userEdits=0,totalEdits=1,finalPageVote=0; double userPageContributionsTemp=0,userPageContributionsTotal=0; int contributionSize=0; for(Edge contributeEdge:userNode.getEdges(Direction.OUT,"@class","Contribute")){ - + contpid=(int)graph.getVertices("title",contributeEdge.getVertex(Direction.IN).getProperty("Page").toString()).iterator().next().getProperty("pid"); contributionSize=contributeEdge.getProperty("contributionSize"); if(userPageContributions.containsKey(contpid)){ contributionSize+=(int)userPageContributions.get(contpid); userPageContributions.put(contpid,(Integer)contributionSize); } else { userPageContributions.put(contpid,(Integer)contributionSize); } } Iterator it = userPageContributions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); contpid=(int)pair.getKey(); userEdits=(int)userPageContributions.get(contpid); totalEdits=(int)pageEditMap.get(contpid); finalPageVote=graph.getVertices("pid",contpid).iterator().next().getProperty("currentPageVote"); if(totalEdits==0)totalEdits=1; userPageContributionsTemp=(finalPageVote*userEdits/totalEdits); userPageContributionsTotal+=userPageContributionsTemp; countContribution++; } if(countContribution==0)countContribution=1; return userPageContributionsTotal/countContribution; } - + /** * This method calculates the parameter 'b'(voteDeviation) for credibility calculation * @param userNode The Vertex of the User class whose credibility is being calculated * @param graph OrientGraph object * @return The value of parameter 'b' */ - + public static double getVoteDeviation(Vertex userNode,OrientGraph graph){ double voteDeviationTemp=0,voteDeviationTotal=0,userVote,versionVote; int countReview=0; try{ for(Edge reviewEdge:userNode.getEdges(Direction.OUT,"@class","Review")){ userVote=reviewEdge.getProperty("vote"); versionVote=reviewEdge.getVertex(Direction.IN).getProperty("previousVote"); voteDeviationTemp=1-Math.abs(userVote-versionVote); voteDeviationTotal+=voteDeviationTemp; countReview++; } }catch(Exception e){e.printStackTrace();} if(countReview==0)countReview=1; return voteDeviationTotal/countReview; - + } } diff --git a/WikiRating/src/main/java/computations/UserVoteFetch.java b/WikiRating/src/main/java/computations/UserVoteFetch.java index fafc490..a27c917 100644 --- a/WikiRating/src/main/java/computations/UserVoteFetch.java +++ b/WikiRating/src/main/java/computations/UserVoteFetch.java @@ -1,120 +1,120 @@ package main.java.computations; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.glassfish.jersey.server.JSONP; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; - +import main.java.utilities.Loggings; import main.java.utilities.Connections; /** * This class is used to fetch the votes given by the user and add that to the database. * Duplicate votes aren't added but previous votes are updated during a revote */ @Path("votePage") public class UserVoteFetch { - + static Class className=UserVoteFetch.class; @GET @Path("userVote") @JSONP(queryParam = "callback") @Produces({ "application/x-javascript" }) /** * This method adds the user vote to the the latest version of the page * @param callback The function name for bypassing SOP * @param pageTitle The title of the page whose latest revision is being voted * @param userName The name of the user who is voting * @param userVote The value of the vote * @return A string stating the process is successful */ public String getAllTestData(@QueryParam("callback") String callback, @QueryParam("pageTitle") String pageTitle,@QueryParam("userName") String userName,@QueryParam("userVote") int userVote) { - + try{ OrientGraph graph = Connections.getInstance().getDbGraph(); Vertex userNode=graph.getVertices("username",userName).iterator().next(); Vertex pageNode=graph.getVertices("title",pageTitle).iterator().next(); Vertex revisionNode = pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); Vertex revisionNodeContributor=null; - + if(revisionNode.getEdges(Direction.IN, "@class", "Contribute").iterator().hasNext()){ - + revisionNodeContributor=revisionNode.getEdges(Direction.IN, "@class", "Contribute").iterator().next().getVertex(Direction.OUT); //Code to prevent contributor from rating their own contributions if(((String)revisionNodeContributor.getProperty("username")).equals(userName)){ - - System.out.println("Users can't vote their own work"); + + Loggings.getLogs(className).info("Users can't vote their own work"); graph.shutdown(); String sJson="{\"pageTitle\":\"User cant vote thier own work\"}"; String result = callback + "(" + sJson + ");"; return result; } - + } - + //Removes the old vote if exists if(IsNotDuplicateVote(userNode, revisionNode)==false){ Vertex votedRevisionNode=null; for(Edge votedRevisionEdge:userNode.getEdges(Direction.OUT, "@class", "Review")){ votedRevisionNode=votedRevisionEdge.getVertex(Direction.IN); if(votedRevisionNode.getId()==revisionNode.getId()){ - System.out.println("Vote removed with value = "+votedRevisionEdge.getProperty("vote")+" having id = "+votedRevisionEdge.getProperty("@RID")); + Loggings.getLogs(className).info("Vote removed with value = "+votedRevisionEdge.getProperty("vote")+" having id = "+votedRevisionEdge.getProperty("@RID")); graph.removeEdge(votedRevisionEdge); - + } } - + } - + //Creates the new vote Edge review = graph.addEdge("review", userNode, revisionNode, "Review"); review.setProperty("vote", userVote/10.0); review.setProperty("voteCredibility",userNode.getProperty("credibility")); graph.commit(); - - System.out.println(pageNode.getProperty("title")); - System.out.println(userNode.getProperty("username")); - System.out.println(userVote); - System.out.println("New Vote added successfully"); - - - - + + Loggings.getLogs(className).info(pageNode.getProperty("title")); + Loggings.getLogs(className).info(userNode.getProperty("username")); + Loggings.getLogs(className).info(userVote); + Loggings.getLogs(className).info("New Vote added successfully"); + + + + graph.shutdown(); }catch(Exception e){ e.printStackTrace(); } - + String sJson="{\"pageTitle\":\"Successful\"}"; - + String result = callback + "(" + sJson + ");"; return result; } /** * This method checks whether the user already voted for the current version or not * @param userNode The user Vertex * @param revisionNode The latest revision of the Page being voted - * @return Either true of false indicating the presence of a duplicate edge + * @return Either true of false indicating the presence of a duplicate edge */ public boolean IsNotDuplicateVote(Vertex userNode,Vertex revisionNode){ Vertex votedRevisionNode=null; for(Edge votedRevisionEdge:userNode.getEdges(Direction.OUT, "@class", "Review")){ votedRevisionNode=votedRevisionEdge.getVertex(Direction.IN); if(votedRevisionNode.getId()==revisionNode.getId()){ - System.out.println("Already voted"); - return false; + Loggings.getLogs(className).info("Already voted"); + return false; } } return true; - + } - - + + } diff --git a/WikiRating/src/main/java/controllers/DisplayStatistics.java b/WikiRating/src/main/java/controllers/DisplayStatistics.java index 6963c97..948abbf 100644 --- a/WikiRating/src/main/java/controllers/DisplayStatistics.java +++ b/WikiRating/src/main/java/controllers/DisplayStatistics.java @@ -1,49 +1,49 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.glassfish.jersey.server.JSONP; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; - +import main.java.utilities.Loggings; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; @Path("stats") public class DisplayStatistics { - + static Class className=DisplayStatistics.class; @GET @Path("display") @JSONP(queryParam = "callback") @Produces({ "application/x-javascript" }) public String getAllStats(@QueryParam("callback") String callback, @QueryParam("pageTitle") String pageTitle) { int nameSpace,badgeNumber;long totalVotes=0; double pageRank,currentPageVote,pageReliability,maxPageReliability,maxPageRank; OrientGraph graph = Connections.getInstance().getDbGraph(); Vertex pageNode=graph.getVertices("title",pageTitle).iterator().next(); - + nameSpace=pageNode.getProperty("namespace"); badgeNumber=pageNode.getProperty("badgeNumber"); pageRank=pageNode.getProperty("Pagerank"); totalVotes=pageNode.getProperty("totalVotes"); currentPageVote=pageNode.getProperty("currentPageVote"); pageReliability=pageNode.getProperty("currentPageReliability"); maxPageReliability=PropertiesAccess.getParameter("maxPageReliability"); maxPageRank=PropertiesAccess.getParameter("maxPageRank"); - + graph.shutdown(); - System.out.println(nameSpace+" "+badgeNumber+" "+pageRank+" "+totalVotes+" "+currentPageVote+" "+pageReliability+" "+maxPageReliability+" "+maxPageRank); - + Loggings.getLogs(className).info(nameSpace+" "+badgeNumber+" "+pageRank+" "+totalVotes+" "+currentPageVote+" "+pageReliability+" "+maxPageReliability+" "+maxPageRank); + //String sJson="{\"pageTitle\":\"dd\",\"currentPageRating\":2,\"maxPageRating\":55,\"badgeNumber\":4}"; - + String sJson="{\"nameSpace\":"+nameSpace+",\"badgeNumber\":"+badgeNumber+",\"pageRank\":"+pageRank+",\"totalVotes\":"+totalVotes+",\"currentPageVote\":"+currentPageVote+",\"pageReliability\":"+pageReliability+",\"maxPageReliability\":"+maxPageReliability+",\"maxPageRank\":"+maxPageRank+"}"; String result = callback + "(" + sJson + ");"; return result; } } diff --git a/WikiRating/src/main/java/controllers/Firstrun.java b/WikiRating/src/main/java/controllers/Firstrun.java index 94f7b93..99dada5 100644 --- a/WikiRating/src/main/java/controllers/Firstrun.java +++ b/WikiRating/src/main/java/controllers/Firstrun.java @@ -1,96 +1,96 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import com.orientechnologies.orient.core.Orient; - +import main.java.utilities.Loggings; import main.java.computations.BadgeGenerator; import main.java.computations.NormalisedVotes; import main.java.computations.PageRating; import main.java.computations.Pagerank; import main.java.computations.RandomVoteGenerator; import main.java.computations.Reliability; import main.java.computations.UserCredibility; import main.java.models.InitialiseDB; import main.java.models.LinkPages; import main.java.models.LinkUserContributions; import main.java.models.Page; import main.java.models.Revision; import main.java.models.User; - +import main.java.utilities.Loggings; /** - * This class will be used to initialize the engine. - * + * This class will be used to initialize the engine. + * */ @Path("firstRun") public class Firstrun { - + static Class className=Firstrun.class; @GET @Produces("application/json") - + // Warning: If you add revisions before the Users, only those users who have // not contributed to Wiki will be added. // However this behaviour can be inverted too - + /** * This method will call different other methods that will initialize the engine * @return Response object showing time taken to run the computation */ public Response pCompute() { long startTime = System.currentTimeMillis(); - + InitialiseDB.createClass(); - System.out.println("==================Classes creation over====================="); - + Loggings.getLogs(className).info("==================Classes creation over====================="); + Page.insertPages(); - System.out.println("==================Page insertion over====================="); - + Loggings.getLogs(className).info("==================Page insertion over====================="); + LinkPages.linkAll("@class","Page"); - System.out.println("==================Page linking over====================="); - - + Loggings.getLogs(className).info("==================Page linking over====================="); + + User.insertAllUsers(); - System.out.println("==================All Users inserted====================="); - + Loggings.getLogs(className).info("==================All Users inserted====================="); + Revision.getAllRevisions("@class","Page"); - System.out.println("==================Page Revisions over====================="); - + Loggings.getLogs(className).info("==================Page Revisions over====================="); + Pagerank.pageRankCompute(); - System.out.println("==================Page rank over====================="); - + Loggings.getLogs(className).info("==================Page rank over====================="); + LinkUserContributions.linkAll(); - System.out.println("==================All Users Linked====================="); - + Loggings.getLogs(className).info("==================All Users Linked====================="); + RandomVoteGenerator.generateVotes(); - System.out.println("==================All Versions voted====================="); - + Loggings.getLogs(className).info("==================All Versions voted====================="); + NormalisedVotes.calculatePageVotes(); - System.out.println("==================All Page Votes computed====================="); - + Loggings.getLogs(className).info("==================All Page Votes computed====================="); + UserCredibility.getUserCredibility(); - System.out.println("==================User Credibility computed====================="); - + Loggings.getLogs(className).info("==================User Credibility computed====================="); + Reliability.calculateReliability(); - System.out.println("==================Vote Reliability computed====================="); - + Loggings.getLogs(className).info("==================Vote Reliability computed====================="); + PageRating.computePageRatings(); - System.out.println("==================Page Ratings computed====================="); - + Loggings.getLogs(className).info("==================Page Ratings computed====================="); + new BadgeGenerator().generateBadges(); - System.out.println("==================Badges given====================="); - - //Orient.instance().shutdown(); - + Loggings.getLogs(className).info("==================Badges given====================="); + + //Orient.instance().shutdown(); + long estimatedTime = System.currentTimeMillis() - startTime; estimatedTime = estimatedTime / 60000; return Response.status(200).entity("Successful and took" + estimatedTime + "Minutes").build(); } } diff --git a/WikiRating/src/main/java/controllers/PageInfo.java b/WikiRating/src/main/java/controllers/PageInfo.java index 2597b2c..cf45469 100644 --- a/WikiRating/src/main/java/controllers/PageInfo.java +++ b/WikiRating/src/main/java/controllers/PageInfo.java @@ -1,59 +1,59 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.glassfish.jersey.server.JSONP; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; - +import main.java.utilities.Loggings; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; /** * This class deals with the basic info requested by the platform's pages */ @Path("display") public class PageInfo { - + static Class className=PageInfo.class; @GET @Path("pageRating") @JSONP(queryParam = "callback") @Produces({ "application/x-javascript" }) /** * This method returns all the parameters requested by the Wiki page during the page load. * @param callback The function name for bypassing SOP * @param pageTitle Page title for which the information is being requested * @return JSON string consisting of requesting parameters. */ public String getAllTestData(@QueryParam("callback") String callback, @QueryParam("pageTitle") String pageTitle) { double currentPageRating=0,maxPageRating=0; int badgeNumber=0; OrientGraph graph = Connections.getInstance().getDbGraph(); try{ Vertex currentPage=graph.getVertices("title",pageTitle).iterator().next(); currentPageRating=currentPage.getProperty("PageRating"); maxPageRating=PropertiesAccess.getParameter("maxRating"); badgeNumber=currentPage.getProperty("badgeNumber"); }catch(Exception e){ e.printStackTrace(); } - + graph.shutdown(); - System.out.println(pageTitle); - System.out.println(currentPageRating); - System.out.println(maxPageRating); - System.out.println(badgeNumber); - + Loggings.getLogs(className).info(pageTitle); + Loggings.getLogs(className).info(currentPageRating); + Loggings.getLogs(className).info(maxPageRating); + Loggings.getLogs(className).info(badgeNumber); + String sJson="{\"pageTitle\":\""+pageTitle+"\",\"currentPageRating\":"+currentPageRating+",\"maxPageRating\":"+maxPageRating+",\"badgeNumber\":"+badgeNumber+"}"; - - + + String result = callback + "(" + sJson + ");"; return result; } } diff --git a/WikiRating/src/main/java/controllers/Secondrun.java b/WikiRating/src/main/java/controllers/Secondrun.java index 1afa97d..caa63fc 100644 --- a/WikiRating/src/main/java/controllers/Secondrun.java +++ b/WikiRating/src/main/java/controllers/Secondrun.java @@ -1,79 +1,80 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; - +import main.java.utilities.Loggings; import main.java.computations.BadgeGenerator; import main.java.computations.NormalisedVotes; import main.java.computations.PageRating; import main.java.computations.Pagerank; import main.java.computations.Reliability; import main.java.computations.UserCredibility; import main.java.models.AddNewPages; import main.java.models.User; import main.java.utilities.PropertiesAccess; /** - * This class will be used for detecting and storing any new changes like + * This class will be used for detecting and storing any new changes like * Page addition, Page modification and User Addition to the platform. */ @Path("secondRun") public class Secondrun { + static Class className=Secondrun.class; @GET @Produces("application/json") /** * This method will call suitable methods to insert all the new users and * update the database for any new changes * @return Response object showing time taken to run the computation */ public Response pCompute() { long startTime = System.currentTimeMillis(); - + /*Now we will check for new pages and add revisions to them too. *Make links to the user contributions too *Calculate the user votes and then calculate the recursive votes too. *We will calculate the backlinks too *Drop backlinks and then create the new ones again *Calculate the votes *Calculate the user reliability */ - + User.insertAllUsers(); - System.out.println("==================Checked for new User's insertion====================="); - + Loggings.getLogs(className).info("==================Checked for new User's insertion====================="); + AddNewPages.checkForPages(); - System.out.println("==================Checked for any new pages,revisions and linked the user contributions and made backlinks====================="); - + Loggings.getLogs(className).info("==================Checked for any new pages,revisions and linked the user contributions and made backlinks====================="); + NormalisedVotes.calculatePageVotes(); - System.out.println("==================Calculated new page votes====================="); - + Loggings.getLogs(className).info("==================Calculated new page votes====================="); + Reliability.calculateReliability(); - System.out.println("==================Calculated new reliabilities====================="); - + Loggings.getLogs(className).info("==================Calculated new reliabilities====================="); + Pagerank.pageRankCompute(); - System.out.println("==================Page rank over====================="); - + Loggings.getLogs(className).info("==================Page rank over====================="); + UserCredibility.getUserCredibility(); - System.out.println("==================User Credibility computed====================="); - + Loggings.getLogs(className).info("==================User Credibility computed====================="); + PageRating.computePageRatings(); - System.out.println("==================Page Ratings computed====================="); - + Loggings.getLogs(className).info("==================Page Ratings computed====================="); + new BadgeGenerator().generateBadges(); - System.out.println("==================Badges given====================="); - - + Loggings.getLogs(className).info("==================Badges given====================="); + + long estimatedTime = System.currentTimeMillis() - startTime; estimatedTime = estimatedTime / 60000; return Response.status(200).entity("Successful and took" + estimatedTime + "Minutes").build(); } - + } diff --git a/WikiRating/src/main/java/controllers/WikiUtil.java b/WikiRating/src/main/java/controllers/WikiUtil.java index 62bb3b0..1ac8020 100644 --- a/WikiRating/src/main/java/controllers/WikiUtil.java +++ b/WikiRating/src/main/java/controllers/WikiUtil.java @@ -1,204 +1,204 @@ package main.java.controllers; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.wikidata.wdtk.wikibaseapi.ApiConnection; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientEdge; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; - +import main.java.utilities.Loggings; /**This class contains various utilities methods for the other classes - * + * */ public class WikiUtil { - + static Class className=WikiUtil.class; /** * This method converts an InputStream object to String * @param in InputStream object to be converted * @return Converted String */ public static String streamToString(InputStream in) { String result = ""; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } StringBuilder builder = new StringBuilder(); String line; try { while ((line = reader.readLine()) != null) { builder.append(line); } result = builder.toString(); in.close(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to fetch all the pages * residing in all the namespaces * @param ns The namespace whose pages are requested * @return Map having parameters */ - + public static Map getPageParam(String ns) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "allpages"); queryParameterMap.put("apfrom", "a"); queryParameterMap.put("aplimit", "max"); queryParameterMap.put("apnamespace", ns); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to fetch all the revisions * of the given page * @param pid The PageID of the page for which revisions are requested * @return Map having parameters */ public static Map getRevisionParam(String pid) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("prop", "revisions"); queryParameterMap.put("pageids", pid); queryParameterMap.put("rvprop", "userid|ids|timestamp|user|flags|size"); queryParameterMap.put("rvlimit", "max"); queryParameterMap.put("rvdir", "newer"); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to get * all the backlinks for the specified page * @param pid The PageID of the page for which backlinks are requested * @return Map having parameters */ - + public static Map getLinkParam(String pid) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "backlinks"); queryParameterMap.put("blpageid", pid); queryParameterMap.put("blfilterredir", "all"); queryParameterMap.put("bllimit", "max"); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to get * all the users * @param username username to continue from in case the results are more than 500 * @return Map having parameters */ public static Map getUserParam(String username) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "allusers"); queryParameterMap.put("aulimit", "max"); queryParameterMap.put("aufrom", username); queryParameterMap.put("rawcontinue", ""); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to get * all the contributions by the specified User * @param username Username for whom the contributions have to be fetched * @return Map having parameters */ public static Map getUserContriParam(String username) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "usercontribs"); queryParameterMap.put("uclimit", "max"); queryParameterMap.put("ucdir", "newer"); queryParameterMap.put("ucuser", username); queryParameterMap.put("ucshow", "!minor"); queryParameterMap.put("ucprop", "sizediff|title|ids|flags"); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method sends a POST request to MediaWiki API and then gets back an InputStream * @param con The ApiConnection object * @param queryParameterMap The Map having all the query parameters * @return InputStream object having the requested data */ public static InputStream reqSend(ApiConnection con, Map queryParameterMap) { InputStream in = null; try { in = con.sendRequest("POST", queryParameterMap); } catch (IOException e) { e.printStackTrace(); } return in; } /** * This method will check for the duplicate entities in the database * @param key The name of the class for which redundancy needs to be checked * @param value The value to be checked * @param graph OrientGraph object * @return true or false depending on whether entity is absent or present respectively */ public static boolean rCheck(String key, int value, OrientGraph graph) { Iterable checkNode = graph.getVertices(key, value); Iterator it = checkNode.iterator(); if (it.hasNext()) { return false; } else return true; } - - + + /** * This method prints all the pages * @return A formatted string containing all the Page names */ public static String printVertex() { String result = ""; OrientGraph graph = Connections.getInstance().getDbGraph(); for (Vertex pageNode : graph.getVertices("@class", "Page")) { result = result + " \n" + pageNode.getProperty("title"); } return result; } } diff --git a/WikiRating/src/main/java/controllers/Wipe.java b/WikiRating/src/main/java/controllers/Wipe.java index ef08aa4..da9f317 100644 --- a/WikiRating/src/main/java/controllers/Wipe.java +++ b/WikiRating/src/main/java/controllers/Wipe.java @@ -1,89 +1,90 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import main.java.utilities.Connections; +import main.java.utilities.Loggings; /** * This class will wipe the database clean * */ @Path("wipe") public class Wipe { - + static Class className=Wipe.class; @GET @Produces("application/json") - + /** * This method will delete all the added classes,Vertices,Edges from the database * @return Response object showing time taken to run the computation */ public Response wipeDatabase() { long startTime = System.currentTimeMillis(); OrientGraphNoTx graph = Connections.getInstance().getDbGraphNT(); - + try{ graph.dropVertexType("Page"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============Page deleted========="); - + Loggings.getLogs(className).info("=============Page deleted========="); + try{ graph.dropVertexType("Revision"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============Revision deleted========="); + Loggings.getLogs(className).info("=============Revision deleted========="); try{ graph.dropVertexType("User"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============User deleted========="); + Loggings.getLogs(className).info("=============User deleted========="); try{ graph.dropEdgeType("Backlink"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============Backlink deleted========="); + Loggings.getLogs(className).info("=============Backlink deleted========="); try{ graph.dropEdgeType("PreviousRevision"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============PreviousRevision deleted========="); + Loggings.getLogs(className).info("=============PreviousRevision deleted========="); try{ graph.dropEdgeType("PreviousVersionOfPage"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============PreviousVersionOfPage deleted========="); - + Loggings.getLogs(className).info("=============PreviousVersionOfPage deleted========="); + try{ graph.dropEdgeType("Contribute"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============Contribute deleted========="); + Loggings.getLogs(className).info("=============Contribute deleted========="); try{ graph.dropEdgeType("Review"); }catch(Exception e){ e.printStackTrace(); } - System.out.println("=============Review deleted========="); + Loggings.getLogs(className).info("=============Review deleted========="); //graph.commit(); graph.shutdown(); - + long estimatedTime = System.currentTimeMillis() - startTime; estimatedTime=estimatedTime/60000; - - return Response.status(200).entity("Successful and took"+estimatedTime+"Minutes").build(); + + return Response.status(200).entity("Successful and took"+estimatedTime+"Minutes").build(); } } diff --git a/WikiRating/src/main/java/models/AddNewPages.java b/WikiRating/src/main/java/models/AddNewPages.java index 817a1b7..56f0f3e 100644 --- a/WikiRating/src/main/java/models/AddNewPages.java +++ b/WikiRating/src/main/java/models/AddNewPages.java @@ -1,301 +1,302 @@ package main.java.models; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.controllers.WikiUtil; import main.java.utilities.AllowedNamespaces; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; +import main.java.utilities.Loggings; /** - * This class will check for new pages, revisions made on previous pages and add them. + * This class will check for new pages, revisions made on previous pages and add them. * Further it will link them to the corresponding User contributions. * It will also link a page to other pages that has a back link towards it. */ public class AddNewPages { - + static Class className=AddNewPages.class; /** * This method will check for all the changes and then call suitable methods to handle them. */ public static void checkForPages(){ - + OrientGraph graph = Connections.getInstance().getDbGraph(); String allPages=""; - + try { //Now we will be iterating over all the namespaces to get all the pages in each og them. - + for(AllowedNamespaces namespace:AllowedNamespaces.values()){ - - + + //JSON interpretation - try { + try { //Getting the JSON formatted String to process. - allPages =Page.getAllPages(namespace.getValue()); + allPages =Page.getAllPages(namespace.getValue()); JSONObject js=new JSONObject(allPages); JSONObject js2=js.getJSONObject("query"); JSONArray arr=js2.getJSONArray("allpages"); JSONObject currentJsonObject; - + //Storing all the pages in a particular namespace - + for(int i=0;i