00001 package cedar.hepdata.model;
00002
00003 import java.util.Set;
00004 import java.util.HashSet;
00005
00006 import javax.persistence.*;
00007
00008 import cedar.hepdata.util.*;
00009
00016 @Entity
00017 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
00018 @Table(name="BaseReactions")
00019 public class Reaction extends Storeable {
00021 @Id @GeneratedValue(strategy=GenerationType.TABLE)
00022 @Column(name="REACTION_ID")
00023 private Long _id;
00024
00026 @org.hibernate.annotations.CollectionOfElements
00027 @JoinTable(name="InitialStates", joinColumns=@JoinColumn(name="REACTION_ID"))
00028 @org.hibernate.annotations.CollectionId(columns=@Column(name="InitialStateId"),
00029 type=@org.hibernate.annotations.Type(type="long"),
00030 generator="sequence")
00031 private Set<Particle> initialState = new HashSet<Particle>();
00032
00034 @org.hibernate.annotations.CollectionOfElements
00035 @JoinTable(name="FinalStates", joinColumns=@JoinColumn(name="REACTION_ID"))
00036 @org.hibernate.annotations.CollectionId(columns=@Column(name="FinalStateId"),
00037 type=@org.hibernate.annotations.Type(type="long"),
00038 generator="sequence")
00039 private Set<Particle> finalState = new HashSet<Particle>();
00040
00042 @ManyToMany
00043 private Set<ReactionGroup> reactionGroups = new HashSet<ReactionGroup>();
00044
00045
00047
00048
00050 public Reaction() {}
00051
00052
00054
00055
00057 public Set<Particle> getInitialState() {
00058 return initialState;
00059 }
00061 public Reaction setInitialState(Set<Particle> initialState) {
00062 this.initialState = initialState;
00063 return this;
00064 }
00069 public Reaction addToInitialState(Particle particle) {
00070 boolean foundType = false;
00071 for (Particle p : this.initialState) {
00072 if (Particle.sameType(p, particle)) {
00073 foundType = true;
00074 p.setMultiplicity(p.getMultiplicity() + particle.getMultiplicity());
00075 break;
00076 }
00077 }
00078 if (!foundType) this.initialState.add(particle);
00079 return this;
00080 }
00087 public Reaction removeFromInitialState(Particle particle) {
00088 for (Particle p : this.initialState) {
00089 if (Particle.sameType(p, particle)) {
00090 int newMult = p.getMultiplicity() - particle.getMultiplicity();
00091 if (newMult <= 0) {
00092 this.initialState.remove(p);
00093 } else {
00094 p.setMultiplicity(newMult);
00095 }
00096 break;
00097 }
00098 }
00099 return this;
00100 }
00102 public Reaction purgeFromInitialState(Particle particle) {
00103 for (Particle p : this.initialState) {
00104 if (Particle.sameType(p, particle)) {
00105 this.initialState.remove(p);
00106 }
00107 }
00108 return this;
00109 }
00110
00111
00113 public Set<Particle> getFinalState() {
00114 return finalState;
00115 }
00117 public Reaction setFinalState(Set<Particle> finalState) {
00118 this.finalState = finalState;
00119 return this;
00120 }
00125 public Reaction addToFinalState(Particle particle) {
00126 boolean foundType = false;
00127 for (Particle p : this.finalState) {
00128 if (Particle.sameType(p, particle)) {
00129 foundType = true;
00130 p.setMultiplicity(p.getMultiplicity() + particle.getMultiplicity());
00131 break;
00132 }
00133 }
00134 if (!foundType) this.finalState.add(particle);
00135 return this;
00136 }
00143 public Reaction removeFromFinalState(Particle particle) {
00144 for (Particle p : this.finalState) {
00145 if (Particle.sameType(p, particle)) {
00146 int newMult = p.getMultiplicity() - particle.getMultiplicity();
00147 if (newMult <= 0) {
00148 this.finalState.remove(p);
00149 } else {
00150 p.setMultiplicity(newMult);
00151 }
00152 break;
00153 }
00154 }
00155 return this;
00156 }
00158 public Reaction purgeFromFinalState(Particle particle) {
00159 for (Particle p : this.finalState) {
00160 if (Particle.sameType(p, particle)) {
00161 this.finalState.remove(p);
00162 }
00163 }
00164 return this;
00165 }
00166
00167
00168
00170 public Set<ReactionGroup> getReactionGroups() {
00171 return reactionGroups;
00172 }
00174 public Reaction setReactionGroups(Set<ReactionGroup> reactionGroups) {
00175 this.reactionGroups = reactionGroups;
00176 Set<ReactionGroup> toAdd = new HashSet<ReactionGroup>();
00177 Set<ReactionGroup> toRemove = new HashSet<ReactionGroup>();
00178 for (ReactionGroup r : this.reactionGroups) {
00179 if (! reactionGroups.contains(r)) toRemove.add(r);
00180 }
00181 for (ReactionGroup r : reactionGroups) {
00182 if (! this.reactionGroups.contains(r)) toAdd.add(r);
00183 }
00184 for (ReactionGroup r : toRemove) removeReactionGroup(r);
00185 for (ReactionGroup r : toAdd) addReactionGroup(r);
00186 return this;
00187 }
00189 public Reaction addReactionGroup(ReactionGroup reactionGroup) {
00190 if (reactionGroup != null) {
00191 reactionGroup.getReactions().add(this);
00192 this.reactionGroups.add(reactionGroup);
00193 } else {
00194 log().warn("Tried to add a null reaction group to a reaction");
00195 }
00196 return this;
00197 }
00199 public Reaction removeReactionGroup(ReactionGroup reactionGroup) {
00200 if (reactionGroup != null) {
00201 reactionGroup.getReactions().remove(this);
00202 this.reactionGroups.remove(reactionGroup);
00203 } else {
00204 log().warn("Tried to remove a null reaction group from a reaction");
00205 }
00206 return this;
00207 }
00208
00209
00211
00212
00214 public boolean equals(Object other) {
00215 log().debug("Comparing reactions...");
00216 if (this == other) return true;
00217 if (! (other instanceof Reaction)) return false;
00218 final Reaction test = (Reaction) other;
00219
00220 if (! this.getInitialState().equals( test.getInitialState() )) return false;
00221 if (test.getInitialState() != null && this.getInitialState() != null) {
00222 Set<Particle> is1 = this.getInitialState();
00223 Set<Particle> is2 = test.getInitialState();
00224 if (is1.size() != is2.size()) return false;
00225 for (Particle p : is1) {
00226 if (! is2.contains(p)) return false;
00227 }
00228 }
00229
00230 if (! this.getFinalState().equals( test.getFinalState() )) return false;
00231 if (test.getFinalState() != null && this.getFinalState() != null) {
00232 Set<Particle> fs1 = this.getFinalState();
00233 Set<Particle> fs2 = test.getFinalState();
00234 if (fs1.size() != fs2.size()) return false;
00235 for (Particle p : fs1) {
00236 if (! fs2.contains(p)) return false;
00237 }
00238 }
00239
00240
00242
00243
00244
00245 return true;
00246 }
00247
00248
00249
00251
00252
00253
00255 public String initialStateToString() {
00256 StringBuffer s = new StringBuffer();
00257 for (Particle ispart : getInitialState()) {
00258 s.append(" " + ispart.toString());
00259 }
00260 return s.toString().trim();
00261 }
00262
00263
00265 public String finalStateToString() {
00266 StringBuffer s = new StringBuffer();
00267 for (Particle fspart : getFinalState()) {
00268 s.append(" " + fspart.toString());
00269 }
00270 return s.toString().trim();
00271 }
00272
00273
00275 public String toString() {
00276 StringBuffer s = new StringBuffer();
00277 s.append(initialStateToString());
00278 s.append(" -> ");
00279 s.append(finalStateToString());
00280 return s.toString().trim();
00281 }
00282
00283 }