001    package org.maltparser.core.syntaxgraph.feature;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.feature.FeatureException;
005    import org.maltparser.core.feature.function.AddressFunction;
006    import org.maltparser.core.feature.function.FeatureFunction;
007    import org.maltparser.core.feature.value.AddressValue;
008    import org.maltparser.core.feature.value.FeatureValue;
009    import org.maltparser.core.feature.value.SingleFeatureValue;
010    import org.maltparser.core.io.dataformat.ColumnDescription;
011    import org.maltparser.core.io.dataformat.DataFormatInstance;
012    import org.maltparser.core.symbol.SymbolTable;
013    import org.maltparser.core.symbol.SymbolTableHandler;
014    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
015    import org.maltparser.core.syntaxgraph.SyntaxGraphException;
016    import org.maltparser.core.syntaxgraph.node.DependencyNode;
017    /**
018    *
019    * @author Johan Hall
020    * @since 1.1
021    **/
022    public class InputArcFeature implements FeatureFunction {
023            protected AddressFunction addressFunction1;
024            protected AddressFunction addressFunction2;
025            protected ColumnDescription column;
026            protected DataFormatInstance dataFormatInstance;
027            protected SymbolTableHandler tableHandler;
028            protected SymbolTable table;
029            protected SingleFeatureValue featureValue;
030    
031            
032            public InputArcFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException {
033                    super();
034                    setDataFormatInstance(dataFormatInstance);
035                    setTableHandler(tableHandler);
036                    setFeatureValue(new SingleFeatureValue(this));
037            }
038            
039            public void initialize(Object[] arguments) throws MaltChainedException {
040                    if (arguments.length != 1) {
041                            throw new FeatureException("Could not initialize InputArcFeature: number of arguments are not correct. ");
042                    }
043                    // Checks that the two arguments are address functions
044    
045                    if (!(arguments[0] instanceof String)) {
046                            throw new FeatureException("Could not initialize InputArcFeature: the first argument is not a string. ");
047                    }
048                    if (!(arguments[1] instanceof AddressFunction)) {
049                            throw new SyntaxGraphException("Could not initialize InputArcFeature: the second argument is not an address function. ");
050                    }
051                    if (!(arguments[2] instanceof AddressFunction)) {
052                            throw new SyntaxGraphException("Could not initialize InputArcFeature: the third argument is not an address function. ");
053                    }
054                    setAddressFunction1((AddressFunction)arguments[1]);
055                    setAddressFunction2((AddressFunction)arguments[2]);
056                    
057                    setColumn(dataFormatInstance.getColumnDescriptionByName((String)arguments[0]));
058                    setSymbolTable(tableHandler.addSymbolTable("ARC_"+column.getName(),ColumnDescription.INPUT, "one"));
059                    table.addSymbol("LEFT");
060                    table.addSymbol("RIGHT");
061            }
062            
063            public Class<?>[] getParameterTypes() {
064                    Class<?>[] paramTypes = { java.lang.String.class };
065                    return paramTypes;
066            }
067            
068            public int getCode(String symbol) throws MaltChainedException {
069                    return table.getSymbolStringToCode(symbol);
070            }
071    
072    
073            public FeatureValue getFeatureValue() {
074                    return featureValue;
075            }
076    
077    
078            public String getSymbol(int code) throws MaltChainedException {
079                    return table.getSymbolCodeToString(code);
080            }
081    
082    
083            public void updateCardinality() throws MaltChainedException {
084                    featureValue.setCardinality(table.getValueCounter());
085            }
086    
087            public void update() throws MaltChainedException {
088                    // Retrieve the address value 
089                    final AddressValue arg1 = addressFunction1.getAddressValue();
090                    final AddressValue arg2 = addressFunction2.getAddressValue();
091                    try {
092    
093                            if (arg1.getAddress() == null || arg2.getAddress() == null) {
094                                    featureValue.setCode(table.getNullValueCode(NullValueId.NO_NODE));
095                                    featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
096                                    featureValue.setKnown(true);
097                                    featureValue.setNullValue(true); 
098                            } else {
099                                    final DependencyNode node1 = (DependencyNode)arg1.getAddress();
100                                    final DependencyNode node2 = (DependencyNode)arg2.getAddress();
101                                    
102                                    int head1 = Integer.parseInt(node1.getLabelSymbol(column.getSymbolTable()));
103                                    int head2 = Integer.parseInt(node2.getLabelSymbol(column.getSymbolTable()));
104                                    if (!node1.isRoot() && head1 == node2.getIndex()) {
105                                            featureValue.setCode(table.getSymbolStringToCode("LEFT"));
106                                            featureValue.setSymbol("LEFT");
107                                            featureValue.setKnown(true);
108                                            featureValue.setNullValue(false);
109                                    } else if (!node2.isRoot() && head2 == node1.getIndex()) {
110                                            featureValue.setCode(table.getSymbolStringToCode("RIGHT"));
111                                            featureValue.setSymbol("RIGHT");
112                                            featureValue.setKnown(true);
113                                            featureValue.setNullValue(false);                       
114                                    } else {
115                                            featureValue.setCode(table.getNullValueCode(NullValueId.NO_NODE));
116                                            featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
117                                            featureValue.setKnown(true);
118                                            featureValue.setNullValue(true);
119                                    }
120                            }
121                    } catch (NumberFormatException e) {
122                            throw new FeatureException("The index of the feature must be an integer value. ", e);
123                    }
124            }
125    
126            public ColumnDescription getColumn() {
127                    return column;
128            }
129    
130            public void setColumn(ColumnDescription column) throws MaltChainedException {
131                    if (column.getType() != ColumnDescription.INTEGER) {
132                            throw new FeatureException("InputArc feature column must be of type integer. ");
133                    }
134                    this.column = column;
135            }
136    
137            /**
138             * Returns the address function 1 (argument 1) 
139             * 
140             * @return the address function 1 (argument 1) 
141             */
142            public AddressFunction getAddressFunction1() {
143                    return addressFunction1;
144            }
145    
146    
147            /**
148             * Sets the address function 1 (argument 1) 
149             * 
150             * @param addressFunction1 a address function 1 (argument 1) 
151             */
152            public void setAddressFunction1(AddressFunction addressFunction1) {
153                    this.addressFunction1 = addressFunction1;
154            }
155            
156            /**
157             * Returns the address function 2 (argument 2) 
158             * 
159             * @return the address function 1 (argument 2) 
160             */
161            public AddressFunction getAddressFunction2() {
162                    return addressFunction2;
163            }
164    
165            /**
166             * Sets the address function 2 (argument 2) 
167             * 
168             * @param addressFunction2 a address function 2 (argument 2) 
169             */
170            public void setAddressFunction2(AddressFunction addressFunction2) {
171                    this.addressFunction2 = addressFunction2;
172            }
173            
174            public DataFormatInstance getDataFormatInstance() {
175                    return dataFormatInstance;
176            }
177    
178            public void setDataFormatInstance(DataFormatInstance dataFormatInstance) {
179                    this.dataFormatInstance = dataFormatInstance;
180            }
181    
182            public void setFeatureValue(SingleFeatureValue featureValue) {
183                    this.featureValue = featureValue;
184            }
185            
186            public SymbolTable getSymbolTable() {
187                    return table;
188            }
189    
190            public void setSymbolTable(SymbolTable table) {
191                    this.table = table;
192            }
193            
194            public SymbolTableHandler getTableHandler() {
195                    return tableHandler;
196            }
197    
198            public void setTableHandler(SymbolTableHandler tableHandler) {
199                    this.tableHandler = tableHandler;
200            }
201            
202            public boolean equals(Object obj) {
203                    if (!(obj instanceof InputArcFeature)) {
204                            return false;
205                    }
206                    if (!obj.toString().equals(this.toString())) {
207                            return false;
208                    }
209                    return true;
210            }
211            
212            public String toString() {
213                    return "InputArc(" + column.getName() + ")";
214            }
215    }