Skip to content

Commit

Permalink
[NewAPI] State machines (#9448)
Browse files Browse the repository at this point in the history
* State machines with instance api

* [NewAPI] Load and create states, transitions and initial states

When updating the value call setExp so that the underlying expression is updated as well otherwise we get wrong printed values.

* Removed ExtenAnnotation::append. Use assignment.
  • Loading branch information
adeas31 committed Sep 29, 2022
1 parent 0b19996 commit bd71b95
Show file tree
Hide file tree
Showing 17 changed files with 785 additions and 263 deletions.
3 changes: 1 addition & 2 deletions OMEdit/OMEditLIB/Annotations/ExtentAnnotation.h
Expand Up @@ -18,8 +18,7 @@ class ExtentAnnotation : public DynamicAnnotation

const QPointF& at(int i) const { return mValue.at(i); }
int size() const { return mValue.size(); }
void append(const QPointF &value) { mValue.append(value); }
void replace(int i, const QPointF &value) { mValue.replace(i, value); }
void replace(int i, const QPointF &value) { mValue.replace(i, value); setExp(); }

auto begin() { return mValue.begin(); }
auto begin() const { return mValue.begin(); }
Expand Down
114 changes: 111 additions & 3 deletions OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp
Expand Up @@ -221,7 +221,7 @@ LineAnnotation::LineAnnotation(LineAnnotation::LineType lineType, Element *pStar
*/
mLineColor = QColor(175, 175, 175);
mSmooth = StringHandler::SmoothBezier;
QString textShape = "true, {0.0, 0.0}, 0, {95, 95, 95}, {0, 0, 0}, LinePattern.Solid, FillPattern.None, 0.25, {{-4, 4}, {-4, 10}}, \"%condition\", 10, {TextStyle.Bold}, TextAlignment.Right";
QString textShape = "true, {0.0, 0.0}, 0, {95, 95, 95}, {0, 0, 0}, LinePattern.Solid, FillPattern.None, 0.25, {{-4, 4}, {-4, 10}}, \"%condition\", 10, {-1, -1, -1}, "", {TextStyle.Bold}, TextAlignment.Right";
mpTextAnnotation = new TextAnnotation(textShape, this);
}
// set the graphics view
Expand Down Expand Up @@ -383,6 +383,54 @@ LineAnnotation::LineAnnotation(QString annotation, QString text, Element *pStart
mpGraphicsView->addItem(this);
}

LineAnnotation::LineAnnotation(ModelInstance::Transition *pTransition, Element *pStartComponent, Element *pEndComponent, bool inherited, GraphicsView *pGraphicsView)
: ShapeAnnotation(inherited, pGraphicsView, 0, 0)
{
mpOriginItem = 0;
setFlag(QGraphicsItem::ItemIsSelectable);
mLineType = LineAnnotation::TransitionType;
setZValue(1000);
mpLine = pTransition->getLine();
// set the default values
GraphicItem::setDefaults();
ShapeAnnotation::setDefaults();
// set the start component
setStartElement(pStartComponent);
setStartElementName(pTransition->getStartConnector()->getName());
// set the end component
setEndElement(pEndComponent);
setEndElementName(pTransition->getEndConnector()->getName());
mStartAndEndElementsSelected = false;
setCondition(pTransition->getCondition() ? "true" : "false");
setImmediate(pTransition->getImmediate());
setReset(pTransition->getReset());
setSynchronize(pTransition->getSynchronize());
setPriority(pTransition->getPriority());
setOldAnnotation("");
setDelay("");
setZf("");
setZfr("");
setAlpha("");
setOMSConnectionType(oms_connection_single);
setActiveState(false);
parseShapeAnnotation();
/* make the points relative to origin */
QList<QPointF> points;
for (int i = 0 ; i < mPoints.size() ; i++) {
QPointF point = mOrigin + mPoints[i];
points.append(point);
}
mPoints = points;
mOrigin = QPointF(0, 0);
if (pTransition->getText()) {
mpTextAnnotation = new TextAnnotation(pTransition->getText(), this);
} else {
mpTextAnnotation = 0;
}
// set the graphics view
mpGraphicsView->addItem(this);
}

LineAnnotation::LineAnnotation(QString annotation, Element *pComponent, GraphicsView *pGraphicsView)
: ShapeAnnotation(false, pGraphicsView, 0, 0)
{
Expand Down Expand Up @@ -426,6 +474,50 @@ LineAnnotation::LineAnnotation(QString annotation, Element *pComponent, Graphics
mpGraphicsView->addItem(this);
}

LineAnnotation::LineAnnotation(ModelInstance::InitialState *pInitialState, Element *pComponent, bool inherited, GraphicsView *pGraphicsView)
: ShapeAnnotation(inherited, pGraphicsView, 0, 0)
{
mpOriginItem = 0;
setFlag(QGraphicsItem::ItemIsSelectable);
mLineType = LineAnnotation::InitialStateType;
setZValue(1000);
mpLine = pInitialState->getLine();
// set the default values
GraphicItem::setDefaults();
ShapeAnnotation::setDefaults();
// set the start component
setStartElement(pComponent);
setStartElementName(pInitialState->getStartConnector()->getName());
// set the end component
setEndElement(0);
setEndElementName("");
mStartAndEndElementsSelected = false;
setCondition("");
setImmediate(true);
setReset(true);
setSynchronize(false);
setPriority(1);
mpTextAnnotation = 0;
setOldAnnotation("");
setDelay("");
setZf("");
setZfr("");
setAlpha("");
setOMSConnectionType(oms_connection_single);
setActiveState(false);
parseShapeAnnotation();
/* make the points relative to origin */
QList<QPointF> points;
for (int i = 0 ; i < mPoints.size() ; i++) {
QPointF point = mOrigin + mPoints[i];
points.append(point);
}
mPoints = points;
mOrigin = QPointF(0, 0);
// set the graphics view
mpGraphicsView->addItem(this);
}

LineAnnotation::LineAnnotation(Element *pParent)
: ShapeAnnotation(0, pParent)
{
Expand Down Expand Up @@ -1239,6 +1331,8 @@ void LineAnnotation::updateToolTip()
.arg(getReset() ? "true" : "false")
.arg(getSynchronize() ? "true" : "false")
.arg(getPriority()));
} else if (mLineType == LineAnnotation::InitialStateType) {
setToolTip(QString("<b>initialState</b>(%1)").arg(getStartElementName()));
}
}

