diff --git a/WikiRating/src/main/java/computations/BadgeGenerator.java b/WikiRating/src/main/java/computations/BadgeGenerator.java new file mode 100644 index 0000000..2aacac8 --- /dev/null +++ b/WikiRating/src/main/java/computations/BadgeGenerator.java @@ -0,0 +1,169 @@ +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; + + + +/** + * This class will calculate the Badges that will be assigned + * to the Pages based on the analysis of Page Rating distribution + */ + +public class BadgeGenerator { + + //These varibales 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_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 custome 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(); + + + String badgeName=""; + int currentPageID=0; + double currentPageRating=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); + + for(PageRatingData currentPage:pageList){ + badgeName=getBadgeName(currentPage.pageRating); + System.out.println(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeName); + } + + } + + /** + * This method will calculate the cutoff for the various badges + * @param pageList The Arraylist containg 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); + + } + + /** + * 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 String getBadgeName(double pageRating){ + + if(pageRating>=platinumBadgeRatingCutoff) + return "PLATINUM"; + + else if(pageRating>=goldBadgeRatingCutoff) + return "GOLD"; + + else if(pageRating>=silverBadgeRatingCutoff) + return "SILVER"; + + else if(pageRating>=bronzeBadgeRatingCutoff) + return "BRONZE"; + + else + return "STONE"; + } + +} diff --git a/WikiRating/src/main/java/controllers/Firstrun.java b/WikiRating/src/main/java/controllers/Firstrun.java index ae0c3df..1218108 100644 --- a/WikiRating/src/main/java/controllers/Firstrun.java +++ b/WikiRating/src/main/java/controllers/Firstrun.java @@ -1,81 +1,86 @@ 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.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.*; import main.java.utilities.AllowedNamespaces; /** * This class will be used to initialize the engine. * */ @Path("firstRun") public class Firstrun { @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====================="); Page.insertPages(); System.out.println("==================Page insertion over====================="); LinkPages.linkAll("@class","Page"); System.out.println("==================Page linking over====================="); User.insertAllUsers(); System.out.println("==================All Users inserted====================="); Revision.getAllRevisions("@class","Page"); System.out.println("==================Page Revisions over====================="); Pagerank.pageRankCompute(); System.out.println("==================Page rank over====================="); LinkUserContributions.linkAll(); System.out.println("==================All Users Linked====================="); RandomVoteGenerator.generateVotes(); System.out.println("==================All Versions voted====================="); NormalisedVotes.calculatePageVotes(); System.out.println("==================All Page Votes computed====================="); UserCredibility.getUserCredibility(); System.out.println("==================User Credibility computed====================="); Reliability.calculateReliability(); System.out.println("==================Vote Reliability computed====================="); PageRating.computePageRatings(); System.out.println("==================Page Ratings computed====================="); + new BadgeGenerator().generateBadges(); + System.out.println("==================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/Secondrun.java b/WikiRating/src/main/java/controllers/Secondrun.java index 1598176..ccee30e 100644 --- a/WikiRating/src/main/java/controllers/Secondrun.java +++ b/WikiRating/src/main/java/controllers/Secondrun.java @@ -1,60 +1,63 @@ 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.computations.BadgeGenerator; import main.java.computations.NormalisedVotes; import main.java.computations.Reliability; import main.java.models.AddNewPages; import main.java.models.User; /** * 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 { @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====================="); AddNewPages.checkForPages(); System.out.println("==================Checked for any new pages,revisions and linked the user contributions and made backlinks====================="); NormalisedVotes.calculatePageVotes(); System.out.println("==================Calculated new page votes====================="); Reliability.calculateReliability(); System.out.println("==================Calculated new reliabilities====================="); + 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/models/AddNewPages.java b/WikiRating/src/main/java/models/AddNewPages.java index d9f1062..05ab716 100644 --- a/WikiRating/src/main/java/models/AddNewPages.java +++ b/WikiRating/src/main/java/models/AddNewPages.java @@ -1,285 +1,286 @@ 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; /** * 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 { /** * 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 { //Getting the JSON formatted String to process. 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