001: /*
002: * <copyright>
003: * Copyright 1997-2004 BBNT Solutions, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency
005: * (DARPA).
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the Cougaar Open Source License as published by
009: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
010: *
011: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
012: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
013: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
014: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
015: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
016: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
017: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
018: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
019: * PERFORMANCE OF THE COUGAAR SOFTWARE.
020: * </copyright>
021: */
022: package org.cougaar.pizza.plugin;
023:
024: import org.cougaar.core.plugin.ComponentPlugin;
025: import org.cougaar.core.service.DomainService;
026: import org.cougaar.planning.ldm.PlanningFactory;
027: import org.cougaar.planning.ldm.asset.NewItemIdentificationPG;
028: import org.cougaar.planning.service.PrototypeRegistryService;
029: import org.cougaar.pizza.asset.KitchenAsset;
030: import org.cougaar.pizza.asset.PropertyGroupFactory;
031: import org.cougaar.util.Arguments;
032:
033: /**
034: * This Plugin creates and publishes the Pizza Provider Kitchen Asset object which identifies
035: * what kind of pizza a provider's kitchen can make. This plugin does not require any inputs,
036: * it simply creates and registers the prototype and then publishes the kitchen asset instances.
037: * Note that this plugin will only run once since there are no inputs(subscriptions) to cause it to run.
038: */
039: public class KitchenPrototypePlugin extends ComponentPlugin {
040:
041: // The domainService provides domain factory services
042: private DomainService domainService = null;
043:
044: // The prototypeRegistryService provides prototype registration services
045: private PrototypeRegistryService prototypeRegistryService = null;
046:
047: // The planning factory we use to create planning objects.
048: private PlanningFactory planningFactory;
049:
050: // Initialize the plugin args to an empty instance
051: private Arguments args = Arguments.EMPTY_INSTANCE;
052:
053: /**
054: * Variables for storing plugin arguments that determine whether the kitchen assets are
055: * capable of creating meat and/or veggie pizzas. Default is to create both.
056: */
057: private boolean createVeggie = true;
058: private boolean createMeat = true;
059: private final String PIZZA_TYPE_PARAM = "PIZZA_TYPES_PROVIDED";
060:
061: /**
062: * Used by the binding utility through introspection to set my DomainService
063: * Services that are required for plugin usage should be set through reflection instead of explicitly
064: * getting each service from your ServiceBroker in the load method. The setter methods are called after
065: * the component is constructed but before the state methods such as initialize, load, setupSubscriptions, etc.
066: * If the service is not available at that time the component will be unloaded.
067: */
068: public void setDomainService(DomainService aDomainService) {
069: domainService = aDomainService;
070: }
071:
072: /**
073: * Used by the binding utility through introspection to set my PrototypeRegistryService
074: */
075: public void setPrototypeRegistryService(
076: PrototypeRegistryService aPrototypeRegistryService) {
077: prototypeRegistryService = aPrototypeRegistryService;
078: }
079:
080: /**
081: * Get the plugin parameters and our planning factory when our component is loaded.
082: * If we want additional services that are not required for the plugin, we should get them in load.
083: * However, if the component gets a service in load, it should override the unload() method and
084: * release the services.
085: */
086: public void load() {
087: super .load();
088: planningFactory = (PlanningFactory) domainService
089: .getFactory("planning");
090: // unload the domain service since we only need it to get the planning factory
091: getServiceBroker().releaseService(this , DomainService.class,
092: domainService);
093:
094: // Process the plugin params
095: args = new Arguments(getParameter());
096: setKitchenParameters();
097: }
098:
099: /**
100: * Generally used to initalize plugin subscriptions. But in this case, we will use it to
101: * create our Kitchen assets since we have no subscriptions and we want to publish the assets
102: * as soon as we can.
103: */
104: protected void setupSubscriptions() {
105: createKitchenAssets();
106: }
107:
108: /**
109: * Create the Kitchen prototype and asset instances with the appropriate
110: * veggie and/or meat PGs based on the existing kitchen prototype and the plugin arguments.
111: */
112: private void createKitchenAssets() {
113: KitchenAsset new_prototype = (KitchenAsset) planningFactory
114: .createPrototype(KitchenAsset.class, "kitchen");
115: // Cache the prototype in the LDM so that it can be used to create
116: // instances of Kitchen assets when asked for by prototype name
117: prototypeRegistryService.cachePrototype("kitchen",
118: new_prototype);
119: KitchenAsset kitchen_asset = (KitchenAsset) planningFactory
120: .createInstance("kitchen");
121: // Check the plugin arguments to see if this plugin should create a Kitchen asset that can make veggie pizza.
122: if (createVeggie) {
123: kitchen_asset.addOtherPropertyGroup(PropertyGroupFactory
124: .newVeggiePG());
125: }
126: // Check the plugin arguments to see if this plugin should create a Kitchen asset that can make meat pizza.
127: if (createMeat) {
128: kitchen_asset.addOtherPropertyGroup(PropertyGroupFactory
129: .newMeatPG());
130: }
131: NewItemIdentificationPG itemIDPG = PropertyGroupFactory
132: .newItemIdentificationPG();
133: itemIDPG.setItemIdentification("Pizza Kitchen");
134: kitchen_asset.setItemIdentificationPG(itemIDPG);
135: getBlackboardService().publishAdd(kitchen_asset);
136: }
137:
138: /**
139: * No subscriptions to process, so this method does nothing.
140: */
141: protected void execute() {
142: }
143:
144: /**
145: * Set the createVeggie and createMeat plugin variables according to the plugin parameter
146: */
147: private void setKitchenParameters() {
148: String paramValue = args.getString(PIZZA_TYPE_PARAM);
149: if (paramValue != null) {
150: if ("all".equals(paramValue)) {
151: createVeggie = true;
152: createMeat = true;
153: } else if ("veggie_only".equals(paramValue)) {
154: createVeggie = true;
155: createMeat = false;
156: } else if ("meat_only".equals(paramValue)) {
157: createVeggie = false;
158: createMeat = true;
159: }
160: }
161: }
162: }
|