# Path: stochpy/implementations/StochPyTools.py
# class Species():
# class StochPySSA_Shared(): 
#     def __init__(self):
#     def Parse(self,model_file,model_dir,IsTauleaping=False,IsNRM=False,IsDelayed = False,IsSMM = False,IsQuiet=False):
#     def SpeciesSelection(self):
#     def RateSelection(self):
#     def SetEvents(self):
#     def Propensities(self,IsTauleaping=False):
#     def BuildPropensityCodes(self, propensities = None): # 21-11-2013 
#     def HandleEvents(self,IsTauleapingStep=False):    
#     def AssignmentRules(self):
#     def rateFunc(self,rate_eval_code,r_vec):
#     def Initial_Conditions(self,IsTauleaping = False):              
#     def GenerateOutput(self,IsTauleaping = False,completion_delayed = False):


import sys,copy,time,os,re,functools
from .StochPyTools import __species__,StochPySSA_Shared,np
                    if self.parse.reaction_orders[j] == 1: 
                        self.tau_arrays[j] = np.delete(self.tau_arrays[j], mol_num)
                    elif self.parse.reaction_orders[j] == 2:                                        
                        reacts = self.parse.depends_on[j]     
                        axis = reacts.index(s_index)                                                # Specifying molecule on row or column
                        self.tau_arrays[j] = np.delete(self.tau_arrays[j], mol_num, axis)           
                        if reacts[0] == reacts[1]:                                                  # e.g. y + y > z. axis was 0.
                            self.tau_arrays[j] = np.delete(self.tau_arrays[j], mol_num, 1)          # Also delete the molecule on the columns.
                    else:                                                                           # An error should be raised earlier, but just to be safe
                        raise Warning("Error: The SMM does not support third-order reactions. Use the fSMM and model it as a normal reaction.")                    
                        
    def Add_Taus_Products(self):
        """ For the molecules produced in the last reaction, add the absolute tau for every reaction they are reactant in. """
        species_produced = self.products[self.reaction_index] # self.parse.product_indices
        for s_index in species_produced:
            reactions_affected = self.parse.species_depends_on[s_index]
            for j in reactions_affected:
                #reacts = self.parse.reactant_indices[j]
                reacts = self.parse.depends_on[j]
                #print(3,j,self.tau_arrays[j])
                if self.parse.reaction_orders[j] == 1: 
                    new_tau = self.distr_functions[j](*self.distr_parameters[j])
                    assert new_tau >= 0, "Error: Negative waiting time(s) generated by the distribution '{0:s}'!".format(self.distr_functions[j].__name__)
                    self.tau_arrays[j] = np.append( self.tau_arrays[j], new_tau + self.sim_t )                
                elif self.parse.reaction_orders[j] == 2:    # Second-order reaction
                    axis = reacts.index(s_index)          # Check whether produced species on row (=0) or column (=1)
                    temp_arr = self.tau_arrays[j]
                    if axis == 0:
                        new_row = self.distr_functions[j](*self.distr_parameters[j], size = temp_arr.shape[1])  # Taus for every molecule it reacts with
                        assert (new_row >= 0).all(), "Error: Negative waiting time(s) generated by the distribution '{0:s}'!".format(self.distr_functions[j].__name__)

# Path: stochpy/implementations/StochPyTools.py
# class Species():
# class StochPySSA_Shared(): 
#     def __init__(self):
#     def Parse(self,model_file,model_dir,IsTauleaping=False,IsNRM=False,IsDelayed = False,IsSMM = False,IsQuiet=False):
#     def SpeciesSelection(self):
#     def RateSelection(self):
#     def SetEvents(self):
#     def Propensities(self,IsTauleaping=False):
#     def BuildPropensityCodes(self, propensities = None): # 21-11-2013 
#     def HandleEvents(self,IsTauleapingStep=False):    
#     def AssignmentRules(self):
#     def rateFunc(self,rate_eval_code,r_vec):
#     def Initial_Conditions(self,IsTauleaping = False):              
#     def GenerateOutput(self,IsTauleaping = False,completion_delayed = False):


# Path: stochpy/implementations/SingleMoleculeMethod.py
import sys,copy,time,os,re,functools
from .StochPyTools import __species__,StochPySSA_Shared,np

                    if self.parse.reaction_orders[j] == 1: 
                        self.tau_arrays[j] = np.delete(self.tau_arrays[j], mol_num)
                    elif self.parse.reaction_orders[j] == 2:                                        
                        reacts = self.parse.depends_on[j]     
                        axis = reacts.index(s_index)                                                # Specifying molecule on row or column
                        self.tau_arrays[j] = np.delete(self.tau_arrays[j], mol_num, axis)           
                        if reacts[0] == reacts[1]:                                                  # e.g. y + y > z. axis was 0.
                            self.tau_arrays[j] = np.delete(self.tau_arrays[j], mol_num, 1)          # Also delete the molecule on the columns.
                    else:                                                                           # An error should be raised earlier, but just to be safe
                        raise Warning("Error: The SMM does not support third-order reactions. Use the fSMM and model it as a normal reaction.")                    
                        
    def Add_Taus_Products(self):
        """ For the molecules produced in the last reaction, add the absolute tau for every reaction they are reactant in. """
        species_produced = self.products[self.reaction_index] # self.parse.product_indices
        for s_index in species_produced:
            reactions_affected = self.parse.species_depends_on[s_index]
            for j in reactions_affected:
                #reacts = self.parse.reactant_indices[j]
                reacts = self.parse.depends_on[j]
                #print(3,j,self.tau_arrays[j])
                if self.parse.reaction_orders[j] == 1: 
                    new_tau = self.distr_functions[j](*self.distr_parameters[j])
                    assert new_tau >= 0, "Error: Negative waiting time(s) generated by the distribution '{0:s}'!".format(self.distr_functions[j].__name__)
                    self.tau_arrays[j] = np.append( self.tau_arrays[j], new_tau + self.sim_t )                
                elif self.parse.reaction_orders[j] == 2:    # Second-order reaction
                    axis = reacts.index(s_index)          # Check whether produced species on row (=0) or column (=1)
                    temp_arr = self.tau_arrays[j]
                    if axis == 0:
                        new_row = self.distr_functions[j](*self.distr_parameters[j], size = temp_arr.shape[1])  # Taus for every molecule it reacts with
                        assert (new_row >= 0).all(), "Error: Negative waiting time(s) generated by the distribution '{0:s}'!".format(self.distr_functions[j].__name__)

                        self.tau_arrays[j] = np.row_stack( (temp_arr, new_row + self.sim_t) )