Приведенный ниже код получает содержимое из файла и пытается создать граф отношений. У меня есть класс с методом, который читает содержимое из файла и возвращает ArrayList, где каждый элемент представляет строку текста. Я протестировал тот же метод, и он возвращает ArrayList всех строк, присутствующих в текстовом файле.
input.txt
Dangal / Aamir Khan / Fatima Sana
Sanju / Ranbir Kapoor / Dia Mirza
PK / Aamir Khan / Anushka Sharma
Munna Bhai MBBS / Sanjay Dutt / Arshad Warsi
Zindagi Na Milegi Dobara / Farhan Akhtar / Katrina Kaif
Однако, при обработке содержимого файла в приведенном ниже коде я получаю следующий вывод:
null
Total number of unique actors/actresses: 2
List of unique actors/actresses:
Aamir Khan , Fatima Sana,
Total number of unique movies: 2
List of unique movies:
Dangal , Sanju ,
Полагаю, я допустил некоторые логические ошибки.
public class GenerateGraph {
//List of all unique actors
private List<ActorVertex> allactors = new ArrayList<>();
public List<ActorVertex> getAllactors() {
return allactors;
}
//List of all unique movies
private List<MovieEdge> allmovies= new ArrayList<>();
public List<MovieEdge> getAllmovies() {
return allmovies;
}
public void parseFileContent() {
/* This method takes each line of the input.txt and creates corresponding vertices(actors) and Edges(movies) and links them to each other to construct the graph. */
ArrayList<String> lines = new FileInput().readFile();
try {
for(String line:lines) {
/* This entire loop iterates for each line in the input.txt */
MovieEdge me=null;
ActorVertex av1=null;
ActorVertex av2=null;
String[] elements = line.split("/"); //Splitting each line with delimiter '/' to extract movie and actor/actress names.
/* Note: For each line elements[0]=movie, elements[1]=actor1, elements[2]=actor2 */
//Creating and adding movie edge
if (allmovies.size()!=0) { //Assuming that this isn't the 1st iterations. Therefore, we need to ensure duplicate entries are not recorded.
for (MovieEdge m:allmovies) {
if (m.getName()==elements[0]) {
return;
}else {
me = new MovieEdge(elements[0]); //Movie does not exists. So new movie edge object is created.
allmovies.add(me); // added to master list for all movies
}
}
}else if (allmovies.size()==0){
me = new MovieEdge(elements[0]);
allmovies.add(me);
}
//Creating and adding actor vertices
if (allactors.size()!=0) { //Assuming that this isn't the 1st iterations. Therefore, we need to ensure duplicate entries are not recorded.
for (ActorVertex v:allactors) {
if (v.getName()==elements[1]) {
av1=v; // If actor already exists then no new actor object will be created. Instead we'll refer to the existing ones.
}else {
av1 = new ActorVertex(elements[1]); //actor does not exists. So new actor object is created.
allactors.add(av1); // added to master list for all actors
}
if (elements[2]!=null && elements[1]!=elements[2]) { /* Applies to situation where two successive actor names are the same or the second actor does not exists.
* In this case only 1 actor object will be created. */
if (v.getName()==elements[2]) { // check whether actor 2 already exists in the master list. If so use the existing one.
av2=v;
}else {
av2 = new ActorVertex(elements[2]); //Actor2 does not exists. So creating actor object.
allactors.add(av2); //added actor2 object to master list all actors.
}
}
}
}else if(allactors.size()==0 && elements[1]!=elements[2]){ /*Applies if this is the 1st iteration and master lists for movies and actors are empty.
*Also, once again checking for duplicates */
//creating new actor vertex objects and adding them to master list - all actors
av1 = new ActorVertex(elements[1]);
av2 = new ActorVertex(elements[2]);
allactors.add(av1);
allactors.add(av2);
}else if (allactors.size()==0 && elements[1]==elements[2]) { //If duplicate entries are found. Will create only 1 actor vertex object.
av1 = new ActorVertex(elements[1]);
allactors.add(av1);
}
/*** Joining the actor vertices with movie edge to construct the graph for each line ***/
/* Associating actors/actresses with movie in which they have played a role */
me.joinActorVertex(av1); //associating 1st actor/actress with its corresponding movie
if(av2!=null) { // checking if actor2 exists for the movie. If so the associate him/her with the movie.
me.joinActorVertex(av2);
}
/*Linking corresponding movie edge to actors */
av1.addMovieEdge(me);
if(av2!=null) { // Again, checking if actor2 exists for the movie. If so the associate him/her with the movie.
av2.addMovieEdge(me);
}
}
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Спасибо заранее. Ценю вашу помощь.