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;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
public class Common2 extends GopherSubGraph{
private String ENTITY_ATT="label";
private String RELATION_ATT="relation";
public void compute(List<SubGraphMessage> messageList){
ArrayList<String[]> allQuery= new ArrayList();
String answerString="";
String remoteValue="";
String remoteEdge="";
String remoteSink="";
Long vid=0L;
HashMap<String, Long> dictIds=new HashMap();
HashMap<String, String> dictValues=new HashMap();
String props="";
if(getSuperStep()==0){
if(messageList.size()==0){
throw new RuntimeException("Query Missing.");
}
}
for(SubGraphMessage msg : messageList){
String message= new String(msg.getData());
String[] queryNanswer = message.split("!");
System.out.println("superstepNo"+getSuperStep());
String[] query_lines=queryNanswer[0].split("-");
if(queryNanswer.length>1){
answerString=queryNanswer[1];
remoteValue=queryNanswer[1];
remoteEdge=queryNanswer[2];
remoteSink=queryNanswer[3];
vid=Long.parseLong(queryNanswer[4]);
System.out.println("REMOTEVALUE "+ remoteValue );
System.out.println("REMOTESink "+ remoteSink);
}
for(String qline: query_lines){
String[] singleQuery= qline.split(":");
allQuery.add(singleQuery);
}
if(getSuperStep()==0){
String[] firstQueryLine=allQuery.get(0);
String value=".*";
if(!(firstQueryLine[0].startsWith("@"))){
value=firstQueryLine[0];
}
for(ITemplateVertex vert: subgraph.vertices()){
if(vert.isRemote());
else{
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.matches(value)){
//System.out.println("Seed vertex matched -- superstep 0"+props);
dictIds.put(firstQueryLine[0],vert.getId());
dictValues.put(firstQueryLine[0],props);
ArrayList<String[]> allQuery_copy= new ArrayList(allQuery);
String answerString_copy=new String(answerString);
HashMap<String, Long> dictIds_copy=new HashMap(dictIds);
HashMap<String, String> dictValues_copy=new HashMap(dictValues);
evaluateQuery(allQuery_copy, answerString_copy, dictIds_copy, dictValues_copy);
}
}
}
}
else{
try{
props=(String) subgraph.getInstances(0,163,subgraph.getVertexProperties(),subgraph.getEdgeProperties(),false).iterator().next().getPropertiesForVertex(vid).getValue(ENTITY_ATT);
}
catch(IOException e){
e.printStackTrace();
}
String remoteSinkValue=".*";
if(!remoteSink.startsWith("@")){
remoteSinkValue=remoteSink;
}
if(props.matches(remoteSinkValue)){
answerString=answerString+"\n"+remoteValue+"\t"+remoteEdge+"\t"+props;
dictIds.put(remoteSink,vid);
dictValues.put(remoteSink,props);
ArrayList<String[]> allQuery_copy= new ArrayList(allQuery);
String answerString_copy=new String(answerString);
HashMap<String, Long> dictIds_copy=new HashMap(dictIds);
HashMap<String, String> dictValues_copy=new HashMap(dictValues);
evaluateQuery(allQuery_copy, answerString_copy, dictIds_copy, dictValues_copy);
}
}
}
voteToHalt();
}
public void evaluateQuery(ArrayList<String[]> allQuery, String answerString, HashMap<String, Long> dictIds, HashMap<String, String> dictValues ){
String edgeProps="";
String valueProps="";
if(allQuery.size()==0){
printToFile(answerString);
}
else{
String[] lineQuery=allQuery.get(0);
System.out.println("Inside evluateQuery -->extracting line query"+Arrays.toString(lineQuery));
allQuery.remove(0);
System.out.println("after removing ");
String firstEntity=lineQuery[0];
String otherEntity=lineQuery[2];
if(dictValues.containsKey(firstEntity)||dictValues.containsKey(otherEntity)){
if(dictValues.containsKey(otherEntity)){
firstEntity=lineQuery[2];
otherEntity=lineQuery[0];
}
for(ITemplateEdge edge: subgraph.getVertex(dictIds.get(firstEntity)).outEdges()){
try{
edgeProps= (String) subgraph.getInstances(0,163,subgraph.getVertexProperties(),subgraph.getEdgeProperties(),false).iterator().next().getPropertiesForEdge(edge.getId()).getValue(RELATION_ATT);
}
catch(IOException e){
e.printStackTrace();
}
if(edgeProps.matches(lineQuery[1])){
System.out.println("EDge matched with edge -->"+edgeProps);
String otherValue=".*";
if(!otherEntity.startsWith("@")){
otherValue=otherEntity;
}
if(dictValues.containsKey(otherEntity)){
otherValue=dictValues.get(otherEntity);
}
ITemplateVertex otherVertex = edge.getSink();
if(otherVertex.isRemote()){
System.out.println("REmote vertex encountered");
long remoteSubId=otherVertex.getRemoteSubgraphId();
String msg2="";
ArrayList<String[]> allQuery_copy2=new ArrayList(allQuery);
for(String key:dictValues.keySet()){
allQuery_copy2=replaceByValue(allQuery_copy2,key,dictValues.get(key));
}
msg2=toStringQuery(allQuery_copy2)+"!"+answerString+"!"+dictValues.get(firstEntity)+"!"+edgeProps+"!"+otherEntity+"!"+otherVertex.getId();
System.out.println("Meassge sent"+msg2);
SubGraphMessage subGraphMessage = new SubGraphMessage(msg2.getBytes());
subGraphMessage.setTargetSubgraph(remoteSubId);
sendMessage(subGraphMessage);
}
else{
try{
valueProps= (String) subgraph.getInstances(0,163,subgraph.getVertexProperties(),subgraph.getEdgeProperties(),false).iterator().next().getPropertiesForVertex(otherVertex.getId()).getValue(ENTITY_ATT);
}
catch(IOException e){
e.printStackTrace();
}
if(valueProps.matches(otherValue)){
dictValues.put(otherEntity,valueProps);
dictIds.put(otherEntity, otherVertex.getId());
answerString=answerString+"\n"+dictValues.get(firstEntity)+"\t"+edgeProps+"\t"+valueProps;
System.out.println("All matched in the same subgraph + "+answerString);
String answerString_copy=new String(answerString);
ArrayList<String[]> allQuery_copy= new ArrayList(allQuery);
HashMap<String, Long> dictIds_copy=new HashMap(dictIds);
HashMap<String, String> dictValues_copy=new HashMap(dictValues);
evaluateQuery(allQuery_copy, answerString_copy, dictIds_copy, dictValues_copy);
}
}
}
}
}
}
}
public ArrayList<String[]> replaceByValue(ArrayList<String[]> originalQuery, String originalText, String newText){
ArrayList<String[]> answer = new ArrayList();
for(String[] eachLine : originalQuery){
for(int i=0;i<3;i++){
if(eachLine[i]==originalText){
eachLine[i]=newText;
}
}
answer.add(eachLine);
}
return answer;
}
public String toStringQuery(ArrayList<String[]> query){
String ans="";
for(String[] eachLine : query){
for(String eachEntity : eachLine){
ans=ans+eachEntity+":";
}
ans=ans.substring(0,ans.length()-1);
ans=ans+"-";
}
ans=ans.substring(0,ans.length()-1);
return ans;
}
public void printToFile(String s){
try{
FileWriter file = new FileWriter("queryResult.txt",true);
file.write(s+"\n----------|-|-|-|-|-|---------\n");
file.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}