Lược đồ quan hệ giữa các lớp trong hệ Auction

Một phần của tài liệu Mô hình tương tác dựa trên role trong hệ đa agent (Trang 88 - 90)

Như đã trình bày ở chương 3, việc cài đặt role bằng ngôn ngữ Java có thể được thực hiện dễ dàng và nhanh chóng hơn nhờ các tài liệu XRole. Bằng cách dùng các XSL thích hợp, ta có thể chuyển các định nghĩa role Seller, Bidder và Auctioneer tương ứng thành các lớp trừu tượng như dưới đây:

public abstract class Seller {

String description="role to make resources available"; String keyword[]= {"seller", "auction"};

/*Request to put the good on sale*/

public static RoleAction ReqOnSale(AgentID addressee); /*Put the good to be sold on sale*/

public static RoleAction putGoodOnSale(String Good_ID, String Description);

/*Auctioneer accept seller's request or not*/

public static final KnownEvent KE_acceptSale(AgentID addressee); /*the good cannot be sold at the auction*/

public static final KnownEvent KE_unSold(AgentID addressee); /*payment from the auctioneer*/

public static final KnownEvent KE_pay(AgentID addressee, String content);

}

Hình 4.7. Mã nguồn Java tương ứng với Seller.xml.

Role A (Abstract) ROLE_ID:String=Auction x RoleA KE_A: KnownEvent KE_B: KnownEvent … ActionA: RoleAction ActionB: RoleAction Auction KnownEvent RoleAction

public abstract class Auctioneer {

String description="role to control the auctions"; String keyword[]= {"Auctioneer", "auction"};

/*notify requesting bidders the current goods on sale*/

public static RoleAction notifyGood(AgentID addressee, string content); /*notify bidders the current situation*/

public static RoleAction notifySituation(AgentID addressee, string content);

/*notify bidders the final winner*/

public static RoleAction youWon(AgentID addressee, Price content); /*request to put goods on sale from sellers*/

public static final KnownEvent KE_reqOnSale(AgentID sender); /*put goods on sale*/

public static final KnownEvent KE_putGoodOnSale(AgentID sender, string content);

/*bidder ask about the current goods*/

public static final KnownEvent KE_askGood(AgentID sender); /*ask about the current situation of the auction*/

public static final KnownEvent KE_askSituation(AgentID sender, string content);

/*bid from the bidders*/

public static final KnownEvent KE_bid(AgentID sender, string content); }

Hình 4.8. Mã nguồn Java tương ứng với Auctioneer.xml

public abstract class bidder {

public string description="role to make a bid for the good "; public string keyword[]= {"bidder"};

/*ASK AUCTIONEER ABOUT GOODS ON SALE*/ (adsbygoogle = window.adsbygoogle || []).push({});

public static RoleAction askGood(AgentID addressee); public static RoleAction askSituation(AgentID addressee); /*make a bid for the good interested*/

public static RoleAction bid(AgentID addressee, Price content); /*PAY THE MONEY TO AUCTIONEER*/

public static RoleAction pay(AgentID addressee, Money content); /*Auctioneer notify the requested bidder the current price*/

public static final KnownEvent KE_notifySituation(String content); /*sellers notify the winning bidder about the win*/

public static final KnownEvent KE_youWon(AgentID addressee, String content);

}

Tuy nhiên, những mã nguồn Java này mới chỉ là khung của các lớp cài đặt role tương ứng. Nội dung cài đặt chi tiết cho từng role, chúng ta sẽ tiếp tục hoàn thiện trong quá trình cài đặt thực sự. Sau đây là một phần mã nguồn cài đặt role Bidder.

...

public static final KnownEvent KE_askSituation=new KnownEvent("askSituation", Bidder.ROLE_ID);

/**

A bidder makes a bid. Sender role: Bidder Content: Bid.

*/

public static final KnownEvent KE_bid=new KnownEvent("bid", Bidder.ROLE_ID, Price.class);

/**

A bidder pays. Sender role: Bidder Content: Payment. */

public static final KnownEvent KE_pay=new KnownEvent("pay", Bidder.ROLE_ID, Money.class);

/**

Notifies to a seller that its sale has been accepted. @param addressee Seller

*/

public static RoleAction saleAccepted(int addressee) {

return new RoleAction("saleAccepted", addressee); }

... }

Một phần của tài liệu Mô hình tương tác dựa trên role trong hệ đa agent (Trang 88 - 90)