001package org.biopax.paxtools.impl.level3;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.model.level3.Conversion;
005import org.biopax.paxtools.model.level3.ConversionDirectionType;
006import org.biopax.paxtools.model.level3.PhysicalEntity;
007import org.biopax.paxtools.model.level3.Stoichiometry;
008import org.biopax.paxtools.util.BPCollections;
009import org.biopax.paxtools.util.SetEquivalenceChecker;
010import org.hibernate.annotations.Cache;
011import org.hibernate.annotations.*;
012import org.hibernate.search.annotations.Indexed;
013
014import javax.persistence.Entity;
015import javax.persistence.*;
016import java.util.Set;
017
018@Entity
019@Proxy(proxyClass= Conversion.class)
020@Indexed
021@DynamicUpdate @DynamicInsert
022@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
023public class ConversionImpl extends InteractionImpl
024                implements Conversion
025{
026// ------------------------------ FIELDS ------------------------------
027
028        private Set<PhysicalEntity> right;
029        private Set<PhysicalEntity> left;
030        private ConversionDirectionType conversionDirection;
031        private Set<Stoichiometry> participantStoichiometry;
032        private Boolean spontaneous;
033
034// --------------------------- CONSTRUCTORS ---------------------------
035
036        public ConversionImpl()
037        {
038                left = BPCollections.I.createSafeSet();
039                right = BPCollections.I.createSafeSet();
040                participantStoichiometry = BPCollections.I.createSafeSet();
041        }
042
043
044// --------------------- Interface BioPAXElement ---------------------
045
046        @Override @Transient
047        public Class<? extends Conversion> getModelInterface()
048        {
049                return Conversion.class;
050        }
051
052// --------------------- Interface Conversion ---------------------
053
054// --------------------- ACCESORS and MUTATORS---------------------
055
056    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
057        @ManyToMany(targetEntity = PhysicalEntityImpl.class)
058        @JoinTable(name="rightParticipant")
059        public Set<PhysicalEntity> getRight()
060        {
061                return right;
062        }
063
064        protected void setRight(Set<PhysicalEntity> right)
065        {
066                this.right= right;
067        }
068
069        public void addRight(PhysicalEntity right)
070        {
071                if(right != null) {
072                        this.right.add(right);
073                        super.addParticipant(right);
074                }
075        }
076
077        public void removeRight(PhysicalEntity right)
078        {
079                if(right != null) {
080                        super.removeParticipant(right);
081                        this.right.remove(right);
082                }
083        }
084
085    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
086        @ManyToMany(targetEntity = PhysicalEntityImpl.class)
087        @JoinTable(name="leftParticipant")
088        public Set<PhysicalEntity> getLeft()
089        {
090                return left;
091        }
092
093        protected void setLeft(Set<PhysicalEntity> left)
094        {
095                this.left = left;
096        }
097
098        public void addLeft(PhysicalEntity left)
099        {
100                if(left != null) {
101                        this.left.add(left);
102                        super.addParticipant(left);
103                }
104        }
105
106        public void removeLeft(PhysicalEntity left)
107        {
108                if(left != null) {
109                        super.removeParticipant(left);
110                        this.left.remove(left);
111                }
112        }
113
114        
115        public Boolean getSpontaneous()
116        {
117                return spontaneous;
118        }
119
120        public void setSpontaneous(Boolean spontaneous)
121        {
122                this.spontaneous = spontaneous;
123        }
124
125    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
126        @ManyToMany(targetEntity = StoichiometryImpl.class)
127        @JoinTable(name="conversionstoichiometry")              
128        public Set<Stoichiometry> getParticipantStoichiometry()
129        {
130                return participantStoichiometry;
131        }
132
133        public void addParticipantStoichiometry(
134                        Stoichiometry participantStoichiometry)
135        {
136                this.participantStoichiometry.add(participantStoichiometry);
137        }
138
139        public void removeParticipantStoichiometry(
140                        Stoichiometry participantStoichiometry)
141        {
142                this.participantStoichiometry.remove(participantStoichiometry);
143        }
144
145        protected void setParticipantStoichiometry(
146                        Set<Stoichiometry> participantStoichiometry)
147        {
148                this.participantStoichiometry = participantStoichiometry;
149        }
150
151
152        @Enumerated
153        public ConversionDirectionType getConversionDirection()
154        {
155                return conversionDirection;
156        }
157
158        public void setConversionDirection(ConversionDirectionType spontanousType)
159        {
160                this.conversionDirection = spontanousType;
161        }
162
163
164        @Override
165        protected boolean semanticallyEquivalent(BioPAXElement element)
166        {
167                if(element.getModelInterface()== this.getModelInterface())
168                {
169                        Conversion that = (Conversion) element;
170                        if(that.getSpontaneous()==this.getSpontaneous() &&
171                       that.getConversionDirection() == this.getConversionDirection())
172                        {
173                                if(SetEquivalenceChecker.isEquivalent(this.getLeft(), that.getLeft()))
174                                {
175                                        return SetEquivalenceChecker.isEquivalent(this.getRight(), that.getRight());
176                                }
177                                else if(SetEquivalenceChecker.isEquivalent(this.getLeft(), that.getRight()))
178                                {
179                                        return(SetEquivalenceChecker.isEquivalent(this.getRight(), that.getLeft()));
180                                }
181                        }
182                }
183                return false;
184        }
185
186        @Override
187        public int equivalenceCode()
188        {
189                return getEqCodeForSet(this.getLeft())*getEqCodeForSet(this.getRight());
190
191        }
192
193        private int getEqCodeForSet(Set<PhysicalEntity> peSet)
194        {
195                int eqCode=0;
196                for (PhysicalEntity pe : peSet)
197                {
198                        eqCode+=pe.equivalenceCode();
199                }
200                return eqCode;
201        }
202
203}