Expand Down Expand Up @@ -2376,6 +2470,8 @@ void CreateConnectionDialog::createConnection()
if (mpGraphicsView->getModelWidget()->isNewApi()) {
mpConnectionLineAnnotation->setLine(new ModelInstance::Line);
mpConnectionLineAnnotation->updateLine();
mpConnectionLineAnnotation->drawCornerItems();
mpConnectionLineAnnotation->setCornerItemsActiveOrPassive();
ModelInfo oldModelInfo = mpGraphicsView->getModelWidget()->createModelInfo();
mpGraphicsView->addConnectionToView(mpConnectionLineAnnotation, false);
mpGraphicsView->addConnectionToClass(mpConnectionLineAnnotation);
Expand Down Expand Up @@ -2503,8 +2599,20 @@ void CreateOrEditTransitionDialog::createOrEditTransition()
mpPrioritySpinBox->value(),
mpTransitionLineAnnotation->getOMCShapeAnnotation()));
} else {
mpGraphicsView->getModelWidget()->getUndoStack()->push(new AddTransitionCommand(mpTransitionLineAnnotation, true));
//mpGraphicsView->getModelWidget()->getLibraryTreeItem()->emitConnectionAdded(mpTransitionLineAnnotation);
if (mpGraphicsView->getModelWidget()->isNewApi()) {
mpTransitionLineAnnotation->setLine(new ModelInstance::Line);
mpTransitionLineAnnotation->updateLine();
mpTransitionLineAnnotation->drawCornerItems();
mpTransitionLineAnnotation->setCornerItemsActiveOrPassive();
ModelInfo oldModelInfo = mpGraphicsView->getModelWidget()->createModelInfo();
mpGraphicsView->addTransitionToView(mpTransitionLineAnnotation, false);
mpGraphicsView->addTransitionToClass(mpTransitionLineAnnotation);
ModelInfo newModelInfo = mpGraphicsView->getModelWidget()->createModelInfo();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new OMCUndoCommand(mpGraphicsView->getModelWidget()->getLibraryTreeItem(), oldModelInfo, newModelInfo, "Add Transition"));
} else {
mpGraphicsView->getModelWidget()->getUndoStack()->push(new AddTransitionCommand(mpTransitionLineAnnotation, true));
//mpGraphicsView->getModelWidget()->getLibraryTreeItem()->emitConnectionAdded(mpTransitionLineAnnotation);
}
}
mpGraphicsView->getModelWidget()->updateModelText();
accept();
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/Annotations/LineAnnotation.h
Expand Up @@ -75,8 +75,10 @@ class LineAnnotation : public ShapeAnnotation
// Used for reading a transition
LineAnnotation(QString annotation, QString text, Element *pStartComponent, Element *pEndComponent, QString condition, QString immediate,
QString reset, QString synchronize, QString priority, GraphicsView *pGraphicsView);
LineAnnotation(ModelInstance::Transition *pTransition, Element *pStartComponent, Element *pEndComponent, bool inherited, GraphicsView *pGraphicsView);
// Used for reading an initial state
LineAnnotation(QString annotation, Element *pComponent, GraphicsView *pGraphicsView);
LineAnnotation(ModelInstance::InitialState *pInitialState, Element *pComponent, bool inherited, GraphicsView *pGraphicsView);
// Used for non-exisiting component
LineAnnotation(Element *pParent);
// Used for non-existing class
Expand Down
5 changes: 3 additions & 2 deletions OMEdit/OMEditLIB/Annotations/RectangleAnnotation.cpp
Expand Up @@ -199,8 +199,9 @@ void RectangleAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsIte
Q_UNUSED(widget);
if (mVisible) {
// state machine visualization
if (mpParentComponent && mpParentComponent->getLibraryTreeItem() && mpParentComponent->getLibraryTreeItem()->isState()
&& mpParentComponent->getGraphicsView()->isVisualizationView()) {
if (mpParentComponent && mpParentComponent->getGraphicsView()->isVisualizationView()
&& ((mpParentComponent->getGraphicsView()->getModelWidget()->isNewApi() && mpParentComponent->getModel() && mpParentComponent->getModel()->isState())
|| (mpParentComponent->getLibraryTreeItem() && mpParentComponent->getLibraryTreeItem()->isState()))) {
if (mpParentComponent->isActiveState()) {
painter->setOpacity(1.0);
} else {
Expand Down
6 changes: 4 additions & 2 deletions OMEdit/OMEditLIB/Annotations/ShapeAnnotation.cpp
Expand Up @@ -361,8 +361,10 @@ void ShapeAnnotation::setDefaults()
mArrowSize = 3;
mSmooth = StringHandler::SmoothNone;
mExtents.clear();
mExtents.append(QPointF(0, 0));
mExtents.append(QPointF(0, 0));
QList<QPointF> extents;
extents.append(QPointF(0, 0));
extents.append(QPointF(0, 0));
mExtents = extents;
mBorderPattern = StringHandler::BorderNone;
mRadius = 0;
mStartAngle = 0;
Expand Down
24 changes: 18 additions & 6 deletions OMEdit/OMEditLIB/Annotations/StringAnnotation.cpp
Expand Up @@ -31,32 +31,44 @@ int StringAnnotation::length() const

QString& StringAnnotation::prepend(const QString &str)
{
return mValue.prepend(str);
mValue.prepend(str);
setExp();
return mValue;
}

QString& StringAnnotation::prepend(QChar ch)
{
return mValue.prepend(ch);
mValue.prepend(ch);
setExp();
return mValue;
}

QString& StringAnnotation::replace(int position, int n, const QString &after)
{
return mValue.replace(position, n, after);
mValue.replace(position, n, after);
setExp();
return mValue;
}

QString& StringAnnotation::replace(int position, int n, QChar after)
{
return mValue.replace(position, n, after);
mValue.replace(position, n, after);
setExp();
return mValue;
}

QString& StringAnnotation::replace(const QRegExp &rx, const QString &after)
{
return mValue.replace(rx, after);
mValue.replace(rx, after);
setExp();
return mValue;
}

QString& StringAnnotation::replace(const QRegularExpression &re, const QString &after)
{
return mValue.replace(re, after);
mValue.replace(re, after);
setExp();
return mValue;
}

QString StringAnnotation::toLower() const
Expand Down
30 changes: 28 additions & 2 deletions OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp
Expand Up @@ -156,6 +156,31 @@ TextAnnotation::TextAnnotation(QString annotation, LineAnnotation *pLineAnnotati
}
}

TextAnnotation::TextAnnotation(ModelInstance::Text *pText, LineAnnotation *pLineAnnotation)
: ShapeAnnotation(pLineAnnotation)
{
mpElement = 0;
mpOriginItem = 0;
mpText = pText;
// set the default values
GraphicItem::setDefaults();
FilledShape::setDefaults();
ShapeAnnotation::setDefaults();
parseShapeAnnotation();
updateTextString();
/* From Modelica Spec 33revision1,
* The extent of the Text is interpreted relative to either the first point of the Line, in the case of immediate=false,
* or the last point (immediate=true).
*/
if (pLineAnnotation->getPoints().size() > 0) {
if (pLineAnnotation->getImmediate()) {
setPos(pLineAnnotation->getPoints().last());
} else {
setPos(pLineAnnotation->getPoints().first());
}
}
}

/*!
* \brief TextAnnotation::TextAnnotation
* Used by OMSimulator FMU ModelWidget\n
Expand Down Expand Up @@ -320,8 +345,9 @@ void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
if (mVisible) {
// state machine visualization
// text annotation on a element
if (mpElement && mpElement->getLibraryTreeItem() && mpElement->getLibraryTreeItem()->isState()
&& mpElement->getGraphicsView()->isVisualizationView()) {
if (mpElement && mpElement->getGraphicsView()->isVisualizationView()
&& ((mpElement->getGraphicsView()->getModelWidget()->isNewApi() && mpElement->getModel() && mpElement->getModel()->isState())
|| (mpElement->getLibraryTreeItem() && mpElement->getLibraryTreeItem()->isState()))) {
if (mpElement->isActiveState()) {
painter->setOpacity(1.0);
} else {
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Annotations/TextAnnotation.h
Expand Up @@ -55,6 +55,7 @@ class TextAnnotation : public ShapeAnnotation
TextAnnotation(Element *pParent);
// Used for transition text
TextAnnotation(QString annotation, LineAnnotation *pLineAnnotation);
TextAnnotation(ModelInstance::Text *pText, LineAnnotation *pLineAnnotation);
// Used for OMSimulator FMU
TextAnnotation(GraphicsView *pGraphicsView);
void parseShapeAnnotation(QString annotation) override;
Expand Down

0 comments on commit bd71b95

Please sign in to comment.