View Javadoc

1   /*
2    * @(#) YellowPage.java Feb 27, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package nl.tudelft.simulation.yellowpage;
15  
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Map;
21  
22  import nl.tudelft.simulation.actor.ActorInterface;
23  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
24  import nl.tudelft.simulation.logger.Logger;
25  import nl.tudelft.simulation.naming.InitialEventContext;
26  import nl.tudelft.simulation.naming.context.ContextUtil;
27  
28  /***
29   * A YellowPage implementation, which uses the Context package to store
30   * information about actors. <br>
31   * <br>
32   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
33   * Delft, the Netherlands. All rights reserved.
34   * 
35   * See for project information <a href="http://www.simulation.tudelft.nl/">
36   * www.simulation.tudelft.nl </a>.
37   * 
38   * The source code and binary code of this software is proprietary information
39   * of Delft University of Technology.
40   * 
41   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
42   *         Jacobs </a>
43   * @version $$Revision: 1.3 $$ $$Date: 2005/04/08 11:29:13 $$
44   */
45  public class YellowPage implements YellowPageInterface
46  {
47  	/*** the serial version uid */
48  	private static final long serialVersionUID = 12L;
49  
50  	/*** a map of entries */
51  	protected Map entries = new HashMap();
52  
53  	/*** the parent of this yellowPage */
54  	protected YellowPage parent = null;
55  
56  	/***
57  	 * resolves the yellowPage
58  	 * 
59  	 * @param simulator the simulator
60  	 * @return YellowPage
61  	 */
62  	public static YellowPageInterface getYellowPage(
63  			final SimulatorInterface simulator)
64  	{
65  		try
66  		{
67  			InitialEventContext context = new InitialEventContext();
68  			String name = simulator.getReplication().getRunControl()
69  					.getTreatment().getExperiment().getRun()
70  					+ "/treatment("
71  					+ simulator.getReplication().getRunControl().getTreatment()
72  							.getNumber()
73  					+ ")/replication("
74  					+ simulator.getReplication().getNumber() + ")/yellowPage";
75  			YellowPageInterface yellowPage = (YellowPageInterface) context
76  					.lookup(name);
77  			if (yellowPage == null)
78  			{
79  				yellowPage = new YellowPage();
80  				context.bind(name, yellowPage);
81  			}
82  			return yellowPage;
83  		} catch (Exception exception)
84  		{
85  			exception.printStackTrace();
86  			Logger.warning(ContextUtil.class, "bindToContext", exception);
87  			return null;
88  		}
89  	}
90  
91  	/***
92  	 * constructs a new YellowPage
93  	 */
94  	protected YellowPage()
95  	{
96  		super();
97  	}
98  
99  	/***
100 	 * @see nl.tudelft.simulation.yellowpage.YellowPageInterface
101 	 *      #findActor(java.lang.String)
102 	 */
103 	public ActorInterface[] findActor(final String regex)
104 	{
105 		List result = new ArrayList();
106 		for (Iterator i = this.entries.keySet().iterator(); i.hasNext();)
107 		{
108 			List actors = (List) this.entries.get(i.next());
109 			for (Iterator ii = actors.iterator(); ii.hasNext();)
110 			{
111 				ActorInterface actor = (ActorInterface) ii.next();
112 				if (actor.getName().matches(regex))
113 				{
114 					result.add(actor);
115 				}
116 			}
117 		}
118 		return (ActorInterface[]) result.toArray(new ActorInterface[result
119 				.size()]);
120 	}
121 
122 	/***
123 	 * @see nl.tudelft.simulation.yellowpage.YellowPageInterface
124 	 *      #findActor(java.lang.String,
125 	 *      nl.tudelft.simulation.yellowpage.Category)
126 	 */
127 	public ActorInterface[] findActor(final String regex,
128 			final Category category)
129 	{
130 		List result = new ArrayList();
131 		for (Iterator i = this.entries.keySet().iterator(); i.hasNext();)
132 		{
133 			Category cat = (Category) i.next();
134 			if (Category.specializationOf(category, cat))
135 			{
136 				List actors = (List) this.entries.get(cat);
137 				for (Iterator ii = actors.iterator(); ii.hasNext();)
138 				{
139 					ActorInterface actor = (ActorInterface) ii.next();
140 					if (actor.getName().matches(regex))
141 					{
142 						result.add(actor);
143 					}
144 				}
145 			}
146 		}
147 		return (ActorInterface[]) result.toArray(new ActorInterface[result
148 				.size()]);
149 	}
150 
151 	/***
152 	 * @see nl.tudelft.simulation.yellowpage.YellowPageInterface#findActor(nl.tudelft.simulation.yellowpage.Category)
153 	 */
154 	public ActorInterface[] findActor(final Category category)
155 	{
156 		List actors = new ArrayList();
157 		for (Iterator i = this.entries.keySet().iterator(); i.hasNext();)
158 		{
159 			Category cat = (Category) i.next();
160 			if (Category.specializationOf(category, cat))
161 			{
162 				actors = (List) this.entries.get(cat);
163 			}
164 		}
165 		return (ActorInterface[]) actors.toArray(new ActorInterface[actors
166 				.size()]);
167 	}
168 
169 	/***
170 	 * @see nl.tudelft.simulation.yellowpage.YellowPageInterface#register(nl.tudelft.simulation.actor.ActorInterface,
171 	 *      nl.tudelft.simulation.yellowpage.Category)
172 	 */
173 	public boolean register(final ActorInterface actor, final Category category)
174 	{
175 		List actors = (List) this.entries.get(category);
176 		if (actors == null)
177 		{
178 			actors = new ArrayList();
179 			this.entries.put(category, actors);
180 		}
181 		return ((ArrayList) actors).add(actor);
182 	}
183 }