Skip to content

Commit

Permalink
2APL ah-scenario fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasschmidtjensen committed Feb 12, 2015
1 parent 367d9c7 commit 11bb78f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 17 deletions.
1 change: 1 addition & 0 deletions apapl/src/aorta/apapl/AortaBeliefbase.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void assertBelief(Literal literal) {
if (literal.getSign()) {
addToAorta(literal);
} else {
literal.setSign(true); // otherwise it tries to remove \+ p
removeFromAorta(literal);
}
}
Expand Down
15 changes: 14 additions & 1 deletion examples/2apl/auctionhouse/buyer.2apl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@ beliefs:
pgrules:
<- want(X) | { adopta(bought(X)); }
registered(Me) <- true | { @ah(register(x,y), S); }
bid(_, Item) <- want(Item) | { @ah(bid(Item), S); }

bought(Item) <- auction(Id,Item,_,_,_) | { @ah(enterAuction(Id), S); }
bought(Item) <- auction(Id,Item,_,_,_) and me(Me) and verified(Me) | { adopta(bid(Me,Item)); }

bid(bob, Item) <- auction(Id,Item,_,_,_) | { @ah(bid(Id,10), S); }
bid(carol, Item) <- auction(Id,Item,_,_,_) and bid_high(Id,bob,_) | { @ah(bid(Id,15), S); }
bid(carol, Item) <- auction(Id,Item,_,_,_) | { @ah(bid(Id,5), S); }

paidFor(Item) <- auction(Id,Item,_,_,_) and won(Id) | { @ah(pay(Id), S); }

pcrules:
event(neg(participant(Id,Me)), ah) <- me(Me) and auction(Id,Item,_,_,_) | { if G(bid(Me,Item)) dropgoal(bid(Me,Item)); }

event(won(Id), ah) <- auction(Id,Item,_,_,_) | { dropgoal(bid(Me,Item)); +bought(Item); @ah(leaveAuction(Id), S); }
14 changes: 9 additions & 5 deletions examples/2apl/auctionhouse/common.2apl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
beliefupdates:
{ true } BadInfo(Ag) { badInfo(Ag) }
{ not registered(Ag) } Register(Ag,X,Y) { registered(Ag), registered(Ag,X,Y) }
{ registered(Ag), not verified(Ag) } Verify(Ag) { verified(Ag) }
{ bid_high(OldId, OldAg, OldBid) } NewBid(Id,Ag,Bid) { not bid_high(OldId, OldAg, OldBid), bid_high(Id,Ag,Bid) }
{ true } NewBid(Id,Ag,Bid) { bid_high(Id,Ag,Bid) }

pcrules:
event(badInfo(Ag), ah) <- true | { BadInfo(Ag); }
event(registered(Ag,X,Y), ah) <- true | { Register(Ag,X,Y); }
event(badInfo(Ag), ah) <- true | { +badInfo(Ag); }
event(registered(Ag,X,Y), ah) <- true | { +registered(Ag); +registered(Ag,X,Y); }
event(auction(Id,Item,Owner,StartPrice,EndTime), ah) <- true | { +auction(Id,Item,Owner,StartPrice,EndTime); }
event(bid_high(Id,Bidder,Price), ah) <- true | { NewBid(Id,Bidder,Price); }
event(bid(Id,Bidder,Price), ah) <- true | { print(bid(Id,Bidder,Price)); +bid(Id,Bidder,Price); }
event(auction_done(Id,Winner,Bid), ah) <- true | { +auction_done(Id,Winner,Bid); }
event(participant(Id,Agent), ah) <- true | { +participant(Id,Agent); }
5 changes: 4 additions & 1 deletion examples/2apl/auctionhouse/manager.2apl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ include: common.2apl;

pgrules:
verified(Agent) <- registered(Agent) and not badInfo(Agent) |
{ @ah(verify(Agent), S); B(S = ok); Verify(Agent); }
{ @ah(verify(Agent), S); B(S = ok); +verified(Agent); }

