Преглед изворни кода

ENH: Added StringConverter plugin to create the command line taking as input a ModuleDescription

Xavi Planes пре 14 година
родитељ
комит
eb20887b1b

+ 5 - 2
Libs/ModuleDescription/CMakeLists.txt

@@ -20,10 +20,12 @@ SET(KIT_SRCS
   ctkModuleParameter.cpp
   ctkModuleParameterGroup.h
   ctkModuleParameterGroup.cpp
-  ctkModuleDescriptionReader.cpp
-  ctkModuleDescriptionConverterInterface.h
   ctkModuleDescriptionReaderInterface.h
   ctkModuleDescriptionReader.h
+  ctkModuleDescriptionReader.cpp
+  ctkModuleDescriptionConverterInterface.h
+  ctkModuleDescriptionConverter.h
+  ctkModuleDescriptionConverter.cpp
   )
 
 # Headers that should run through moc
@@ -31,6 +33,7 @@ SET(KIT_MOC_SRCS
   ctkModuleDescriptionReaderInterface.h
   ctkModuleDescriptionReader.h
   ctkModuleDescriptionConverterInterface.h
+  ctkModuleDescriptionConverter.h
 )
 
 # UI files

+ 38 - 0
Libs/ModuleDescription/ctkModuleDescriptionConverter.cpp

@@ -0,0 +1,38 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 CISTIB - Universitat Pompeu Fabra
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+
+#include "ctkModuleDescriptionConverter.h"
+
+ctkModuleDescriptionConverter::ctkModuleDescriptionConverter() 
+{
+
+}
+
+ctkModuleDescriptionConverter::~ctkModuleDescriptionConverter()
+{
+
+}
+
+void ctkModuleDescriptionConverter::setModuleDescription( const ctkModuleDescription &val )
+{
+  this->ModuleDescription = val;
+}
+

+ 50 - 0
Libs/ModuleDescription/ctkModuleDescriptionConverter.h

@@ -0,0 +1,50 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 CISTIB - Universitat Pompeu Fabra
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+#ifndef __ctkModuleDescriptionConverter_h
+#define __ctkModuleDescriptionConverter_h
+
+#include <QString>
+
+#include "CTKModuleDescriptionExport.h"
+#include "ctkModuleDescriptionConverterInterface.h"
+
+/** 
+ * \brief Base Reader of ModuleDescription
+ *
+ * This is the base interface
+ */
+class CTK_MODULDESC_EXPORT ctkModuleDescriptionConverter :
+  virtual public ctkModuleDescriptionConverterInterface
+{
+  Q_OBJECT
+public:
+  ctkModuleDescriptionConverter();
+  ~ctkModuleDescriptionConverter();
+
+  ///
+  virtual void setModuleDescription(const ctkModuleDescription &val);
+
+protected:
+  ///
+  ctkModuleDescription ModuleDescription;
+};
+
+#endif

+ 1 - 1
Libs/ModuleDescription/ctkModuleDescriptionConverterInterface.h

@@ -48,7 +48,7 @@ public:
   virtual void Update( ) = 0;
 
   //!
-  virtual const QVariant& GetOutput( ) = 0;
+  virtual const QVariant GetOutput( ) = 0;
 };
 
 #endif

+ 1 - 1
Libs/ModuleDescription/ctkModuleDescriptionReader.cpp

@@ -38,7 +38,7 @@ void ctkModuleDescriptionReader::setXmlContent( const QString &val )
 
 const ctkModuleDescription &ctkModuleDescriptionReader::moduleDescription() const
 {
-  return this->Description;
+  return this->ModuleDescription;
 }
 
 

+ 1 - 1
Libs/ModuleDescription/ctkModuleDescriptionReader.h

@@ -49,7 +49,7 @@ protected:
   ///
   QString XmlContent;
   ///
-  ctkModuleDescription Description;
+  ctkModuleDescription ModuleDescription;
 };
 
 #endif

+ 4 - 0
Plugins/org.commontk.slicermodule/CMakeLists.txt

