001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.graphic.pert;
051:
052: import java.util.HashMap;
053: import java.util.HashSet;
054: import java.util.Iterator;
055: import java.util.LinkedList;
056: import java.util.List;
057: import java.util.Set;
058:
059: import com.projity.pm.graphic.model.cache.GraphicDependency;
060: import com.projity.pm.graphic.model.cache.GraphicNode;
061: import com.projity.pm.graphic.model.cache.NodeModelCache;
062:
063: /**
064: *
065: */
066: public class DependencyGraph {
067: protected HashMap nodeMap = new HashMap();
068: protected NodeModelCache cache;
069:
070: public void setCache(NodeModelCache cache) {
071: this .cache = cache;
072: nodeMap.clear();
073: }
074:
075: public void insertDependency(GraphicDependency dependency) {
076: //System.out.println("insertDependency");
077: GraphicNode preValue = (GraphicNode) dependency
078: .getPredecessor();
079: GraphicNode sucValue = (GraphicNode) dependency.getSuccessor();
080: Node pre = (Node) nodeMap.get(preValue);
081: if (pre == null) {
082: pre = new Node(preValue);
083: nodeMap.put(preValue, pre);
084: }
085: Node suc = (Node) nodeMap.get(sucValue);
086: if (suc == null) {
087: suc = new Node(sucValue);
088: nodeMap.put(sucValue, suc);
089: }
090:
091: pre.addSuccessor(suc);
092: suc.addPredecessor(pre);
093: }
094:
095: public void removeDependency(GraphicDependency dependency) {
096: //System.out.println("removeDependency");
097: GraphicNode preValue = (GraphicNode) dependency
098: .getPredecessor();
099: GraphicNode sucValue = (GraphicNode) dependency.getSuccessor();
100: Node pre = (Node) nodeMap.get(preValue);
101: Node suc = (Node) nodeMap.get(sucValue);
102: if (pre == null || suc == null)
103: return;
104:
105: pre.removeSuccessor(suc);
106: suc.removePredecessor(pre);
107: if (pre.isolated())
108: nodeMap.remove(pre.getValue());
109: if (suc.isolated())
110: nodeMap.remove(suc.getValue());
111: }
112:
113: public void insertDependencies(List dependencies) {
114: for (Iterator i = dependencies.iterator(); i.hasNext();)
115: insertDependency((GraphicDependency) i.next());
116: }
117:
118: public void removeDependencies(List dependencies) {
119: for (Iterator i = dependencies.iterator(); i.hasNext();)
120: removeDependency((GraphicDependency) i.next());
121: }
122:
123: public void updatePertLevels() {
124: // System.out.println("updatePertLevels");
125: for (Iterator i = cache.getIterator(); i.hasNext();) {
126: resetCachePertLevel((GraphicNode) i.next());
127: }
128:
129: Set predecessors = new HashSet();
130: Set successors = new HashSet();
131: for (Iterator i = nodeMap.values().iterator(); i.hasNext();) {
132: Node node = (Node) i.next();
133: GraphicNode gnode = (GraphicNode) node.getValue();
134: //resetCachePertLevel(gnode);
135: if (node.getPredecessors().size() == 0)
136: predecessors.add(node);
137: }
138:
139: while (predecessors.size() > 0) {
140: updateSuccessorsPertLevel(predecessors, successors);
141: Set tmp = predecessors;
142: predecessors = successors;
143: successors = tmp;
144: successors.clear();
145: }
146: }
147:
148: private void updateSuccessorsPertLevel(Set predecessors,
149: Set successors) {
150: for (Iterator i = predecessors.iterator(); i.hasNext();) {
151: Node pre = (Node) i.next();
152: GraphicNode gpre = (GraphicNode) pre.getValue();
153: for (Iterator j = pre.getSuccessors().iterator(); j
154: .hasNext();) {
155: Node suc = (Node) j.next();
156: successors.add(suc);
157: GraphicNode gsuc = (GraphicNode) suc.getValue();
158: correctPertLevel(gpre, gsuc);
159: }
160: }
161: }
162:
163: private void resetCachePertLevel(GraphicNode gnode) {
164: cache.setPertLevel(gnode, cache.getLevel(gnode));
165: }
166:
167: private void correctPertLevel(GraphicNode gpre, GraphicNode gsuc) {
168: if (cache.getPertLevel(gsuc) <= cache.getPertLevel(gpre)) {
169: cache.setPertLevel(gsuc, cache.getPertLevel(gpre) + 1);
170: }
171: }
172:
173: public class Node {
174: protected Object value;
175: protected List predecessors;
176: protected List successors;
177:
178: public Node(Object value) {
179: this .value = value;
180: predecessors = new LinkedList();
181: successors = new LinkedList();
182: }
183:
184: public Object getValue() {
185: return value;
186: }
187:
188: public void setValue(Object value) {
189: this .value = value;
190: }
191:
192: public void addSuccessor(Node successor) {
193: successors.add(successor);
194: }
195:
196: public void removeSuccessor(Node successor) {
197: successors.remove(successor);
198: }
199:
200: public List getSuccessors() {
201: return successors;
202: }
203:
204: public void addPredecessor(Node predecessor) {
205: predecessors.add(predecessor);
206: }
207:
208: public void removePredecessor(Node predecessor) {
209: predecessors.remove(predecessor);
210: }
211:
212: public List getPredecessors() {
213: return predecessors;
214: }
215:
216: public boolean isolated() {
217: return predecessors.size() == 0 && successors.size() == 0;
218: }
219:
220: }
221:
222: }
|