changeset 1795:af6840eb23ee worklists

HierarchicalMatcher
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Nov 2015 17:41:22 +0100
parents bdfae6e17d23
children 5e08a5fe6b27
files CMakeLists.txt OrthancServer/DicomProtocol/IWorklistRequestHandler.h OrthancServer/Search/HierarchicalMatcher.cpp OrthancServer/Search/HierarchicalMatcher.h OrthancServer/Search/IFindConstraint.h OrthancServer/Search/ListConstraint.cpp OrthancServer/Search/ListConstraint.h OrthancServer/Search/RangeConstraint.h OrthancServer/Search/ValueConstraint.h OrthancServer/Search/WildcardConstraint.cpp OrthancServer/Search/WildcardConstraint.h OrthancServer/main.cpp
diffstat 12 files changed, 304 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Nov 19 16:02:35 2015 +0100
+++ b/CMakeLists.txt	Thu Nov 19 17:41:22 2015 +0100
@@ -179,6 +179,7 @@
   OrthancServer/OrthancRestApi/OrthancRestSystem.cpp
   OrthancServer/ParsedDicomFile.cpp
   OrthancServer/QueryRetrieveHandler.cpp
+  OrthancServer/Search/HierarchicalMatcher.cpp
   OrthancServer/Search/IFindConstraint.cpp
   OrthancServer/Search/LookupIdentifierQuery.cpp
   OrthancServer/Search/LookupResource.cpp
--- a/OrthancServer/DicomProtocol/IWorklistRequestHandler.h	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/DicomProtocol/IWorklistRequestHandler.h	Thu Nov 19 17:41:22 2015 +0100
@@ -50,7 +50,7 @@
      * https://www.dabsoft.ch/dicom/4/V.4.1/
      **/
     virtual bool Handle(DicomFindAnswers& answers,
-                        const ParsedDicomFile& query,
+                        ParsedDicomFile& query,
                         const std::string& remoteIp,
                         const std::string& remoteAet) = 0;
   };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/Search/HierarchicalMatcher.cpp	Thu Nov 19 17:41:22 2015 +0100
@@ -0,0 +1,182 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "../PrecompiledHeadersServer.h"
+#include "HierarchicalMatcher.h"
+
+#include "../../Core/OrthancException.h"
+#include "../FromDcmtkBridge.h"
+
+#include <dcmtk/dcmdata/dcfilefo.h>
+
+namespace Orthanc
+{
+  HierarchicalMatcher::HierarchicalMatcher(ParsedDicomFile& query,
+                                           bool caseSensitivePN)
+  {
+    Setup(*query.GetDcmtkObject().getDataset(), 
+          caseSensitivePN,
+          query.GetEncoding());
+  }
+
+
+  HierarchicalMatcher::~HierarchicalMatcher()
+  {
+    for (Constraints::iterator it = constraints_.begin();
+         it != constraints_.end(); ++it)
+    {
+      if (it->second != NULL)
+      {
+        delete it->second;
+      }
+    }
+
+    for (Sequences::iterator it = sequences_.begin();
+         it != sequences_.end(); ++it)
+    {
+      if (it->second != NULL)
+      {
+        delete it->second;
+      }
+    }
+  }
+
+
+  void HierarchicalMatcher::Setup(DcmItem& dataset,
+                                  bool caseSensitivePN,
+                                  Encoding encoding)
+  {
+    for (unsigned long i = 0; i < dataset.card(); i++)
+    {
+      DcmElement* element = dataset.getElement(i);
+      if (element == NULL)
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+
+      DicomTag tag(FromDcmtkBridge::Convert(element->getTag()));
+      ValueRepresentation vr = FromDcmtkBridge::GetValueRepresentation(tag);
+
+      if (constraints_.find(tag) != constraints_.end() ||
+          sequences_.find(tag) != sequences_.end())
+      {
+        throw OrthancException(ErrorCode_BadRequest);        
+      }
+
+      if (vr == ValueRepresentation_Sequence)
+      {
+        DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(*element);
+
+        if (sequence.card() == 0 ||
+            (sequence.card() == 1 && sequence.getItem(0)->card() == 0))
+        {
+          // Universal matching of a sequence
+          sequences_[tag] = NULL;
+        }
+        else if (sequence.card() == 1)
+        {
+          sequences_[tag] = new HierarchicalMatcher(*sequence.getItem(0), caseSensitivePN, encoding);
+        }
+        else
+        {
+          throw OrthancException(ErrorCode_BadRequest);        
+        }
+      }
+      else
+      {
+        std::auto_ptr<DicomValue> value(FromDcmtkBridge::ConvertLeafElement
+                                        (*element, DicomToJsonFlags_None, encoding));
+
+        if (value->IsBinary() ||
+            value->IsNull())
+        {
+          throw OrthancException(ErrorCode_BadRequest);
+        }
+        else if (value->GetContent().empty())
+        {
+          // This is an universal matcher
+          constraints_[tag] = NULL;
+        }
+        else
+        {
+          // DICOM specifies that searches must be case sensitive, except
+          // for tags with a PN value representation
+          bool sensitive = true;
+          if (vr == ValueRepresentation_PatientName)
+          {
+            sensitive = caseSensitivePN;
+          }
+
+          constraints_[tag] = IFindConstraint::ParseDicomConstraint(tag, value->GetContent(), sensitive);
+        }
+      }
+    }
+  }
+
+
+  std::string HierarchicalMatcher::Format(const std::string& prefix) const
+  {
+    std::string s;
+    
+    for (Constraints::const_iterator it = constraints_.begin();
+         it != constraints_.end(); ++it)
+    {
+      s += prefix + it->first.Format() + " ";
+
+      if (it->second == NULL)
+      {
+        s += "*\n";
+      }
+      else
+      {
+        s += it->second->Format() + "\n";
+      }
+    }
+
+    for (Sequences::const_iterator it = sequences_.begin();
+         it != sequences_.end(); ++it)
+    {
+      s += prefix + it->first.Format() + " ";
+
+      if (it->second == NULL)
+      {
+        s += "*\n";
+      }
+      else
+      {
+        s += "Sequence:\n" + it->second->Format(prefix + "  ");
+      }
+    }
+
+    return s;
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/Search/HierarchicalMatcher.h	Thu Nov 19 17:41:22 2015 +0100
@@ -0,0 +1,71 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../../Core/DicomFormat/DicomMap.h"
+#include "IFindConstraint.h"
+#include "../ParsedDicomFile.h"
+
+class DcmItem;
+
+namespace Orthanc
+{
+  class HierarchicalMatcher : public boost::noncopyable
+  {
+  private:
+    typedef std::map<DicomTag, IFindConstraint*>      Constraints;
+    typedef std::map<DicomTag, HierarchicalMatcher*>  Sequences;
+
+    Constraints  constraints_;
+    Sequences    sequences_;
+
+    void Setup(DcmItem& query,
+               bool caseSensitivePN,
+               Encoding encoding);
+
+    HierarchicalMatcher(DcmItem& query,
+                        bool caseSensitivePN,
+                        Encoding encoding)
+    {
+      Setup(query, caseSensitivePN, encoding);
+    }
+
+  public:
+    HierarchicalMatcher(ParsedDicomFile& query,
+                        bool caseSensitivePN);
+
+    ~HierarchicalMatcher();
+
+    std::string Format(const std::string& prefix = "") const;
+  };
+}
--- a/OrthancServer/Search/IFindConstraint.h	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/IFindConstraint.h	Thu Nov 19 17:41:22 2015 +0100
@@ -50,6 +50,8 @@
 
     virtual bool Match(const std::string& value) const = 0;
 
+    virtual std::string Format() const = 0;
+
     static IFindConstraint* ParseDicomConstraint(const DicomTag& tag,
                                                  const std::string& dicomQuery,
                                                  bool caseSensitive);
--- a/OrthancServer/Search/ListConstraint.cpp	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/ListConstraint.cpp	Thu Nov 19 17:41:22 2015 +0100
@@ -75,4 +75,23 @@
 
     return allowedValues_.find(v) != allowedValues_.end();
   }
+
+
+  std::string ListConstraint::Format() const
+  {
+    std::string s;
+
+    for (std::set<std::string>::const_iterator
+           it = allowedValues_.begin(); it != allowedValues_.end(); ++it)
+    {
+      if (!s.empty())
+      {
+        s += "\\";
+      }
+
+      s += *it;
+    }
+
+    return s;
+  }
 }
