Skip to content

Commit

Permalink
Partially support opening JSON files in the transformations browser
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23214 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 5, 2014
1 parent 23b28c2 commit 353d55a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 64 deletions.
136 changes: 92 additions & 44 deletions OMEdit/OMEditGUI/TransformationalDebugger/TransformationsWidget.cpp
Expand Up @@ -37,6 +37,7 @@
*/

#include "TransformationsWidget.h"
#include <qjson/parser.h>

/*!
\class TVariablesTreeItem
Expand Down Expand Up @@ -277,34 +278,28 @@ QModelIndex TVariablesTreeModel::tVariablesTreeItemIndexHelper(const TVariablesT
return QModelIndex();
}

void TVariablesTreeModel::insertTVariablesItems()
void TVariablesTreeModel::insertTVariablesItems(QHashIterator<QString, OMVariable> variables)
{
QHashIterator<QString, OMVariable> variables(mpTVariablesTreeView->getTransformationsWidget()->getInfoXMLFileHandler()->variables);
while (variables.hasNext())
{
variables.next();
OMVariable variable = variables.value();
const OMVariable &variable = variables.value();
if (variable.name.startsWith("$PRE."))
continue;

QStringList tVariables;
QString parentTVariable;
if (variable.name.startsWith("der("))
{
if (variable.name.startsWith("der(")) {
QString str = variable.name;
str.chop((str.lastIndexOf("der(")/4)+1);
tVariables = StringHandler::makeVariableParts(str.mid(str.lastIndexOf("der(") + 4));
}
else
{
} else {
tVariables = StringHandler::makeVariableParts(variable.name);
}
int count = 1;
TVariablesTreeItem *pParentTVariablesTreeItem = 0;
foreach (QString tVariable, tVariables)
{
if (count == 1) /* first loop iteration */
{
foreach (QString tVariable, tVariables) {
if (count == 1) /* first loop iteration */ {
pParentTVariablesTreeItem = mpRootTVariablesTreeItem;
}
QString findVariable;
Expand All @@ -327,32 +322,31 @@ void TVariablesTreeModel::insertTVariablesItems()
If pParentTVariablesTreeItem is 0 and it is first loop iteration then use mpRootTVariablesTreeItem as parent.
If loop iteration is not first and pParentTVariablesTreeItem is 0 then find the parent item.
*/
if (!pParentTVariablesTreeItem && count > 1)
{
if (!pParentTVariablesTreeItem && count > 1) {
pParentTVariablesTreeItem = findTVariablesTreeItem(parentTVariable, mpRootTVariablesTreeItem);
}
else
{
} else {
pParentTVariablesTreeItem = mpRootTVariablesTreeItem;
}
QModelIndex index = tVariablesTreeItemIndex(pParentTVariablesTreeItem);
QVector<QVariant> tVariableData;
QString parentVarName = pParentTVariablesTreeItem->getVariableName();
parentVarName = parentVarName.isEmpty() ? parentVarName : parentVarName.append(".");
/* if last item */
if (tVariables.size() == count && variable.name.startsWith("der("))
if (tVariables.size() == count && variable.name.startsWith("der(")) {
tVariableData << variable.name << "der(" + tVariable + ")" << variable.comment << variable.info.lineStart << variable.info.file;
else
} else {
tVariableData << parentVarName + tVariable << tVariable << variable.comment << variable.info.lineStart << variable.info.file;
}
TVariablesTreeItem *pTVariablesTreeItem = new TVariablesTreeItem(tVariableData, pParentTVariablesTreeItem);
int row = rowCount(index);
beginInsertRows(index, row, row);
pParentTVariablesTreeItem->insertChild(row, pTVariablesTreeItem);
endInsertRows();
if (count == 1)
if (count == 1) {
parentTVariable = tVariable;
else
} else {
parentTVariable += "." + tVariable;
}
count++;
}
}
Expand Down Expand Up @@ -755,17 +749,73 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
}
}

static OMOperation* variantToOperationPtr(QVariantMap var)
{
QStringList lst;
lst << var["display"].toString();
OMOperation *op = new OMOperationBeforeAfter(var["op"].toString(),lst);
qDebug() << op->toString();
return op;
}


static void variantToSource(QVariantMap var, OMInfo &info, QStringList &types, QList<OMOperation*> &ops)
{
QVariantMap vinfo = var["info"].toMap();
info.file = vinfo["file"].toString();
info.lineStart = vinfo["lineStart"].toInt();
info.lineEnd = vinfo["lineEnd"].toInt();
info.colStart = vinfo["colStart"].toInt();
info.colEnd = vinfo["colEnd"].toInt();
info.isValid = true;
QVariantList vops = var["operations"].toList();
foreach (QVariant vop, vops) {
ops += variantToOperationPtr(vop.toMap());
}
}