banned(Agent) <- participant(Id,Agent) and auction(Id,_,_,_,_) |
{ @ah(removeFromAuction(Id,Agent), S); }
3 changes: 2 additions & 1 deletion examples/2apl/auctionhouse/seller.2apl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ beliefs:
pgrules:
<- have(X) | { adopta(sold(X)); }
registered(Me) <- true | { @ah(register(x,y), S); }
auction(_, Item) <- have(Item) | { @ah(startAuction(Item, 10, 10), S); }
sold(Item) <- me(Me) and verified(Me) | { adopta(auction(Me,Item)); }
auction(_, Item) <- me(Me) and registered(Me,_,_) and have(Item) and not auction(_,Item,Me,_,_) | { print("Starting auction"); @ah(startAuction(Item, 5, 10), S); }
163 changes: 154 additions & 9 deletions examples/2apl/auctionhouse/src/aorta/auctionhouse/AuctionHouse.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package aorta.auctionhouse;

import aorta.auctionhouse.model.Auction;
import aorta.auctionhouse.model.Bid;
import aorta.auctionhouse.model.Customer;
import apapl.Environment;
import apapl.ExternalActionFailedException;
import apapl.data.APLFunction;
import apapl.data.APLIdent;
import apapl.data.APLNum;
Expand Down Expand Up @@ -46,29 +48,29 @@ protected void addAgent(String ag) {
addPercept(ag, badInfo);
}