@@ -4,13 +4,17 @@ SET(PLUGIN_export_directive "org_commontk_slicermodule_EXPORT")
 
 SET(PLUGIN_SRCS
   ctkSlicerModulePlugin.cpp
+  ctkSlicerModuleReader.h
   ctkSlicerModuleReader.cxx
+  ctkSlicerModuleStringConverter.h
+  ctkSlicerModuleStringConverter.cpp
 )
 
 # Files which should be processed by Qts moc
 SET(PLUGIN_MOC_SRCS
   ctkSlicerModulePlugin_p.h
   ctkSlicerModuleReader.h
+  ctkSlicerModuleStringConverter.h
 )
 
 # Qt Designer files which should be processed by Qts uic

+ 58 - 23
Plugins/org.commontk.slicermodule/Testing/Cpp/ctkSlicerModuleTest.cpp

@@ -24,6 +24,7 @@
 #include "ctkPluginFramework.h"
 #include "ctkPluginException.h"
 #include "ctkModuleDescriptionReaderInterface.h"
+#include "ctkModuleDescriptionConverterInterface.h"
 #include "ctkCommandLineParser.h"
 
 // STD includes
@@ -32,6 +33,10 @@
 #include "QFile"
 
 
+ctkModuleDescription ReadModuleDescription( ctkPluginContext* context, const QString &xmlFileName ) ;
+void BuildCommandLine( ctkPluginContext* context, const ctkModuleDescription& module ) ;
+
+
 //-----------------------------------------------------------------------------
 int ctkSlicerModuleTest(int argc, char * argv [] )
 {
@@ -74,30 +79,10 @@ int ctkSlicerModuleTest(int argc, char * argv [] )
 
     framework->start();
 
-    ctkServiceReference* serviceRef;
-    serviceRef = framework->getPluginContext()->getServiceReference( 
-      "ctkModuleDescriptionReaderInterface" );
-
-    ctkModuleDescriptionReaderInterface* reader;
-    reader = qobject_cast<ctkModuleDescriptionReaderInterface*>
-      (framework->getPluginContext()->getService(serviceRef));
-
-
-    // Read file
-    QFile file( xmlFileName );
-    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
-    {
-      std::cout << "XML file " << xmlFileName.toStdString( ) << " could not be opened." << endl;
-      return false;
-    }
-    QTextStream stream( &file );
-
-    // Parse XML file
-    reader->setXmlContent( stream.readAll() );
-    reader->Update();
+    ctkModuleDescription module;
+    module = ReadModuleDescription( framework->getPluginContext(), xmlFileName );
 
-    // Print module description
-    QTextStream(stdout) << reader->moduleDescription( );
+    BuildCommandLine( framework->getPluginContext(), module );
 
   }
   catch (const ctkPluginException& e)
@@ -107,3 +92,53 @@ int ctkSlicerModuleTest(int argc, char * argv [] )
 
   return EXIT_SUCCESS;
 }
+
+
+
+ctkModuleDescription ReadModuleDescription( 
+    ctkPluginContext* context, const QString &xmlFileName )
+{
+
+  ctkServiceReference* serviceRef;
+  serviceRef = context->getServiceReference( 
+    "ctkModuleDescriptionReaderInterface" );
+
+  ctkModuleDescriptionReaderInterface* reader;
+  reader = qobject_cast<ctkModuleDescriptionReaderInterface*>
+    (context->getService(serviceRef));
+
+
+  // Read file
+  QFile file( xmlFileName );
+  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+  {
+    std::cout << "XML file " << xmlFileName.toStdString( ) << " could not be opened." << endl;
+    exit(1);
+  }
+  QTextStream stream( &file );
+
+  // Parse XML file
+  reader->setXmlContent( stream.readAll() );
+  reader->Update();
+  QTextStream(stdout) << reader->moduleDescription( );
+
+  return reader->moduleDescription( );
+}
+
+void BuildCommandLine( ctkPluginContext* context, const ctkModuleDescription& module )
+{
+  ctkServiceReference* serviceRef;
+  serviceRef = context->getServiceReference( 
+    "ctkModuleDescriptionConverterInterface" );
+
+  ctkModuleDescriptionConverterInterface* converter;
+  converter = qobject_cast<ctkModuleDescriptionConverterInterface*>
+    (context->getService(serviceRef));
+
+  QStringList commandLineString;
+  converter->setModuleDescription( module );
+  converter->Update();
+  commandLineString = converter->GetOutput().toStringList();
+  QTextStream(stdout) << commandLineString;
+}
+

