private static void addCommunitiesInPlace( LinkedList<GraphCommunity> accumulatedCommunities, List<GraphCommunity> newCommunities) { for (GraphCommunity newCommunity : newCommunities) { addCommunityInPlace(accumulatedCommunities, newCommunity); } }
@Override public int hashCode() { return (getHash(_communities)); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (null == obj) return false; if (!(obj instanceof GraphAnalyticsRecord)) return false; GraphAnalyticsRecord that = (GraphAnalyticsRecord) obj; if (this.getNumCommunities() != that.getNumCommunities()) { return false; } else if ((this.getCommunities()!= null) && (!listsEqual(this.getCommunities(), that.getCommunities()))) { return false; } else { return true; } }
/** * Combine two records. */ public static GraphAnalyticsRecord addRecords( GraphAnalyticsRecord... records) { if (null == records || 0 == records.length) return null; int numCommunities = records[0].getNumCommunities(); LinkedList<GraphCommunity> communities = new LinkedList<>(records[0].getCommunities()); for (int i = 1; i < records.length; ++i) { numCommunities += records[i].getNumCommunities(); addCommunitiesInPlace(communities, records[i].getCommunities()); } return new GraphAnalyticsRecord(numCommunities, communities); }
/** * Add a graph community to an existing record, summing counts as needed, and keeping * the MAX_COMMUNITIES largest communities. */ public static GraphAnalyticsRecord addCommunityToRecord( GraphAnalyticsRecord record, GraphCommunity newCommunity) { if (null == record) return null; int numCommunities = record.getNumCommunities() + 1; LinkedList<GraphCommunity> accumulatedCommunities = new LinkedList<>(record.getCommunities()); addCommunityInPlace(accumulatedCommunities, newCommunity); return new GraphAnalyticsRecord(numCommunities, accumulatedCommunities); }
@Test public void testInterEdgeToCommunity () { GraphCommunity a = _sampleCommunity; GraphEdge e1 = new GraphEdge(987L, 0.1, 0.2, 999L); GraphCommunity b = a; b.addInterEdgeToCommunity(e1); List<GraphEdge> edges = Arrays.asList(e1, new GraphEdge(0L, 4.3, 2.1, 5L), new GraphEdge(43L, 5.6, 7.8, 3L)); GraphCommunity c = new GraphCommunity(_hierLevel, _id, _coords, _radius, _degree, _numNodes, _metadata, _bIsPrimaryNode, _parentID, _parentCoords, _parentRadius, _statsList, edges, _intraEdges); Assert.assertEquals(c, b); }
@Test public void testIntraEdgeToCommunity () { GraphCommunity a = _sampleCommunity; GraphEdge e2 = new GraphEdge(988L, 0.11, 0.22, 1L); GraphCommunity b = a; b.addIntraEdgeToCommunity(e2); List<GraphEdge> edges = Arrays.asList(new GraphEdge(2L, 4.2, 2.0, 6L), new GraphEdge(44L, 5.5, 7.7, 4L), e2); GraphCommunity c = new GraphCommunity(_hierLevel, _id, _coords, _radius, _degree, _numNodes, _metadata, _bIsPrimaryNode, _parentID, _parentCoords, _parentRadius, _statsList, _interEdges, edges); Assert.assertEquals(c, b); }
@Test public void testCommunityToRecord () { GraphAnalyticsRecord a = new GraphAnalyticsRecord(1, Arrays.asList(_sampleCommunity)); GraphCommunity community_b = new GraphCommunity(_hierLevel, 456L, new Pair<Double, Double>(3.3, 4.4), 3.4, 4, 54, "blah4\tblah5", true, _parentID, _parentCoords, _parentRadius, _statsList, _interEdges, _intraEdges); GraphAnalyticsRecord c = new GraphAnalyticsRecord(2, Arrays.asList(community_b, _sampleCommunity)); Assert.assertEquals(c, GraphAnalyticsRecord.addCommunityToRecord(a, community_b)); }
@Test public void testEmptyRecordAggregation () { GraphAnalyticsRecord a = new GraphAnalyticsRecord(0, null); GraphCommunity community_b = new GraphCommunity(_hierLevel, 456L, new Pair<Double, Double>(3.3, 4.4), 3.4, 4, 54, "blah4\tblah5", true, _parentID, _parentCoords, _parentRadius, _statsList, _interEdges, _intraEdges); GraphAnalyticsRecord b = new GraphAnalyticsRecord(1, Arrays.asList(community_b)); Assert.assertEquals(b, GraphAnalyticsRecord.addRecords(a, b)); }
@Override protected void setEntryValue (GenericRecord avroEntry, GraphAnalyticsRecord rawEntry) { try { Schema entrySchema = getEntrySchema(); avroEntry.put("numCommunities", rawEntry.getNumCommunities()); avroEntry.put("communities", communityListToAvro(entrySchema, rawEntry.getCommunities())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
@Override protected GraphAnalyticsRecord getEntryValue (GenericRecord entry) { return new GraphAnalyticsRecord((Integer)entry.get("numCommunities"), communityListTOJava(entry)); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (null == obj) return false; if (!(obj instanceof GraphEdge)) return false; GraphEdge that = (GraphEdge) obj; if (this.getDstID() != that.getDstID()) { return false; } else if (!(this.getDstCoords().equals(that.getDstCoords()))) { return false; } else if ((this.getWeight() != that.getWeight())) { return false; } else { return true; } }
@Test public void testRecordsEqual() { GraphAnalyticsRecord a = new GraphAnalyticsRecord(1, Arrays.asList(_sampleCommunity)); Assert.assertEquals(_sampleRecord, a); }
/** * Add an inter-community edge to an existing community, summing counts as needed, and keeping * the MAX_EDGES largest edges. */ public void addInterEdgeToCommunity(GraphEdge newEdge) { LinkedList<GraphEdge> accumulatedEdges = new LinkedList<>(_interEdges); addEdgeInPlace(accumulatedEdges, newEdge); _interEdges = accumulatedEdges; }
private static void minInPlace(GraphCommunity accumulatedMin, List<GraphCommunity> newMin) { for (int i = 0; i < newMin.size(); ++i) { accumulatedMin.minInPlace(newMin.get(i)); } }
private static void maxInPlace(GraphCommunity accumulatedMin, List<GraphCommunity> newMin) { for (int i = 0; i < newMin.size(); ++i) { accumulatedMin.maxInPlace(newMin.get(i)); } }
private static <T> boolean listsEqual(List<T> a, List<T> b) { if (null == a) return null == b; if (null == b) return false; if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); ++i) { if (!objectsEqual(a.get(i), b.get(i))) return false; } return true; }
private static <T> boolean listsEqual(List<T> a, List<T> b) { if (null == a) return null == b; if (null == b) return false; if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); ++i) { if (!objectsEqual(a.get(i), b.get(i))) return false; } return true; } }
@Override protected TileSerializer<List<GraphAnalyticsRecord>> create () throws ConfigurationException { return new GraphAnalyticsAvroSerializer(TileSerializerFactory.getCodecFactory(this)); }
/** * Add an intra-community edge to an existing community, summing counts as needed, and keeping * the MAX_EDGES largest edges. */ public void addIntraEdgeToCommunity(GraphEdge newEdge) { LinkedList<GraphEdge> accumulatedEdges = new LinkedList<>(_intraEdges); addEdgeInPlace(accumulatedEdges, newEdge); _intraEdges = accumulatedEdges; }