org.cougaar.pizza.plugin
Class PlaceOrderPlugin

java.lang.Object
  extended byorg.cougaar.util.GenericStateModelAdapter
      extended byorg.cougaar.core.blackboard.BlackboardClientComponent
          extended byorg.cougaar.core.plugin.ComponentPlugin
              extended byorg.cougaar.pizza.plugin.PlaceOrderPlugin
All Implemented Interfaces:
org.cougaar.core.blackboard.BlackboardClient, org.cougaar.core.component.Component, org.cougaar.util.GenericStateModel, org.cougaar.core.plugin.PluginBase
Direct Known Subclasses:
SDPlaceOrderPlugin

public class PlaceOrderPlugin
extends org.cougaar.core.plugin.ComponentPlugin

This plugin orders the pizza for a pizza party. It subscribes to a PizzaPreferences object published by the InvitePlugin when it knows how much of what kind of pizza to order.

On receiving the PizzaPreferences, it creates and publishes a Task with the Verb "Order" and a Direct Object of type Pizza (Asset). Next, it expands the Order Task into a workflow of two subtasks, one subtask per type of pizza with a quantity Preference for the number of servings needed. To place the order, it allocates the subtasks to its pizza provider, Joes Local Pizza Shack. This customer/provider relationship is defined in the XML configuration files and is established when the agents start up. See PizzaNode1.xml and PizzaNode2.xml for details.

In this example, the plugin cannot successfully complete the pizza order because Joe's Local Pizza Shack doesn't make veggie pizzas. The final result is a failed Expansion on the parent order Task due to the failed Allocation of the veggie pizza subtask.


Nested Class Summary
 
Nested classes inherited from class org.cougaar.core.blackboard.BlackboardClient
org.cougaar.core.blackboard.BlackboardClient.Local
 
Field Summary
protected static org.cougaar.util.UnaryPredicate ALLOCATION_PRED
          This predicate matches Allocations on "Order" tasks.
protected  org.cougaar.core.blackboard.IncrementalSubscription allocationSub
           
protected  org.cougaar.core.service.DomainService domainService
           
protected static org.cougaar.util.UnaryPredicate EXPANSION_PRED
          This predicate matches Expansions on "Order" tasks.
protected  org.cougaar.core.blackboard.IncrementalSubscription expansionSub
           
protected  org.cougaar.core.service.LoggingService logger
           
protected static org.cougaar.util.UnaryPredicate PIZZA_PREF_PRED
          This predicate matches PizzaPreferences objects.
protected  org.cougaar.core.blackboard.IncrementalSubscription pizzaPrefSub
           
protected  org.cougaar.planning.ldm.PlanningFactory planningFactory
           
protected static org.cougaar.util.UnaryPredicate SELF_PRED
          This predicate matches the Entity object of the agent.
protected  org.cougaar.core.blackboard.IncrementalSubscription selfSub
           
 
Fields inherited from class org.cougaar.core.blackboard.BlackboardClientComponent
agentId, agentIdentificationService, alarmService, blackboard, blackboardClientName
 
Fields inherited from interface org.cougaar.core.plugin.PluginBase
INSERTION_POINT
 
Fields inherited from interface org.cougaar.util.GenericStateModel
ACTIVE, IDLE, LOADED, UNINITIALIZED, UNLOADED
 
Fields inherited from interface org.cougaar.core.blackboard.BlackboardClient
current
 
Constructor Summary
PlaceOrderPlugin()
           
 
Method Summary
protected  void allocateSubtasks(java.util.Collection subtasks, org.cougaar.planning.ldm.asset.Entity provider)
          Creates and publishes Allocations for each subtask in the collection.
protected  org.cougaar.planning.ldm.plan.Task createOrderTask()
          Returns a Task for ordering pizza.
protected  java.util.Collection createPizzaSubtasks(PizzaPreferences pizzaPrefs, org.cougaar.planning.ldm.plan.Task parentTask)
          Returns a Collection of subtasks for ordering meat and veggie pizzas.
protected  void execute()
          When there are changes to the plugins's subscriptions, we: 1) When the PizzaPreferences arrives, create the root Order Task, and expand it by the types of pizza.
protected  org.cougaar.planning.ldm.plan.Expansion getChangedExpansion()
          Returns the Expansion that was changed (the one and only typically).
protected  PizzaPreferences getPizzaPreferences()
          Returns any added PizzaPreferences object from the PizzaPreferences Subscription.
protected  org.cougaar.planning.ldm.asset.Entity getProvider()
          Returns a pizza provider for this agent.
protected  org.cougaar.planning.ldm.asset.Entity getSelfEntity()
          Returns the Entity representing the agent.
 void load()
          Loads services used by the plugin.
protected  void logExpansionResults(org.cougaar.planning.ldm.plan.Expansion exp)
          Log (at SHOUT) the results of the given Expansion, showing what was ordered, how much, from whom, and with what success.
protected  void makeExpansion(org.cougaar.planning.ldm.plan.Task parentTask, java.util.Collection subtasks)
          Creates and adds an Expansion on a Task and publishes it to the blackboard.
