<!--
<H3>The buyer agent that purchases books for its user.</H3>
The buyer agent comes with a user interface in which the
human user can enter its purchase book orders consisting of
a title, start price, price limit and a deadline. The agent
subsequently tries to buy the book and changes the price
according to the deadline.
-->
<agent xmlns="http://jadex.sourceforge.net/jadex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jadex.sourceforge.net/jadex
http://jadex.sourceforge.net/jadex-0.96.xsd"
name="Buyer" package="jadex.examples.booktrading.buyer">
<imports>
<import>jadex.examples.booktrading.common.*</import>
<import>jadex.adapter.fipa.SFipa</import>
<import>java.util.*</import>
<import>jadex.runtime.impl.*</import>
<import>jadex.planlib.*</import>
</imports>
<capabilities>
<capability name="procap" file="jadex.planlib.Protocols"/>
<capability name="dfcap" file="jadex.planlib.DF"/>
</capabilities>
<beliefs>
<beliefset name="orders" class="Order">
<facts evaluationmode="dynamic">
select $g.getParameter("order").getValue()
from IRGoal $g in $goalbase.getGoals("purchase_book")
</facts>
</beliefset>
<belief name="time" class="long" updaterate="1000">
<fact>System.currentTimeMillis()</fact>
</belief>
<belief name="initial_orders" class="Order[]" exported="true"/>
<beliefset name="negotiation_reports" class="NegotiationReport"/>
<belief name="gui" class="Gui"/>
</beliefs>
<goals>
<!-- Initiate negotiation rounds every 10 secs. -->
<achievegoal name="purchase_book" recur="true" recurdelay="10000">
<parameter name="order" class="Order">
<bindingoptions>$beliefbase.initial_orders</bindingoptions>
</parameter>
<unique/>
<creationcondition>$beliefbase.initial_orders!=null</creationcondition>
<targetcondition>Order.DONE.equals($goal.order.getState())</targetcondition>
<failurecondition>$beliefbase.time > $goal.order.getDeadline().getTime()</failurecondition>
</achievegoal>
<achievegoalref name="df_search">
<concrete ref="dfcap.df_search"/>
</achievegoalref>
<achievegoalref name="cnp_initiate">
<concrete ref="procap.cnp_initiate"/>
</achievegoalref>
<querygoalref name="cnp_evaluate_proposals">
<concrete ref="procap.cnp_evaluate_proposals"/>
</querygoalref>
</goals>
<plans>
<plan name="purchase_book_plan">
<parameter name="order" class="Order">
<goalmapping ref="purchase_book.order"/>
</parameter>
<body class="PurchaseBookPlan" />
<!-- <body>new PurchaseBookPlan()</body> -->
<trigger>
<goal ref="purchase_book"/>
</trigger>
</plan>
<plan name="evaluate_proposals_plan">
<parameter name="cfp" class="Object">
<goalmapping ref="cnp_evaluate_proposals.cfp"/>
</parameter>
<parameter name="cfp_info" class="Object" optional="true">
<goalmapping ref="cnp_evaluate_proposals.cfp_info"/>
</parameter>
<parameterset name="proposals" class="Object">
<goalmapping ref="cnp_evaluate_proposals.proposals"/>
</parameterset>
<parameterset name="history" class="NegotiationRecord" optional="true">
<goalmapping ref="cnp_evaluate_proposals.history"/>
</parameterset>
<parameterset name="acceptables" class="Object" direction="out">
<goalmapping ref="cnp_evaluate_proposals.acceptables"/>
</parameterset>
<body class="EvaluateProposalsPlan" />
<trigger>
<goal ref="cnp_evaluate_proposals"/>
</trigger>
</plan>
</plans>
<expressions>
<expression name="search_reports">
select NegotiationReport $nr from $beliefbase.negotiation_reports
where $nr.getOrder().equals($order)
order by $nr.getTime()
<parameter name="$order" class="Order"/>
</expression>
</expressions>
<properties>
<property name="service_seller">
SFipa.createAgentDescription(null, SFipa.createServiceDescription(null, "service_seller", null))
</property>
<!--<property name="logging.level">java.util.logging.Level.FINE</property>-->
<!--<property name="debugging">true</property>-->
</properties>
<configurations>
<configuration name="default">
<beliefs>
<initialbelief ref="gui">
<fact>new Gui($agent.getExternalAccess(), true)</fact>
</initialbelief>
</beliefs>
<goals>
<!--<initialgoal ref="purchase_book">
<parameter ref="order">
<value>new Order("All about agents",
new Date(System.currentTimeMillis()+60000), 75, 110, true)</value>
</parameter>
</initialgoal>-->
</goals>
</configuration>
</configurations>
</agent>package jadex.examples.booktrading.buyer;
import jadex.planlib.ParticipantProposal;
import jadex.runtime.Plan;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Evaluate the received proposals.
*/
public class EvaluateProposalsPlan extends Plan
{
/**
* The plan body.
*/
public void body()
{
// Get order properties and calculate acceptable price.
int acceptable_price = ((Integer)getParameter("cfp_info").getValue()).intValue();
ParticipantProposal[] proposals = (ParticipantProposal[])getParameterSet("proposals").getValues();
// Determine acceptables
List accs = new ArrayList();
for(int i=0; i<proposals.length; i++)
{
if(((Integer)proposals[i].getProposal()).intValue() <= acceptable_price)
accs.add(proposals[i]);
}
// Sort acceptables by price.
if(accs.size()>1)
{
Collections.sort(accs, new Comparator()
{
public int compare(Object arg0, Object arg1) {
return ((Comparable) ((ParticipantProposal)arg0).getProposal())
.compareTo(((ParticipantProposal)arg1).getProposal());
}
});
}
if(accs.size()>0)
getParameterSet("acceptables").addValue(accs.get(0));
}
}package jadex.examples.booktrading.buyer;
import jadex.adapter.fipa.AgentDescription;
import jadex.adapter.fipa.AgentIdentifier;
import jadex.examples.booktrading.common.NegotiationReport;
import jadex.examples.booktrading.common.Order;
import jadex.planlib.NegotiationRecord;
import jadex.planlib.ParticipantProposal;
import jadex.planlib.Selector;
import jadex.runtime.GoalFailureException;
import jadex.runtime.IGoal;
import jadex.runtime.Plan;
import jadex.util.SUtil;
import java.util.Comparator;
import java.util.Date;
/**
* The plan tries to purchase a book.
*/
public class PurchaseBookPlan extends Plan
{
//-------- methods --------
/**
* The body method is called on the
* instatiated plan instance from the scheduler.
*/
public void body()
{
// Get order properties and calculate acceptable price.
Order order = (Order)getParameter("order").getValue();
double time_span = order.getDeadline().getTime() - order.getStartTime();
double elapsed_time = System.currentTimeMillis() - order.getStartTime();
double price_span = order.getLimit() - order.getStartPrice();
int acceptable_price = (int)(price_span * elapsed_time / time_span)
+ order.getStartPrice();
// Find available seller agents.
IGoal df_search = createGoal("df_search");
df_search.getParameter("description").setValue(getPropertybase().getProperty("service_seller"));
dispatchSubgoalAndWait(df_search);
AgentDescription[] result = (AgentDescription[])df_search
.getParameterSet("result").getValues();
if(result.length == 0)
fail();
AgentIdentifier[] sellers = new AgentIdentifier[result.length];
for(int i = 0; i < result.length; i++)
sellers[i] = result[i].getName();
//System.out.println("found: "+SUtil.arrayToString(sellers));
// Initiate a call-for-proposal.
IGoal cnp = createGoal("cnp_initiate");
cnp.getParameter("cfp").setValue(order.getTitle());
cnp.getParameter("cfp_info").setValue(new Integer(acceptable_price));
cnp.getParameterSet("receivers").addValues(sellers);
try
{
dispatchSubgoalAndWait(cnp);
NegotiationRecord rec = (NegotiationRecord)cnp.getParameterSet("history").getValues()[0];
generateNegotiationReport(order, rec, acceptable_price);
// If contract-net succeeds, store result in order object.
order.setExecutionPrice((Integer)(cnp.getParameterSet("result").getValues()[0]));
order.setExecutionDate(new Date());
}
catch(GoalFailureException e)
{
NegotiationRecord rec = (NegotiationRecord)cnp.getParameterSet("history").getValues()[0];
generateNegotiationReport(order, rec, acceptable_price);
fail();
}
//System.out.println("result: "+cnp.getParameter("result").getValue());
}
/**
* Generate and add a negotiation report.
*/
protected void generateNegotiationReport(Order order, NegotiationRecord rec, double acceptable_price)
{
String report = "Accepable price: "+acceptable_price+", proposals: ";
ParticipantProposal[] proposals = rec.getProposals();
for(int i=0; i<proposals.length; i++)
{
report += proposals[i].getProposal()+"-"+proposals[i].getParticipant().getLocalName();
if(i+1<proposals.length)
report += ", ";
}
NegotiationReport nr = new NegotiationReport(order, report, rec.getStarttime());
//System.out.println("REPORT of agent: "+getAgentName()+" "+report);
getBeliefbase().getBeliefSet("negotiation_reports").addFact(nr);
}
}