001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
003 * 
004 * LearnLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * LearnLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with LearnLib; if not, see
015 * <http://www.gnu.de/documents/lgpl.en.html>.
016 */
017package de.learnlib.algorithms.lstargeneric.table;
018
019import net.automatalib.words.Word;
020
021
022/**
023 * A row in an observation table. Minimally, a row consists of a prefix (the row label)
024 * and a unique identifier in its observation table which remains constant throughout the
025 * whole process.
026 * <p>
027 * Apart from that, a row is also associated with contents (via an integer id). The prefix of a row
028 * may be either a short or long prefix. In the former case, the row will also have successor rows
029 * (one-step futures) associated with it.
030 * 
031 * @author Malte Isberner <malte.isberner@gmail.com>
032 *
033 * @param <I> input symbol class
034 */
035public final class Row<I> {
036        private final Word<I> prefix;
037        private final int rowId;
038        
039        private int rowContentId = -1;
040        private int lpIndex = 0;
041        private Row<I>[] successors = null; 
042        
043        /**
044         * Constructor.
045         * @param prefix the prefix (label) of this row 
046         * @param rowId the unique row identifier
047         */
048        public Row(Word<I> prefix, int rowId) {
049                this.prefix = prefix;
050                this.rowId = rowId;
051        }
052        
053        /**
054         * Constructor for short prefix rows. 
055         * @param prefix the prefix (label) of this row
056         * @param rowId the unique row identifier
057         * @param alphabetSize the size of the alphabet, used for initializing the successor array
058         */
059        public Row(Word<I> prefix, int rowId, int alphabetSize) {
060                this(prefix, rowId);
061                
062                makeShort(alphabetSize);
063        }
064        
065        /**
066         * Makes this row a short prefix row. This leads to a successor array being created.
067         * If this row already is a short prefix row, nothing happens.
068         * @param alphabetSize the size of the input alphabet.
069         */
070        @SuppressWarnings("unchecked")
071        public void makeShort(int alphabetSize) {
072                if(lpIndex == -1)
073                        return;
074                lpIndex = -1;
075                this.successors = (Row<I>[])new Row<?>[alphabetSize];
076        }
077        
078        /**
079         * Retrieves the successor row for this short prefix row and the given alphabet
080         * symbol (by index). If this is no short prefix row, an exception might occur.
081         * @param inputIdx the index of the alphabet symbol.
082         * @return the successor row (may be <code>null</code>)
083         */
084        public Row<I> getSuccessor(int inputIdx) {
085                return successors[inputIdx];
086        }
087        
088        /**
089         * Sets the successor row for this short prefix row and the given alphabet symbol
090         * (by index). If this is no short prefix row, an exception might occur.
091         * @param inputIdx the index of the alphabet symbol.
092         * @param succ the successor row
093         */
094        public void setSuccessor(int inputIdx, Row<I> succ) {
095                successors[inputIdx] = succ;
096        }
097        
098        /**
099         * Retrieves the prefix (row label) associated with this row.
100         * @return the prefix
101         */
102        public Word<I> getPrefix() {
103                return prefix;
104        }
105        
106        /**
107         * Retrieves the unique row identifier associated with this row.
108         * @return the row identifier
109         */
110        public int getRowId() {
111                return rowId;
112        }
113        
114        /**
115         * Retrieves the ID of the row contents (may be <code>-1</code> if this row has not
116         * yet been initialized).
117         * @return the contents id.
118         */
119        public int getRowContentId() {
120                return rowContentId;
121        }
122        
123        /**
124         * Sets the ID of the row contents.
125         * @param id the contents id
126         */
127        public void setRowContentId(int id) {
128                this.rowContentId = id;
129        }
130        
131        /**
132         * Retrieves whether this is a short prefix row.
133         * @return <code>true</code> if this is a short prefix row, <code>false</code> otherwise.
134         */
135        public boolean isShortPrefix() {
136                return (lpIndex == -1);
137        }
138
139        public boolean hasContents() {
140                return (rowContentId != -1);
141        }
142        
143        int getLpIndex() {
144                return lpIndex;
145        }
146        
147        void setLpIndex(int lpIndex) {
148                this.lpIndex = lpIndex;
149        }
150        
151}