File size: 5,688 Bytes
6f3ebfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package org.maltparser.concurrent.graph;

import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;

import org.maltparser.concurrent.graph.dataformat.ColumnDescription;
import org.maltparser.concurrent.graph.dataformat.DataFormat;
/**
* Immutable and tread-safe dependency edge implementation.
* 
* @author Johan Hall
*/
public final class ConcurrentDependencyEdge implements Comparable<ConcurrentDependencyEdge> {
	private final ConcurrentDependencyNode source;
	private final ConcurrentDependencyNode target;
	private final SortedMap<Integer, String> labels;
	
	protected ConcurrentDependencyEdge(ConcurrentDependencyEdge edge) throws ConcurrentGraphException {
		this.source = edge.source;
		this.target = edge.target;
		this.labels = new TreeMap<Integer, String>(edge.labels);
	}
	
	protected ConcurrentDependencyEdge(DataFormat dataFormat, ConcurrentDependencyNode _source, ConcurrentDependencyNode _target, SortedMap<Integer, String> _labels) throws ConcurrentGraphException {
		if (_source == null) {
			throw new ConcurrentGraphException("Not allowed to have an edge without a source node");
		}
		if (_target == null) {
			throw new ConcurrentGraphException("Not allowed to have an edge without a target node");
		}
		this.source = _source;
		this.target = _target;
		if (this.target.getIndex() == 0) {
			throw new ConcurrentGraphException("Not allowed to have an edge target as root node");
		}
		this.labels = new TreeMap<Integer, String>();
		if (_labels != null) {
			for (Integer i : _labels.keySet()) {
				if (dataFormat.getColumnDescription(i).getCategory() == ColumnDescription.DEPENDENCY_EDGE_LABEL) {
					this.labels.put(i, _labels.get(i));
				}
			}
		}
	}
	
	/**
	 * Returns the source node of the edge.
	 * 
	 * @return the source node of the edge.
	 */
	public ConcurrentDependencyNode getSource() {
		return source;
	}

	/**
	 * Returns the target node of the edge.
	 * 
	 * @return the target node of the edge.
	 */
	public ConcurrentDependencyNode getTarget() {
		return target;
	}
	
	/**
	 * Returns an edge label
	 * 
	 * @param column a column description that describes the label
	 * @return an edge label described by the column description. An empty string is returned if the label is not found. 
	 */
	public String getLabel(ColumnDescription column) {
		if (labels.containsKey(column.getPosition())) {
			return labels.get(column.getPosition());
		} else if (column.getCategory() == ColumnDescription.IGNORE) {
			return column.getDefaultOutput();
		}
		return "";
	}
	
	/**
	 * Returns an edge label
	 * 
	 * @param columnName the name of the column that describes the label.
	 * @return an edge label. An empty string is returned if the label is not found.
	 */
	public String getLabel(String columnName) {
		ColumnDescription column = source.getDataFormat().getColumnDescription(columnName);
		if (column != null) {
			if (labels.containsKey(column.getPosition())) {
				return labels.get(column.getPosition());
			} else if (column.getCategory() == ColumnDescription.IGNORE) {
				return column.getDefaultOutput();
			}
		}
		return "";
	}
	
	/**
	 * Returns the number of labels of the edge.
	 * 
	 * @return the number of labels of the edge.
	 */
	public int nLabels() {
		return labels.size();
	}
	
	/**
	 * Returns <i>true</i> if the edge has one or more labels, otherwise <i>false</i>.
	 * 
	 * @return <i>true</i> if the edge has one or more labels, otherwise <i>false</i>.
	 */
	public boolean isLabeled() {
		return labels.size() > 0;
	}

	public int compareTo(ConcurrentDependencyEdge that) {
		final int BEFORE = -1;
	    final int EQUAL = 0;
	    final int AFTER = 1;
	    
	    if (this == that) return EQUAL;
	    
	    if (this.target.getIndex() < that.target.getIndex()) return BEFORE;
	    if (this.target.getIndex() > that.target.getIndex()) return AFTER;
	    
	    if (this.source.getIndex() < that.source.getIndex()) return BEFORE;
	    if (this.source.getIndex() > that.source.getIndex()) return AFTER;
	    
	    
	    if (this.labels.equals(that.labels)) return EQUAL;
		
		Iterator<Integer> itthis = this.labels.keySet().iterator();
		Iterator<Integer> itthat = that.labels.keySet().iterator();
		while (itthis.hasNext() && itthat.hasNext()) {
			int keythis = itthis.next();
			int keythat = itthat.next();
			if (keythis < keythat) return BEFORE;
			if (keythis > keythat) return AFTER;
			if (this.labels.get(keythis).compareTo(that.labels.get(keythat)) != EQUAL) {
				return this.labels.get(keythis).compareTo(that.labels.get(keythat));
			}	
		}
		if (itthis.hasNext() == false && itthat.hasNext() == true) return BEFORE;
		if (itthis.hasNext() == true && itthat.hasNext() == false) return AFTER;

	    
		return EQUAL;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((source == null) ? 0 : source.hashCode());
		result = prime * result + ((target == null) ? 0 : target.hashCode());
		result = prime * result + ((labels == null) ? 0 : labels.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ConcurrentDependencyEdge other = (ConcurrentDependencyEdge) obj;
		if (source == null) {
			if (other.source != null)
				return false;
		} else if (!source.equals(other.source))
			return false;
		if (target == null) {
			if (other.target != null)
				return false;
		} else if (!target.equals(other.target))
			return false;
		if (labels == null) {
			if (other.labels != null)
				return false;
		} else if (!labels.equals(other.labels))
			return false;
		return true;
	}
}