IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Delphi Discussion :

Problème de récursivité


Sujet :

Delphi

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2002
    Messages : 109
    Points : 71
    Points
    71
    Par défaut Problème de récursivité
    Bonjour à tous,

    Je dois créer des rapports pour un logiciel de gestion de projets. Il y a deux tables importantes: Projets et Taches. Connaissant le projet, j'aimerais afficher toutes les tâches dans le même ordre qu'elle ont été créées (même principe qu'un arbre). Chacune des tâches peut avoir des sous-tâches, de là provient ma complexité car celles-ci doivent être ordonnées.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Projet
     Tâche 1
       Tâche 1.1
       Tâche 1.2
        Tâche 1.2.1
     Tâche 2
      Tâche 2.1
      Tâche 2.2
      Tâche 2.3
    La table utilisée est donc Taches. Pour chaque ligne j'ai donc les champs ProjetID, TacheID, Nom. ParentID, PrecedentID et SuivantID.

    En regardant les données, je vois bien que lorsqu'une tâche a comme ParentID la valeur -1, c'est qu'elle est à la racine directement du projet.

    Lorsqu'une tâche a comme PrecedentID la valeur -1, c'est que c'est la première sous-tâche (du projet ou d'une tâche) et quand le SuivantID a la valeur -1 c'est la dernière sous-tâche (du projet ou de la sous-tâche).

    J'essaie tant bien que mal de faire une représentation récursive de tout cela, mais ça ne fonctionne pas à merveille. Pour me balader de façon ordonnée
    dans l'arbre, j'ai décidé d'utiliser le ParentID et le PrecedentID sachant que -1 et -1 me donnait immédiatement la tête de mon arbre.

    Je veux afficher le tout dans un TTreeView pour le moment, je démarre donc ma fonction comme suit:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    tvwArbre.Items.Clear();
    afficher(iProjetID, -1, -1, -1, 0, nil, nil);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    function TForm1.afficher(iProjetID, iPrevParentID, iParentID, iPrecedentID, iSuivantID: Int64; tndPrevParent, tndParent: TTreeNode): TTreeNode;
     
    var
       orclqryRequete: TOracleQuery;
       Tache: TTache;
       tndNouveau: TTreenode;
     
    begin
     
       orclqryRequete := TOracleQuery.Create(Self);
       with orclqryRequete do
          begin
     
             Session:= orclssnSession;
     
             Close();
             SQL.Clear();
             SQL.Add('SELECT * ');
             SQL.Add('FROM Taches ');
             SQL.Add(' WHERE ProjetID = ' + IntToStr(iProjetID));
     
             if (iParentID <> 0) then
                SQL.Add('   AND ParentID = ' + IntToStr(iParentID));
     
             if (iPrecedentID <> 0) then
                SQL.Add('   AND PrecedentID = ' + IntToStr(iPrecedentID));
     
             Execute();
     
             if (not(Eof)) then
                begin
                   while (Not(Eof)) do
                      begin
     
                         with Tache do
                            begin
     
                               iProjetID := StrToInt64(FieldAsString('PROJETID'));
                               iTacheID := StrToInt64(FieldAsString('TACHEID'));
                               iParentID := StrToInt64(FieldAsString('PARENTID'));
                               iPrecedentID := StrToInt64(FieldAsString('PRECEDENTID'));
                               iSuivantID := StrToInt64(FieldAsString('SUIVANTID'));
                               strNom := FieldAsString('NOM');
     
                               tndNouveau := tvwArbre.Items.AddChild(tndParent, strNom);
     
                               tvwArbre.FullExpand();
     
                               tndParent := afficher(iProjetID, iParentID, iTacheID, -1, iSuivantID, tndParent, tndNouveau);
     
                               Next();
     
                            end;
     
                      end;
                end
             else
     
                if (iNextOid <> -1) then             
                      tndNouveau := afficher(iProjetID, iParentID, iPrevParentID, iParentID, 0, nil, tndPrevParent);
     
     
          end;
     
       FreeAndNil(orclqryRequete);
     
       Result := tndParent;
     
    end;
    Voilà, ça ne fonctionne pas à merveille, mon traitement bloque quand j'atteinds la fin d'une feuille... Je suis débutant en récursivité, merci de votre aide!

  2. #2
    Membre éprouvé

    Profil pro
    Inscrit en
    Mai 2003
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2003
    Messages : 582
    Points : 915
    Points
    915
    Par défaut
    question...
    d'où vient iNextOid ou est-il initialisé?

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2002
    Messages : 109
    Points : 71
    Points
    71
    Par défaut
    J'ai changé le code un peu pour le copier ici, iNextOid c'est iSuivantID en fait...

  4. #4
    Membre éprouvé

    Profil pro
    Inscrit en
    Mai 2003
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2003
    Messages : 582
    Points : 915
    Points
    915
    Par défaut
    Bon voila ce que ca donne

    Donnees dans la table:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    ProjetID	TacheID	Nom		ParentID PrecedentID	SuivantID
    500		1	Tache 01        -1	 -1		2
    500		2	Taches 01.01	1	  1		3
    500		3	Taches 01.02	1	  2		4
    500		4	Taches 01.03	1	  3		-1
    500		5	Taches 02	-1	  1		6
    500		6	Taches 02.01	5	  5		7
    500		7	Taches 02.02	5	  6		-1
    Debut:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    //*** Appele de la routine
    procedure TForm1.Button3Click(Sender: TObject);
    var
        tndProjet: TTreenode;
    begin
        tvwArbre.Items.Clear();
        tndProjet := tvwArbre.Items.AddChild(nil, 'nom_Projet');
        afficher(iProjetID,-1,-1,tndProjet,nil);
    end;
    Fonction recursive
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    //*** Procedure Recursive pour afficher les taches
    procedure TForm1.afficher(iProjetID, iParentID,iPrecedentID : Int64;
                             tndPrevParent, tndParent: TTreeNode;
    var
       orclqryRequete: TQuery;
       Tache: TTache;
       tndNouveau: TTreenode;
    begin
        orclqryRequete := TQuery.Create(Self);
        with orclqryRequete do
        begin
             Session:= orclssnSession;
            Close();
            SQL.Clear();
            SQL.Add('SELECT * ');
            SQL.Add('FROM MesTaches ');
            SQL.Add(' WHERE ProjetID = ' + IntToStr(iProjetID));
            SQL.Add('   AND ParentID = ' + IntToStr(iParentID));
            SQL.Add('   AND PrecedentID = ' + IntToStr(iPrecedentID));
            Execute();
                while (Not(Eof)) do
                begin
                    with Tache do
                    begin
                        iProjetID := StrToInt64(FieldAsString('PROJETID'));
                        iTacheID := StrToInt64(FieldAsString('TACHEID'));
                        iParentID := StrToInt64(FieldAsString('PARENTID'));
                        iPrecedentID := StrToInt64(FieldAsString('PRECEDENTID'));
                        iSuivantID := StrToInt64(FieldAsString('SUIVANTID'));
                        strNom := FieldAsString('NOM');
                        if iParentID<>-1 then
                            tndNouveau := tvwArbre.Items.AddChild(tndParent, strNom)
                        else
                            tndNouveau := tvwArbre.Items.AddChild(tndPrevParent, strNom);
     
                        tvwArbre.FullExpand();
                        if (iSuivantID<>-1) then
                            // cherche des sous-enfants
                            afficher(iProjetID,  iTacheID,iTacheID,  tndParent, tndNouveau);
                        // cherche si il y a d'autre taches qui suive la tache actuelle
                        afficher(iProjetID,  iParentID,iTacheID,  tndPrevParent, tndNouveau);
                        Next();
                    end;
                end;
        end;
        FreeAndNil(orclqryRequete);
    end;
    bon je sens que nos donnees sont differentes alors faudrait ajuster...
    ou m'en transmettre une partie...

  5. #5
    Membre habitué
    Inscrit en
    Août 2002
    Messages
    144
    Détails du profil
    Informations personnelles :
    Âge : 48

    Informations forums :
    Inscription : Août 2002
    Messages : 144
    Points : 157
    Points
    157
    Par défaut Affichage recursif
    Bonjour, je te propose d'utiliser un composant alternatif à tTreeview, qui te permettera beaucoup plus de possibilités :http://www.delphi-gems.com/supplemen...load.php?ID=28.

    Voila, le code à utiliser. Je n'ai pas pu le tester sachant que je n'ai pas Oracle. mais cela devrais normalement fonctionner. Je te joins le code dans le fichier zip.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    unit Main;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, VirtualTrees, StdCtrls, Oracle;
     
    type
      TTask = class
        ProjectID : integer;
        TaskID    : integer;
        Name      : String;
        ParentID  : integer;
        PreviousID: integer;
        NextID    : integer;
      end;
     
    type
      TForm1 = class(TForm)
        Edit: TEdit;
        Button: TButton;
        TaskList: TVirtualStringTree;
        OracleSession: TOracleSession;
        procedure FormCreate(Sender: TObject);
        procedure ButtonClick(Sender: TObject);
        procedure TaskListGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
          Column: TColumnIndex; TextType: TVSTTextType;
          var CellText: WideString);
        procedure TaskListCompareNodes(Sender: TBaseVirtualTree; Node1,
          Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
        function FindTask(ParentTask:integer):pVirtualNode;
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    uses Math;
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      TaskList.NodeDataSize := SizeOf(TTask);
    end;
     
    procedure TForm1.ButtonClick(Sender: TObject);
    var
      NewTask : tTask;
    begin
      TaskList.Clear;
      with TOracleQuery.Create(Self) do
      begin
        Session:= OracleSession;
        Close();
        SQL.Clear();
        SQL.Add('SELECT * ');
        SQL.Add('FROM Taches ');
        SQL.Add(' WHERE ProjetID = ' + edit.text + ' ORDER BY TACHEID');
        Execute();
        First();
        While not eof do
        begin
          NewTask := TTask.Create;
          NewTask.ProjectID := StrToInt64(FieldAsString('PROJETID'));
          NewTask.TaskID    := StrToInt64(FieldAsString('TACHEID'));
          NewTask.ParentID  := StrToInt64(FieldAsString('PARENTID'));
          NewTask.PreviousID:= StrToInt64(FieldAsString('PRECEDENTID'));
          NewTask.NextID    := StrToInt64(FieldAsString('SUIVANTID'));
          Newtask.Name      := FieldAsString('NOM');
          TaskList.AddChild(FindTask(NewTask.ParentID),NewTask);
          Next;
        end;
        Free;
      end;
      TaskList.FullExpand(TaskList.GetFirst);
      TaskList.Sort(TaskList.GetFirst,1,sdAscending );
    end;
     
    function TForm1.FindTask(ParentTask: integer): pVirtualNode;
    var
      CurrentNode : pVirtualNode;
      Data : ^tTask;
    begin
      Result := nil;
      CurrentNode := TaskList.GetFirst;
      While CurrentNode <> nil do
      begin
        Data := TaskList.GetNodeData(currentNode);
        if Data.TaskID = ParentTask  then
        begin
          Result := CurrentNode;
          exit;
        end;
        CurrentNode := TaskList.GetNext(CurrentNode);
      end;
    end;
     
    procedure TForm1.TaskListGetText(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    var
      Data : ^Ttask;
    begin
      Data := Sender.GetNodeData(Node);
      case Column of
        0: CellText := Data.Name;
        1: CellText := IntToStr(Data.TaskID);
      end;
    end;
     
    procedure TForm1.TaskListCompareNodes(Sender: TBaseVirtualTree; Node1,
      Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
    var
      Data1, Data2 : ^TTask;
    begin
      Data1 := Sender.GetNodeData(Node1);
      Data2 := Sender.GetNodeData(Node2);
      Case Column of
        1: Result := CompareValue(Data1.PreviousID,Data2.PreviousID);
      end;
    end;
     
    end.
    Recursive Oracle.zip

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2002
    Messages : 109
    Points : 71
    Points
    71
    Par défaut
    Merci de votre aide, je vais tester tout ça!

    En ce moment, j'ai quand même pu régler mon problème sans utiliser la récursivité (construction d'une liste chaînée d'objets TTache).

    Merci encore!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de récursivité
    Par amestoche dans le forum Langage
    Réponses: 2
    Dernier message: 20/04/2007, 15h41
  2. Problème Arnaud <> récursivité
    Par Kanter dans le forum Delphi
    Réponses: 13
    Dernier message: 20/02/2007, 16h54
  3. Problème de récursivité en Prolog
    Par poooky dans le forum Prolog
    Réponses: 5
    Dernier message: 04/01/2007, 17h35
  4. Problème de récursivité
    Par mehdi.berra dans le forum C
    Réponses: 8
    Dernier message: 14/12/2006, 17h42
  5. Problème de récursivité
    Par tazmania dans le forum C
    Réponses: 24
    Dernier message: 14/12/2005, 14h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo