home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / PROGRAMS / DBSHARE.PRO < prev    next >
Encoding:
Prolog Source  |  1990-03-26  |  10.9 KB  |  396 lines

  1. /***************************************************************************
  2.  
  3.                PDC Prolog
  4.  
  5.           Demo example of filesharing ext. db.
  6.  
  7.  PURPOSE: Demo of filesharing in ext. db.
  8.  SEE: DBSHARE.HLP
  9.  
  10.  ═══════════════╦══════╦════════════════════════════════════════════════════
  11.   Date Modified,║ By,  ║  Comments.
  12.  ═══════════════╬══════╬════════════════════════════════════════════════════
  13.                 ║      ║
  14.  
  15.  
  16. WINDOWS:
  17.   2: Names and statess of the text's
  18.   3: Command window
  19.   4: Edit window
  20.   5: List of current users
  21.  
  22. ***************************************************************************/
  23.  
  24.  
  25. /***************************************************************************
  26.     DOMAINS FOR DBSHARE
  27. ***************************************************************************/
  28.  
  29. DOMAINS
  30.   locker = string
  31.   modifier = string
  32.   text = text_descr(string,modifier,locker)
  33.   db_selector = dba
  34.  
  35.  
  36. /***************************************************************************
  37.     Global facts
  38. ***************************************************************************/
  39.  
  40. DATABASE 
  41.   determ lockindex(bt_selector)
  42.   determ index(bt_selector)
  43.   determ usrindex(bt_selector)
  44.   determ mark(real)
  45.   determ username(string)
  46.  
  47.  
  48. /***************************************************************************
  49.     Small help predicates
  50. ***************************************************************************/
  51.  
  52. PREDICATES
  53.   nondeterm repeat
  54.   wr_err(integer)   
  55.    
  56. CLAUSES           
  57.   repeat.
  58.   repeat:-repeat.
  59.  
  60.   wr_err(E):-
  61.       errormsg("PROLOG.ERR",E,Errormsg,_),
  62.       write(Errormsg),readchar(_).
  63.   
  64.  
  65. /***************************************************************************
  66.     Updating the screen
  67. ***************************************************************************/
  68.  
  69. PREDICATES
  70.   list  % List texts and their status already inside transaction
  71.   translist  % List texts and their status
  72.   list_texts(bt_selector)
  73.   list_users(bt_selector)
  74.   show_textname(string)
  75.    
  76. CLAUSES
  77.   % Print one name and display who last modified the text/who is updating it
  78.   show_textname(Key):-
  79.     lockindex(LockIndex),
  80.        key_search(dba,LockIndex,Key,Ref),!,
  81.        ref_term(dba,text,Ref,text_descr(_,_,Locker)),
  82.     write("\n*",Key,"\n Locked by: ",Locker).
  83.   show_textname(Key):-
  84.     index(Index),
  85.        key_search(dba,Index,Key,Ref),!,
  86.        ref_term(dba,text,Ref,text_descr(_,Modifier,_)),
  87.        write("\n ",Key,"\n Modified by: ",Modifier).
  88.  
  89.   % List names of all text in database
  90.   list_texts(Index) :-
  91.     key_current(dba, Index, Key, _),
  92.     show_textname(Key),
  93.     key_next(dba, Index, _), !,
  94.     list_texts(Index).
  95.   list_texts(_).
  96.    
  97.  
  98.   % List names of all uusers that has logged into the database
  99.   list_users(UsrIndex) :-
  100.     key_current(dba, UsrIndex, Key, _),
  101.     write(Key),nl,
  102.     key_next(dba,UsrIndex, _), !,
  103.     list_users(UsrIndex).
  104.   list_users(_).
  105.  
  106.   % Update both windows
  107.   list:-
  108.     shiftwindow(5),
  109.     clearwindow,
  110.     usrindex(UsrIndex),
  111.     key_first(dba,UsrIndex,_),
  112.     list_users(UsrIndex),
  113.     shiftwindow(2),
  114.     clearwindow,
  115.     index(Index),
  116.     key_first(dba,Index,_),
  117.     list_texts(Index),!.
  118.   list.
  119.  
  120.   translist:-
  121.     db_begintransaction(dba,read),
  122.     list,
  123.     db_endtransaction(dba).
  124.  
  125.  
  126. /***************************************************************************
  127.   Implementing user locking of text's by inserting the REF for the
  128.   text in the lock index
  129. ***************************************************************************/
  130.  
  131. PREDICATES
  132.    lock(string,bt_selector,bt_selector)
  133.    
  134. CLAUSES           
  135.   lock(Name,Index,LockIndex) :-
  136.     not(key_search(dba, LockIndex, Name, _)), !,
  137.     key_search(dba,Index,Name,Ref),
  138.     key_insert(dba, LockIndex, Name, Ref).
  139.   lock(Name,_,_):-
  140.     write(Name," is being updated by another user.\n Access denied"),
  141.     fail.
  142.  
  143.  
  144. /***************************************************************************
  145.     Edit texts in the database
  146. ***************************************************************************/
  147.  
  148. PREDICATES
  149.    ed(bt_selector, bt_selector, string)
  150.  
  151. CLAUSES
  152.   % Edit existing text
  153.   ed(Index,LockIndex,Name) :-
  154.     username(User),
  155.     db_begintransaction(dba,readwrite),
  156.       key_search(dba, Index, Name, Ref),
  157.       lock(Name,Index,LockIndex), !,
  158.       ref_term(dba, text, Ref, text_descr(Str,_,_)),
  159.       term_replace(dba,text,Ref,text_descr(Str,"",User)),
  160.     db_endtransaction(dba),
  161.     translist,
  162.     shiftwindow(4),
  163.     edit(Str, Str1, "Edit", NAME, "", 0, "PROLOG.HLP", 1,1,1,1,_,_),
  164.     clearwindow,
  165.     db_begintransaction(dba,readwrite),
  166.       term_replace(dba, text, Ref, text_descr(Str1,User,"")),
  167.       key_delete(dba, LockIndex, Name, Ref), %unlock
  168.     db_endtransaction(dba),
  169.     translist.
  170.   %No such text or locked by another user
  171.   ed(_,_,Name):-
  172.     db_endtransaction(dba),
  173.     write("The text: ",Name," cannot be opened.\nPress any key to continue"),
  174.     readchar(_),nl.
  175.  
  176.  
  177. /***************************************************************************
  178.     Create a new text
  179. ***************************************************************************/
  180.  
  181. PREDICATES
  182.   create(bt_selector,bt_selector,string)
  183.  
  184. CLAUSES
  185.   create(_,_,""):-!.
  186.   create(Index,LockIndex, Name):-
  187.      username(User),
  188.      db_begintransaction(dba,readwrite),
  189.       not(key_search(dba,Index,Name,_)),!,
  190.       chain_insertz(dba, file_chain, text,text_descr("",User,User), Ref),
  191.       key_insert(dba, Index, Name, Ref),
  192.      db_endtransaction(dba),
  193.      translist,
  194.      ed(Index,LockIndex, Name).
  195.   create(_,_,Name):-
  196.     db_endtransaction(dba),
  197.     write("The text: ",Name," already exist.\nPress any key to continue"),
  198.     readchar(_).
  199.  
  200. /***************************************************************************
  201.     Interpret commands
  202. ***************************************************************************/
  203.  
  204. PREDICATES
  205.   main(db_selector, bt_selector, bt_selector)
  206.   interpret(char, bt_selector, bt_selector)
  207.   check_update_view
  208.   check_timeout
  209.   mk_prompt
  210.   terminator(CHAR)
  211.      
  212. CLAUSES
  213.   % Loop until 'Q' is pressed
  214.   main(dba, Index, LockIndex) :-
  215.     mk_prompt,
  216.     repeat,
  217.       sleep(5),
  218.       check_timeout,
  219.       shiftwindow(3),
  220.       inkey(C),
  221.       upper_lower(Command,C),
  222.       write(Command),
  223.       trap(interpret(Command,Index, LockIndex),E,wr_err(E)),
  224.       translist,
  225.       mk_prompt,
  226.     terminator(Command),!.
  227.  
  228.   terminator(27).
  229.   terminator('Q').
  230.  
  231.   mk_prompt:-
  232.       shiftwindow(3),
  233.       clearwindow,
  234.       write("Command: "),
  235.       cursor(R,C),
  236.       cursor(R,C).
  237.  
  238.  
  239.   check_timeout:-
  240.        mark(T),timeout(T),!,
  241.     marktime(100,Mark),
  242.        retractall(mark(_)),
  243.        assert(mark(Mark)),
  244.        check_update_view.
  245.   check_timeout.
  246.  
  247.   check_update_view:-
  248.     db_begintransaction(dba,read),
  249.      db_updated(dba),!,
  250.      list,
  251.     db_endtransaction(dba).
  252.   check_update_view:-
  253.     db_endtransaction(dba).
  254.  
  255.  
  256.   %interpret commandlineinput if not recognized show help info
  257.   interpret(' ',_,_):-!.
  258.   interpret(27,_,_):-!.
  259.   interpret('Q',_,_):-!.
  260.   interpret('H',_,_):-!,
  261.     shiftwindow(4),
  262.     file_str("dbshare.hlp",Str),
  263.     edit(Str,_, "VIEW", "DBSHARE - HELP", "", 0, "PROLOG.HLP", 0,1,1,1,_,_),
  264.     clearwindow.
  265.   interpret('E',Index,LockIndex):-!,
  266.     write("\nFile Name: "),readln(Name),
  267.     trace(on),
  268.     ed(Index,LockIndex,Name).
  269.   interpret('C',Index,LockIndex):-!,
  270.     write("\nFile Name: "),readln(Name),
  271.     create(Index,LockIndex,Name).
  272.   interpret('V',Index,_):-
  273.     write("\nFile Name: "),readln(Name),
  274.     db_begintransaction(dba,read),
  275.       key_search(dba,Index,Name,Ref),!,
  276.       ref_term(dba,text,Ref,text_descr(Str,_,_)),
  277.     db_endtransaction(dba),
  278.     shiftwindow(4),
  279.     edit(Str,_, "VIEW", NAME, "", 0, "PROLOG.HLP", 0,1,1,1,_,_),
  280.     clearwindow.
  281.   interpret('V',_,_):-!,
  282.     db_endtransaction(dba),
  283.     write("\nNot found.\nPress any key to continue"),readchar(_).
  284.   interpret('D',Index,LockIndex):-
  285.     write("\nDelete text: "),readln(Name),
  286.     db_begintransaction(dba,readwrite),
  287.       key_search(dba,Index,Name,Ref),
  288.       not(key_search(dba,LockIndex,Name,_)),
  289.       key_delete(dba,Index,Name,Ref),!,
  290.       term_delete(dba,file_chain,Ref),
  291.     db_endtransaction(dba),
  292.     translist.
  293.   interpret('D',_,_):-!,
  294.     db_endtransaction(dba),
  295.     write("\nCan't delete.\nPress any key to continue"),readchar(_).
  296.   interpret(_,_,_):-
  297.        beep,
  298.        makewindow(6,7,7,"HELP",0,0,21,50),
  299.     write("COMMANDS:\n\nHelp: H\nCreate text:C\nEdit text:E\n"),
  300.     write("View text:V\nDelete text:D\nQuit:Q\n\nPress any key to continue:"),
  301.     readchar(_),
  302.     clearwindow,
  303.     removewindow.
  304.  
  305.  
  306. /***************************************************************************
  307.     Open the database file / Create new if not existing
  308. ***************************************************************************/
  309.  
  310. PREDICATES
  311.   open_dbase(bt_selector,bt_selector,bt_selector)
  312.  
  313. CLAUSES
  314.   open_dbase(INDEX,LOCKINDEX, USRINDEX):-
  315.     existfile("dbshare.dba"),!,
  316.     db_open(dba, "dbshare.dba",readwrite,denynone),
  317.     db_begintransaction(dba,read),
  318.       bt_open(dba, "locks", LOCKINDEX),
  319.       bt_open(dba, "ndx", INDEX),
  320.       bt_open(dba, "usridx", USRINDEX),
  321.     db_endtransaction(dba).
  322.   open_dbase(INDEX,LOCKINDEX,USRINDEX):-
  323.     db_create(dba,"dbshare.dba" , in_file),
  324.     bt_create(dba, "locks",TEMPLOCKINDEX,20, 4),
  325.     bt_create(dba, "ndx",TEMPINDEX , 20, 4),
  326.     bt_create(dba, "usridx", TEMPUSRINDEX, 20, 4),
  327.     bt_close(dba, TEMPINDEX),
  328.     bt_close(dba, TEMPLOCKINDEX),
  329.     bt_close(dba, TEMPUSRINDEX),
  330.     db_close(dba),
  331.     open_dbase(INDEX,LOCKINDEX,USRINDEX).
  332.  
  333.  
  334. /***************************************************************************
  335.     Register a new user in the database file
  336. ***************************************************************************/
  337.  
  338. PREDICATES
  339.   logout(bt_selector)
  340.   login(bt_selector)
  341.   
  342. CLAUSES
  343.   login(USRINDEX):-
  344.     shiftwindow(3),
  345.     clearwindow,
  346.     write("Please enter your name: "),
  347.     readln(Name),
  348.     Name<>"",!,
  349.     retractall(username(_)),
  350.     assert( username(Name)),
  351.     db_begintransaction(dba,readwrite),
  352.      chain_inserta(dba,user_chain,string,Name,Ref),
  353.      key_insert(dba,USRINDEX,Name,Ref),
  354.     db_endtransaction(dba).
  355.   login(USRINDEX):-login(USRINDEX).
  356.    
  357.   logout(USRINDEX):-
  358.     retract(username(Name)),!,
  359.     db_begintransaction(dba,readwrite),
  360.      key_search(dba,USRINDEX,Name,Ref),
  361.      key_delete(dba,USRINDEX,Name,Ref),
  362.      term_delete(dba,user_chain,Ref),
  363.     db_endtransaction(dba).
  364.             
  365.  
  366. /***************************************************************************
  367.     Main GOAL
  368. ***************************************************************************/
  369.  
  370. GOAL
  371.     break(off),
  372.     makewindow(2,23,23,"TEXTS (*=Locked)",0,50,17,30),
  373.     makewindow(4,7,7,"EDIT",0,0,21,50),
  374.     makewindow(5,7,7,"CURRENT USERS",17,50,8,30),
  375.     makewindow(3,13,13,"H:Help C:Create E:Edit V:View D:Delete",21,0,4,50),
  376.     open_dbase(INDEX,LOCKINDEX,USRINDEX),
  377.     assert(index(INDEX)),
  378.     assert(lockindex(LOCKINDEX)),
  379.     assert(usrindex(USRINDEX)),
  380.     marktime(10,Mark),    % Initialize timeout for screen update    
  381.     assert(mark(Mark)),
  382.     login(USRINDEX),
  383.     translist,
  384.     main(dba, INDEX,LOCKINDEX),
  385.     logout(USRINDEX),
  386.     db_begintransaction(dba,read),
  387.       bt_close(dba,INDEX),
  388.       bt_close(dba,LOCKINDEX),
  389.       bt_close(dba,USRINDEX),
  390.     db_endtransaction(dba),
  391.     db_close(dba),
  392.     removewindow,
  393.     removewindow,
  394.     removewindow,
  395.     removewindow.
  396.