
/*         KLONE4        settembre 91   */

/* procedure per la gestione della rete
   e la lettura e scrittura su file      */




 clear_net :-
        work(A),
        (
          B=net_node(g_conc,C,A)
        ;
          (
            B=net_node(i_conc,D,A)
          ;
            (
              B=net_node(g_role,E,F,A)
            ;
              (
                B=net_node(i_role,G,H,A)
              ;
                (
                  B=net_arc(super_c,I,J,A)
                ;
                  (
                    B=net_arc(indiv_c,K,L,A)
                  ;
                    (
                      B=net_arc(role,M,N,A)
                    ;
                      (
                        B=net_arc(mods,O,P,Q,R,A)
                      ;
                        (
                          B=net_arc(diffs,S,T,U,V,A)
                        ;
                          (
                            B=net_arc(sats,W,X,Y,Z,A)
                          ;
                            (
                              B=net_arc(vr_facet,A1,B1,C1,A)
                            ;
                              (
                                B=net_arc(number_facet,D1,E1,F1,A)
                              ;
                                (
                                  B=net_arc(modality_facet,G1,H1,I1,A)
                                ;
                                  B=net_arc(value_facet,J1,K1,L1,A)
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        ),
        retract(B),
        clear_net.
 clear_net.


 is_net(A) :-
        atom(A),
        klone_file_name(A,B),
        nofileerrors,
        see(B),
        fileerrors,
        read(C),
        C==si_net_mark,
        seen.
 is_net(_):- seen, fileerrors, fail.

 load_net(A) :-
        atom(A),
        is_net(A),
        klone_file_name(A,B),
        skip_line(1),
        write_line(3,[loading,B,'...']),
        skip_line(2),
        see(B),
        load_net_x,
        seen.
 load_net(A) :-
        atom(A),
        not is_net(A),
        write('File not exists').

load_net_x :- repeat,
              read(T),
              load_net_xx(T).

load_net_xx(si_net_mark) :- !, fail.
load_net_xx(end_of_file) :- !.
load_net_xx(T) :- assertz(T), !, fail. 

 save_net(A) :-
        atom(A),
        not is_net(A),
        klone_file_name(A,B),
        tell(B),
        write('si_net_mark.'),
        nl,
        sn,
        told,
        write_line(3,[save_net,'>> ',A,' <<']),
        skip_line(1).
 save_net(A) :-
        (
          atom(A),
          is_net(A),
          write_line(3,[do,you,want,to,overwrite,it,'?',' (y/n) =>']),
          read(B),
          (
            B==y
          ;
            B==yes
          ),
          klone_file_name(A,F),
          tell(F),
          write('si_net_mark.'),
          nl,
          sn,
          told, 
          write_line(3,[save_net,'>> ',F,' <<']),
          skip_line(1),
          '!'
        ;
          '!',
          fail
        ).


/* usato da save_net */

 sn :-
        work(A),
        (
          B=net_node(g_conc,C,A)
        ;
          (
            B=net_node(i_conc,D,A)
          ;
            (
              B=net_node(g_role,E,F,A)
            ;
              (
                B=net_node(i_role,G,H,A)
              ;
                (
                  B=net_arc(super_c,I,J,A)
                ;
                  (
                    B=net_arc(indiv_c,K,L,A)
                  ;
                    (
                      B=net_arc(role,M,N,A)
                    ;
                      (
                        B=net_arc(mods,O,P,Q,R,A)
                      ;
                        (
                          B=net_arc(diffs,S,T,U,V,A)
                        ;
                          (
                            B=net_arc(sats,W,X,Y,Z,A)
                          ;
                            (
                              B=net_arc(vr_facet,A1,B1,C1,A)
                            ;
                              (
                                B=net_arc(number_facet,D1,E1,F1,A)
                              ;
                                (
                                  B=net_arc(modality_facet,G1,H1,I1,A)
                                ;
                                  B=net_arc(value_facet,J1,K1,L1,A)
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        ),
        clause(B,true),
        write(B),
        write('.'),
        nl,
        fail.
 sn.


klone_file_name(A,B) :-
     name(A,L1),
     name('.kl1',L2),
     append(L1,L2,L3),
     name(B,L3).

append([],L2,L2).
append([T1|C1],L2,[T1|C3]) :- append(C1,L2,C3).
