001: package org.drools.eclipse.editors.rete;
002:
003: /*
004: * Copyright 2006 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.drools.reteoo.BaseVertex;
027:
028: /**
029: * Represents one row in rete graph
030: *
031: */
032: public class Row {
033:
034: private final int depth;
035:
036: //List<BaseVertex>
037: private List vertices;
038:
039: /**
040: * Default constructor.
041: *
042: * @param depth row depth
043: */
044: public Row(final int depth) {
045: super ();
046: this .vertices = new ArrayList();
047: this .depth = depth;
048: }
049:
050: /**
051: * Returns row depth
052: *
053: * @return row depth
054: */
055: public int getDepth() {
056: return this .depth;
057: }
058:
059: /**
060: * Adds new vertex to this row.
061: *
062: * @param vertex new vertex to be added
063: */
064: public void add(final BaseVertex vertex) {
065: this .vertices.add(vertex);
066: }
067:
068: /**
069: * Returns all vertices from this row.
070: *
071: * @return list of vertices with type BaseVertex
072: */
073: public List getVertices() {
074: return this .vertices;
075: }
076:
077: /**
078: * @param vertex
079: * @return <code>true</code> if vertex is found in row. <code>false</code> otherwise.
080: */
081: public boolean contains(final BaseVertex vertex) {
082: return this .vertices.contains(vertex);
083: }
084:
085: /**
086: * @return number of vertices in row
087: */
088: public int getWidth() {
089: return this .vertices.size();
090: }
091:
092: /**
093: * Optimizing vertices for optimal presentation
094: *
095: */
096: public void optimize() {
097: final List sorted = new ArrayList(this .vertices);
098:
099: Collections.sort(sorted, new Comparator() {
100: public int compare(final Object o1, final Object o2) {
101: final BaseVertex v1 = (BaseVertex) o1;
102: final BaseVertex v2 = (BaseVertex) o2;
103:
104: int v1OutDegree = v1.getSourceConnections().size();
105: int v2OutDegree = v2.getSourceConnections().size();
106:
107: if (v1OutDegree < v2OutDegree) {
108: return 1;
109: }
110:
111: if (v1OutDegree > v2OutDegree) {
112: return -1;
113: }
114:
115: return 0;
116: }
117: });
118:
119: final LinkedList optimized = new LinkedList();
120:
121: boolean front = false;
122:
123: for (final Iterator vertexIter = sorted.iterator(); vertexIter
124: .hasNext();) {
125: final BaseVertex vertex = (BaseVertex) vertexIter.next();
126:
127: if (front) {
128: optimized.addFirst(vertex);
129: } else {
130: optimized.addLast(vertex);
131: }
132:
133: front = !front;
134: }
135:
136: this.vertices = optimized;
137: }
138: }
|