1   package ixa.kaflib;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   
6   /** Class for representing relations between entities and/or features. */
7   public class Relation implements Serializable {
8   
9       /* Relation's ID (required) */
10      private String id;
11  
12      /* From (required) */
13      private Relational from;
14  
15      /* To (required) */
16      private Relational to;
17  
18      /* Confidence (optional) */
19      private float confidence;
20  
21      Relation (String id, Relational from, Relational to) {
22  	this.id = id;
23  	this.from = from;
24  	this.to = to;
25  	this.confidence = -1.0f;
26      }
27  
28      Relation(Relation relation, HashMap<String, Relational> relational) {
29  	this.id = id;
30  	if (relation.from != null) {
31  	    this.from = relational.get(relation.from.getId());
32  	    if (this.from == null) {
33  		throw new IllegalStateException("Couldn't find relational " + relation.from.getId() + " when copying " + relation.getId());
34  	    }
35  	}
36  	if (relation.to != null) {
37  	    this.to = relational.get(relation.to.getId());
38  	    if (this.to == null) {
39  		throw new IllegalStateException("Couldn't find relational " + relation.to.getId() + " when copying " + relation.getId());
40  	    }
41  	}
42  	this.confidence = relation.confidence;
43      }
44  
45      public String getId() {
46  	return this.id;
47      }
48  
49      public void setId(String id) {
50  	this.id = id;
51      }
52  
53      public Relational getFrom() {
54          return this.from;
55      }
56  
57      public void setFrom(Relational obj) {
58  	this.from = obj;
59      }
60  
61      public Relational getTo() {
62          return this.to;
63      }
64  
65      public void setTo(Relational obj) {
66  	this.to = obj;
67      }
68  
69      public boolean hasConfidence() {
70  	return confidence >= 0;
71      }
72  
73      public float getConfidence() {
74  	if (confidence < 0) {
75  	    return 1.0f;
76  	}
77  	return confidence;
78      }
79  
80      public void setConfidence(float confidence) {
81  	if ((confidence < 0.0f) || (confidence > 1.0f)) {
82  	    throw new IllegalStateException("Confidence's value in a relation must be >=0 and <=1. [0, 1].");
83  	}
84  	this.confidence = confidence;
85      }
86  
87      public String getStr() {
88  	String str = "(" + this.from.getStr() + ", " + this.to.getStr() + ")";
89  	if (this.hasConfidence()) {
90  	    str += " [" + this.getConfidence() + "]";
91  	}
92  	return str;
93      }
94  }