Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prediction;
- import java.io.DataInput;
- import java.io.DataOutput;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.SortedSet;
- import java.util.TreeSet;
- import org.apache.hadoop.io.Writable;
- public class PredictionVertexValue implements Writable{
- private long stepCount;
- /** holds two step neighbors and their first step neighbors */
- private HashMap<Long, Set<Long>> neighbors;
- private Set<Long> myNeighbors;
- private SortedSet<LinkScore> linkScore;
- private HashMap<Long, Double> multiLables;
- private HashMap<Long, Integer> communitySizes;
- private HashMap<Long, HashMap<Long, Double>> nodeCommunityInfo;
- public PredictionVertexValue() {
- stepCount = 0;
- neighbors = new HashMap<Long, Set<Long>>(10);
- linkScore = new TreeSet<LinkScore>();
- multiLables = new HashMap<Long, Double>();
- communitySizes = new HashMap<Long, Integer>();
- nodeCommunityInfo =new HashMap<Long, HashMap<Long, Double>>();
- }
- public PredictionVertexValue(HashMap<Long, Double> comLabels,
- HashMap<Long, Integer> comSizes) {
- this();
- this.communitySizes = comSizes;
- this.multiLables = comLabels;
- }
- public void addLinkScore(long targetNode, double score) {
- LinkScore ls = new LinkScore(targetNode, score);
- linkScore.add(ls);
- }
- public double getLinkScore(long target) {
- LinkScore ls = new LinkScore(target, 0);
- if (linkScore.contains(ls)) {
- Iterator<LinkScore> iter = linkScore.iterator();
- for (LinkScore ls2 = iter.next(); iter.hasNext(); iter.next()) {
- if (target == ls2.getTargetNode()) {
- return ls2.getScore();
- }
- }
- }
- return -1;
- }
- public HashMap<Long, Double> getMultiLabels() {
- return multiLables;
- }
- public HashMap<Long, Integer> getCommunitySizes() {
- return communitySizes;
- }
- public void setMyNeighborSet(Set<Long> set) {
- myNeighbors = set;
- }
- public Set<Long> getMyNeighborSet() {
- return myNeighbors;
- }
- public void addTwoStepNeighborSet(long id, Set<Long> myNeighbors) {
- neighbors.put(id, myNeighbors);
- }
- /**
- * Returns a list of twostep neighbors
- * @return
- */
- public List<Long> getTwoStepNeighbors() {
- return new LinkedList<>(neighbors.keySet());
- }
- /**
- * Returns the set of friends of a two step neighbor
- * @param id
- * @return
- */
- public Set<Long> getFriendsOf(Long id) {
- return neighbors.get(id);
- }
- public long getStepCount() {
- return stepCount;
- }
- public void setCurrentStepDone() {
- stepCount++;
- }
- public void addNeighborCommunityInfo(long neighbor,
- HashMap<Long, Double> multiLables) {
- nodeCommunityInfo.put(neighbor, multiLables);
- }
- public HashMap<Long, Double> getNeighborCommunityInfo(long neighbor) {
- return nodeCommunityInfo.get(neighbor);
- }
- @Override
- public void write(DataOutput out) throws IOException {
- // write neighbors
- out.writeInt(neighbors.size());
- for (Entry<Long, Set<Long>> entry : neighbors.entrySet()) {
- out.writeLong(entry.getKey());
- out.writeInt(entry.getValue().size());
- for(Long l : entry.getValue()) {
- out.writeLong(l);
- }
- }
- // write myNeigbors
- out.writeInt(myNeighbors.size());
- for (Long l : myNeighbors) {
- out.writeLong(l);
- }
- // write linkScore
- out.writeInt(linkScore.size());
- for (LinkScore ls : linkScore) {
- ls.write(out);
- }
- // write multiLables
- out.writeInt(multiLables.size());
- for (Entry<Long, Double> entry : multiLables.entrySet()){
- out.writeLong(entry.getKey());
- out.writeDouble(entry.getValue());
- }
- // write communitySizes
- out.writeInt(communitySizes.size());
- for (Entry<Long, Integer> entry : communitySizes.entrySet()){
- out.writeLong(entry.getKey());
- out.writeInt(entry.getValue());
- }
- // write nodeCommunityInfo
- out.writeInt(nodeCommunityInfo.size());
- for (Entry<Long, HashMap<Long, Double>> entry : nodeCommunityInfo.entrySet()){
- out.writeLong(entry.getKey());
- out.writeInt(entry.getValue().size());
- for (Entry<Long, Double> entry2 : entry.getValue().entrySet()){
- out.writeLong(entry2.getKey());
- out.writeDouble(entry2.getValue());
- }
- }
- }
- @Override
- public void readFields(DataInput in) throws IOException {
- // read neighbors
- int size = in.readInt();
- neighbors = new HashMap<Long, Set<Long>>(size);
- for (int i = 0; i < size; i++) {
- long key = in.readLong();
- int size2 = in.readInt();
- Set<Long> nSet = new HashSet<Long>(size2);
- for (int j = 0; j < size2; j++) {
- long l = in.readLong();
- nSet.add(l);
- }
- neighbors.put(key, nSet);
- }
- // read myNeigbors
- size = in.readInt();
- myNeighbors = new HashSet<Long>(size);
- for (int i = 0; i < size; i++) {
- myNeighbors.add(in.readLong());
- }
- // read linkScore
- size = in.readInt();
- linkScore = new TreeSet<LinkScore>();
- for (int i = 0; i < size; i++) {
- LinkScore ls = new LinkScore();
- ls.readFields(in);
- linkScore.add(ls);
- }
- // read multiLables
- size = in.readInt();
- multiLables = new HashMap<Long, Double>(size);
- for (int i = 0; i < size; i++) {
- long key = in.readLong();
- double value = in.readDouble();
- multiLables.put(key, value);
- }
- // read communitySizes
- size = in.readInt();
- communitySizes = new HashMap<Long, Integer>(size);
- for (int i = 0; i < size; i++) {
- long key = in.readLong();
- int value = in.readInt();
- communitySizes.put(key, value);
- }
- // read nodeCommunityInfo
- size = in.readInt();
- nodeCommunityInfo = new HashMap<Long, HashMap<Long,Double>>(size);
- for (int i = 0; i < size; i++) {
- long key = in.readLong();
- int size2 = in.readInt();
- HashMap<Long, Double> tmp = new HashMap<Long, Double>(size2);
- for (int j = 0; j < size2; j++) {
- long key2 = in.readLong();
- double value = in.readDouble();
- tmp.put(key2, value);
- }
- nodeCommunityInfo.put(key, tmp);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement