XPCE
XPCE
  test de PL_FILE
    PL_FILE PIPO.pl
      ceci est un test
        creation du fichier au bon endroit
        contenu
      *exec
        print ('pipo !!!!')
      foo_bar
  PL_FILE ANIMATIONS
    notations pour animation
      CHK ::=
        check is {a1} exists; if it does, delete it
        (object( @{a1}), !, free( @{a1}); true)
    :-use_module(library(pce)).
    DOC
      we just want to
        create a window with a cube
        move the cube in the window
          from right to left
          at a certain speed
    now create some trace in the console to also follow the calculation
    sm:-
      sm_a(600,4,40).
        %run this
          %runs animation over 600 pixels, 4 seconds long, with 40 FPS
    notations pour animation
      CHK ::=
        check is {a1} exists; if it does, delete it
          (object( @{a1}), !, free( @{a1}); true)
        {L}
      ::=
        send (@{a1}, {a2_})
    sm_a (Length,Duration,FPS):-
      CREATE OBJECTS
        Drawing as an array
          *setf DRAWING
            OBJECT
              object name
                one line stuff
                  create
                one or many lines
                  after
                  set parameters
                what to do to display the object
                  display
              window
                picture('Window',size(1500,500))
                  create
                %make sure to use double-buffering
                  after
                  window,buffered_update,true
                window, open
                  display
              floor
                box (800, 10)
                  create
                %colour floor
                  after
                  floor,fill_pattern,colour(green)
                window,display,@floor,point(50,300)
                  display
              box
                box (50, 0)
                  create
                %colours box yellow
                  after
                  box, fill_pattern,colour(yellow)
                will be displayed in real time later
                  display
          EXTRACT TABLE OBJECT, create, after, display
            by_item
            TABLE
              {DRAWING}
            FORMAT
                CHK {OBJECT}
                display {OBJECT}
                  new (@{OBJECT}, {create})
                stuff to do after creation (parameters)
                  {after}
                display
                  {display}
        creation ok
      ANIMATION
        animation start
          parameters for animation
            animation
              %how many frames are to be drawn
                Frames is Duration*FPS
              %what's the distance between steps
                Step is Length/Frames
              %what is the delay between steps
                Delay is 1/FPS
        display objects
          %shows Object
            window,display,@box,point(50,100)
            window, flush
        simple animation
          anime(Step, 0.05, 300)
            ani(Step,Delay,Frames,point(50,20)).
              (old)
        animation finished
    animation LG
      doc sur alarmes
      DEF anime, (Step, Delay, Number)
        args
          Step, Delay, 0
            !
              goals
              end of animation
                anime fail
                  fail
            stops the animation
              notes
          Step, Delay, K
            K > 0
              goals
            just trace K
              goals
              echo K
            moves box
              goals
              Decal is 50 + Step*K
                echo Decal
              send( @box,x(50+Step*K))
            flushes
              goals
              send( @window,flush)
            new delai
              goals
              write ('.....>')
                write (K)
                  nl
              alarm(Delay, (K2 is K -1, anime(Step, Delay, K2)), Foo)
              write ('<...')
                write (K)
                  nl
            does the animation
              notes
    :- pce_begin_class(my_timer, object).
    some constants
      variable(mytimer, timer, both, "timer lançant l'animation fréquence 20 fois par seconde").
      variable(step, number, both, "delta x").
      variable(frames, number, both, "number of moves").
      variable(posX, number, both, "xpos of the box").
  PL_FILE TESTS_XPCE
    :- use_module(library(pce)).
    les notations sont décrites plus haut
    DOC
      tests pour utilisation de PCE
        1) affichage de fenetres et boutons
          saisies de
            texte, nombre, menus, etc
            graphiques
              fixes
              animés
            Sous-sujet 3
        2) génération des actions
          pour activer prolog
          pour continuer des dialogues avec xpce
      MODE D'EMPLOI
        lancer
          test_g.
            juste test des graphiques
          test_m
            Génération d'un frame de saisie
            lancement d'actions simples
      REFERENCES ET EXEMPLES
        https://www.swi-prolog.org/packages/xpce/UserGuide/sec-4.1.html
        more sophisticated samples here
          https://github.com/SWI-Prolog/packages-xpce/tree/master/prolog/demo
            https://github.com/SWI-Prolog/packages-xpce/tree/master/prolog/contrib/rubik
              https://github.com/SWI-Prolog/packages-xpce/blob/master/prolog/contrib/rubik/README
              https://github.com/SWI-Prolog/packages-xpce/blob/master/prolog/contrib/rubik/maplist.pl
              https://github.com/SWI-Prolog/packages-xpce/blob/master/prolog/contrib/rubik/rubik.pl
              https://github.com/SWI-Prolog/packages-xpce/blob/master/prolog/contrib/rubik/rubikpce.pl
    utilitaires divers
      chk (X) :-
        IF var (X)
          true
          IF object ( @X)
            !
              free ( @X )
            true
    exemples de programmes
      test 1 des macros
        test_g :-
          graphique de référence
          construction des objets
            chk (demo)
              demo += dialog ('Demo Window')
            CREATE OBJECTS
              Drawing as an array
                *setf DRAWING
                  OBJECT
                    object name
                      one line stuff
                        create
                      one or many lines
                        after
                        set parameters
                      what to do to display the object
                        display
                    window
                      picture('Window',size(1500,500))
                        create
                      %make sure to use double-buffering
                        after
                        window,buffered_update,true
                      @window /> open
                        display
                    floor
                      box (800, 10)
                        create
                      %colour floor
                        after
                        floor,fill_pattern,colour(green)
                      @window /> display,@floor,point(50,300)
                        display
                    box
                      box (50, 0)
                        create
                      %colours box yellow
                        after
                        box, fill_pattern,colour(yellow)
                      will be displayed in real time later
                        display
                EXTRACT TABLE OBJECT, create, after
                  by_item
                  TABLE
                    {DRAWING}
                  FORMAT
                      *exec
                        var_high = False
                          if "{OBJECT}" [0] in "AZERTYUIOPQSDFGHJKLMWXCVBN_"
                            var_high = True
                      *if var_high
                        display {OBJECT}
                          {OBJECT} += {create}
                        *else
                          chk(@{OBJECT})
                          display {OBJECT}
                            @{OBJECT} += {create}
                      stuff to do after creation (parameters)
                        {after}
              creation ok
                EXTRACT TABLE OBJECT, display
                  by_item
                  TABLE
                    {DRAWING}
                  FORMAT
                    display {OBJECT}
                      {display}
          affichage
      test de dialogue
        test_m :-
          fabrication d'un formulaire de saisie
            Drawing as an array
              *setf DRAWING
                OBJECT
                  object name
                    one line stuff
                      create
                    what to do to display the object
                      display
                    description
                      note
                    once all objects are created, add functionalities
                      prepare
                    Sous-sujet 5
                      tipe
                    Sous-sujet 6
                      name
                    juste après create
                      after
                  Dialog
                    dialog ('Define employee')
                      create
                    Dialog /> open
                      display
                    main window
                      note
                    Dialog /> default_button, Enter
                      prepare
                    window
                      tipe
                    send_list (
                      prepare
                      Dialog
                      append
                      [
                        N1, N2, Sex, Age, Dpt
                        Cancel, Show_prolog, Show_pce, Enter
                  N1
                    text_item (first_name)
                      create
                    get first_name
                      note
                    get
                      tipe
                  N2
                    text_item (family_name)
                      create
                    get family_name
                      note
                    get
                      tipe
                  Sex
                    get sex
                      note
                    menu(sex)
                      create
                    send_list(Sex, append, [male, female])
                      prepare
                    get
                      tipe
                  Age
                    get age
                      note
                    int_item (age, low := 18, high := 65)
                      create
                    get
                      tipe
                  Dpt
                    get department
                      note
                    menu(department, cycle)
                      create
                    send_list(Dpt, append, [research, development, marketing])
                      prepare
                    get
                      tipe
                  Enter
                    valid inputs
                      note
                      and close window
                    button
                      tipe
                    and (
                      create
                      message (
                        @prolog
                        assert_employee
                        [
                          N1?selection
                            N2?selection
                          Sex?selection
                          Age?selection
                          Dpt?selection
                      message(
                        Dialog
                        destroy
                    enter
                      name
                  Show_pce
                    prints some stuff
                      note
                    button
                      tipe
                    message (@pce, format, "Hello World - Show !")
                      create
                    show_pce
                      name
                  Show_prolog
                    prints some stuff
                      note
                    button
                      tipe
                    message (@prolog, say_hello)
                      create
                    show_prolog
                      name
                  Cancel
                    cancels and close the window
                      note
                    button
                      tipe
                    message (Dialog, destroy)
                      create
                    cancel
                      name
              EXTRACT TABLE OBJECT, create, after, note, tipe, name
                by_item
                TABLE
                  {DRAWING}
                FORMAT
                    *exec
                      var_high = False
                        if "{OBJECT}" [0] in "AZERTYUIOPQSDFGHJKLMWXCVBN_"
                          var_high = True
                    =====> description of {OBJECT} :
                      {note}
                    ..............................creation of {OBJECT}
                      *if '{tipe}' = 'button'
                        new(
                          {OBJECT}
                          button (
                            {name}
                            {create}
                        *else
                          *if var_high
                            display {OBJECT}
                              new (
                                {OBJECT}
                                {create}
                            *else
                              chk(@{OBJECT})
                              display {OBJECT}
                                new (
                                  @{OBJECT}
                                  {create}
                    *if after
                      stuff to do after creation (parameters)
                        {after}
            creation ok
              STEP 2 : prepare
                EXTRACT TABLE OBJECT, prepare
                  by_item
                  TABLE
                    {DRAWING}
                  FORMAT
                    prepare {OBJECT}
                      {prepare}
                writeln("... >>> creation ok 2")
              finally
                display
                  Dialog /> open
                  writeln("... >>> creation ok 3")
        assert_employee(FirstName, FamilyName, Sex, Age, Depth) :-
          write ('hello ici')
            format (
              'Adding ~w ~w ~w, age ~w, working at ~w~n'
              [ Sex, FirstName, FamilyName, Age, Depth]
          say_hello
        say_hello :-
          write ('hello world')
      SEP calculette
        structure
          exemple d'écran
            Mde d'emploi
              taper une expression arithmétique dans Text
              cliquer sur Calcul_3
                le résultat s'affiche
          origine
            https://github.com/daFish/prolog-xpce/blob/master/calculator.pl
          structure
            smc
              small_calculator
                calc
                calc_2
                calc_3
          utilité
            comprendre le passsage d'argument et de résultats entre Prolog et xpcd
            développement de la foncton générique pce_pl (X, Y)
        smc :-
          small_calculator
        small_calculator:-
          préparation interface
            fenetre principale
              new(Dialog, dialog('Calculette')),
            champs
              new(Text_E, text_item('Text')),
              new(Text_A, text_item('Result')),
            actions sur les boutons
              new(B_Calc,button(calcul,message(@prolog,calc,Dialog))),
              new(B_Close,button(fini,message(Dialog,destroy))),
              new(B_Calc_2,button(calcul_2,message(@prolog,calc_2,Dialog, Text_E?selection))),
              new(B_Calc_3,button(calcul_3,message(@prolog,calc_3, Text_E?selection, Text_A))),
              new(B_essais,button(essais,message(@prolog,essais, Text_E?selection)))
          positionnement des objets
            send(Dialog,append(Text_E)),
            send(Dialog,append(Text_A)),
            send(Dialog,append(B_Calc)),
              send(Dialog,append(B_Calc_2)),
                send(Dialog,append(B_Calc_3)),
            send(Dialog,append(B_essais)),
            send(Dialog,append(B_Close)),
          activation
            send(Dialog,default_button(B_Calc)),
            send(Dialog, open).
        calc (Dialog):-
          get input
            get(Dialog, member('Text'),T_1),
            get(T_1, selection, E1),
          Calculer
            préparation du terme
              string_concat(E1,'.',E2),
            ouverture du stream pce --> prolog
              pce_open(E2, read, In),
                read(In, Term),
                  %den Inhalt aus Stream lesen
                close(In),
                  %stream schliessen
            le calcul
              Res is (eval(Term)),
          écrire le résultat
            get(Dialog,member('Result'),A_1),
            send(A_1,selection(Res)).
        calc_2 (Dialog, E1):-
          Calculer
            préparation du terme
              string_concat(E1,'.',E2),
            ouverture du stream pce --> prolog
              pce_open(E2, read, In),
                read(In, Term),
                close(In),
            le calcul
              Res is (eval(Term)),
                write ([E2, '-->', Res])
                  nl
          écrire le résultat
            get(Dialog,member('Result'),A_1),
            send(A_1,selection(Res)).
        calc_3 ( E1, R):-
          préparation du terme
            string_concat(E1,'.',E2)
              pce_open(E2, Term)
          Calculer
            le calcul
              Res is (eval(Term))
                write ([E2, '-->', Res])
                  nl
          écrire le résultat
            send(R,selection (Res))
        essais (N) :-
          préparation du terme
            pce_open("write ('hello world, vive les essais').", read, In)
              read(In, _),
              close(In),
          Calculer
            le calcul
          écrire le résultat
        pce_pl (X, Y) :-
          ouverture du stream pce --> prolog
            pce_open(X, read, In)
              read(In, Y)
                close(In)