Skip to content

Commit

Permalink
more trying to do this now onto ordering the expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
skeating committed Dec 20, 2024
1 parent ac9fe51 commit 30eb874
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 55 deletions.
71 changes: 44 additions & 27 deletions src/sbml/conversion/ExpressionAnalyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ using namespace std;
LIBSBML_CPP_NAMESPACE_BEGIN


bool compareExpressions(SubstitutionValues_t* values1, SubstitutionValues_t* values2)
{
if (values1->type >= values2->type)
return true;
return false;
}

ExpressionAnalyser::ExpressionAnalyser()
: mModel (NULL),
mODEs (),
Expand Down Expand Up @@ -340,6 +347,13 @@ bool ExpressionAnalyser::expressionExists(SubstitutionValues_t* current,
bool ExpressionAnalyser::parentExpressionExists(SubstitutionValues_t* current, SubstitutionValues_t* mightAdd)
{
bool parentExists = false;

// if the expressions are on the same level then one cannot be the child of another
if (current->levelInExpression == mightAdd->levelInExpression)
{
return false;
}

// here we want to find out if the expression is actually a child of another
// and therefore need not be logged
// all expressions will have K and X but the type may not be the same
Expand Down Expand Up @@ -530,7 +544,7 @@ SubstitutionValues_t* ExpressionAnalyser::getExpression(unsigned int index)
* e.g. if we have k-x-y do not need to analyse k-x
*/
bool
ExpressionAnalyser::shouldAddExpression(SubstitutionValues_t* value)
ExpressionAnalyser::shouldAddExpression(SubstitutionValues_t* value, ASTNodePair currentNode)
{
bool found = false;
bool foundParent = false;
Expand Down Expand Up @@ -559,8 +573,22 @@ bool
ExpressionAnalyser::analyseNode(ASTNode* node, SubstitutionValues_t *value)
{
// cout << "current node: " << SBML_formulaToL3String(node) << endl;
unsigned int numChildren = node->getNumChildren();
ASTNodeType_t type = node->getType();

// type must be plus or minus
if (type != AST_PLUS && type != AST_MINUS)
{
return false;
}
unsigned int numChildren = node->getNumChildren();

// we must have two children
if (numChildren != 2)
{
return false;
}


ASTNode* rightChild = node->getRightChild();
ASTNode* leftChild = node->getLeftChild();
//cout << "RIGHT CHILD: " << SBML_formulaToL3String(rightChild) << endl;
Expand Down Expand Up @@ -648,11 +676,11 @@ ExpressionAnalyser::detect_minusXPlusYOnly()
value->type = TYPE_MINUS_X_PLUS_Y;
value->current = currentNode;
value->odeIndex = odeIndex;
if (!shouldAddExpression(value))
{
printSubstitutionValues(value);
mExpressions.push_back(value);
}
//if (shouldAddExpression(value))
////{
// printSubstitutionValues(value);
// mExpressions.push_back(value);
//}

}

Expand All @@ -670,19 +698,20 @@ ExpressionAnalyser::analyse(bool minusXPlusYOnly)
ASTNode* odeRHS = ode.second;
odeRHS->decompose();
odeRHS->reduceToBinary();
List* operators = odeRHS->getListOfNodes((ASTNodePredicate)ASTNode_isOperator);
ListIterator it = operators->begin();
ASTNodeLevels operators = odeRHS->getListOfNodesWithLevel();
ASTNodeLevelsIterator it = operators.begin();

while (it != operators->end())
while (it != operators.end())
{
ASTNode* currentNode = (ASTNode*)*it;
ASTNodePair currentNode = (ASTNodePair)*it;
SubstitutionValues_t* value = createBlankSubstitutionValues();

cout << "current node: " << SBML_formulaToL3String(currentNode) << endl;
if (analyseNode(currentNode, value))
cout << "Level " << currentNode.first << ": " << SBML_formulaToL3String(currentNode.second) << endl;
if (analyseNode(currentNode.second, value))
{
value->odeIndex = odeIndex;
if (shouldAddExpression(value))
value->levelInExpression = currentNode.first;
if (shouldAddExpression(value, currentNode))
{
//printSubstitutionValues(value);
mExpressions.push_back(value);
Expand All @@ -695,18 +724,7 @@ ExpressionAnalyser::analyse(bool minusXPlusYOnly)

void ExpressionAnalyser::orderExpressions()
{
for (unsigned int i = 0; i < mExpressions.size(); i++)
{
for (unsigned int j = i + 1; j < mExpressions.size(); j++)
{
if (mExpressions[i]->type > mExpressions[j]->type)
{
SubstitutionValues_t* temp = mExpressions[i];
mExpressions[i] = mExpressions[j];
mExpressions[j] = temp;
}
}
}
std::sort(mExpressions.begin(), mExpressions.end(), compareExpressions);
}

void
Expand Down Expand Up @@ -1251,4 +1269,3 @@ LIBSBML_CPP_NAMESPACE_END

#endif /* __cplusplus */


4 changes: 3 additions & 1 deletion src/sbml/conversion/ExpressionAnalyser.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ struct SubstitutionValues_t {
std::string z_value;
ASTNode* z_expression;
unsigned int odeIndex;
unsigned int levelInExpression;
};

bool compareExpressions(SubstitutionValues_t* values1, SubstitutionValues_t* values2);


class LIBSBML_EXTERN ExpressionAnalyser
Expand Down Expand Up @@ -236,7 +238,7 @@ class LIBSBML_EXTERN ExpressionAnalyser
/*
* Loops through expressions already recorded and checks for exact matches
*/
bool shouldAddExpression(SubstitutionValues_t* value);
bool shouldAddExpression(SubstitutionValues_t* value, ASTNodePair currentNode);

bool expressionExists(SubstitutionValues_t* current, SubstitutionValues_t* mightAdd);

Expand Down
8 changes: 4 additions & 4 deletions src/sbml/conversion/SBMLRateRuleConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,10 +863,10 @@ SBMLRateRuleConverter::populateInitialODEinfo()
}
}

for (unsigned int odeIndex = 0; odeIndex < mODEs.size(); odeIndex++)
{
cout << mODEs[odeIndex].first << ": " << SBML_formulaToL3String(mODEs[odeIndex].second) << endl;
}
//for (unsigned int odeIndex = 0; odeIndex < mODEs.size(); odeIndex++)
//{
// cout << mODEs[odeIndex].first << ": " << SBML_formulaToL3String(mODEs[odeIndex].second) << endl;
//}
}


Expand Down
71 changes: 52 additions & 19 deletions src/sbml/conversion/test/TestExpressionAnalyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,38 @@ START_TEST(test_analyse_4)
}
END_TEST

START_TEST(test_order_expressions_1)
{
RateRule* rr = d->getModel()->createRateRule();
rr->setVariable("b");
rr->setMath(SBML_parseFormula("k - x + w - y"));

RateRule* rrr = d->getModel()->createRateRule();
rrr->setVariable("a");
rrr->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() == 2);
SubstitutionValues_t* value = analyser->getExpression(0);
//fail_unless(value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y);
SubstitutionValues_t* value1 = analyser->getExpression(1);
//fail_unless(value1->type == TYPE_K_MINUS_X_MINUS_Y);

analyser->orderExpressions();
fail_unless(analyser->getNumExpressions() == 2);
SubstitutionValues_t* value2 = analyser->getExpression(0);
fail_unless(value2->type == TYPE_K_MINUS_X_MINUS_Y);
SubstitutionValues_t* value3 = analyser->getExpression(1);
fail_unless(value3->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y);
}
END_TEST


START_TEST(test_reorder_minusXplusYIteratively_simple)
{
RateRule* rr = d->getModel()->createRateRule();
Expand All @@ -495,25 +527,26 @@ START_TEST(test_reorder_minusXplusYIteratively_simple)

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

analyser->detect_minusXPlusYOnly();
analyser->analyse();

fail_unless(analyser->getNumExpressions() == 1);
// since we decomposed the term we no longer need to identify this type

analyser->reorderMinusXPlusYIteratively();
SubstitutionValues_t* value = analyser->getExpression(0);
fail_unless(value->k_value.empty());
fail_unless(value->x_value == "x");
fail_unless(value->y_value == "y");
fail_unless(value->z_value.empty());
fail_unless(value->type == TYPE_MINUS_X_PLUS_Y);
fail_unless(formulas_equal("-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));
fail_unless(analyser->getNumExpressions() == 0);

//SubstitutionValues_t* value = analyser->getExpression(0);
//fail_unless(value->k_value.empty());
//fail_unless(value->x_value == "x");
//fail_unless(value->y_value == "y");
//fail_unless(value->z_value.empty());
//fail_unless(value->type == TYPE_MINUS_X_PLUS_Y);
//fail_unless(formulas_equal("-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

Expand Down Expand Up @@ -724,7 +757,7 @@ Suite *suite = suite_create("ExpressionAnalyser");

if (testing)
{
tcase_add_test(tcase, test_analyse_1_two_terms);
tcase_add_test(tcase, test_order_expressions_1);
}
else
{
Expand All @@ -738,7 +771,7 @@ Suite *suite = suite_create("ExpressionAnalyser");
tcase_add_test(tcase, test_analyse_1_same); //k+v-x-y
tcase_add_test(tcase, test_analyse_1_two_terms); //(k+v-x-y)+(k-x)
tcase_add_test(tcase, test_analyse_1_different); //k+v-x-y
//tcase_add_test(tcase, test_order_of_replacements);
tcase_add_test(tcase, test_reorder_minusXplusYIteratively_simple);
//tcase_add_test(tcase, test_order_of_replacements1);
//tcase_add_test(tcase, test_order_of_replacements2);

Expand Down
2 changes: 1 addition & 1 deletion src/sbml/math/ASTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ void ASTNode::fillListOfNodesWithLevel(ASTNodePredicate predicate, ASTNodeLevels

if (predicate(this) != 0)
{
cout << "Level " << level << ": " << SBML_formulaToL3String(this) << endl;
//cout << "Level " << level << ": " << SBML_formulaToL3String(this) << endl;
vector_pairs.push_back(std::make_pair(level, (ASTNode*)this));
}

Expand Down
3 changes: 3 additions & 0 deletions src/sbml/math/ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,13 @@ LIBSBML_CPP_NAMESPACE_END
#ifdef __cplusplus
LIBSBML_CPP_NAMESPACE_BEGIN

#ifndef SWIG

typedef std::pair<unsigned int, ASTNode*> ASTNodePair;
typedef std::vector<ASTNodePair> ASTNodeLevels;
typedef ASTNodeLevels::iterator ASTNodeLevelsIterator;

#endif // !SWIG


class List;
Expand Down
Loading

0 comments on commit 30eb874

Please sign in to comment.