package edu.usc.goffish.gopher.sample; /* * 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; * * */ import edu.usc.goffish.gofs.*; import edu.usc.goffish.gopher.api.*; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import java.io.FileNotFoundException; /*** * Input is given as entity1:entity2 * Finds the common vertex between vertex given by entity 1 and entity 2 * * @Varshitha * */ public class Common2 extends GopherSubGraph{ private final String ENTITY_ATT="label"; private final String RELATION_ATT="relation"; List<Edgepair> edgepath=new ArrayList<Edgepair>(); private ISubgraphInstance currentInstance; @Override public void compute(List<SubGraphMessage> messageList){ boolean en1=false; boolean en2=false; long entity1_vertexid=-1; long entity2_vertexid=-1; //System.out.println("Checkpoint 1"); if(getSuperStep()==0) { if(messageList.size()==0){ throw new RuntimeException("Entity 1 and Entity 2 missing"); //voteToHalt(); } else{ String message= new String(messageList.get(0).getData()); String[] msg=message.split(":"); String entity1=msg[0]; String entity2=msg[1]; String e1=entity1.trim().replace(" ","_").toLowerCase(); String e2=entity2.trim().replace(" ","_").toLowerCase(); System.out.println("ENTITY 1"+e1); System.out.println("ENTITY 2"+e2); for (ITemplateVertex vert: subgraph.vertices()){ String props=""; try{ props=(String) subgraph.getInstances(0,163,subgraph.getVertexProperties(),subgraph.getEdgeProperties(),false).iterator().next().getPropertiesForVertex(vert.getId()).getValue(ENTITY_ATT); } catch(IOException e){ e.printStackTrace();} if(props.equals(e1)){ System.out.println("ENTITY 1 match found"+e1); System.out.println("ENTITY 1 match found"+e1); entity1_vertexid=vert.getId(); String msg1="s:"+entity1_vertexid; SubGraphMessage subGraphMessage = new SubGraphMessage(msg1.getBytes()); subGraphMessage.setTargetSubgraph(subgraph.getId()); sendMessage(subGraphMessage); en1=true; } if(props.equals(e2)){ System.out.println("ENTITY 2 match found"+e2); System.out.println("ENTITY 2 match found"+e2); entity2_vertexid=vert.getId(); String msg2="d:"+entity2_vertexid; SubGraphMessage subGraphMessage = new SubGraphMessage(msg2.getBytes()); subGraphMessage.setTargetSubgraph(subgraph.getId()); sendMessage(subGraphMessage); en2=true; } }} if(en1 && en2){ //System.out.println("Both in the same graph"); List<String> edges_with1=allEdges(entity1_vertexid); System.out.println("Entity 1 vertex id"+entity1_vertexid); for(String edge1:edges_with1){ long sourceId=Long.parseLong(edge1.split(":")[0]); long sinkId=Long.parseLong(edge1.split(":")[1]); long midVertex= (sourceId==entity1_vertexid) ? sinkId : sourceId; List<String> midVertexEdges=allEdges(midVertex); for(String edge2:midVertexEdges) { sourceId=Long.parseLong(edge2.split(":")[0]); sinkId=Long.parseLong(edge2.split(":")[1]); long finalVertex = (sourceId==midVertex) ? sinkId : sourceId; if (finalVertex==entity2_vertexid){ edgepath.add(new Edgepair(edge1,edge2)); } } } //voteToHalt(); } else if(en1||en2) { System.out.println("Both not in the same graph"); long vert= (en1)? entity1_vertexid : entity2_vertexid; List<String> edges_with1=allEdges(vert); for(String edge1:edges_with1){ long sourceId=Long.parseLong(edge1.split(":")[0]); long sinkId=Long.parseLong(edge1.split(":")[1]); long midVertex= (sourceId==entity1_vertexid) ? sinkId : sourceId; if(subgraph.getVertex(midVertex).isRemote()){ long remote_subgraphId=subgraph.getVertex(midVertex).getRemoteSubgraphId(); String msg3="1:"+edge1;//send it to remote subgraph SubGraphMessage subGraphMessage3 = new SubGraphMessage(msg3.getBytes()); subGraphMessage3.setTargetSubgraph(remote_subgraphId); sendMessage(subGraphMessage3); } else{ List<String> edges_with2=allEdges(midVertex); for (String edge2:edges_with2){ long sourceId2=Long.parseLong(edge2.split(":")[0]); long sinkId2=Long.parseLong(edge2.split(":")[1]); long endVertex= (sourceId==midVertex) ? sinkId2 : sourceId2; if(subgraph.getVertex(endVertex).isRemote()){ String msg4="2:"+edge1+":"+edge2; long remote_subgraphId=subgraph.getVertex(endVertex).getRemoteSubgraphId(); SubGraphMessage subGraphMessage4 = new SubGraphMessage(msg4.getBytes()); subGraphMessage4.setTargetSubgraph(remote_subgraphId); sendMessage(subGraphMessage4); } } } } } //end of first superstep } if(getSuperStep()==1||getSuperStep()==2) { List<String> singleedge=new ArrayList<String>(); List<String> doubleedge=new ArrayList<String>(); for(SubGraphMessage submsg:messageList){ String msg=new String(submsg.getData()); if(msg.startsWith("s")){ en1=true; entity1_vertexid=Long.parseLong(msg.split(":")[1]); String msg1="s:"+entity1_vertexid; SubGraphMessage subGraphMessage = new SubGraphMessage(msg1.getBytes()); subGraphMessage.setTargetSubgraph(subgraph.getId()); sendMessage(subGraphMessage); } if(msg.startsWith("d")){ en2=true; entity2_vertexid=Long.parseLong(msg.split(":")[1]); String msg2="d:"+entity2_vertexid; SubGraphMessage subGraphMessage = new SubGraphMessage(msg2.getBytes()); subGraphMessage.setTargetSubgraph(subgraph.getId()); sendMessage(subGraphMessage); } if(msg.startsWith("1")){ singleedge.add(msg); } if(msg.startsWith("2")){ doubleedge.add(msg); } } if(en1||en2){ long id=(en1) ? entity1_vertexid : entity2_vertexid; for(String s:doubleedge){ long sour =Long.parseLong(s.split(":")[6]); long sin =Long.parseLong(s.split(":")[7]); if((sour==id)||(sin==id)) { String[] splitmsg=s.split(":"); String edge1=splitmsg[1]+":"+splitmsg[2]+":"+splitmsg[3]+":"+splitmsg[4]+":"+splitmsg[5]; String edge2=splitmsg[6]+":"+splitmsg[7]+":"+splitmsg[8]+":"+splitmsg[9]+":"+splitmsg[10]; edgepath.add(new Edgepair(edge1,edge2)); } } } else if (singleedge.size()>0) { for(String s:singleedge){ String [] splmsg=s.split(":"); String edge1=splmsg[1]+":"+splmsg[2]+":"+splmsg[3]+":"+splmsg[4]+":"+splmsg[5]; long sourceId_=Long.parseLong(s.split(":")[1]); long sinkId_=Long.parseLong(s.split(":")[2]); long midVertex= (subgraph.getVertex(sourceId_).isRemote()) ? sinkId_ : sourceId_ ; List<String> edges_with2=allEdges(midVertex); for (String edge2:edges_with2){ long sourceId2=Long.parseLong(edge2.split(":")[0]); long sinkId2=Long.parseLong(edge2.split(":")[1]); long endVertex= (sourceId2==midVertex) ? sinkId2 : sourceId2; long remote_subgraphId=subgraph.getVertex(endVertex).getRemoteSubgraphId(); if(subgraph.getVertex(endVertex).isRemote()){ String msg4="2:"+edge1+":"+edge2; SubGraphMessage subGraphMessage4 = new SubGraphMessage(msg4.getBytes()); subGraphMessage4.setTargetSubgraph(remote_subgraphId); sendMessage(subGraphMessage4); } } } } } try{ FileWriter file = new FileWriter("randomWalk.txt",true); //file.write("Hello\n"); if (edgepath.size()>0){ for(Edgepair path: edgepath) {file.write("\n"+path.edge_1+"\t"+path.edge_2); }} file.close(); } catch (IOException e) { e.printStackTrace(); } voteToHalt(); //end of compute } private class Edgepair{ public String edge_1; public String edge_2; public Edgepair(String e1,String e2){ edge_1=e1; edge_2=e2; } } private ArrayList<String> allEdges(long vertexId){ ArrayList<String> edgelist=new ArrayList<String>(); String edge_info; Long source; Long sink; Long edgeid; for(ITemplateEdge edge:subgraph.edges()) { source=edge.getSource().getId(); sink=edge.getSink().getId(); edgeid=edge.getId(); if((source==vertexId)||(sink==vertexId)){ try{ currentInstance= subgraph.getInstances(0,163,subgraph.getVertexProperties(),subgraph.getEdgeProperties(),false).iterator().next(); edge_info=source+":"+sink+":"+(String)currentInstance.getPropertiesForVertex(source).getValue(ENTITY_ATT) +":"+(String)currentInstance.getPropertiesForVertex(sink).getValue(ENTITY_ATT)+":"+(String)currentInstance.getPropertiesForEdge(edgeid).getValue("relation"); edgelist.add(edge_info); } catch(IOException e){ e.printStackTrace();} } } return edgelist; } }