--- a/OrthancServer/Search/ListConstraint.h	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/ListConstraint.h	Thu Nov 19 17:41:22 2015 +0100
@@ -67,5 +67,7 @@
                        const DicomTag& tag) const;
 
     virtual bool Match(const std::string& value) const;
+
+    virtual std::string Format() const;
   };
 }
--- a/OrthancServer/Search/RangeConstraint.h	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/RangeConstraint.h	Thu Nov 19 17:41:22 2015 +0100
@@ -64,5 +64,10 @@
                        const DicomTag& tag) const;
 
     virtual bool Match(const std::string& value) const;
+
+    virtual std::string Format() const
+    {
+      return lower_ + "-" + upper_;
+    }
   };
 }
--- a/OrthancServer/Search/ValueConstraint.h	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/ValueConstraint.h	Thu Nov 19 17:41:22 2015 +0100
@@ -61,5 +61,10 @@
                        const DicomTag& tag) const;
 
     virtual bool Match(const std::string& value) const;
+
+    virtual std::string Format() const
+    {
+      return value_;
+    }
   };
 }
--- a/OrthancServer/Search/WildcardConstraint.cpp	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/WildcardConstraint.cpp	Thu Nov 19 17:41:22 2015 +0100
@@ -78,4 +78,9 @@
   {
     lookup.AddConstraint(tag, IdentifierConstraintType_Wildcard, pimpl_->wildcard_);
   }
+
+  std::string WildcardConstraint::Format() const
+  {
+    return pimpl_->wildcard_;
+  }
 }
--- a/OrthancServer/Search/WildcardConstraint.h	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/Search/WildcardConstraint.h	Thu Nov 19 17:41:22 2015 +0100
@@ -59,5 +59,7 @@
                        const DicomTag& tag) const;
 
     virtual bool Match(const std::string& value) const;
+
+    virtual std::string Format() const;
   };
 }
--- a/OrthancServer/main.cpp	Thu Nov 19 16:02:35 2015 +0100
+++ b/OrthancServer/main.cpp	Thu Nov 19 17:41:22 2015 +0100
@@ -52,6 +52,8 @@
 #include "../Plugins/Engine/OrthancPlugins.h"
 #include "FromDcmtkBridge.h"
 
+#include "Search/HierarchicalMatcher.h"
+
 using namespace Orthanc;
 
 
@@ -101,11 +103,17 @@
   }
 
   virtual bool Handle(DicomFindAnswers& answers,
-                      const ParsedDicomFile& query,
+                      ParsedDicomFile& query,
                       const std::string& remoteIp,
                       const std::string& remoteAet)
   {
     printf("Worklist\n");
+
+    bool caseSensitivePN = Configuration::GetGlobalBoolParameter("CaseSensitivePN", false);
+    HierarchicalMatcher matcher(query, caseSensitivePN);
+
+    std::cout << matcher.Format();
+
     return true;
   }
 };