Home
Web Interface
Download
About
Write your ABS code in the text area:
//Working File (not completed) // by Lizeth Tapia (sltarifa) UIO module BookShop; import * from ABS.StdLib; //type synonyms type Key = String ; type Bookname = Key ; type Bookquantity = Int ; type Bookweight = Int ; type Bookbackordertime = Int ; type Countryname = Key ; type Countryshiptime = Int ; type Countryshipcost = Int ; type Shippingcost = Int ; type Shippingtime = Int ; //Data Type declarations data Bookinfo = ConsBookinfo(Bookquantity, Bookweight, Bookbackordertime) | NoBook ; data Countryinfo = ConsCountryinfo(Countryshiptime, Countryshipcost) | NoCountry ; data OrderResult = OrderFail | Result(List
, Shippingcost, Shippingtime) ; data DatabaseInfo = InfoFail | Info(Map
, Pair
) ; //**************************************************************************************** //***************************************function definitions***************************** //**************************************************************************************** def B mylookup
(Map
ms, A k) = // retrieve from the map case ms { InsertAssoc(Pair(k, y), _) => y; // InsertAssoc(_, tm) => lookup(tm, k); // Rewritten for aPET (to avoid having fresh vars in the nomatch not present in the match) InsertAssoc(Pair(_, _), tm) => mylookup(tm, k); }; //List def List
shuffle
(List
list) = case list { Nil => Nil ; Cons(head, tail) => concatenate(tail, Cons(head, Nil)) ; }; def A chose
(List
list) = head( list); // chose the head of a non-empty list def List
choseN
(List
list, Int n) = case n > 0 { True => case list { Nil => Nil ; Cons(head,tail) => Cons(head,choseN(tail,n-1)) ; } ; False => case list { Nil => Nil ; Cons(_,_) => Nil ; } ; }; //Pair //def Key key
(Pair
p) = // case p {Pair(k,i) => k;}; def A info
(Pair
p) = case p {Pair(k,i) => i;}; //Map def Set
domain
(Map
map) = case map { EmptyMap => EmptySet; InsertAssoc(Pair(b,_), tail) => Insert(b,domain(tail)) ; } ; def List
map2list
(Map
map) = case map { EmptyMap => Nil ; InsertAssoc(Pair(b,_), tail) => Cons(b, map2list(tail)) ; }; def Map
concatMap
(Map
map1, Map
map2) = case map1 { EmptyMap => map2 ; InsertAssoc(head, tail) => case head { Pair("",NoBook) => concatMap(tail, map2); Pair(k,i) => InsertAssoc(Pair(k, i), concatMap(tail, map2)) ; }; }; //Bookinfo def Bookweight bookweight(Bookinfo book) = case book { ConsBookinfo( _, w,_ ) => w ; }; //Countryinfo def Countryshiptime countryshiptime(Countryinfo country) = case country { ConsCountryinfo(t, _)=> t ; }; def Countryshipcost countryshipcost(Countryinfo country) = case country { ConsCountryinfo(_, c) => c ; }; // DatabaseInfo def Map
getBookMap(DatabaseInfo db) = case db { InfoFail => EmptyMap ; Info(bm, _) => bm ; }; def Pair
getCountryinfo (DatabaseInfo db) = case db { InfoFail => Pair("",ConsCountryinfo(0,0)) ; Info(_, c) => c ; }; //**************************************************************************************** //******************************************** bookshop model****************************** //**************************************************************************************** //session def Int calculateweight(Map
books) = case books { EmptyMap => 0 ; InsertAssoc(Pair(_,info), tail) => bookweight(info) + calculateweight(tail) ; }; def Int calculatecost(Int w, Int c) = w*c; def Int calculatetime(Map
books, Int time) = case books { InsertAssoc(Pair(_, ConsBookinfo(0, _, t)), tail) => calculatetime(tail, time+t); InsertAssoc(Pair(_, ConsBookinfo(q, _,t)), tail)=> calculatetime(tail, time); EmptyMap => time ; }; // Database def Map
getBooks(Map
booksDB,List
books ) = case books { Nil => EmptyMap; Cons(head, tail) => case (contains(domain(booksDB),head)) { False => getBooks(booksDB,tail) ; True => InsertAssoc(Pair(head,mylookup(booksDB, head)),getBooks(booksDB, tail)); }; }; def Map
updateBooks(Map
booksDB,List
books ) = case books { Nil => booksDB; Cons(head, tail) => updateBooks(updateBook(booksDB,head,EmptyMap),tail); }; def Map
updateBook(Map
booksDB,Bookname book,Map
bookDB2) = case booksDB { EmptyMap => bookDB2; InsertAssoc(Pair(book,ConsBookinfo(0,w,t)),tail) => concatMap(bookDB2, InsertAssoc(Pair(book,ConsBookinfo(0,w,t)),tail)); InsertAssoc(Pair(book,ConsBookinfo(q,w,t)),tail) => concatMap(bookDB2, InsertAssoc(Pair(book,ConsBookinfo(q-1,w,t)),tail)); // InsertAssoc(head,tail) => updateBook(tail, book,concatMap(bookDB2, InsertAssoc(head,EmptyMap))); // Rewritten for aPET (to avoid having in the nomatch fresh vars not present in the match) InsertAssoc(Pair(book2,ConsBookinfo(q,w,t)),tail) => updateBook(tail, book,concatMap(bookDB2, InsertAssoc(Pair(book2,ConsBookinfo(q,w,t)),EmptyMap))); }; //*************************************************************************************************** //*************************************************classes******************************************* //*************************************************************************************************** interface Client { Unit run(); } interface Agent { Session getsession(); Unit free(Session session); } interface Session { OrderResult order(List
books, Countryname country); } interface Database { DatabaseInfo getInfo(List
books, Countryname country); Bool confirmOrder(List
books); } // agent class AgentImp(Database db) implements Agent { List
sessions = Nil; [sessions <= max(sessions)] Unit free(Session session) { sessions = appendright(sessions, session); } Session getsession() { Session session; if (isEmpty(sessions)) { session = new local SessionImp(this,db); } else { session = head(sessions); sessions = tail(sessions); } return session;} } // session class SessionImp(Agent agent, Database db) implements Session { Shippingcost shippingcost = 0; Shippingtime shippingtime = 0; OrderResult result = OrderFail; Bool updatedb = False; OrderResult order(List
books, Countryname country) { Fut
preresult_fut; Fut
updatedb_fut; Fut
fut; DatabaseInfo preresult = InfoFail ; preresult_fut = db!getInfo(books,country); preresult=preresult_fut.get; if (preresult != InfoFail) { shippingcost = calculatecost(calculateweight(getBookMap(preresult)),countryshipcost(info(getCountryinfo(preresult)))); shippingtime = calculatetime(getBookMap(preresult), countryshiptime(info(getCountryinfo(preresult)))); updatedb_fut = db!confirmOrder(map2list(getBookMap(preresult))); updatedb=updatedb_fut.get; if (updatedb){result = Result(map2list(getBookMap(preresult)), shippingcost, shippingtime);} else {result = OrderFail;} } else {result = OrderFail;} fut=agent!free(this); await fut?; return result; } } // Database; class DatabaseImp(Map
bDB, Map
cDB) implements Database { Map
booksDB = bDB; [booksDB <= max(booksDB)][cDB <= max(cDB)] DatabaseInfo getInfo(List
books, Countryname country){ DatabaseInfo result = InfoFail; Map
myorder = EmptyMap ; List
mybooks = Nil; mybooks = books ; myorder = getBooks(booksDB, books) ; result = Info(myorder, Pair(country,mylookup(cDB, country))); return result; } [booksDB <= max(booksDB)] Bool confirmOrder(List
books){ booksDB = updateBooks(booksDB, books); return True; } } //client class SyncClient(Agent agent, List
books, List
countries) implements Client { List
b = books; List
c = countries; OrderResult result = OrderFail ; [b <= max(b)][c <= max(c)] Unit run() { List
chosenBooks = Nil; Countryname chosenCountry = ""; Session session ; Fut
session_fut ; Fut
result_fut; c = shuffle(c); b = shuffle(b); chosenCountry = chose(c); chosenBooks = choseN(b,4); session_fut = agent!getsession(); session=session_fut.get; result_fut = session!order(chosenBooks,chosenCountry); result=result_fut.get; // this!run(); } } //*************************************************************** //****************************main****************************** //*************************************************************** { /* Name q w bo ---------------- book1 0 2 60 book2 5 2 7 book3 2 3 1 Name st scXw ----------------- Peru 15 150 Norway 2 50 Spain 10 100 */ Map
bDB = map[Pair("book1",ConsBookinfo(0, 2, 60)), Pair("book2",ConsBookinfo(5, 2, 7)), Pair("book3",ConsBookinfo(2, 3, 1))]; Map
cDB = map[Pair("Peru",ConsCountryinfo(15, 150)), Pair("Norway",ConsCountryinfo(2, 50)), Pair("Spain",ConsCountryinfo(10, 100))]; List
lb1 = list["book3", "book", "book1", "book2", "other"] ; List
lb2 = list["book3", "book2"] ; Agent agent; Client client1; Client client2; Database db; db = new local DatabaseImp(bDB, cDB); agent = new local AgentImp(db); client1 = new SyncClient(agent,lb1, map2list(cDB)); client2 = new local SyncClient(agent,lb2, map2list(cDB)); client1.run(); client2.run(); /* result client 1: available books: book1, book2 shipping cost: 200 ------- 4kg * 50 shipping time: 62 ------- 2(Norway) + 60(backup order for book1) client 2: available books: book2, book3 shipping cost: 250 ------- 5kg * 50 shipping time: 2 ------- 2(Norway) book database Name q w bo ---------------- book1 0 2 60 book2 3 2 7 book3 1 3 1 */ }
Or you can choose one of our examples:
priorities/emmi1.abs
priorities/emmi2.abs
priorities/Running.abs
priorities/Prio1.abs
priorities/Prio2.abs
priorities/Prio3.abs
ReplicationSystem.abs
fullTradingSystem.abs
MailServer.abs
DemoExample.abs
BookShop.abs
PeerToPeer.abs
BoundedBuffer.abs
Chat.abs