protected  PizzaAsset makePizzaAsset(java.lang.String assetType)
          Creates an instance of a PizzaAsset of the specified asset type.
protected  org.cougaar.planning.ldm.plan.Preference makeQuantityPreference(int value)
          Returns a quantity Preference, representing the number of servings of pizza to order.
protected  org.cougaar.planning.ldm.plan.NewTask makeTask(org.cougaar.planning.ldm.plan.Verb verb, org.cougaar.planning.ldm.asset.Asset directObject)
          Returns a NewTask.
protected  java.lang.String printAllocationResults(org.cougaar.planning.ldm.plan.PlanElement pe)
           
protected  java.lang.String printExpansionResults(org.cougaar.planning.ldm.plan.Expansion exp)
           
 void setDomainService(org.cougaar.core.service.DomainService aDomainService)
          Services that are absolutely required by the plugin can be loaded via introspection by the binding utility instead of explicitly getting each service from the ServiceBroker in load().
protected  void setupSubscriptions()
          Initialize the subcriptions the plugin is interested in: self Entity, the PizzaPreferences, and existing Allocations or Expansions or Order Tasks.
protected  void updateOrderAllocationResults()
          Updates the estimated AllocationResults with reported AllocationResults.
 
Methods inherited from class org.cougaar.core.plugin.ComponentPlugin
getConfigFinder
 
Methods inherited from class org.cougaar.core.blackboard.BlackboardClientComponent
currentTimeMillis, cycle, getAgentIdentifier, getAlarmService, getBindingSite, getBlackboardClientName, getBlackboardService, getMessageAddress, getParameter, getParameters, getServiceBroker, halt, precycle, requestCycle, resume, setAgentIdentificationService, setAlarmService, setBindingSite, setBlackboardService, setParameter, setSchedulerService, setServiceBroker, setSuicideService, shouldExecute, start, stop, suspend, toString, unload, wasAwakened
 
Methods inherited from class org.cougaar.util.GenericStateModelAdapter
getModelState, initialize, transitState
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.cougaar.util.GenericStateModel
getModelState, halt, initialize, resume, start, stop, suspend, unload
 

Field Detail

logger

protected org.cougaar.core.service.LoggingService logger

domainService

protected org.cougaar.core.service.DomainService domainService

planningFactory

protected org.cougaar.planning.ldm.PlanningFactory planningFactory

selfSub

protected org.cougaar.core.blackboard.IncrementalSubscription selfSub

pizzaPrefSub

protected org.cougaar.core.blackboard.IncrementalSubscription pizzaPrefSub

allocationSub

protected org.cougaar.core.blackboard.IncrementalSubscription allocationSub

expansionSub

protected org.cougaar.core.blackboard.IncrementalSubscription expansionSub

SELF_PRED

protected static final org.cougaar.util.UnaryPredicate SELF_PRED
This predicate matches the Entity object of the agent.


PIZZA_PREF_PRED

protected static final org.cougaar.util.UnaryPredicate PIZZA_PREF_PRED
This predicate matches PizzaPreferences objects.


ALLOCATION_PRED

protected static final org.cougaar.util.UnaryPredicate ALLOCATION_PRED
This predicate matches Allocations on "Order" tasks.


EXPANSION_PRED

protected static final org.cougaar.util.UnaryPredicate EXPANSION_PRED
This predicate matches Expansions on "Order" tasks.

Constructor Detail

PlaceOrderPlugin

public PlaceOrderPlugin()
Method Detail

setDomainService

public void setDomainService(org.cougaar.core.service.DomainService aDomainService)
Services that are absolutely required by the plugin can be loaded via introspection by the binding utility instead of explicitly getting each service from the ServiceBroker in load(). The setter methods are called after the component is constructed but before the state methods such as initialize, load, setupSubscriptions, etc. If the service is not available at that time, the component will be unloaded.


load

public void load()
Loads services used by the plugin.


setupSubscriptions

protected void setupSubscriptions()
Initialize the subcriptions the plugin is interested in: self Entity, the PizzaPreferences, and existing Allocations or Expansions or Order Tasks.


execute

protected void execute()
When there are changes to the plugins's subscriptions, we: 1) When the PizzaPreferences arrives, create the root Order Task, and expand it by the types of pizza. Then allocate these to the available provider. 2) Propagate up any changed Allocation Results 3) See if the root Expansion changed. If so, we may be done.


logExpansionResults

protected void logExpansionResults(org.cougaar.planning.ldm.plan.Expansion exp)
Log (at SHOUT) the results of the given Expansion, showing what was ordered, how much, from whom, and with what success.

Parameters:
exp - The Expansion of the root Order task, whose details we print

getPizzaPreferences

protected PizzaPreferences getPizzaPreferences()
Returns any added PizzaPreferences object from the PizzaPreferences Subscription. Checks the added collection on the subscription and returns the first element in the collection. Will return null if the added collection is empty.

Note that since we only check the Added list, this method will only return an object once in our application. This keeps the plugin from publishing the Order Tasks each time the plugin runs.

