booktrading /seller

<html><head><title>

jadex.examples.booktrading.seller

</title></head><body>
 
This package contains the seller agent as part of the booktrading scenario.

</body></html>
<!--
 <H3>The seller agent sells books for its user.</H3>

 The seller agent comes with a user interface in which the
 human user can enter its sell book orders consisting of
 a title, start price, price limit and a deadline. The agent
 subsequently tries to sell 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="Seller" package="jadex.examples.booktrading.seller">

 <imports>
  <import>jadex.examples.booktrading.common.*</import>
  <import>jadex.adapter.fipa.SFipa</import>
  <import>jadex.runtime.*</import>
  <import>jadex.runtime.impl.*</import>
  <import>java.util.*</import>
  <import>jadex.planlib.*</import>
 </imports>

 <capabilities>
  <capability name="procap" file="jadex.planlib.Protocols"/>
  <capability name="dfcap" file="jadex.planlib.DF"/>
 </capabilities>

 <beliefs>
  <belief name="time" class="long" updaterate="1000">
   <fact>System.currentTimeMillis()</fact>
  </belief>
  
  <beliefset name="orders" class="Order">
   <facts evaluationmode="dynamic">
    select $g.getParameter("order").getValue()
    from IRGoal $g in $goalbase.getGoals("sell_book")
   </facts>
  </beliefset>
  
  <belief name="initial_orders" class="Order[]" exported="true"/>
  
  <beliefref name="cnp_filter" class="IFilter">
   <concrete ref="procap.cnp_filter"/>
  </beliefref>
  
  <beliefset name="negotiation_reports" class="NegotiationReport"/>
  
  <belief name="gui" class="Gui"/>
 </beliefs>

 <goals>
  <achievegoal name="sell_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>
  
  <querygoalref name="cnp_make_proposal">
   <concrete ref="procap.cnp_make_proposal"/>
  </querygoalref>
  
  <achievegoalref name="cnp_execute_task">
   <concrete ref="procap.cnp_execute_task"/>
  </achievegoalref>
  
  <achievegoalref name="df_register">
   <concrete ref="dfcap.df_register"/>
  </achievegoalref>
  
  <achievegoalref name="df_deregister">
   <concrete ref="dfcap.df_deregister"/>
  </achievegoalref>
 </goals>

 <plans>
  <plan name="make_proposal">
   <parameter name="cfp" class="Object">
    <goalmapping ref="cnp_make_proposal.cfp"/>
   </parameter>
   <parameter name="proposal" class="Object" direction="out">
    <goalmapping ref="cnp_make_proposal.proposal"/>
   </parameter>
   <parameter name="proposal_info" class="Order" direction="out">
    <goalmapping ref="cnp_make_proposal.proposal_info"/>
   </parameter>
   <parameterset name="suitableorders" class="Order">
    <values>select Order $order from $beliefbase.orders
     where $order.getTitle().equals($plan.task) &amp;&amp;
     $order.getState().equals(Order.OPEN)
     order by ($beliefbase.time - $order.getStartTime()) /
     ($order.getDeadline().getTime()-$order.getStartTime())</values>
   </parameterset>
   <body class="MakeProposalPlan" />
   <!-- <body>new MakeProposalPlan()</body> -->
   <trigger>
    <goal ref="cnp_make_proposal"/>
   </trigger>
  </plan>
  
  <plan name="execute_order">
   <parameter name="proposal" class="Object">
    <goalmapping ref="cnp_execute_task.proposal"/>
   </parameter>
   <parameter name="proposal_info" class="Object">
    <goalmapping ref="cnp_execute_task.proposal_info"/>
   </parameter>
   <parameter name="result" class="Object">
    <goalmapping ref="cnp_execute_task.result"/>
   </parameter>
   <body class="ExecuteOrderPlan" />
   <!-- <body>new ExecuteOrderPlan()</body> -->
   <trigger>
    <goal ref="cnp_execute_task"/>
   </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="logging.level">java.util.logging.Level.FINE</property>-->
 </properties>

 <configurations>
  <configuration name="default">
   <beliefs>
    <initialbelief ref="cnp_filter">
     <fact>IFilter.ALWAYS</fact>
    </initialbelief>
    <initialbelief ref="gui">
     <fact>new Gui($agent.getExternalAccess(), false)</fact>
    </initialbelief>
   </beliefs>
   <goals>
    <initialgoal ref="df_register">
     <parameter ref="description">
      <value>SFipa.createAgentDescription(null, SFipa.createServiceDescription("sell",
       "service_seller", "UniHH"))</value>
     </parameter>
    </initialgoal>
    <!--<initialgoal ref="sell_book">
     <parameter ref="order">
      <value>new Order("All about agents", new Date(
       System.currentTimeMillis()+60000), 85, 65, false)</value>
     </parameter>
    </initialgoal>-->
    <endgoal ref="df_deregister"/>
   </goals>
  </configuration>
 </configurations>

