Skip to content

Commit

Permalink
committing these as I need to backup my hard drive
Browse files Browse the repository at this point in the history
  • Loading branch information
skeating committed Dec 12, 2024
1 parent 67b2eef commit 8cb6215
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 162 deletions.
181 changes: 25 additions & 156 deletions src/sbml/conversion/ExpressionAnalyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,28 @@ bool ExpressionAnalyser::expressionExists(SubstitutionValues_t* current,
matchesXValue(current, mightAdd) &&
matchesDxdtExpression(current, mightAdd);

if (alreadyExists && current->type == mightAdd->type)
{
switch (current->type)
{
case TYPE_K_MINUS_X_MINUS_Y:
alreadyExists = alreadyExists && matchesYValue(current, mightAdd) &&
matchesDydtExpression(current, mightAdd);
break;
case TYPE_K_PLUS_V_MINUS_X_MINUS_Y:
alreadyExists = alreadyExists && matchesVExpression(current, mightAdd) &&
matchesYValue(current, mightAdd);
break;
case TYPE_K_MINUS_X_PLUS_W_MINUS_Y:
alreadyExists = alreadyExists && matchesWExpression(current, mightAdd) &&
matchesYValue(current, mightAdd);
break;
default:
break;
}
return alreadyExists;
}

//switch (current->type)
//{
//case TYPE_K_MINUS_X_MINUS_Y:
Expand Down Expand Up @@ -567,8 +589,8 @@ ExpressionAnalyser::analyseNode(ASTNode* node, SubstitutionValues_t *value)
ASTNodeType_t type = node->getType();
ASTNode* rightChild = node->getRightChild();
ASTNode* leftChild = node->getLeftChild();
cout << "RIGHT CHILD: " << SBML_formulaToL3String(rightChild) << endl;
cout << "left CHILD: " << SBML_formulaToL3String(leftChild) << endl;
//cout << "RIGHT CHILD: " << SBML_formulaToL3String(rightChild) << endl;
//cout << "left CHILD: " << SBML_formulaToL3String(leftChild) << endl;

if (isTypeKminusX(numChildren, rightChild, leftChild, type, value) ||
isTypeKminusXminusY(numChildren, rightChild, leftChild, type, value) ||
Expand All @@ -583,150 +605,6 @@ ExpressionAnalyser::analyseNode(ASTNode* node, SubstitutionValues_t *value)
}



//switch (type)
//{
////case AST_PLUS:
// // -x+y node binary; plus; left child type minus; rightchild var/const
// // +
// // - y
// // x
// // we might have encountered w+(k-x)as part of k-x+w-y but it might not be part of it
// if (rightChild->getNumChildren() == 2 && analyseNode(rightChild, value) && value->type == TYPE_K_MINUS_X)
// {
// value->w_expression = leftChild;
// value->type = TYPE_K_MINUS_X_PLUS_W_MINUS_Y;
// value->current = node;
// return true;
// }
// if (numChildren != 2 || rightChild->getType() != AST_NAME
// || leftChild->getType() != AST_MINUS
// || leftChild->getNumChildren() != 1)
// return false;

// // if we get to this point, the only thing left to check is
// // whether the ->left->right grandchild (the x in -x+y) is a variable species.
// if (isVariableSpeciesOrParameter(leftChild->getChild(0)))
// {
// value->x_value = leftChild->getChild(0)->getName();
// value->y_value = rightChild->getName();
// value->dydt_expression = getODEFor(rightChild->getName());
// value->dxdt_expression = getODEFor(leftChild->getChild(0)->getName());
// value->type = TYPE_MINUS_X_PLUS_Y;
// value->current = node;
// return true;
// }
// break;

//case AST_MINUS:
// // - -
// // k x - y
// // k x
// // k-x or k-x-y node binary; right child (x,y) is variable
// // - - -
// // + x - y + y
// // k v + x - w
// // k v k x
// //
// // // k+v-x; right child x var; left child plus node with left child k constant
// // k+v-x-y; rightchild var y; left child minus == k+v-x
// // k-x+w-y; rightchild var y; left child plus with left child == k-x
// if (numChildren != 2 || !isVariableSpeciesOrParameter(rightChild))
// return false;
// // if left child is numerical constant or a parameter and right child variable, it IS k-x
// if (isNumericalConstantOrConstantParameter(leftChild, isNumber)
// && isVariableSpeciesOrParameter(rightChild))
// {
//
// if (isNumber)
// {
// value->k_value = "number";
// value->k_real_value = leftChild->getValue();
// }
// else
// {
// value->k_value = leftChild->getName();
// }
// value->x_value = rightChild->getName();
// value->dxdt_expression = getODEFor(rightChild->getName());
// value->type = TYPE_K_MINUS_X;
// value->current = node;
// return true;
// }
// // left child + with it's left child const we have k+v-x
// // left child + with it's left child k-x+w-y we have already finished
// else if (leftChild->getType() == AST_PLUS)
// {

// // TO DO fix this for k Or w being a number
// if (value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y)
// {
// return true;
// }
// else if (analyseNode(leftChild, value))
// {
// if (value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y)
// {
// value->y_value = rightChild->getName();
// value->dydt_expression = getODEFor(rightChild->getName());
// value->current = node;
// return true;
// }
// else if (value->type == TYPE_K_MINUS_X)
// {
// value->y_value = rightChild->getName();
// value->dydt_expression = getODEFor(rightChild->getName());
// value->w_expression = leftChild->getChild(1);
// value->type = TYPE_K_MINUS_X_PLUS_W_MINUS_Y;
// value->current = node;
// return true;

// }
// }
// else if (isNumericalConstantOrConstantParameter(leftChild->getChild(0), isNumber))
// {
// value->k_value = leftChild->getChild(0)->getName();
// value->x_value = rightChild->getName();
// value->dxdt_expression = getODEFor(rightChild->getName());
// value->v_expression = leftChild->getChild(1);
// value->type = TYPE_K_PLUS_V_MINUS_X;
// value->current = node;
// return true;
// }
// }
// else if (leftChild->getType() == AST_MINUS
// && isVariableSpeciesOrParameter(leftChild->getRightChild()))
// {
// // if left child is k+v-x we have k+v-x-ymean you'll have to come againthat you can pay3
// // or if left child is k-x we have k-x-y
// if (analyseNode(leftChild, value))
// {
// if (value->type == TYPE_K_PLUS_V_MINUS_X)
// {
// value->type = TYPE_K_PLUS_V_MINUS_X_MINUS_Y;
// value->y_value = rightChild->getName();
// value->dydt_expression = getODEFor(value->y_value);
// value->current = node;
// return true;
// }
// else if (value->type == TYPE_K_MINUS_X)
// {
// value->y_value = rightChild->getName();
// value->dydt_expression = getODEFor(rightChild->getName());
// value->type = TYPE_K_MINUS_X_MINUS_Y;
// value->current = node;
// return true;
// }
// }
// return false;
// }
// break;
//default:
// return false;
//}
//return false;


/*
* Return the ODE for the given variable
* or an ASTNode representing zero if there is no time derivative
Expand Down Expand Up @@ -824,21 +702,12 @@ ExpressionAnalyser::analyse(bool minusXPlusYOnly)
while (it != operators->end())
{
ASTNode* currentNode = (ASTNode*)*it;
if (minusXPlusYOnly)
cout << "current node in -x+y analyze: " << SBML_formulaToL3String(currentNode) << endl;
else
cout << "current node in not -x+y analyze: " << SBML_formulaToL3String(currentNode) << endl;
if (minusXPlusYOnly && currentNode->getType() != AST_PLUS)
{
it++;
continue;
}
SubstitutionValues_t* value = createBlankSubstitutionValues();

if (analyseNode(currentNode, value))
{
value->odeIndex = odeIndex;
if (!hasExpressionAlreadyBeenRecorded(value))
if (hasExpressionAlreadyBeenRecorded(value) == false)
{
//printSubstitutionValues(value);
mExpressions.push_back(value);
Expand Down
81 changes: 78 additions & 3 deletions src/sbml/conversion/test/TestExpressionAnalyser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file TestExpressionAnalyser.cpp
+* @file TestExpressionAnalyser.cpp
* @brief Tests for raterule to reaction converter
* @author Sarah Keating
* @author Alessandro Felder
Expand Down Expand Up @@ -174,6 +174,80 @@ START_TEST(test_analyse)
}
END_TEST

START_TEST(test_analyse_same_expression)
{
RateRule* rr = d->getModel()->createRateRule();
rr->setVariable("a");
rr->setMath(SBML_parseFormula("k-x-y"));

RateRule* rr1 = d->getModel()->createRateRule();
rr1->setVariable("b");
rr1->setMath(SBML_parseFormula("k-x-y"));


converter->populateInitialODEinfo();
ExpressionAnalyser* analyser = new ExpressionAnalyser(m, converter->getOdePairs());

fail_unless(analyser->getNumExpressions() == 0);

analyser->analyse();

fail_unless(analyser->getNumExpressions() == 1);
SubstitutionValues_t* value = analyser->getExpression(0);
fail_unless(value->k_value == "k");
fail_unless(value->x_value == "x");
fail_unless(value->y_value == "y");
fail_unless(value->z_value.empty());
fail_unless(value->type == TYPE_K_MINUS_X_MINUS_Y);
fail_unless(formulas_equal("k - x - y", value->current));
fail_unless(formulas_equal("0", value->dxdt_expression));
fail_unless(formulas_equal("0", value->dydt_expression));
fail_unless(value->v_expression == NULL);
fail_unless(value->w_expression == NULL);
fail_unless(value->z_expression == NULL);
fail_unless(value->odeIndex == 0);
fail_unless(util_isNaN(value->k_real_value));
}
END_TEST


START_TEST(test_analyse_different_expression)
{
// the second expression is the same type but has a different variable
RateRule* rr = d->getModel()->createRateRule();
rr->setVariable("a");
rr->setMath(SBML_parseFormula("k-x-y"));

RateRule* rr1 = d->getModel()->createRateRule();
rr1->setVariable("b");
rr1->setMath(SBML_parseFormula("k-x-a"));


converter->populateInitialODEinfo();
ExpressionAnalyser* analyser = new ExpressionAnalyser(m, converter->getOdePairs());

fail_unless(analyser->getNumExpressions() == 0);

analyser->analyse();

fail_unless(analyser->getNumExpressions() == 2);
SubstitutionValues_t* value = analyser->getExpression(1);
fail_unless(value->k_value == "k");
fail_unless(value->x_value == "x");
fail_unless(value->y_value == "c");
fail_unless(value->z_value.empty());
fail_unless(value->type == TYPE_K_MINUS_X_MINUS_Y);
fail_unless(formulas_equal("k - x - c", value->current));
fail_unless(formulas_equal("0", value->dxdt_expression));
fail_unless(formulas_equal("0", value->dydt_expression));
fail_unless(value->v_expression == NULL);
fail_unless(value->w_expression == NULL);
fail_unless(value->z_expression == NULL);
fail_unless(value->odeIndex == 0);
fail_unless(util_isNaN(value->k_real_value));
}
END_TEST

START_TEST(test_analyse_1)
{
RateRule* rr = d->getModel()->createRateRule();
Expand Down Expand Up @@ -524,15 +598,15 @@ END_TEST
Suite *
create_suite_TestExpressionAnalyser (void)
{
bool testing = false;
bool testing = true;
Suite *suite = suite_create("ExpressionAnalyser");
TCase *tcase = tcase_create("ExpressionAnalyser");
tcase_add_checked_fixture(tcase,
ExpressionAnalyser_setup, ExpressionAnalyser_teardown);

if (testing)
{
tcase_add_test(tcase, test_analyse_2);
tcase_add_test(tcase, test_analyse_different_expression);
}
else
{
Expand All @@ -541,6 +615,7 @@ Suite *suite = suite_create("ExpressionAnalyser");
tcase_add_test(tcase, test_analyse_2); //k-x+w-y
tcase_add_test(tcase, test_analyse_3); //k-x
tcase_add_test(tcase, test_analyse_4); //k+v-x
tcase_add_test(tcase, test_analyse_same_expression); //k-x-y
//tcase_add_test(tcase, test_order_of_replacements);
//tcase_add_test(tcase, test_order_of_replacements1);
//tcase_add_test(tcase, test_order_of_replacements2);
Expand Down
7 changes: 5 additions & 2 deletions src/sbml/conversion/test/TestRunner.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Suite *create_suite_TestLevelVersionConverter(void);
Suite *create_suite_TestRateOfConverter(void);
Suite *create_suite_TestSBMLRateRuleConverter(void);

Suite* create_suite_TestExpressionAnalyser(void);

/**
* Global.
Expand Down Expand Up @@ -118,7 +119,8 @@ main (void)
int num_failed;

setTestDataDirectory();
SRunner *runner = srunner_create(create_suite_TestSBMLRateRuleConverter());
SRunner *runner = srunner_create(create_suite_TestExpressionAnalyser());
// srunner_add_suite(runner, create_suite_TestSBMLRateRuleConverter());

/*SRunner *runner = srunner_create( create_suite_TestConversionOption() );
srunner_add_suite( runner, create_suite_TestSBMLRuleConverter () );
Expand All @@ -131,7 +133,8 @@ main (void)
srunner_add_suite( runner, create_suite_TestStripPackageConverter () );
srunner_add_suite( runner, create_suite_TestLevelVersionConverter () );
srunner_add_suite( runner, create_suite_TestRateOfConverter () );
srunner_add_suite( runner, create_suite_TestSBMLRateRuleConverter () );*/
srunner_add_suite( runner, create_suite_TestSBMLRateRuleConverter () );
srunner_add_suite( runner, create_suite_TestExpressionAnalyser () );*/

/* srunner_set_fork_status(runner, CK_NOFORK); */

Expand Down
Loading

0 comments on commit 8cb6215

Please sign in to comment.