package pythonToJava; import java.sql.Types; import java.util.List; import com.hp.hpl.guess.Graph; import com.hp.hpl.guess.Guess; import com.hp.hpl.guess.Node; import com.hp.hpl.guess.Schema; public class Prestige { /** * Calculates a simple input domain for a node. Should be run on a directed * graph. * * @param node - * Node to find the input domain for * @param pathLimit -- * Maximum depth to search for input domain. A value of zero * means to use no limit * @return input domain for node */ public static int inputDomain(Node node, int pathLimit) { int prestige = 0; for (final Object o : Guess.getGraph().getNodes()) { Node node2 = (Node) o; List path = node2.unweightedShortestPath(node); int length = path.size(); if ((length > 0) && ((length <= pathLimit) || (pathLimit == 0))) { prestige++; } } return prestige; } /** * Finds the simple Proximity Prestige for a single node based on an * unlimited input domain * * @param node - * Node to find proximity prestige for. * @return */ public static double simpleProximityPrestige(Node node) { double domainSize = 0.0; double summedPathLength = 0.0; for (final Object o : Guess.getGraph().getNodes()) { Node node2 = (Node) o; List path = node2.unweightedShortestPath(node); int length = path.size(); if (length > 0) { domainSize++; summedPathLength += length; } } double average = 0.0; if (summedPathLength == 0) { return average; } average = summedPathLength / domainSize; return (domainSize / (Guess.getGraph().getNodes().size() - 1)) / average; } /** * Calculates input domain for every node in the graph and saves the results * as a new node field. Also calculates the average distance from the source * to every node in its input domain. * * The naming convention for the new fields is: For input domain: * "inputdomain#" where # is the path limit specified For average distance: * "averagedistance#" where # is the path limit specified * * @param pathLimit - * specifies the maximum path length included in input domain. 0 * means no limit */ public static void allInputDomain(int pathLimit) { Graph g = Guess.getGraph(); Schema s = g.getNodeSchema(); if (s.getField("inputdomain" + pathLimit) == null) { g.addNodeField("inputDomain" + pathLimit, Types.INTEGER, 0); g.addNodeField("averagedistance" + pathLimit, Types.DOUBLE, 0.0); for (final Object o : g.getNodes()) { Node node = (Node) o; int domainSize = 0; int summedPathLength = 0; for (final Object o2 : g.getNodes()) { Node node2 = (Node) o2; List path = node2.unweightedShortestPath(node); int length = path.size(); if (length > 0 && ((pathLimit == 0) || (length <= pathLimit))) { domainSize++; summedPathLength += length; } } node.__setattr__("inputdomain" + pathLimit, domainSize); double average = 0.0; if (summedPathLength != 0) { average = summedPathLength * 1.0 / domainSize; } node.__setattr__("averagedistance" + pathLimit, average); } } } /** * Uses allInputDomain to find and store the proximity prestige for each * node. Stores the value as a new node field. Naming convention for new * field: "proximityprestige#" where # is the specified maximum path length * to consider in the input domain * * @param pathLimit - * maximum path length to consider in the input domain. 0 means * no max. */ public static void allProximityPrestige(int pathLimit) { double proxPrestige = 0.0; Graph g = Guess.getGraph(); Schema s = g.getNodeSchema(); int numNodes = g.getNodes().size(); if (s.getField("proximityprestige" + pathLimit) == null) { g.addNodeField("proximityprestige" + pathLimit, Types.DOUBLE, 0.0); allInputDomain(pathLimit); for (final Object o : g.getNodes()) { Node node = (Node) o; proxPrestige = 0.0; if (((Double) node.__getattr__("averagedistance" + pathLimit)) .doubleValue() > 0) { proxPrestige = ((Integer) node.__getattr__("inputdomain" + pathLimit)).doubleValue(); proxPrestige = (proxPrestige * 1.0) / (numNodes - 1.0); proxPrestige = proxPrestige / (((Double) (node.__getattr__("averagedistance" + pathLimit))).doubleValue()); } node.__setattr__("proximityprestige" + pathLimit, proxPrestige); } } } }