</agent>package jadex.examples.booktrading.seller;

import jadex.examples.booktrading.common.NegotiationReport;
import jadex.examples.booktrading.common.Order;
import jadex.runtime.Plan;

import java.util.Date;

/**
 * Execute the order by setting execution price and date.
 */
public class ExecuteOrderPlan extends Plan
{
 /**
  * The body method is called on the
  * instatiated plan instance from the scheduler.
  */
 public void body()
 {
  // Extract order data.
  Integer price = (Integer)getParameter("proposal").getValue();
  Order order = (Order)getParameter("proposal_info").getValue();
  
  // Initiate payment and delivery.
  // IGoal pay = createGoal("payment");
  // pay.getParameter("order").setValue(order);
  // dispatchSubgoalAndWait(pay);
  // IGoal delivery = createGoal("delivery");
  // delivery.getParameter("order").setValue(order);
  // dispatchSubgoalAndWait(delivery);
 
  // Save successful transaction data.
  order.setExecutionPrice(price);
  order.setExecutionDate(new Date());
  
  String report = "Sold for: "+price;
  NegotiationReport nr = new NegotiationReport(order, report, System.currentTimeMillis());
  getBeliefbase().getBeliefSet("negotiation_reports").addFact(nr);

  getParameter("result").setValue(price);
 }
}
package jadex.examples.booktrading.seller;

import jadex.examples.booktrading.common.NegotiationReport;
import jadex.examples.booktrading.common.Order;
import jadex.runtime.Plan;

/**
 * The plan has the purpose to make an proposal for selling a book.
 */
public class MakeProposalPlan extends Plan
{
 /**
  * The body method is called on the
  * instatiated plan instance from the scheduler.
  */
 public void body()
 {
  // Search suitable open orders.
//  Order[] suitableorders = (Order[])getParameterSet("suitableorders").getValues();
  Order order = (Order)createExpression("select one Order $order from $beliefbase.orders"
   + " where $order.getTitle().equals($task) && $order.getState().equals(Order.OPEN)"
   + " order by ($beliefbase.time - $order.getStartTime()) / ($order.getDeadline().getTime()-$order.getStartTime())")
   .execute("$task", getParameter("cfp").getValue());
  
  // Use most urgent order for preparing proposal.
//  if(suitableorders.length > 0)
  if(order!=null)
  {
//   Order order = suitableorders[0];
   
   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();
   getLogger().info(getAgentName()+" proposed: " + acceptable_price);
   
   // Store proposal data in plan parameters.
   getParameter("proposal").setValue(new Integer(acceptable_price));
   getParameter("proposal_info").setValue(order);
   
   String report = "Made proposal: "+acceptable_price;
   NegotiationReport nr = new NegotiationReport(order, report, System.currentTimeMillis());
   getBeliefbase().getBeliefSet("negotiation_reports").addFact(nr);
  }
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章