+ 9 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModulePlugin.cpp

@@ -21,6 +21,7 @@
 
 #include "ctkSlicerModulePlugin_p.h"
 #include "ctkSlicerModuleReader.h"
+#include "ctkSlicerModuleStringConverter.h"
 #include <QtPlugin>
 #include <QDebug>
 
@@ -45,6 +46,10 @@ void ctkSlicerModulePlugin::start(ctkPluginContext* context)
   context->registerService(QStringList("ctkModuleDescriptionReaderInterface"),
     this->reader);
 
+  this->stringConverter = new ctkSlicerModuleStringConverter( );
+  context->registerService(QStringList("ctkModuleDescriptionConverterInterface"),
+    this->stringConverter);
+
   qDebug() << "Registered Slicer Module Description";
 }
 
@@ -52,6 +57,10 @@ void ctkSlicerModulePlugin::stop(ctkPluginContext* context)
 {
   delete this->reader;
   this->reader = NULL;
+
+  delete this->stringConverter;
+  this->stringConverter = NULL;
+
   Q_UNUSED(context)
 }
 

+ 2 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModulePlugin_p.h

@@ -25,6 +25,7 @@
 #include <ctkPluginActivator.h>
 
 class ctkModuleDescriptionReaderInterface;
+class ctkSlicerModuleStringConverter;
 
 class ctkSlicerModulePlugin :
   public QObject, public ctkPluginActivator
@@ -50,6 +51,7 @@ private:
   static ctkSlicerModulePlugin* instance;
   ctkPluginContext* context;
   ctkModuleDescriptionReaderInterface* reader;
+  ctkSlicerModuleStringConverter* stringConverter;
 
 
 }; // ctkSlicerModulePlugin

+ 16 - 9
Plugins/org.commontk.slicermodule/ctkSlicerModuleReader.cxx

@@ -1,16 +1,23 @@
-/*=========================================================================
+/*=============================================================================
 
-  Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
+Library: CTK
 
-  See Doc/copyright/copyright.txt
-  or http://www.slicer.org/copyright/copyright.txt for details.
+Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
 
-  Program:   Module Description Parser
-  Module:    $HeadURL: http://svn.slicer.org/Slicer3/trunk/Libs/SlicerExecutionModel/ctkSlicerModuleReader/ctkSlicerModuleReader.cxx $
-  Date:      $Date: 2010-07-22 21:09:03 +0200 (jue, 22 jul 2010) $
-  Version:   $Revision: 14306 $
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
 
-==========================================================================*/
 #include "ctkSlicerModuleReader.h"
 #include <QtXml/QDomDocument>
 

+ 158 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModuleStringConverter.cpp

