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: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.drools.reteoo.BaseVertex;
023:
024: /**
025: * Holder for Row elements.
026: *
027: */
028: public class RowList {
029:
030: // List<Row>
031: private List rows;
032:
033: /**
034: * Default constructor.
035: */
036: public RowList() {
037: super ();
038: this .rows = new ArrayList();
039: }
040:
041: /**
042: * Adds new vertex to specified depth
043: *
044: * @param depth depth for vertex
045: * @param vertex vertex
046: */
047: public void add(final int depth, final BaseVertex vertex) {
048: if (this .rows.size() < (depth + 1)) {
049: final int addRows = depth - this .rows.size() + 1;
050:
051: for (int i = 0; i < addRows; ++i) {
052: this .rows.add(new Row((depth - addRows) + i));
053: }
054: }
055:
056: ((Row) this .rows.get(depth)).add(vertex);
057: }
058:
059: /**
060: * @return number of rows in RowList
061: */
062: public int getDepth() {
063: return this .rows.size();
064: }
065:
066: /**
067: * @param row index of the row to be returned (0..n-1)
068: * @return specified row
069: */
070: public Row get(final int row) {
071: return (Row) this .rows.get(row);
072: }
073:
074: /**
075: * Finds specified vertex from the rows.
076: *
077: * @param vertex vertex
078: *
079: * @return row number where vertex was found (0..n-1). <code>-1</code> if not found.
080: */
081: public int getRow(final BaseVertex vertex) {
082: final int numRows = this .rows.size();
083:
084: for (int i = 0; i < numRows; ++i) {
085: if (((Row) this .rows.get(i)).contains(vertex)) {
086: return i;
087: }
088: }
089:
090: return -1;
091: }
092:
093: /**
094: * Finds the longest row width.
095: *
096: * @return width of the longest row
097: */
098: public int getWidth() {
099: int width = 0;
100:
101: for (final Iterator rowIter = this .rows.iterator(); rowIter
102: .hasNext();) {
103: final Row row = (Row) rowIter.next();
104: final int rowWidth = row.getWidth();
105:
106: if (rowWidth > width) {
107: width = rowWidth;
108: }
109: }
110:
111: return width;
112: }
113:
114: /**
115: * Width of the row at specified index.
116: *
117: * @param row
118: * @return width
119: */
120: public int getWidth(final int row) {
121: return ((Row) this .rows.get(row)).getWidth();
122: }
123:
124: /**
125: * @param vertex vertex to search
126: * @return column where vertex was found
127: */
128: public int getColumn(final BaseVertex vertex) {
129: final int row = getRow(vertex);
130:
131: if (row < 0) {
132: return -1;
133: }
134:
135: final List rowVertices = get(row).getVertices();
136:
137: final int numCols = rowVertices.size();
138:
139: for (int i = 0; i < numCols; ++i) {
140: if (rowVertices.get(i).equals(vertex)) {
141: return i;
142: }
143: }
144:
145: return -1;
146: }
147:
148: /**
149: * Dumps all row vertices to System.err
150: */
151: public void dump() {
152: final int numRows = this .rows.size();
153:
154: for (int i = 0; i < numRows; ++i) {
155: System.err.println(i + ": " + get(i).getVertices());
156: }
157: }
158:
159: /**
160: * Optimizes all rows for optimal presentation
161: */
162: public void optimize() {
163: final int numRows = this .rows.size();
164:
165: for (int i = 0; i < numRows; ++i) {
166: get(i).optimize();
167: }
168: }
169: }
|