static OMEquation* getOMEquation(QList<OMEquation*> equations, int index)
{
for (int i = 1 ; i < equations.size() ; i++) {
if (equations[i]->index == index)
return equations[i];
}
return NULL;
}

void TransformationsWidget::loadTransformations()
{
QFile infoXMLFile(mInfoXMLFullFileName);
mpInfoXMLFileHandler = new MyHandler(infoXMLFile);
mpTVariablesTreeModel->insertTVariablesItems();
/* load equations */
parseProfiling(mProfJSONFullFileName);
fetchEquations();
QFile file(mInfoXMLFullFileName);
mEquations.clear();
mVariables.clear();
if (mInfoXMLFullFileName.endsWith(".json")) {
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse(&file, &ok).toMap();
QVariantMap vars = result["variables"].toMap();
QVariantList eqs = result["equations"].toList();
for(QVariantMap::const_iterator iter = vars.begin(); iter != vars.end(); ++iter) {
QVariantMap value = iter.value().toMap();
OMVariable *var = new OMVariable();
var->name = iter.key();
var->comment = value["comment"].toString();
variantToSource(value["source"].toMap(), var->info, var->types, var->ops);
foreach (OMOperation *op, var->ops) {
qDebug() << "op: " << op->toString();
}
mVariables[iter.key()] = *var;
}
mpTVariablesTreeModel->insertTVariablesItems(mVariables);
} else {
mpInfoXMLFileHandler = new MyHandler(file,mVariables,mEquations);
mpTVariablesTreeModel->insertTVariablesItems(mVariables);
/* load equations */
parseProfiling(mProfJSONFullFileName);
fetchEquations();
}
}