@@ -0,0 +1,158 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 CISTIB - Universtitat Pompeu Fabra
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+#include "ctkSlicerModuleStringConverter.h"
+#include <QVector>
+
+
+void ctkSlicerModuleStringConverter::Update()
+{
+  SetTarget( );
+
+  SetAllParameters( );
+}
+
+const QVariant ctkSlicerModuleStringConverter::GetOutput()
+{
+  return this->CommandLineAsString;
+}
+
+void ctkSlicerModuleStringConverter::SetAllParameters()
+{
+  foreach( const ctkModuleParameterGroup& itGroup, this->ModuleDescription.parameterGroups())
+  {
+    foreach( const ctkModuleParameter& itParam, itGroup.parameters())
+    {
+      SetParameterValue( itParam );
+    }
+  }
+}
+
+void ctkSlicerModuleStringConverter::SetParameterValue( const ctkModuleParameter &param )
+{
+  QString prefix;
+  QString flag;
+  bool hasFlag = false;
+
+  if ( param["LongFlag"] != "")
+  {
+    prefix = "--";
+    flag = param["LongFlag"];
+    hasFlag = true;
+  }
+  else if (param["Flag"] != "")
+  {
+    prefix = "-";
+    flag = param["Flag"];
+    hasFlag = true;
+  }
+
+
+  if (hasFlag)
+  {
+    if (   param["Tag"] != "boolean"
+      && param["Tag"] != "file" 
+      && param["Tag"] != "directory"
+      && param["Tag"] != "string"
+      && param["Tag"] != "integer-vector"
+      && param["Tag"] != "float-vector"
+      && param["Tag"] != "double-vector"
+      && param["Tag"] != "string-vector"
+      && param["Tag"] != "image"
+      && param["Tag"] != "point"
+      && param["Tag"] != "region"
+      && param["Tag"] != "transform"
+      && param["Tag"] != "geometry"
+      && param["Tag"] != "table"
+      && param["Tag"] != "measurement")
+    {
+      // simple parameter, write flag and value
+      this->CommandLineAsString.push_back(prefix + flag);
+      this->CommandLineAsString.push_back(param["Default"]);
+    }
+    else if (param["Tag"] == "boolean" && param["Default"] == "true")
+    {
+      this->CommandLineAsString.push_back(prefix + flag);
+    }
+    else if (param["Tag"] == "file" 
+      || param["Tag"] == "directory"
+      || param["Tag"] == "string"
+      || param["Tag"] == "integer-vector"
+      || param["Tag"] == "float-vector"
+      || param["Tag"] == "double-vector"
+      || param["Tag"] == "string-vector")
+    {
+      // Only write out the flag if value is not empty
+      if ( param["Default"] != "")
+      {
+        this->CommandLineAsString.push_back(prefix + flag);
+        this->CommandLineAsString.push_back( param["Default"] );
+      }
+    }
+    // data passed as parameter
+    else if ( param["Tag"] == "image" 
+      || param["Tag"] == "geometry"
+      || param["Tag"] == "transform" 
+      || param["Tag"] == "table" 
+      || param["Tag"] == "measurement" )
+    {
+      if ( param["Default"] != "")
+      {
+        this->CommandLineAsString.push_back(prefix + flag);
+        this->CommandLineAsString.push_back(param["Default"]);
+      }
+    }
+    else if ( param["Tag"] == "region" )
+    {
+      this->CommandLineAsString.push_back(prefix + flag);
+      this->CommandLineAsString.push_back( param["Default"] );
+    }
+    else if ( param["Tag"] == "point" )
+    {
+      QStringList points = param["Default"].split( ";");
+      foreach ( const QString &it, points )
+      {
+        this->CommandLineAsString.push_back(prefix + flag);
+        this->CommandLineAsString.push_back( it );
+      }
+    }
+
+  }
+
+  // If index is not empty -> It's a command line argument arg0, arg1, ... without flag prefix
+  if ( param["Index"] != "")
+  {
+    this->CommandLineAsString.push_back( param["Default"] );
+  }
+}
+
+void ctkSlicerModuleStringConverter::SetTarget()
+{
+  this->CommandLineAsString.clear();
+
+  if (!this->ModuleDescription["Location"].isEmpty() && 
+    this->ModuleDescription["Location"] != this->ModuleDescription["Target"])
+  {
+    this->CommandLineAsString.push_back(this->ModuleDescription["Location"]);
+  }
+  this->CommandLineAsString.push_back( this->ModuleDescription["Target"] );
+}
+
+

+ 65 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModuleStringConverter.h

@@ -0,0 +1,65 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) 2010 CISTIB - Universtitat Pompeu Fabra
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#ifndef __ctkSlicerModuleStringConverter_h
+#define __ctkSlicerModuleStringConverter_h
+
+#include <string>
+
+#include "ctkModuleDescriptionConverter.h"
+
+class ModuleDescription;
+class ModuleParameterGroup;
+
+
+/**
+ * Convert Slicer Module description to command line string QStringList
+ *
+ */
+class ctkSlicerModuleStringConverter : public ctkModuleDescriptionConverter
+{
+  Q_OBJECT
+public:
+  ctkSlicerModuleStringConverter() {};
+  ~ctkSlicerModuleStringConverter() {};
+
+  ///
+  void Update( );
+
+  ///
+  virtual const QVariant GetOutput( );
+
+protected:
+
+  //!
+  void SetAllParameters();
+
+  //!
+  void SetParameterValue( const ctkModuleParameter &param );
+
+  ///
+  void SetTarget();
+
+protected:
+
+  QStringList CommandLineAsString;
+};
+
+#endif