public Term register(String agName, APLIdent address, APLIdent account) {
public Term register(String agName, APLIdent address, APLIdent account) throws ExternalActionFailedException {
if (!customers.containsKey(agName)) {
Customer c = new Customer(agName, address.getName(), account.getName());
customers.put(agName, c);
addPercept(c.toAPL());
return new APLIdent("ok");
return ok();
} else {
return null;
throw new ExternalActionFailedException("not a customer");
}
}

public Term verify(String agName, APLIdent agent) {
public Term verify(String agName, APLIdent agent) throws ExternalActionFailedException {
if (customers.containsKey(agent.getName())) {
Customer c = customers.get(agent.getName());
c.setVerified(true);
addPercept(c.verifiedToAPL());
return new APLIdent("ok");
return ok();
} else {
return null;
throw new ExternalActionFailedException("not a customer");
}
}

public Term startAuction(String agName, APLIdent name, APLNum startPrice, APLNum endTime) {
public Term startAuction(String agName, APLIdent name, APLNum startPrice, APLNum endTime) throws ExternalActionFailedException {
if (customers.containsKey(agName)) {
Auction a = new Auction(generateId(), name.getName(), agName, startPrice.toInt(), endTime.toInt());
auctions.put(a.getId(), a);
Expand All @@ -77,13 +79,147 @@ public Term startAuction(String agName, APLIdent name, APLNum startPrice, APLNum

a.addParticipant(agName);
addPercept(createParticipantPercept(a.getId(), agName));

Thread t = new Thread() {
@Override
public void run() {
try {
sleep(endTime.toInt()*1000);
} catch (InterruptedException ex) {
return;
}

return new APLIdent("ok");
String winner = "no_winner";
int bid = 0;
if (a.getCurrentBid() != null) {
winner = a.getCurrentBid().getBidder();
bid = a.getCurrentBid().getBid();
}
a.setEnded(true);

APLFunction ended = new APLFunction("auction_done", new APLNum(a.getId()), new APLIdent(winner), new APLNum(bid));

addPercept(ended);

if (!"no_winner".equals(winner)) {
addPercept(winner, a.wonToAPL());
}

System.out.println("Auction ended: " + ended);
}
};
t.start();

return ok();
} else {
throw new ExternalActionFailedException("not a customer");
}
}

public Term bid(String agName, APLNum idA, APLNum bidA) throws ExternalActionFailedException {
int id = idA.toInt();
int bid = bidA.toInt();
if (customers.containsKey(agName) && auctions.containsKey(id) && auctions.get(id).participates(agName)) {
Auction auction = auctions.get(id);
Bid prev = auction.getCurrentBid();
Bid b = new Bid(id, agName, bid);
if (auction.bid(b)) {
addPercept(b.toAPL());
addPercept(b.highToAPL());
return ok();
} else {
addPercept(b.errorToAPL());
throw new ExternalActionFailedException("not a participant in auction");
}
} else {
throw new ExternalActionFailedException("not a participant in auction");
}
}

public Term pay(String agName, APLNum idA) throws ExternalActionFailedException {
int id = idA.toInt();
if (customers.containsKey(agName) && auctions.containsKey(id)) {
Auction a = auctions.get(id);
Bid currentBid = a.getCurrentBid();
if (a.getEndTime() <= time && !a.isPaid() && currentBid != null && currentBid.getBidder().equals(agName)) {
a.setPaid(true);
addPercept(createPaidPercept(id));

return ok();
}
}

throw new ExternalActionFailedException("not the winner in auction");
}

public Term deliver(String agName, APLNum idA) throws ExternalActionFailedException {
int id = idA.toInt();
if (customers.containsKey(agName) && auctions.containsKey(id)) {
Auction a = auctions.get(id);
Bid currentBid = a.getCurrentBid();
if (a.getSeller().equals(agName) && currentBid != null) {
a.setDelivered(true);
addPercept(createDeliveredPercept(id));

return ok();
}
}

throw new ExternalActionFailedException("not participant in auction");
}

public Term enterAuction(String agName, APLNum idA) throws ExternalActionFailedException {
int id = idA.toInt();
if (customers.containsKey(agName) && auctions.containsKey(id) && !auctions.get(id).participates(agName)) {
auctions.get(id).addParticipant(agName);

APLFunction percept = createParticipantPercept(id, agName);
addPercept(percept);

return ok();
} else {
return null;
throw new ExternalActionFailedException("Could not enter");
}
}

public Term removeFromAuction(String agName, APLNum idA, APLIdent agentA) throws ExternalActionFailedException {
int id = idA.toInt();
String agent = agentA.getName();

if (auctions.containsKey(id) && auctions.get(id).participates(agent)) {
Bid newHighest = auctions.get(id).removeParticipant(agent);

// TODO: Remove bids by that agent
APLFunction percept = createParticipantPercept(id, agent);
removePercept(percept);

Bid bid = new Bid(id, agent, 0);
removePercept(bid.toAPL());

if (newHighest != null) {
removePercept(bid.highToAPL());
addPercept(newHighest.highToAPL());
}

return ok();
} else {
throw new ExternalActionFailedException("Not participating in auction");
}
}

private static APLIdent ok() {
return new APLIdent("ok");
}

private APLFunction createPaidPercept(int id) {
APLFunction percept = new APLFunction("paid", new APLNum(id));
return percept;
}

private APLFunction createDeliveredPercept(int id) {
APLFunction percept = new APLFunction("delivered", new APLNum(id));
return percept;
}
private APLFunction createParticipantPercept(int id, String ag) {
APLFunction percept = new APLFunction("participant", new APLNum(id), new APLIdent(ag));
return percept;
Expand All @@ -94,6 +230,15 @@ private void addPercept(APLFunction f) {
throwEvent(f);
}

private void removePercept(APLFunction f) {
f = new APLFunction("neg", f);
throwEvent(f);
}
private void removePercept(String agent, APLFunction f) {
f = new APLFunction("neg", f);
throwEvent(f, agent);
}

private void addPercept(String agent, APLFunction f) {
log("[" + agent + "] " + f.toString());
throwEvent(f, agent);
Expand Down

0 comments on commit 11bb78f

Please sign in to comment.