END_TEST START_TEST (test_FunctionDefinition_setId) { const char *id = "pow3"; FunctionDefinition_setId(FD, id); fail_unless( !strcmp(FunctionDefinition_getId(FD), id) ); fail_unless( FunctionDefinition_isSetId(FD) ); if (FunctionDefinition_getId(FD) == id) { fail("FunctionDefinition_setId(...) did not make a copy of string."); } /* Reflexive case (pathological) */ FunctionDefinition_setId(FD, FunctionDefinition_getId(FD)); fail_unless( !strcmp(FunctionDefinition_getId(FD), id) ); FunctionDefinition_setId(FD, NULL); fail_unless( !FunctionDefinition_isSetId(FD) ); if (FunctionDefinition_getId(FD) != NULL) { fail("FunctionDefinition_setId(FD, NULL) did not clear string."); } }
void printFunctionDefinition (unsigned int n, FunctionDefinition_t *fd) { const ASTNode_t *math; char *formula; if ( FunctionDefinition_isSetMath(fd) ) { math = FunctionDefinition_getMath(fd); /* Print function body. */ if (ASTNode_getNumChildren(math) == 0) { printf("(no body defined)"); } else { math = ASTNode_getChild(math, ASTNode_getNumChildren(math) - 1); formula = SBML_formulaToDot(math); fprintf(fout, "subgraph cluster%u {\n", noClusters); fprintf(fout, "label=\"FunctionDefinition: %s\";\n%s\n", FunctionDefinition_getId(fd), formula); free(formula); noClusters++; } } }
END_TEST START_TEST (test_FunctionDefinition_setId2) { char *id = "e1"; int i = FunctionDefinition_setId(E, id); fail_unless( i == LIBSBML_OPERATION_SUCCESS ); fail_unless( !strcmp(FunctionDefinition_getId(E), id) ); fail_unless( FunctionDefinition_isSetId(E) ); i = FunctionDefinition_setId(E, NULL); fail_unless( i == LIBSBML_OPERATION_SUCCESS ); fail_unless( !FunctionDefinition_isSetId(E) ); }
void printFunctionDefinition (unsigned int n, const FunctionDefinition_t *fd) { const ASTNode_t *math; char *formula; if ( FunctionDefinition_isSetMath(fd) ) { printf("FunctionDefinition %d, %s(", n, FunctionDefinition_getId(fd)); math = FunctionDefinition_getMath(fd); /* Print function arguments. */ if (ASTNode_getNumChildren(math) > 1) { printf("%s", ASTNode_getName( ASTNode_getLeftChild(math) )); for (n = 1; n < ASTNode_getNumChildren(math) - 1; ++n) { printf(", %s", ASTNode_getName( ASTNode_getChild(math, n) )); } } printf(") := "); /* Print function body. */ if (ASTNode_getNumChildren(math) == 0) { printf("(no body defined)"); } else { math = ASTNode_getChild(math, ASTNode_getNumChildren(math) - 1); formula = SBML_formulaToString(math); printf("%s\n", formula); free(formula); } } }
END_TEST START_TEST (test_FunctionDefinition_createWith) { ASTNode_t *math = SBML_parseFormula("lambda(x, x^3)"); FunctionDefinition_t *fd = FunctionDefinition_create(2, 4); FunctionDefinition_setId(fd, "pow3"); FunctionDefinition_setMath(fd, math); const ASTNode_t * math1; char * formula; fail_unless( SBase_getTypeCode((SBase_t *) fd) == SBML_FUNCTION_DEFINITION ); fail_unless( SBase_getMetaId ((SBase_t *) fd) == NULL ); fail_unless( SBase_getNotes ((SBase_t *) fd) == NULL ); fail_unless( SBase_getAnnotation((SBase_t *) fd) == NULL ); fail_unless( FunctionDefinition_getName(fd) == NULL ); math1 = FunctionDefinition_getMath(fd); fail_unless( math1 != NULL ); formula = SBML_formulaToString(math1); fail_unless( formula != NULL ); fail_unless( !strcmp(formula, "lambda(x, x^3)") ); fail_unless( FunctionDefinition_getMath(fd) != math ); fail_unless( FunctionDefinition_isSetMath(fd) ); fail_unless( !strcmp(FunctionDefinition_getId(fd), "pow3") ); fail_unless( FunctionDefinition_isSetId(fd) ); ASTNode_free(math); safe_free(formula); FunctionDefinition_free(fd); }
int main (int argc, char* argv[]) { unsigned int i,j,errors; const char* filename; SBMLDocument_t* document; Model_t* m; if (argc != 2) { printf("\nUsage: printNotes filename\n\n"); return 1; } filename = argv[1]; document = readSBML(filename); errors = SBMLDocument_getNumErrors( document); printf("\n%s\n\n", filename); if(errors > 0) { SBMLDocument_printErrors(document, stderr); SBMLDocument_free(document); return errors; } /* Model */ m = SBMLDocument_getModel(document); printNotes((SBase_t*)m, Model_getId(m)); for(i=0; i < Model_getNumReactions(m); i++) { Reaction_t* re = Model_getReaction( m, i); printNotes((SBase_t*)re, Reaction_getId(re)); /* SpeciesReference (Reactant) */ for(j=0; j < Reaction_getNumReactants( re); j++) { SpeciesReference_t* rt = Reaction_getReactant(re, j); if (SBase_isSetNotes((SBase_t*) rt)) printf(" "); printNotes((SBase_t*)rt, SpeciesReference_getSpecies( rt ) ); } /* SpeciesReference (Product) */ for(j=0; j < Reaction_getNumProducts( re ); j++) { SpeciesReference_t* rt = Reaction_getProduct( re, j); if (SBase_isSetNotes((SBase_t*) rt)) printf(" "); printNotes((SBase_t*)rt, SpeciesReference_getSpecies( rt ) ); } /* ModifierSpeciesReference (Modifiers) */ for(j=0; j < Reaction_getNumModifiers( re ); j++) { SpeciesReference_t* md = Reaction_getModifier(re, j); if (SBase_isSetNotes((SBase_t*) md)) printf(" "); printNotes((SBase_t*)md, SpeciesReference_getSpecies( md ) ); } /* KineticLaw */ if(Reaction_isSetKineticLaw( re )) { KineticLaw_t* kl = Reaction_getKineticLaw( re ); if (SBase_isSetNotes((SBase_t*) kl)) printf(" "); printNotes((SBase_t*)kl, ""); /* Parameter */ for(j=0; j < KineticLaw_getNumParameters( kl ); j++) { Parameter_t* pa = KineticLaw_getParameter( kl, j); if (SBase_isSetNotes((SBase_t*) pa)) printf(" "); printNotes((SBase_t*)pa, Parameter_getId(pa)); } } } /* Species */ for(i=0; i < Model_getNumSpecies(m); i++) { Species_t* sp = Model_getSpecies(m, i); printNotes((SBase_t*)sp, Species_getId(sp)); } /* Compartments */ for(i=0; i < Model_getNumCompartments( m ); i++) { Compartment_t* sp = Model_getCompartment(m, i); printNotes((SBase_t*)sp, Compartment_getId(sp)); } /* FunctionDefinition */ for(i=0; i < Model_getNumFunctionDefinitions(m); i++) { FunctionDefinition_t* sp = Model_getFunctionDefinition(m, i); printNotes((SBase_t*)sp, FunctionDefinition_getId(sp)); } /* UnitDefinition */ for(i=0; i < Model_getNumUnitDefinitions(m); i++) { UnitDefinition_t* sp = Model_getUnitDefinition( m, i); printNotes((SBase_t*)sp, UnitDefinition_getId(sp)); } /* Parameter */ for(i=0; i < Model_getNumParameters( m ); i++) { Parameter_t* sp = Model_getParameter( m, i); printNotes((SBase_t*)sp, Parameter_getId(sp)); } /* Rule */ for(i=0; i < Model_getNumReactions( m ); i++) { Rule_t* sp = Model_getRule(m, i); printNotes((SBase_t*)sp, ""); } /* InitialAssignment */ for(i=0; i < Model_getNumInitialAssignments(m); i++) { InitialAssignment_t* sp = Model_getInitialAssignment(m, i); printNotes((SBase_t*)sp, ""); } /* Event */ for(i=0; i < Model_getNumEvents(m); i++) { Event_t* sp = Model_getEvent(m, i); printNotes((SBase_t*)sp, Event_getId(sp)); /* Trigger */ if(Event_isSetTrigger( sp )) { Trigger_t* tg = Event_getTrigger(sp); if (SBase_isSetNotes( (SBase_t*) tg)) printf( " " ); printNotes((SBase_t*)tg, ""); } /* Delay */ if(Event_isSetDelay(sp)) { Delay_t* dl = Event_getDelay(sp); if (SBase_isSetNotes( (SBase_t*) dl)) printf( " " ); printNotes((SBase_t*) dl, ""); } /* EventAssignment */ for(j=0; j < Event_getNumEventAssignments(sp); j++) { EventAssignment_t* ea = Event_getEventAssignment(sp, j); if (SBase_isSetNotes( (SBase_t*) ea)) printf( " " ); printNotes((SBase_t*)ea, ""); } } /* SpeciesType */ for(i=0; i < Model_getNumSpeciesTypes(m); i++) { SpeciesType_t* sp = Model_getSpeciesType(m, i); printNotes((SBase_t*)sp, SpeciesType_getId(sp)); } /* Constraints */ for(i=0; i < Model_getNumConstraints(m); i++) { Constraint_t* sp = Model_getConstraint(m, i); printNotes((SBase_t*)sp, ""); } SBMLDocument_free( document ); return errors; }
void printReactions(Model_t *m, FILE *f) { int i,j,k; Reaction_t *r; SpeciesReference_t *sref; KineticLaw_t *kl; Rule_t *rl; AssignmentRule_t *asr; AlgebraicRule_t *alr; RateRule_t *rr; Event_t *e; EventAssignment_t *ea; Parameter_t *p; FunctionDefinition_t *fd; SBMLTypeCode_t type; const ASTNode_t *math; math = NULL; fprintf(f, "\n"); for(i=0;i<Model_getNumParameters(m);i++){ if(i==0) fprintf(f, "# Global parameters:\n"); p = Model_getParameter(m,i); if(Parameter_isSetId(p)) fprintf(f, "%s ", Parameter_getId(p)); if(Parameter_isSetName(p)) fprintf(f, "(%s) ", Parameter_getName(p)); if(Parameter_isSetValue(p)) fprintf(f, "= %g; ", Parameter_getValue(p)); if(Parameter_isSetUnits(p)) fprintf(f, "[%s]; ", Parameter_getUnits(p)); if(!Parameter_getConstant(p)) fprintf(f, "(variable);"); fprintf(f, "\n"); if ( i==Model_getNumParameters(m)-1 ) fprintf(f, "\n"); } fprintf(f, "# Reactions:\n"); for ( i=0; i<Model_getNumReactions(m); i++ ) { r = Model_getReaction(m,i); fprintf(f, "%s: %s", Reaction_isSetName(r) ? Reaction_getName(r) : Reaction_getId(r), Reaction_getFast(r) ? "(fast)" : ""); for ( k=0; k<Reaction_getNumReactants(r); k++ ) { sref = Reaction_getReactant(r,k); if ( SpeciesReference_isSetStoichiometryMath(sref) ) fprintf(f, "%s ", SBML_formulaToString(\ SpeciesReference_getStoichiometryMath(sref))); else if ( SpeciesReference_getStoichiometry(sref) != 1. ) fprintf(f, "%g ", SpeciesReference_getStoichiometry(sref)); fprintf(f, "%s", SpeciesReference_getSpecies(sref)); if(k+1<Reaction_getNumReactants(r)) fprintf(f, "%s", " + "); } fprintf(f, "%s", Reaction_getReversible(r) ? " <-> " : " -> "); for ( k=0; k<Reaction_getNumProducts(r); k++ ) { sref = Reaction_getProduct(r,k); if ( SpeciesReference_isSetStoichiometryMath(sref) ) fprintf(f, "%s ", SBML_formulaToString(\ SpeciesReference_getStoichiometryMath(sref))); else if ( SpeciesReference_getStoichiometry(sref) != 1. ) fprintf(f, "%g ", SpeciesReference_getStoichiometry(sref)); fprintf(f, "%s", SpeciesReference_getSpecies(sref)); if(k+1<Reaction_getNumProducts(r)) fprintf(f, "%s", " + "); } fprintf(f, "; "); if(Reaction_isSetKineticLaw(r)){ kl = Reaction_getKineticLaw(r); math = KineticLaw_getMath(kl); fprintf(f, "%s;", SBML_formulaToString(math)); for(k=0;k<KineticLaw_getNumParameters(kl);k++){ p = KineticLaw_getParameter(kl,k); fprintf(f, " %s", Parameter_getId(p)); if(Parameter_isSetName(p)) fprintf(f, " (%s)", Parameter_getName(p)); if(Parameter_isSetValue(p)) fprintf(f, " = %g", Parameter_getValue(p)); if(Parameter_isSetUnits(p)) fprintf(f, " [%s]", Parameter_getUnits(p)); if ( !Parameter_getConstant(p) ) fprintf(f, " (variable)"); fprintf(f, ";"); } /* fprintf(f, "\n"); */ }else fprintf(f, "# no rate law is set for this reaction."); fprintf(f, "\n"); } for(i=0;i<Model_getNumRules(m);i++){ rl = Model_getRule(m,i); if ( i == 0 ) { fprintf(f, "# Rules:\n"); } type = SBase_getTypeCode((SBase_t *)rl); if ( type == SBML_RATE_RULE ) { rr = (RateRule_t *) rl; fprintf(f, " rateRule: d%s/dt = ", RateRule_getVariable(rr)); } if ( type == SBML_ALGEBRAIC_RULE ) { alr = (AlgebraicRule_t *) rl; fprintf(f, " algebraicRule: 0 = "); } if ( type == SBML_ASSIGNMENT_RULE ) { asr = (AssignmentRule_t *) rl; fprintf(f, " assignmentRule (%s): %s = ", RuleType_toString(AssignmentRule_getType(asr)), AssignmentRule_getVariable(asr)); } if(!Rule_isSetMath(rl)){ if(Rule_isSetFormula(rl)){ Rule_setMathFromFormula(rl); } } if(Rule_isSetMath(rl)) fprintf(f, "%s\n", SBML_formulaToString(Rule_getMath(rl))); } fprintf(f, "\n"); for(i=0;i<Model_getNumEvents(m);i++){ if(i==0) fprintf(f, "# Events:\n"); e = Model_getEvent(m,i); if(Event_isSetId(e)) fprintf(f, "%s: ", Event_getId(e)); if(Event_isSetName(e)) fprintf(f, "(%s) ", Event_getName(e)); if(Event_isSetTrigger(e)) { math = Event_getTrigger(e); fprintf(f, "trigger: %s\n", SBML_formulaToString(math)); } if(Event_isSetDelay(e)) fprintf(f, "delay: %s;\n", SBML_formulaToString(Event_getDelay(e))); if(Event_isSetTimeUnits(e)) fprintf(f, "time Units: %s;\n", Event_getTimeUnits(e)); for(k=0;k<Event_getNumEventAssignments(e);k++){ ea = Event_getEventAssignment(e,k); if(EventAssignment_isSetVariable(ea)) fprintf(f, " event: %s = %s;\n", EventAssignment_getVariable(ea), EventAssignment_isSetMath(ea) ? SBML_formulaToString(EventAssignment_getMath(ea)) : "# no math set;\n"); } if(i==Model_getNumEvents(m)-1) fprintf(f, "\n"); } for ( i=0; i<Model_getNumFunctionDefinitions(m); i++ ) { if ( i==0 ) fprintf(f, "# Functions:\n"); fd = Model_getFunctionDefinition(m,i); if ( FunctionDefinition_isSetName(fd) ) fprintf(f, "%s: ", FunctionDefinition_getName(fd)); if(FunctionDefinition_isSetId(fd) && FunctionDefinition_isSetMath(fd)){ fprintf(f, "%s( ", FunctionDefinition_getId(fd)); math = FunctionDefinition_getMath(fd); for(j=0;j<ASTNode_getNumChildren(math)-1;j++){ fprintf(f, "%s", SBML_formulaToString(ASTNode_getChild(math, j))); if(j<ASTNode_getNumChildren(math)-2) fprintf(f, ", "); if(j==ASTNode_getNumChildren(math)-2) fprintf(f, ") = "); } fprintf(f, "%s;", SBML_formulaToString(ASTNode_getRightChild(math))); } fprintf(f, "\n"); } }
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; }