001package org.maltparser.core.syntaxgraph.feature;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.feature.function.AddressFunction;
005import org.maltparser.core.feature.function.FeatureFunction;
006import org.maltparser.core.feature.value.FeatureValue;
007import org.maltparser.core.feature.value.SingleFeatureValue;
008import org.maltparser.core.io.dataformat.ColumnDescription;
009import org.maltparser.core.symbol.SymbolTable;
010import org.maltparser.core.symbol.SymbolTableHandler;
011import org.maltparser.core.syntaxgraph.SyntaxGraphException;
012
013public final class ExistsFeature implements FeatureFunction {
014        public final static  Class<?>[] paramTypes = { org.maltparser.core.feature.function.AddressFunction.class };
015        private AddressFunction addressFunction;
016        private final SymbolTableHandler tableHandler;
017        private SymbolTable table;
018        private final SingleFeatureValue featureValue;
019        
020        public ExistsFeature(SymbolTableHandler tableHandler) throws MaltChainedException {
021                this.tableHandler = tableHandler;
022                this.featureValue = new SingleFeatureValue(this);
023        }
024        
025        /**
026         * Initialize the exists feature function
027         * 
028         * @param arguments an array of arguments with the type returned by getParameterTypes()
029         * @throws MaltChainedException
030         */
031        public void initialize(Object[] arguments) throws MaltChainedException {
032                if (arguments.length != 1) {
033                        throw new SyntaxGraphException("Could not initialize ExistsFeature: number of arguments are not correct. ");
034                }
035                // Checks that the two arguments are address functions
036                if (!(arguments[0] instanceof AddressFunction)) {
037                        throw new SyntaxGraphException("Could not initialize ExistsFeature: the first argument is not an address function. ");
038                }
039
040                setAddressFunction((AddressFunction)arguments[0]);
041                
042                // Creates a symbol table called "EXISTS" using one null value
043//              setSymbolTable(tableHandler.addSymbolTable("EXISTS", ColumnDescription.INPUT, "one"));
044//              
045//              table.addSymbol("TRUE"); // The address exists
046//              table.addSymbol("FALSE"); // The address don't exists
047        }
048        
049        /**
050         * Returns an array of class types used by the feature extraction system to invoke initialize with
051         * correct arguments.
052         * 
053         * @return an array of class types
054         */
055        public Class<?>[] getParameterTypes() {
056                return paramTypes; 
057        }
058        /**
059         * Returns the string representation of the integer <code>code</code> according to the exists feature function. 
060         * 
061         * @param code the integer representation of the symbol
062         * @return the string representation of the integer <code>code</code> according to the exists feature function.
063         * @throws MaltChainedException
064         */
065        public String getSymbol(int code) throws MaltChainedException {
066                return (code == 1)?"true":"false"; 
067        }
068        
069        /**
070         * Returns the integer representation of the string <code>symbol</code> according to the exists feature function.
071         * 
072         * @param symbol the string representation of the symbol
073         * @return the integer representation of the string <code>symbol</code> according to the exists feature function.
074         * @throws MaltChainedException
075         */
076        public int getCode(String symbol) throws MaltChainedException {
077                return (symbol.equals("true"))?1:0;
078        }
079        
080        /**
081         * Cause the feature function to update the feature value.
082         * 
083         * @throws MaltChainedException
084         */
085        public void update() throws MaltChainedException {
086                featureValue.setIndexCode(1);
087                featureValue.setNullValue(false);
088                if (addressFunction.getAddressValue().getAddress() != null) {
089                        featureValue.setSymbol("true");
090                        featureValue.setValue(1);
091                } else {
092                        featureValue.setSymbol("false");
093                        featureValue.setValue(0);
094                }
095        }
096        
097        /**
098         * Returns the feature value
099         * 
100         * @return the feature value
101         */
102        public FeatureValue getFeatureValue() {
103                return featureValue;
104        }
105        
106        /**
107         * Returns the symbol table used by the exists feature function
108         * 
109         * @return the symbol table used by the exists feature function
110         */
111        public SymbolTable getSymbolTable() {
112                return table;
113        }
114        
115        /**
116         * Returns the address function 
117         * 
118         * @return the address function 
119         */
120        public AddressFunction getAddressFunction() {
121                return addressFunction;
122        }
123
124
125        /**
126         * Sets the address function 
127         * 
128         * @param addressFunction a address function 
129         */
130        public void setAddressFunction(AddressFunction addressFunction) {
131                this.addressFunction = addressFunction;
132        }
133        
134        /**
135         * Returns symbol table handler
136         * 
137         * @return a symbol table handler
138         */
139        public SymbolTableHandler getTableHandler() {
140                return tableHandler;
141        }
142
143        /**
144         * Sets the symbol table used by the exists feature function
145         * 
146         * @param table
147         */
148        public void setSymbolTable(SymbolTable table) {
149                this.table = table;
150        }
151        
152        public  int getType() {
153                return ColumnDescription.BOOLEAN;
154        }
155        
156        public String getMapIdentifier() {
157                return "EXISTS";
158        }
159        
160        public boolean equals(Object obj) {
161                if (this == obj)
162                        return true;
163                if (obj == null)
164                        return false;
165                if (getClass() != obj.getClass())
166                        return false;
167                return obj.toString().equals(this.toString());
168        }
169        
170        public int hashCode() {
171                return 217 + (null == toString() ? 0 : toString().hashCode());
172        }
173        
174        public String toString() {
175                final StringBuilder sb = new StringBuilder();
176                sb.append("Exists(");
177                sb.append(addressFunction.toString());
178                sb.append(')');
179                return sb.toString();
180        }
181}