Returns:
first PizzaPrefereneces object from the subscription

createOrderTask

protected org.cougaar.planning.ldm.plan.Task createOrderTask()
Returns a Task for ordering pizza. Creates a Task with Verb "Order", makes a Pizza asset and sets it as the direct object of the task. Next, it publishes the task to the blackboard.

Returns:
a task for ordering pizza

createPizzaSubtasks

protected java.util.Collection createPizzaSubtasks(PizzaPreferences pizzaPrefs,
                                                   org.cougaar.planning.ldm.plan.Task parentTask)
Returns a Collection of subtasks for ordering meat and veggie pizzas. Creates a meat pizza subtask and a veggie pizza subtask. The number of individuals requesting meat and veggie pizzas are obtained from the PizzaPreferences object. A quantity Preference is created to represent the number of servings needed and is added to the appropriate subtask. The parentTask is set as the parent of the subtasks.

Parameters:
pizzaPrefs - contains the number of people requesting meat or veggie pizzas
parentTask - the parent of the subtasks
Returns:
a Collection of pizza subtasks

makeExpansion

protected void makeExpansion(org.cougaar.planning.ldm.plan.Task parentTask,
                             java.util.Collection subtasks)
Creates and adds an Expansion on a Task and publishes it to the blackboard. An Expansion is created containing a Workflow of the specified subtasks. This Expansion is set to the parent task. The subtasks are published to the blackboard first, then the Expansion is published.

Parameters:
parentTask - the parent task to be expanded
subtasks - the collection of subtasks to be added to the Workflow of Expansion

allocateSubtasks

protected void allocateSubtasks(java.util.Collection subtasks,
                                org.cougaar.planning.ldm.asset.Entity provider)
Creates and publishes Allocations for each subtask in the collection. The Allocation includes: the provider assigned to compelete the task, an estimated AllocationResult, and the Role of PizzaProvider. The estimated AllocationResult includes: an estimated confidence rating of 0.25 and success is true. Valid range for the confidence rating is between 0 and 1. The assumption is, as the subtasks are completed, the allocation results will become closer to 1. The Allocations are then published to the blackboard.

Parameters:
subtasks - a collection of subtasks to be allocated
provider - the provider to which the subtasks are allocated

updateOrderAllocationResults

protected void updateOrderAllocationResults()
Updates the estimated AllocationResults with reported AllocationResults.

Remember that the infrastructure sets the ReportedResult for you (in this case, by copying it from the provider agent when the provider settles the Task), but you must copy that up typically.

Checks the changed collection on the Allocation subscription. If the collection is not empty, it retrieves the Allocation PlanElement from the subscription. If the reported AllocationResults have changed, the results are udpated and a publish change is called on the PlanElement. The reported AllocationResults are changed by the provider assigned to the task.


getChangedExpansion

protected org.cougaar.planning.ldm.plan.Expansion getChangedExpansion()
Returns the Expansion that was changed (the one and only typically).

Checks the changed collection on the Subscription. If the collection is not empty, it returns the first element. If empty it returns null.

Returns:
the Expansion that was changed if any

makeTask

protected org.cougaar.planning.ldm.plan.NewTask makeTask(org.cougaar.planning.ldm.plan.Verb verb,
                                                         org.cougaar.planning.ldm.asset.Asset directObject)
Returns a NewTask. Creates a task with the specified verb and sets the Asset as the direct object.

Parameters:
verb - the verb of the newtask
directObject - asset that this Task is acting on
Returns:
a NewTask

makePizzaAsset

protected PizzaAsset makePizzaAsset(java.lang.String assetType)
Creates an instance of a PizzaAsset of the specified asset type. Adds an ItemIdentification property group to the pizzaAsset instance marking the pizza type.

Parameters:
assetType - the name of the type of asset (veg/meat)
Returns:
a PizzaAsset

makeQuantityPreference

protected org.cougaar.planning.ldm.plan.Preference makeQuantityPreference(int value)
Returns a quantity Preference, representing the number of servings of pizza to order.

Creates a Preference with a Strictly-at ScoringFunction and a quantity AspectType from the specified value. In other words, says I want this many, and you get 0 credit for any more or less.

Parameters:
value - the number required
Returns:
a Preference requesting that number of items exactly

getProvider

protected org.cougaar.planning.ldm.asset.Entity getProvider()
Returns a pizza provider for this agent. Get all relationships that match the Role of PizzaProvider from the RelationshipSchedule. Return the provider Entity from the first Relationship found. If there are no pizza provider relationships, null will be returned.

Returns:
a pizza provider Entity

getSelfEntity

protected org.cougaar.planning.ldm.asset.Entity getSelfEntity()
Returns the Entity representing the agent. Checks the self entity subscription and returns the first element. In this example, there should be only one self entity. Will return null if the subscription is empty.

Returns:
the Entity representing the agent.

printExpansionResults

protected java.lang.String printExpansionResults(org.cougaar.planning.ldm.plan.Expansion exp)

printAllocationResults

protected java.lang.String printAllocationResults(org.cougaar.planning.ldm.plan.PlanElement pe)