/*
* Copyright 2013 University of Southern California
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.package edu.usc.goffish.gopher.sample;
*/
package edu.usc.goffish.gopher.sample;
import edu.usc.goffish.gofs.ISubgraph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.io.*;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.*;
import java.lang.*;
import edu.usc.goffish.gofs.ISubgraphInstance;
import edu.usc.goffish.gofs.ISubgraphTemplate;
import edu.usc.goffish.gofs.ITemplateEdge;
import edu.usc.goffish.gofs.ITemplateVertex;
import edu.usc.goffish.gofs.Property;
import edu.usc.goffish.gofs.PropertySet;
import edu.usc.goffish.gopher.api.GopherSubGraph;
import edu.usc.goffish.gopher.api.SubGraphMessage;
public class Country extends GopherSubGraph {
// public ITemplateVertex vertex;
private static PropertySet vertexSet;
private final static String VERTEX_VAL ="label";
private final static String EDGE_VAL ="iteration";
private static PropertySet edgeSet;
// private ISubgraphTemplate template;
private ISubgraphInstance currentInstance;
private Iterator<? extends ISubgraphInstance> instanceIterator;
String label_property = "label";
//@Override
public void compute(List<SubGraphMessage> subGraphMessages) {
Set<String> myset = new HashSet<String>();
for(ITemplateVertex v : subgraph.vertices()){
System.out.println("printing vertex id " + v.getId());
}
if (getIteration() == 0 && getSuperStep() == 0) {
try{
List<Property> vertexProperties = new ArrayList<Property>();
//vertexProperties.add(subgraph.getProperties());
//for(Property s : vertexProperties ) System.out.println(" properties are : " +s);
if((subgraph.getVertexProperties()).isEmpty())System.out.println(" Vertex properties are NULL ");
vertexProperties.add(subgraph.getVertexProperties().getProperty(VERTEX_VAL));
for(String s : subgraph.getVertexProperties().propertyNames() ) System.out.println("vertex properties names: " +s);
for (Property p:vertexProperties)
System.out.println("printing vertex properties " + p);
vertexSet = new PropertySet(vertexProperties);
List<Property> edgeProperties = new ArrayList<Property>(0);
//properties.add(subgraph.getEdgeProperties().getProperty(EDGE_VAL)); //int
edgeSet = new PropertySet(edgeProperties);
instanceIterator = subgraph.getInstances(0,
Long.MAX_VALUE, vertexSet,edgeSet, false).iterator();
currentInstance = instanceIterator.hasNext() ? instanceIterator.next():null;
System.out.println("Instance is "+ currentInstance);
}catch(IOException e){System.out.println(e);}
}
String pattern ="book";
//template=getTemplate();
if(getIteration() == 0 && getSuperStep() == 0) {
for(ITemplateVertex v : subgraph.vertices()){
if(!(v.isRemote())) {
System.out.println("Processing vertex id : " + v.getId());
for(ITemplateEdge e : v.outEdges()){
System.out.println("Processing edge id : " + e.getId());
ITemplateVertex SinkVertex = e.getSink(v);
if((!(SinkVertex.isRemote())) &&((String)currentInstance.getPropertiesForVertex(SinkVertex.getId()).getValue(label_property)).equals(pattern)) {
// add to remote set ...
System.out.println("Processing Sink vertex with label book id : " + SinkVertex.getId());
String name =(String)currentInstance.getPropertiesForVertex(v.getId()).getValue(VERTEX_VAL);
System.out.println("Book title is : " + name );
myset.add(name);
}
//long remoteSubgraphId = subgraph.getVertex(v.getId()).getRemoteSubgraphId();
//System.out.println(remoteSubgraphId);
//myset.add(remoteSubgraphId);
}
}
} int i=0;
String[] str =new String[myset.size()];
String msg = "book title ";
for(String l:myset){
msg = msg + l+":";
System.out.println("message contents are : " + msg );
}
SubGraphMessage msg1 = new SubGraphMessage(msg.getBytes());
sendMessage(1,msg1);
} else if (getIteration() == 0 && getSuperStep() == 1){
if(partition.getId() == 1){
Iterator<ISubgraph> itsub = partition.iterator();
System.out.println("this is partition 1 : " );
int max = -1;
long maxSubId = -1;
while(itsub.hasNext()){
ISubgraph s = itsub.next();
if(s.numVertices() > max){
maxSubId = s.getId();
max = s.numVertices();
}
}
if(subgraph.getId() == maxSubId) {
System.out.println("max sub Id is : " + maxSubId );
//String data = new String(subGraphMessages.get(0).getData());
//String[] parts = data.split(":");
//System.out.println("received msg" + parts[1]);
// for (String message : subGraphMessages) {
//String[] tokens = message.split(":");
//System.out.println(tokens[1]);
try{
Path logRootDir = Paths.get(".");
Path filepath = logRootDir.resolve("Book.grp");
File file = new File(filepath.toString());
PrintWriter writer = new PrintWriter(file);
String file_string = "";
for(SubGraphMessage msg : subGraphMessages){
// writer.println((msg.getData().toString()));
System.out.println("writing message contents : " +( msg.getData().toString()) );
byte[] _bytes = msg.getData();
for(int i = 0; i < _bytes.length; i++)
{
file_string += (char)_bytes[i];
}
System.out.println("After conversion message contents : " +file_string );
writer.println(file_string);
}
writer.flush();
writer.close();
}catch(Exception e){System.out.println(e);}
}
}
voteToHalt();
}
}
}