public User(string username, int id, ModuloIAProlog.ModuloIaClient prologService, int numTagsTotal)
    {
        double minraio = 3;
        double maxraio = 7.5;

        Id       = id;
        Username = username;

        Profile = Graphs4Social_AR.User.LoadProfileByUser(username);

        IList <Graphs4Social_AR.Tag> tagList = Graphs4Social_AR.Tag.LoadAllByUsername(username);

        Tags = new List <string>();
        foreach (Graphs4Social_AR.Tag tag in tagList)
        {
            Tags.Add(tag.Nome);
        }

        Z = Convert.ToDouble(prologService.tamanhoRede(2, Username));
        //
        int numTags = Tags.Count;

        //
        Raio = minraio + ((maxraio - minraio) * (Convert.ToDouble(numTags) / Convert.ToDouble(numTagsTotal)));
    }
    public string caminhoMaisCurto(string username1, string username2)
    {
        ModuloIAProlog.ModuloIaClient prologService = new ModuloIAProlog.ModuloIaClient();

        string aux = prologService.caminhoCurto(username1, username2);

        prologService.Close();

        return(aux);
    }
    public string caminhoMaisCurto(string username1, string username2)
    {
        ModuloIAProlog.ModuloIaClient prologService = new ModuloIAProlog.ModuloIaClient();

            string aux = prologService.caminhoCurto(username1, username2);

            prologService.Close();

            return aux;
    }
    public static IList <Ligacao> trataListas(IList <string> ligacoes1, string username, ModuloIAProlog.ModuloIaClient prologService, int numTagsTotal)
    {
        IList <string> ligacoes = new List <string>();
        IList <string> tags     = new List <string>();



        foreach (string lig in ligacoes1)
        {
            if (!lig.Equals(""))
            {
                ligacoes.Add(lig);
            }
        }


        IList <User>   users     = new List <User>();
        IList <string> usernames = new List <string>();

        IList <Ligacao> ligacoesRetornadas = new List <Ligacao>();
        IList <Ligacao> ligacoesDirectas   = new List <Ligacao>();

        users.Add(new User(username, 0, prologService, numTagsTotal));
        usernames.Add(username);

        int id = 1;

        for (int k = 0; k < ligacoes.Count - 1; k = k + 3)
        {
            if (!usernames.Contains(ligacoes[k + 1]))
            {
                usernames.Add(ligacoes[k + 1]);
                users.Add(new User(ligacoes[k + 1], id, prologService, numTagsTotal));
                id++;
            }

            if (usernames.IndexOf(ligacoes[k]) == 0)
            {
                ligacoesDirectas.Add(new Ligacao(users[0], users[usernames.IndexOf(ligacoes[k + 1])], Convert.ToInt32(ligacoes[k + 2])));
            }
            else
            {
                ligacoesRetornadas.Add(new Ligacao(users[usernames.IndexOf(ligacoes[k])], users[usernames.IndexOf(ligacoes[k + 1])], Convert.ToInt32(ligacoes[k + 2])));
            }
        }



        Ligacao aux;

        //  Depois fazemos sort
        for (int i = 0; i < ligacoesDirectas.Count - 1; i++)
        {
            if (ligacoesDirectas[i].Forca < ligacoesDirectas[i + 1].Forca)
            {
                aux = ligacoesDirectas[i];
                ligacoesDirectas[i]     = ligacoesDirectas[i + 1];
                ligacoesDirectas[i + 1] = aux;
                i = 0;
            }
        }

        Ligacao[] arraytemp = new Ligacao[ligacoesDirectas.Count + ligacoesRetornadas.Count];


        ligacoesDirectas.CopyTo(arraytemp, 0);
        ligacoesRetornadas.CopyTo(arraytemp, ligacoesDirectas.Count);

        ligacoesRetornadas = arraytemp.ToList();

        ligacoesDirectas = new List <Ligacao>();

        foreach (Ligacao lig in ligacoesRetornadas)
        {
            if (lig.User2.Id > lig.User1.Id)
            {
                ligacoesDirectas.Add(lig);
            }
        }

        return(ligacoesDirectas);
    }
    //  Username apenas para grafo simples.
    //  Ambos usernames definidos para grafo de amigos em comum

    public Grafo(string username, string username1)
    {
        ModuloIAProlog.ModuloIaClient prologService = new ModuloIAProlog.ModuloIaClient();

        Users    = new List <User>();
        Ligacoes = new List <Ligacao>();
        int numTagsTotal = Graphs4Social_AR.Tag.LoadAllTag().Count;

        //

        if (!username1.Equals(""))
        {
            string pedidosuser  = prologService.redeNivel(2, username);
            string pedidosuser1 = prologService.redeNivel(2, username1);

            char[] separator = new char[3];

            separator[0] = ']';
            separator[1] = ',';
            separator[2] = '[';

            string[] arraytemp = new string[pedidosuser.Length + pedidosuser1.Length];

            IList <string> ligacoes = pedidosuser.Split(separator);

            IList <string> ligacoes1 = pedidosuser1.Split(separator);
            ligacoes.CopyTo(arraytemp, 0);

            ligacoes1.CopyTo(arraytemp, ligacoes.Count);



            ligacoes = new List <string>();

            for (int i = 0; i < arraytemp.Length; i++)
            {
                if (!ligacoes.Contains(arraytemp[i]) && !(arraytemp[i].Equals("") || arraytemp[i] == null))
                {
                    ligacoes.Add(arraytemp[i]);
                }
            }


            //  Carrega o user "dono" do grafo
            Users.Add(new User(username, 0, prologService, numTagsTotal));
            Users[0].Definido = true;
            Users[0].X        = -50;
            Users[0].Y        = -50;
            //  Carrega as ligações do user
            //  IList<string> ligacoes = RedeNivel2(userdono,U)

            Users.Add(new User(username1, 1, prologService, numTagsTotal));
            Users[1].Definido = true;
            Users[1].X        = 50;
            Users[1].Y        = 50;
        }
        else
        {
            //  Carrega as ligações do grafo do username a nível 3
            string pedidoLigacoes = prologService.grafoNivel3(username);

            pedidoLigacoes = pedidoLigacoes.Substring(1, pedidoLigacoes.Length - 2);



            char [] separator = new char [3];

            separator[0] = ']';
            separator[1] = ',';
            separator[2] = '[';

            IList <string> ligacoes = pedidoLigacoes.Split(separator);



            while (true)
            {
                if (!(ligacoes.Count < 2))
                {
                    Ligacoes = Ligacao.trataListas(ligacoes, username, prologService, numTagsTotal);
                    break;
                }

                pedidoLigacoes = prologService.grafoNivel3(username);

                pedidoLigacoes = pedidoLigacoes.Substring(1, pedidoLigacoes.Length - 2);

                ligacoes = pedidoLigacoes.Split(separator);

                prologService.Close();
            }



            foreach (Ligacao lig in Ligacoes)
            {
                if (!Users.Contains(lig.User1))
                {
                    Users.Add(lig.User1);
                }
                if (!Users.Contains(lig.User2))
                {
                    Users.Add(lig.User2);
                }
            }

            //  Carrega o user "dono" do grafo
            Users[0].Definido = true;
            Users[0].X        = 0;
            Users[0].Y        = 0;
        }

        prologService.Close();

        NrNos   = Users.Count;
        NrArcos = Ligacoes.Count;

        int  tentativas = 0;
        bool notDone    = true;
        bool flagBreak  = false;

        int    max   = 8 * NrArcos;
        Random valor = new Random();


        while (notDone)
        {
            for (int k = 0; k < Ligacoes.Count; k++)
            {
                if (!Ligacoes[k].User2.Definido)
                {
                    if (valor.NextDouble() >= 0.5)
                    {
                        Ligacoes[k].User2.X = valor.Next(10, max) * -1;
                    }
                    else
                    {
                        Ligacoes[k].User2.X = valor.Next(10, max);
                    }
                    //
                    if (valor.NextDouble() >= 0.5)
                    {
                        Ligacoes[k].User2.Y = valor.Next(10, max) * -1;
                    }
                    else
                    {
                        Ligacoes[k].User2.Y = valor.Next(10, max);
                    }
                }
                else
                {
                    tentativas = 49;
                }


                for (int j = 0; j < NrArcos; j++)
                {
                    if (!(tentativas >= 50))
                    {
                        if (!(Ligacoes[j].User1.Id == Ligacoes[k].User1.Id && Ligacoes[j].User2.Id == Ligacoes[k].User2.Id))
                        {
                            if (intersecta(Ligacoes[j].User1, Ligacoes[j].User2, Ligacoes[k].User1, Ligacoes[k].User2))
                            {
                                tentativas++;
                                flagBreak = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (tentativas >= 50)
                {
                    flagBreak = false;
                    break;
                }
                else if (!flagBreak)
                {
                    Ligacoes[k].User2.Definido = true;
                }
                else
                {
                    flagBreak = false;
                    k--;
                }
            }
            if (!(tentativas >= 50))
            {
                notDone = false;
            }
            else
            {
                tentativas = 0;
                for (int j = 0; j < NrNos; j++)
                {
                    Users[j].Definido = false;
                }
            }
        }
    }
    //  Username apenas para grafo simples.
    //  Ambos usernames definidos para grafo de amigos em comum
    public Grafo(string username,string username1)
    {
        ModuloIAProlog.ModuloIaClient prologService = new ModuloIAProlog.ModuloIaClient();

        Users = new List<User>();
        Ligacoes = new List<Ligacao>();
        int numTagsTotal = Graphs4Social_AR.Tag.LoadAllTag().Count;
        //

        if (!username1.Equals(""))
        {
            string pedidosuser = prologService.redeNivel(2, username);
            string pedidosuser1 = prologService.redeNivel(2, username1);

            char[] separator = new char[3];

            separator[0] = ']';
            separator[1] = ',';
            separator[2] = '[';

            string[] arraytemp = new string[pedidosuser.Length + pedidosuser1.Length];

            IList<string> ligacoes = pedidosuser.Split(separator);

            IList<string> ligacoes1 = pedidosuser1.Split(separator);
            ligacoes.CopyTo(arraytemp, 0);

            ligacoes1.CopyTo(arraytemp, ligacoes.Count);

            ligacoes = new List<string>();

            for (int i = 0; i < arraytemp.Length; i++)
            {
                if (!ligacoes.Contains(arraytemp[i]) && !(arraytemp[i].Equals("") || arraytemp[i] == null))
                {
                    ligacoes.Add(arraytemp[i]);
                }

            }

            //  Carrega o user "dono" do grafo
            Users.Add(new User(username, 0, prologService,numTagsTotal));
            Users[0].Definido = true;
            Users[0].X = -50;
            Users[0].Y = -50;
            //  Carrega as ligações do user
            //  IList<string> ligacoes = RedeNivel2(userdono,U)

            Users.Add(new User(username1, 1, prologService,numTagsTotal));
            Users[1].Definido = true;
            Users[1].X = 50;
            Users[1].Y = 50;
        }
        else
        {

            //  Carrega as ligações do grafo do username a nível 3
            string pedidoLigacoes = prologService.grafoNivel3(username);

            pedidoLigacoes = pedidoLigacoes.Substring(1, pedidoLigacoes.Length - 2);

            char [] separator = new char [3];

            separator[0]=']';
            separator[1]=',';
            separator[2]='[';

            IList<string> ligacoes = pedidoLigacoes.Split(separator);

            while(true){

                if (!(ligacoes.Count < 2))
                {
                    Ligacoes = Ligacao.trataListas(ligacoes, username, prologService, numTagsTotal);
                    break;
                }

                pedidoLigacoes = prologService.grafoNivel3(username);

                pedidoLigacoes = pedidoLigacoes.Substring(1, pedidoLigacoes.Length - 2);

                ligacoes = pedidoLigacoes.Split(separator);

                prologService.Close();
            }

            foreach(Ligacao lig in Ligacoes)
            {
                if(!Users.Contains(lig.User1)){
                    Users.Add(lig.User1);
                }
                if(!Users.Contains(lig.User2)){
                    Users.Add(lig.User2);
                }
            }

            //  Carrega o user "dono" do grafo
            Users[0].Definido = true;
            Users[0].X = 0;
            Users[0].Y = 0;
        }

        prologService.Close();

        NrNos = Users.Count;
        NrArcos = Ligacoes.Count;

        int tentativas = 0;
        bool notDone = true;
        bool flagBreak = false;

        int max = 8 * NrArcos;
        Random valor = new Random();

        while (notDone)
        {
            for(int k = 0;k<Ligacoes.Count;k++)
            {
                if (!Ligacoes[k].User2.Definido)
                {
                    if (valor.NextDouble() >= 0.5)
                    {
                        Ligacoes[k].User2.X = valor.Next(10, max) * -1;
                    }
                    else
                    {
                        Ligacoes[k].User2.X = valor.Next(10, max);
                    }
                    //
                    if (valor.NextDouble() >= 0.5)
                    {
                        Ligacoes[k].User2.Y = valor.Next(10, max) * -1;
                    }
                    else
                    {
                        Ligacoes[k].User2.Y = valor.Next(10, max);
                    }

                }
                else
                    tentativas = 49;

                for (int j = 0; j < NrArcos; j++)
                {
                    if (!(tentativas >= 50))
                    {
                        if (!(Ligacoes[j].User1.Id == Ligacoes[k].User1.Id && Ligacoes[j].User2.Id == Ligacoes[k].User2.Id))
                        {
                            if (intersecta(Ligacoes[j].User1, Ligacoes[j].User2, Ligacoes[k].User1, Ligacoes[k].User2))
                            {
                                tentativas++;
                                flagBreak = true;
                                break;
                            }

                        }
                    }
                    else
                        break;

                }
                if (tentativas >= 50)
                {
                    flagBreak = false;
                    break;
                }
                else if (!flagBreak)
                    Ligacoes[k].User2.Definido = true;
                else
                {
                    flagBreak = false;
                    k--;
                }
            }
            if (!(tentativas >= 50))
                notDone = false;
            else
            {
                tentativas = 0;
                for (int j = 0; j < NrNos; j++)
                {
                    Users[j].Definido = false;
                }
            }
        }
    }