void post_ev_alter_tree_structure(Model_t *m, ASTNode_t **node_p, ASTNode_t *parent, int child_order){ ASTNode_t *divide_node, *times_node; ASTNode_t *compartment_node; ASTNode_t *node, *next_node; unsigned int i; Species_t *sp; node = *node_p; for(i=0; i<ASTNode_getNumChildren(node); i++){ next_node = ASTNode_getChild(node, i); post_ev_alter_tree_structure(m, &next_node, *node_p, i); } if(ASTNode_getType(node) == AST_NAME){ for(i=0; i<Model_getNumSpecies(m); i++){ sp = (Species_t*)ListOf_get(Model_getListOfSpecies(m), i); if(strcmp(Species_getId(sp), ASTNode_getName(node)) == 0){ if(!Species_getHasOnlySubstanceUnits(sp) && Compartment_getSpatialDimensions(Model_getCompartmentById(m, Species_getCompartment(sp))) != 0){/* use val/comp in calculation */ divide_node = ASTNode_createWithType(AST_DIVIDE); compartment_node = ASTNode_createWithType(AST_NAME); ASTNode_setName(compartment_node, Compartment_getId(Model_getCompartmentById(m, Species_getCompartment(sp)))); ASTNode_addChild(divide_node, node); ASTNode_addChild(divide_node, compartment_node); if(parent != NULL){ ASTNode_replaceChild(parent, child_order, divide_node); }else{ *node_p = divide_node; } node = *node_p; break; }else if(Species_getHasOnlySubstanceUnits(sp) && Species_isSetInitialConcentration(sp) && Compartment_getSpatialDimensions(Model_getCompartmentById(m, Species_getCompartment(sp))) != 0){/* use val*comp in calculation */ times_node = ASTNode_createWithType(AST_TIMES); compartment_node = ASTNode_createWithType(AST_NAME); ASTNode_setName(compartment_node, Compartment_getId(Model_getCompartmentById(m, Species_getCompartment(sp)))); ASTNode_addChild(times_node, node); ASTNode_addChild(times_node, compartment_node); if(parent != NULL){ ASTNode_replaceChild(parent, child_order, times_node); }else{ *node_p = times_node; } node = *node_p; break; } } } } return; }
END_TEST START_TEST (test_SpeciesReference_setStoichiometryMath1) { StoichiometryMath_t * sm = StoichiometryMath_create(2, 4); ASTNode_t *math = ASTNode_createWithType(AST_TIMES); ASTNode_t *a = ASTNode_create(); ASTNode_t *b = ASTNode_create(); ASTNode_setName(a, "a"); ASTNode_setName(b, "b"); ASTNode_addChild(math, a); ASTNode_addChild(math, b); StoichiometryMath_setMath(sm, math); int i = SpeciesReference_setStoichiometryMath(sr, sm); fail_unless( i == LIBSBML_OPERATION_SUCCESS); fail_unless( SpeciesReference_isSetStoichiometryMath(sr) ); fail_unless( SpeciesReference_getStoichiometry(sr) == 1 ); i = SpeciesReference_unsetStoichiometryMath(sr); fail_unless( i == LIBSBML_OPERATION_SUCCESS); fail_unless( !SpeciesReference_isSetStoichiometryMath(sr) ); StoichiometryMath_free(sm); }
END_TEST START_TEST (test_Rule_setMath1) { ASTNode_t *math = ASTNode_createWithType(AST_TIMES); ASTNode_t *a = ASTNode_create(); ASTNode_t *b = ASTNode_create(); ASTNode_setName(a, "a"); ASTNode_setName(b, "b"); ASTNode_addChild(math, a); ASTNode_addChild(math, b); char *formula; const ASTNode_t *math1; int i = Rule_setMath(R, math); fail_unless( i == LIBSBML_OPERATION_SUCCESS); fail_unless( Rule_isSetMath(R) ); math1 = Rule_getMath(R); fail_unless( math1 != NULL ); formula = SBML_formulaToString(math1); fail_unless( formula != NULL ); fail_unless( !strcmp(formula, "a * b") ); ASTNode_free(math); }
END_TEST START_TEST (test_SpeciesReference_setStoichiometryMath5) { SpeciesReference_t *sr1 = SpeciesReference_create(1, 2); StoichiometryMath_t * sm = StoichiometryMath_create(2, 4); ASTNode_t *math = ASTNode_createWithType(AST_TIMES); ASTNode_t *a = ASTNode_create(); ASTNode_t *b = ASTNode_create(); ASTNode_setName(a, "a"); ASTNode_setName(b, "b"); ASTNode_addChild(math, a); ASTNode_addChild(math, b); StoichiometryMath_setMath(sm, math); int i = SpeciesReference_setStoichiometryMath(sr1, sm); fail_unless( i == LIBSBML_UNEXPECTED_ATTRIBUTE); fail_unless( !SpeciesReference_isSetStoichiometryMath(sr1) ); StoichiometryMath_free(sm); SpeciesReference_free(sr1); }
END_TEST START_TEST (test_StoichiometryMath_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_DIVIDE); int i = StoichiometryMath_setMath(D, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !StoichiometryMath_isSetMath(D) ); ASTNode_free(math); }
END_TEST START_TEST (test_Trigger_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_TIMES); int i = Trigger_setMath(D, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !Trigger_isSetMath(D) ); ASTNode_free(math); }
END_TEST START_TEST (test_EventAssignment_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_DIVIDE); int i = EventAssignment_setMath(E, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !EventAssignment_isSetMath(E) ); ASTNode_free(math); }
END_TEST START_TEST (test_Priority_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_DIVIDE); int i = Priority_setMath(P, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !Priority_isSetMath(P) ); ASTNode_free(math); }
END_TEST START_TEST (test_Constraint_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_DIVIDE); int i = Constraint_setMath(C, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !Constraint_isSetMath(C) ); ASTNode_free(math); }
END_TEST START_TEST (test_FunctionDefinition_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_TIMES); int i = FunctionDefinition_setMath(E, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !FunctionDefinition_isSetMath(E) ); ASTNode_free(math); }
void alg_alter_tree_structure(ASTNode_t **node_p, ASTNode_t *parent, int child_order) { ASTNode_t *node, *left, *right; ASTNode_t *times_node, *one_node; node = *node_p; if((left=ASTNode_getLeftChild(node)) != NULL) { alg_alter_tree_structure(&left, node, 0); } if((right=ASTNode_getRightChild(node)) != NULL) { alg_alter_tree_structure(&right, node, 1); } if(ASTNode_getType(node) == AST_NAME) { times_node = ASTNode_createWithType(AST_TIMES); one_node = ASTNode_createWithType(AST_INTEGER); ASTNode_setInteger(one_node, 1); ASTNode_addChild(times_node, one_node); ASTNode_addChild(times_node, node); if(parent != NULL) { ASTNode_replaceChild(parent, child_order, times_node); } else { *node_p = times_node; } } return; }
END_TEST START_TEST (test_KineticLaw_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_TIMES); ASTNode_t *a = ASTNode_create(); ASTNode_setName(a, "a"); ASTNode_addChild(math, a); int i = KineticLaw_setMath(kl, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !KineticLaw_isSetMath(kl) ); ASTNode_free(math); }
END_TEST START_TEST (test_Rule_setMath2) { ASTNode_t *math = ASTNode_createWithType(AST_DIVIDE); ASTNode_t *a = ASTNode_create(); ASTNode_setName(a, "a"); ASTNode_addChild(math, a); int i = Rule_setMath(R, math); fail_unless( i == LIBSBML_INVALID_OBJECT); fail_unless( !Rule_isSetMath(R) ); ASTNode_free(math); }
void pre_ev_alter_tree_structure(ASTNode_t **node_p, ASTNode_t *parent, int child_order, ASTNode_t *delay_math){ unsigned int i; ASTNode_t *node, *next_node, *delay_node; node = *node_p; for(i=0; i<ASTNode_getNumChildren(node); i++){ next_node = ASTNode_getChild(node, i); pre_ev_alter_tree_structure(&next_node, *node_p, i, delay_math); } if(ASTNode_getType(node) == AST_NAME){ delay_node = ASTNode_createWithType(AST_FUNCTION_DELAY); ASTNode_addChild(delay_node, node); ASTNode_addChild(delay_node, delay_math); if(parent != NULL){ ASTNode_replaceChild(parent, child_order, delay_node); }else{ *node_p = delay_node; } } }
END_TEST START_TEST (test_SpeciesReference_setStoichiometryMath2) { StoichiometryMath_t * sm = StoichiometryMath_create(2, 4); ASTNode_t *math = ASTNode_createWithType(AST_TIMES); ASTNode_t *a = ASTNode_create(); ASTNode_setName(a, "a"); ASTNode_addChild(math, a); StoichiometryMath_setMath(sm, math); int i = SpeciesReference_setStoichiometryMath(sr, sm); /* once the StoichiometryMath_setMath function does not set * an invalid ASTNode this changes to i == LIBSBML_OPERATION_SUCCESS fail_unless( i == LIBSBML_INVALID_OBJECT); */ fail_unless ( i == LIBSBML_OPERATION_SUCCESS ); fail_unless( SpeciesReference_isSetStoichiometryMath(sr) ); StoichiometryMath_free(sm); }
void _prepare_reversible_fast_reaction(Model_t *m, myASTNode *myNode, myReaction *re, mySpecies *sp[], myParameter *param[], myCompartment *comp[], myReaction *re_whole[], double sim_time, double dt, double *time, myInitialAssignment *initAssign[], char *time_variant_target_id[], unsigned int num_of_time_variant_targets, timeVariantAssignments *timeVarAssign, char *target_id, int p_or_r, allocated_memory *mem){ ASTNode_t *minus_node, *zero_node, *final_eq_node; myASTNode *eq_root_node; int minus_sign; if(myNode->left != NULL){ _prepare_reversible_fast_reaction(m, myNode->left, re, sp, param, comp, re_whole, sim_time, dt, time, initAssign, time_variant_target_id, num_of_time_variant_targets, timeVarAssign, target_id, p_or_r, mem); } if(myNode->right != NULL){ _prepare_reversible_fast_reaction(m, myNode->right, re, sp, param, comp, re_whole, sim_time, dt, time, initAssign, time_variant_target_id, num_of_time_variant_targets, timeVarAssign, target_id, p_or_r, mem); } if(ASTNode_getType(myNode->origin) == AST_NAME){ if(strcmp(ASTNode_getName(myNode->origin), target_id) == 0){ ASTNode_setType(myNode->origin, AST_INTEGER); ASTNode_setInteger(myNode->origin, 1); eq_root_node = myNode; minus_sign = 1; while(eq_root_node->parent != NULL){ if(ASTNode_getType(eq_root_node->parent->origin) != AST_TIMES && ASTNode_getType(eq_root_node->parent->origin) != AST_DIVIDE){ if(ASTNode_getType(eq_root_node->parent->origin) == AST_MINUS && eq_root_node->parent->right == eq_root_node){ minus_sign *= -1; } if(eq_root_node->parent->parent != NULL){ if(eq_root_node->parent->parent->left == eq_root_node->parent){ eq_root_node->parent->parent->left = eq_root_node; }else{ eq_root_node->parent->parent->right = eq_root_node; } eq_root_node->parent = eq_root_node->parent->parent; }else{ eq_root_node->parent = NULL; break; } }else{ eq_root_node = eq_root_node->parent; } } final_eq_node = eq_root_node->origin; TRACE(("myASTNode is\n")); check_myAST(eq_root_node); ASTNode_recreate(eq_root_node, final_eq_node); if(minus_sign == -1){ minus_node = ASTNode_createWithType(AST_MINUS); zero_node = ASTNode_createWithType(AST_INTEGER); ASTNode_setInteger(zero_node, 0); ASTNode_addChild(minus_node, zero_node); ASTNode_addChild(minus_node, final_eq_node); final_eq_node = minus_node; } if(p_or_r == 0){/* products coefficient */ TRACE(("AST of product numerator is\n")); check_AST(final_eq_node, NULL); re->products_equili_numerator->math_length = get_equation(m, re->products_equili_numerator, sp, param, comp, re_whole, final_eq_node, 0, sim_time, dt, time, initAssign, time_variant_target_id, num_of_time_variant_targets, timeVarAssign, mem); }else{/* reactants coefficient */ minus_node = ASTNode_createWithType(AST_MINUS); zero_node = ASTNode_createWithType(AST_INTEGER); ASTNode_setInteger(zero_node, 0); ASTNode_addChild(minus_node, zero_node); ASTNode_addChild(minus_node, final_eq_node); final_eq_node = minus_node; TRACE(("AST of reactant numerator is\n")); check_AST(final_eq_node, NULL); re->reactants_equili_numerator->math_length = get_equation(m, re->reactants_equili_numerator, sp, param, comp, re_whole, final_eq_node, 0, sim_time, dt, time, initAssign, time_variant_target_id, num_of_time_variant_targets, timeVarAssign, mem); } } } }
void ev_alter_tree_structure(Model_t *m, ASTNode_t **node_p, ASTNode_t *parent, int child_order, copied_AST *cp_AST){ ASTNode_t *zero_node; ASTNode_t *node, *next_node; ASTNode_t *pc_eq, *pc_cd, *times_node, *and_node, *not_node; unsigned int i, j; int p; ASTNode_t *arg_node_list[MAX_ARG_NUM]; unsigned int arg_node_num; FunctionDefinition_t *fd; ASTNode_t *fd_arg; ASTNode_t *fd_body; node = *node_p; for(i=0; i<ASTNode_getNumChildren(node); i++){ next_node = ASTNode_getChild(node, i); if(ASTNode_getNumChildren(node) == 1 && ASTNode_getType(node) == AST_MINUS){ zero_node = ASTNode_create(); ASTNode_setType(zero_node, AST_REAL); ASTNode_setReal(zero_node, 0); ASTNode_replaceChild(node, 0, zero_node); ASTNode_addChild(*node_p, next_node); }else{ ev_alter_tree_structure(m, &next_node, *node_p, i, cp_AST); } } if(ASTNode_getType(node) == AST_FUNCTION){ arg_node_num = ASTNode_getNumChildren(node); for(i=0; i<arg_node_num; i++){ arg_node_list[i] = ASTNode_getChild(node, i); } for(i=0; i<Model_getNumFunctionDefinitions(m); i++){ fd = (FunctionDefinition_t*)ListOf_get(Model_getListOfFunctionDefinitions(m), i); fd_body = (ASTNode_t*)FunctionDefinition_getBody(fd); if(strcmp(FunctionDefinition_getId(fd), ASTNode_getName(node)) == 0){ fd_body = ASTNode_deepCopy(fd_body); cp_AST->ast[cp_AST->num_of_copied_AST++] = fd_body; for(j=0; j<FunctionDefinition_getNumArguments(fd); j++){ fd_arg = (ASTNode_t*)FunctionDefinition_getArgument(fd, j); ASTNode_replaceArgument(fd_body, (char*)ASTNode_getName(fd_arg), arg_node_list[j]); } /* check_AST(fd_body, NULL); */ if(parent != NULL){ ASTNode_replaceChild(parent, child_order, fd_body); }else{ *node_p = fd_body; } node = *node_p; break; } } } if(ASTNode_getType(node) == AST_FUNCTION_PIECEWISE){ ASTNode_setType(node, AST_PLUS); ASTNode_setName(node, NULL); times_node = ASTNode_createWithType(AST_TIMES); pc_eq = ASTNode_getRightChild(node); ASTNode_addChild(times_node, pc_eq); if(ASTNode_getNumChildren(node) > 3){ and_node = ASTNode_createWithType(AST_LOGICAL_AND); ASTNode_addChild(times_node, and_node); for(p=(int)ASTNode_getNumChildren(node)-2; p >= 1; p = p-2){ pc_cd = ASTNode_getChild(node, p); not_node = ASTNode_createWithType(AST_LOGICAL_NOT); ASTNode_addChild(not_node, pc_cd); ASTNode_addChild(and_node, not_node); } ASTNode_reduceToBinary(and_node); }else{ pc_cd = ASTNode_getChild(node, 1); not_node = ASTNode_createWithType(AST_LOGICAL_NOT); ASTNode_addChild(not_node, pc_cd); ASTNode_addChild(times_node, not_node); } ASTNode_replaceChild(node, ASTNode_getNumChildren(node)-1, times_node); for(p=ASTNode_getNumChildren(node)-2; p >= 1; p = p-2){ times_node = ASTNode_createWithType(AST_TIMES); pc_eq = ASTNode_getChild(node, p-1); pc_cd = ASTNode_getChild(node, p); ASTNode_addChild(times_node, pc_eq); ASTNode_addChild(times_node, ASTNode_deepCopy(pc_cd)); ASTNode_removeChild(node, p); ASTNode_replaceChild(node ,p-1, times_node); } ASTNode_reduceToBinary(node); } return; }