void TransformationsWidget::fetchDefinedInEquations(OMVariable &variable)
void TransformationsWidget::fetchDefinedInEquations(const OMVariable &variable)
{
/* Clear the defined in tree. */
clearTreeWidgetItems(mpDefinedInEquationsTreeWidget);
Expand All @@ -774,7 +824,7 @@ void TransformationsWidget::fetchDefinedInEquations(OMVariable &variable)
{
if (variable.definedIn[i])
{
OMEquation *equation = mpInfoXMLFileHandler->getOMEquation(variable.definedIn[i]);
OMEquation *equation = getOMEquation(mEquations, variable.definedIn[i]);
QStringList values;
values << QString::number(variable.definedIn[i]) << OMEquationTypeToString(equation->kind) << equation->toString();
QTreeWidgetItem *pDefinedInTreeItem = new IntegerTreeWidgetItem(values, mpDefinedInEquationsTreeWidget);
Expand All @@ -786,7 +836,7 @@ void TransformationsWidget::fetchDefinedInEquations(OMVariable &variable)
}
}

void TransformationsWidget::fetchUsedInEquations(OMVariable &variable)
void TransformationsWidget::fetchUsedInEquations(const OMVariable &variable)
{
/* Clear the used in tree. */
clearTreeWidgetItems(mpUsedInEquationsTreeWidget);
Expand All @@ -795,7 +845,7 @@ void TransformationsWidget::fetchUsedInEquations(OMVariable &variable)
{
foreach (int index, variable.usedIn[i])
{
OMEquation *equation = mpInfoXMLFileHandler->getOMEquation(index);
OMEquation *equation = getOMEquation(mEquations, index);
QStringList values;
values << QString::number(index) << OMEquationTypeToString(equation->kind) << equation->toString();
QTreeWidgetItem *pUsedInTreeItem = new IntegerTreeWidgetItem(values, mpUsedInEquationsTreeWidget);
Expand All @@ -807,7 +857,7 @@ void TransformationsWidget::fetchUsedInEquations(OMVariable &variable)
}
}

void TransformationsWidget::fetchOperations(OMVariable &variable)
void TransformationsWidget::fetchOperations(const OMVariable &variable)
{
/* Clear the operations tree. */
clearTreeWidgetItems(mpVariableOperationsTreeWidget);
Expand Down Expand Up @@ -843,7 +893,7 @@ void TransformationsWidget::fetchOperations(OMVariable &variable)

QTreeWidgetItem* TransformationsWidget::makeEquationTreeWidgetItem(int equationIndex, int allowChild)
{
OMEquation *equation = mpInfoXMLFileHandler->equations[equationIndex];
OMEquation *equation = mEquations[equationIndex];
if (!allowChild && equation->parent) {
return NULL; // Only output equations in one position
}
Expand All @@ -870,7 +920,7 @@ QTreeWidgetItem* TransformationsWidget::makeEquationTreeWidgetItem(int equationI

void TransformationsWidget::fetchEquations()
{
for (int i = 1 ; i < mpInfoXMLFileHandler->equations.size() ; i++)
for (int i = 1 ; i < mEquations.size() ; i++)
{
QTreeWidgetItem *pEquationTreeItem = makeEquationTreeWidgetItem(i,0);
if (pEquationTreeItem) {
Expand All @@ -882,7 +932,7 @@ void TransformationsWidget::fetchEquations()

void TransformationsWidget::fetchNestedEquations(QTreeWidgetItem *pParentTreeWidgetItem, int index)
{
foreach (int nestedIndex, mpInfoXMLFileHandler->equations[index]->eqs)
foreach (int nestedIndex, mEquations[index]->eqs)
{
// OMEquation *nestedEquation = mpInfoXMLFileHandler->equations[nestedIndex];
QTreeWidgetItem *pNestedEquationTreeItem = makeEquationTreeWidgetItem(nestedIndex,1);
Expand Down Expand Up @@ -910,7 +960,7 @@ QTreeWidgetItem* TransformationsWidget::findEquationTreeItem(int equationIndex)

void TransformationsWidget::fetchEquationData(int equationIndex)
{
OMEquation *equation = mpInfoXMLFileHandler->getOMEquation(equationIndex);
OMEquation *equation = getOMEquation(mEquations, equationIndex);
/* fetch defines */
fetchDefines(equation);
/* fetch depends */
Expand Down Expand Up @@ -1104,7 +1154,7 @@ void TransformationsWidget::fetchVariableData(const QModelIndex &index)
if (!pTVariableTreeItem || pTVariableTreeItem->getChildren().size() != 0)
return;

OMVariable variable = mpInfoXMLFileHandler->variables.value(pTVariableTreeItem->getVariableName());
const OMVariable &variable = mVariables.value(pTVariableTreeItem->getVariableName());
/* fetch defined in equations */
fetchDefinedInEquations(variable);
/* fetch used in equations */
Expand Down Expand Up @@ -1149,8 +1199,6 @@ void TransformationsWidget::fetchEquationData(QTreeWidgetItem *pEquationTreeItem
fetchEquationData(equationIndex);
}

#include <qjson/parser.h>

void TransformationsWidget::parseProfiling(QString fileName)
{
QFile *file = new QFile(fileName);
Expand All @@ -1169,11 +1217,11 @@ void TransformationsWidget::parseProfiling(QString fileName)
QVariantMap eq = list[i].toMap();
long id = eq["id"].toInt();
double time = eq["time"].toDouble();
mpInfoXMLFileHandler->equations[id]->ncall = eq["ncall"].toInt();
mpInfoXMLFileHandler->equations[id]->maxTime = eq["maxTime"].toDouble();
mpInfoXMLFileHandler->equations[id]->time = time;
mpInfoXMLFileHandler->equations[id]->fraction = time / totalStepsTime;
mpInfoXMLFileHandler->equations[id]->profileBlock = i + functions.size();
mEquations[id]->ncall = eq["ncall"].toInt();
mEquations[id]->maxTime = eq["maxTime"].toDouble();
mEquations[id]->time = time;
mEquations[id]->fraction = time / totalStepsTime;
mEquations[id]->profileBlock = i + functions.size();
}
delete file;
}
Expand Up @@ -92,7 +92,7 @@ class TVariablesTreeModel : public QAbstractItemModel
QModelIndex tVariablesTreeItemIndex(const TVariablesTreeItem *pTVariablesTreeItem) const;
QModelIndex tVariablesTreeItemIndexHelper(const TVariablesTreeItem *pTVariablesTreeItem, const TVariablesTreeItem *pParentTVariablesTreeItem,
const QModelIndex &parentIndex) const;
void insertTVariablesItems();
void insertTVariablesItems(QHashIterator<QString, OMVariable> variables);
void clearTVariablesTreeItems();
private:
TVariablesTreeView *mpTVariablesTreeView;
Expand Down Expand Up @@ -172,9 +172,9 @@ class TransformationsWidget : public QWidget
QSplitter* getTransformationsVerticalSplitter() {return mpTransformationsVerticalSplitter;}
QSplitter* getTransformationsHorizontalSplitter() {return mpTransformationsHorizontalSplitter;}
void loadTransformations();
void fetchDefinedInEquations(OMVariable &variable);
void fetchUsedInEquations(OMVariable &variable);
void fetchOperations(OMVariable &variable);
void fetchDefinedInEquations(const OMVariable &variable);
void fetchUsedInEquations(const OMVariable &variable);
void fetchOperations(const OMVariable &variable);
void fetchEquations();
void fetchNestedEquations(QTreeWidgetItem *pParentTreeWidgetItem, int index);
QTreeWidgetItem* findEquationTreeItem(int equationIndex);
Expand Down Expand Up @@ -214,6 +214,8 @@ class TransformationsWidget : public QWidget
QSplitter *mpEquationsHorizontalSplitter;
QSplitter *mpTransformationsVerticalSplitter;
QSplitter *mpTransformationsHorizontalSplitter;
QHash<QString,OMVariable> mVariables;
QList<OMEquation*> mEquations;

void parseProfiling(QString fileName);
QTreeWidgetItem* makeEquationTreeWidgetItem(int equationIndex, int allowChild);
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Util/Helper.cpp
Expand Up @@ -55,7 +55,7 @@ QString Helper::imageFileTypes = "SVG (*.svg);;PNG image (*.png);;Windows BMP im
QString Helper::bitmapFileTypes = "PNG image (*.png);;Windows BMP image (*.bmp);;JPEG (*.jpg *.jpeg)";
QString Helper::fmuFileTypes = "FMU Files (*.fmu)";
QString Helper::xmlFileTypes = "XML Files (*.xml)";
QString Helper::infoXmlFileTypes = "OM Info XML Files (*_info.xml)";
QString Helper::infoXmlFileTypes = "OM Info Files (*_info.xml *_info.json)";
QString Helper::matFileTypes = "MAT Files (*.mat)";
QString Helper::omResultFileTypes = "OpenModelica Result Files (*.mat *.plt *.csv)";
#ifdef WIN32
Expand Down
29 changes: 19 additions & 10 deletions OMEdit/OMEditGUI/Util/OMDumpXML.cpp
Expand Up @@ -67,6 +67,20 @@ QString OMOperation::diffHtml(QString &before, QString &after)
return dmp.diff_prettyHtml(diffs);
}

OMOperationJSON::OMOperationJSON(QVariant var) : var(var)
{
}

QString OMOperationJSON::toString()
{
return var.toString();
}

QString OMOperationJSON::toHtml()
{
return var.toString();
}

OMOperationBeforeAfter::OMOperationBeforeAfter(QString name, QStringList ops) : name(name)
{
before = ops.size() > 0 ? ops[0] : "";
Expand Down Expand Up @@ -192,6 +206,7 @@ OMVariable::OMVariable(const OMVariable &var)
usedIn[i] = var.usedIn[i];
}
foreach (OMOperation *op, var.ops) {
qDebug() << "dynamic_cast op: " << op->toString();
if (dynamic_cast<OMOperationSimplify*>(op))
ops.append(new OMOperationSimplify(*dynamic_cast<OMOperationSimplify*>(op)));
else if (dynamic_cast<OMOperationScalarize*>(op))
Expand All @@ -214,6 +229,8 @@ OMVariable::OMVariable(const OMVariable &var)
ops.append(new OMOperationDummyDerivative(*dynamic_cast<OMOperationDummyDerivative*>(op)));
else if (dynamic_cast<OMOperationFlattening*>(op))
ops.append(new OMOperationFlattening(*dynamic_cast<OMOperationFlattening*>(op)));
else if (dynamic_cast<OMOperationJSON*>(op))
ops.append(new OMOperationJSON(*dynamic_cast<OMOperationJSON*>(op)));
else
ops.append(new OMOperation(*op));
}
Expand Down Expand Up @@ -253,7 +270,7 @@ QString OMEquation::toString()
}
}

MyHandler::MyHandler(QFile &file)
MyHandler::MyHandler(QFile &file, QHash<QString,OMVariable> &variables, QList<OMEquation*> &equations) : variables(variables), equations(equations)
{
hasOperationsEnabled = false;
QXmlSimpleReader xmlReader;
Expand All @@ -269,20 +286,12 @@ MyHandler::MyHandler(QFile &file)

MyHandler::~MyHandler()
{

foreach (OMEquation *eq, equations) {
delete eq;
}
}

OMEquation* MyHandler::getOMEquation(int index)
{
for (int i = 1 ; i < equations.size() ; i++) {
if (equations[i]->index == index)
return equations[i];
}
return NULL;
}

bool MyHandler::startDocument()
{
variables.clear();
Expand Down

0 comments on commit 353d55a

Please sign in to comment.