TransitionDialog.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "TransitionDialog.h"
25 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_TransitionDialog.h>
26 
27 #include "../../../ObserverPropertiesPlugin/ObserverItemModel.h"
28 
29 #include <QComboBox>
30 #include <QFileDialog>
31 #include <QLineEdit>
32 #include <QMessageBox>
33 
34 
35 namespace armarx
36 {
37 
38  TransitionDialog::TransitionDialog(statechartmodel::TransitionCPtr transition, statechartmodel::StatePtr state, QStringList profileNames, Ice::CommunicatorPtr ic, VariantInfoPtr variantInfo, QWidget* parent) :
39  QDialog(parent),
40  ui(new Ui::TransitionDialog),
41  transition(transition),
42  parentState(state),
43  ic(ic),
44  variantInfo(variantInfo),
45  profileNames(profileNames)
46  {
47  ui->setupUi(this);
48  setup();
49  }
50 
52  {
53  delete ui;
54  }
55 
57  {
58  return ui->inputTableMapping->getMapping();
59  }
60 
62  {
63  return ui->parentLocalMapping->getMapping();
64  }
65 
67  {
68  return ui->parentOutputMapping->getMapping();
69  }
70 
71  size_t TransitionDialog::LevenshteinEditDistance(const std::string& s1, const std::string& s2)
72  {
73  // from https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C++
74  const std::size_t len1 = s1.size(), len2 = s2.size();
75  std::vector<std::vector<unsigned int>> d(len1 + 1, std::vector<unsigned int>(len2 + 1));
76 
77  d[0][0] = 0;
78  for (unsigned int i = 1; i <= len1; ++i)
79  {
80  d[i][0] = i;
81  }
82  for (unsigned int i = 1; i <= len2; ++i)
83  {
84  d[0][i] = i;
85  }
86 
87  for (unsigned int i = 1; i <= len1; ++i)
88  for (unsigned int j = 1; j <= len2; ++j)
89  // note that std::min({arg1, arg2, arg3}) works only in C++11,
90  // for C++98 use std::min(std::min(arg1, arg2), arg3)
91  d[i][j] = std::min({ d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1) });
92  return d[len1][len2];
93  }
94 
95 
96  void TransitionDialog::setup()
97  {
98  // ui->editEventName->setText(transition->eventName);
99  // ui->editEventName->setFocus();
100  ui->labelEventName->setText(transition->eventName);
101 
102  if (!transition->destinationState)
103  {
104  ui->labelDestinationState->setText("Transition is detached");
105  return;
106  }
107 
108 
109  if (transition->sourceState)
110  {
111  ui->labelSourceState->setText(transition->sourceState->getInstanceName());
112  setWindowTitle("Edit Transition " + transition->eventName + " [" + transition->sourceState->getInstanceName() + " -> " + transition->destinationState->getInstanceName() + "]");
113  }
114  else
115  {
116  ui->labelSourceState->setText("This is the initial transition to the start state");
117  setWindowTitle("Edit Initial Transition -> " + transition->destinationState->getInstanceName());
118  }
119 
120  ui->labelDestinationState->setText(transition->destinationState->getInstanceName());
121 
122 
124 
125  if (transition->destinationState->getType() != eFinalState)
126  {
127  if (transition->destinationState->getStateClass())
128  {
129  destDict = transition->destinationState->getStateClass()->getInputParameters();
130  }
131  else
132  {
133  QMessageBox::warning(this, "Transition Editing Error", "Transition editing not possible because destination state is not loaded - load statechart group of destination state.");
134  return;
135  }
136  }
137  else
138  {
139  destDict = transition->destinationState->getParent()->getOutputParameters();
140  }
141 
142  ui->inputTableMapping->setup(destDict, transition->mappingToNextStatesInput, transition, parentState, profileNames, ic, variantInfo);
143 
144  if (transition->sourceState)
145  {
146  ui->tabWidget->setTabEnabled(1, true);
147  ui->tabWidget->setTabEnabled(2, true);
148  ui->parentLocalMapping->hideColumn(TransitionMappingTable::eMappingRequired);
149  ui->parentOutputMapping->hideColumn(TransitionMappingTable::eMappingRequired);
150  ui->parentLocalMapping->setup(parentState->getLocalParameters(), transition->mappingToParentStatesLocal, transition, parentState, profileNames, ic, variantInfo);
151  ui->parentOutputMapping->setup(parentState->getOutputParameters(), transition->mappingToParentStatesOutput, transition, parentState, profileNames, ic, variantInfo);
152  }
153  else
154  {
155  ui->tabWidget->setTabEnabled(1, false);
156  ui->tabWidget->setTabEnabled(2, false);
157  }
158 
159  }
160 
161 
162 
163 
164 
165 }
166 
167 void armarx::TransitionDialog::on_btnAutoMap_clicked()
168 {
169  TransitionMappingTable* mapping;
170  if (ui->tabWidget->currentIndex() == 0)
171  {
172  mapping = ui->inputTableMapping;
173  }
174  else if (ui->tabWidget->currentIndex() == 1)
175  {
176  mapping = ui->parentLocalMapping;
177  }
178  else
179  {
180  mapping = ui->parentOutputMapping;
181  }
182 
183 
184  auto caseSensitive = [](QString s1, QString s2)
185  {
186  return std::make_pair(s1 == s2, 0.0f);
187  };
188  auto caseInsensitive = [](QString s1, QString s2)
189  {
190  return std::make_pair(s1.compare(s1, s2, Qt::CaseInsensitive) == 0, 0.0f);
191  };
192  auto editDistance = [](QString s1, QString s2)
193  {
194  auto distance = LevenshteinEditDistance(s1.toLower().toStdString(), s2.toLower().toStdString());
195  return std::make_pair(distance <= 3, (float)distance);
196  };
197 
198  ARMARX_INFO << "Casesensitive";
199  auto mappedRows = mapping->mapByCriteria(caseSensitive);
200  // ARMARX_INFO << mappedRows.size() << " Caseinsensitive";
201  mappedRows.append(mapping->mapByCriteria(caseInsensitive));
202  // ARMARX_INFO << mappedRows.size() << " editdistance";
203  mappedRows.append(mapping->mapByCriteria(editDistance));
204  // ARMARX_INFO << mappedRows.size() << " after editdistance";
205  QString foundMappingsString;
206  for (auto& elem : mappedRows)
207  {
208  foundMappingsString += elem.first + " from " + elem.second + "\n";
209  }
210  if (!foundMappingsString.isEmpty())
211  {
212  foundMappingsString = ":\n" + foundMappingsString;
213  }
214  QMessageBox::information(this, "Auto Mapping Results", "Mapped " + QString::number(mappedRows.size()) + (mappedRows.size() == 1 ? " entry" : " entries") + foundMappingsString);
215 }
armarx::TransitionDialog::getMappingToParentStateLocal
statechartmodel::ParameterMappingList getMappingToParentStateLocal() const
Definition: TransitionDialog.cpp:61
armarx::TransitionDialog::~TransitionDialog
~TransitionDialog() override
Definition: TransitionDialog.cpp:51
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
armarx::TransitionDialog::TransitionDialog
TransitionDialog(statechartmodel::TransitionCPtr transition, statechartmodel::StatePtr parentState, QStringList profileNames, Ice::CommunicatorPtr ic=NULL, VariantInfoPtr variantInfo=VariantInfoPtr(), QWidget *parent=0)
Definition: TransitionDialog.cpp:38
IceInternal::Handle< ::Ice::Communicator >
armarx::TransitionMappingTable::eMappingRequired
@ eMappingRequired
Definition: TransitionMappingTable.h:47
armarx::TransitionDialog
Definition: TransitionDialog.h:40
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::TransitionDialog::LevenshteinEditDistance
static size_t LevenshteinEditDistance(const std::string &s1, const std::string &s2)
Definition: TransitionDialog.cpp:71
armarx::TransitionDialog::getMappingToNextStateInput
statechartmodel::ParameterMappingList getMappingToNextStateInput() const
Definition: TransitionDialog.cpp:56
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::TransitionDialog::getMappingToParentStateOutput
statechartmodel::ParameterMappingList getMappingToParentStateOutput() const
Definition: TransitionDialog.cpp:66
armarx::VariantInfoPtr
std::shared_ptr< VariantInfo > VariantInfoPtr
Definition: VariantInfo.h:39
armarx::statechartmodel::TransitionCPtr
std::shared_ptr< const Transition > TransitionCPtr
Definition: Transition.h:94
armarx::statechartmodel::ParameterMappingList
QList< ParameterMappingPtr > ParameterMappingList
Definition: XmlWriter.h:49
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
min
T min(T t1, T t2)
Definition: gdiam.h:42
TransitionDialog.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28