public static boolean mappingContainsVertexLeft(ArrayList<VertexPair> mapping, Vertex v) { for (VertexPair vp : mapping) { if (vp.getLeft().getID().equals(v.getID())) { return true; } } return false; }
/** * Finds the match between gateways, the decision is made based on the match of gateway * parents/children match, if the parent/child is also a gateway, then the decision is done * recursively * * @param g1Vertices - graph g1 vertices that need to be matched with graph g1 vertices * @param g2Vertices - graph g2 vertices * @param threshold - if node similarity is >= than threshold then these nodes are considered to * be matched. * @param stemmer - stemmer for wrord stemming, if == null, then english stemmer is used * @param lookParents - if == 0, then gateways are not matched, if == 1, then only parent are * looked, if == 2, then only children are looked * @return */ public static ArrayList<VertexPair> getMappingsGateways( ArrayList<Vertex> g1Vertices, ArrayList<Vertex> g2Vertices, double threshold, SnowballStemmer stemmer, int lookParents) { ArrayList<Vertex> g1Gateways = new ArrayList<Vertex>(); ArrayList<Vertex> g2Gateways = new ArrayList<Vertex>(); ArrayList<VertexPair> possibleMatches = new ArrayList<VertexPair>(); for (Vertex v : g1Vertices) { if (v.getType().equals(Vertex.Type.gateway)) { g1Gateways.add(v); } } for (Vertex v : g2Vertices) { if (v.getType().equals(Vertex.Type.gateway)) { g2Gateways.add(v); } } if (g1Gateways.size() == 0 || g2Gateways.size() == 0) { return possibleMatches; } int dimFunc = g1Gateways.size() > g2Gateways.size() ? g1Gateways.size() : g2Gateways.size(); double costs[][] = new double[dimFunc][dimFunc]; double costsCopy[][] = new double[dimFunc][dimFunc]; for (int i = 0; i < g1Gateways.size(); i++) { for (int j = 0; j < g2Gateways.size(); j++) { double edScore = 0; ArrayList<VertexPair> map; if (lookParents == 2) { map = getMappingsVetrex( g1Gateways.get(i).getChildren(), g2Gateways.get(j).getChildren(), threshold, stemmer, lookParents); for (VertexPair vp : map) { edScore += vp.getWeight(); } edScore = map.size() == 0 ? 0 : edScore / map.size(); } else if (lookParents == 1) { map = getMappingsVetrex( g1Gateways.get(i).getParents(), g2Gateways.get(j).getParents(), threshold, stemmer, lookParents); for (VertexPair vp : map) { edScore += vp.getWeight(); } edScore = map.size() == 0 ? 0 : edScore / map.size(); } if (edScore < threshold) edScore = 0; costs[i][j] = (-1) * edScore; } } for (int i = 0; i < costs.length; i++) { for (int j = 0; j < costs[0].length; j++) { costsCopy[i][j] = costs[i][j]; } } int[][] result = HungarianAlgorithm.computeAssignments(costsCopy); for (int i = 0; i < result.length; i++) { double pairCost = (-1) * costs[result[i][0]][result[i][1]]; if (result[i][0] < g1Gateways.size() && result[i][1] < g2Gateways.size() && pairCost > 0) { possibleMatches.add( new VertexPair(g1Gateways.get(result[i][0]), g2Gateways.get(result[i][1]), pairCost)); } } return possibleMatches; }