Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.84 KB | None | 0 0
  1. /**
  2.    \file ForEachProxyFunctor.cpp
  3.  
  4.    \brief Implementação do proxy responsável pela leitura da interface
  5.    legada do functor ForEach.
  6.  
  7.    Copyright© 1998-2012 Centro de Sensoriamento Remoto / Universidade
  8.    Federal de Minas Gerais - Brazil.
  9.    All Rights Reserved.
  10.  
  11.    \author Hermann Oliveira Rodrigues
  12.    \author Gustavo Coutinho Cerqueira
  13. */
  14.  
  15. #include "Config.h"
  16.  
  17. #define _DFF_CPP_LIBRARY_ TableLib
  18.  
  19. #include <memory>
  20. #include <boost/assert.hpp>
  21. #include "BooleanValueType.h"
  22. #include "ContainerProxyFunctor.h"
  23. #include "RealValueType.h"
  24. #include "ForEachProxyFunctor.h"
  25. #include "FunctorBuilderDSL.h"
  26. #include "FunctorBuilderDSL.h"
  27. #include "ImageExpressionEditor.h"
  28. #include "Log.h"
  29. #include "LookupTableType.h"
  30. #include "PositiveIntegerValueType.h"
  31. #include "ProxyFunctorUtils.h"
  32.  
  33.  
  34. namespace Table {
  35.  
  36.  
  37. namespace {
  38.  
  39.  
  40. void handleSkipZerosInputPort( std::vector< std::shared_ptr< DFF::Functor > >& functors,
  41.   DFF::Functor& targetFunctor, const DFF::Functor& sourceFunctor );
  42.  
  43.  
  44. }
  45.  
  46.  
  47. ForEachProxy::ForEachProxy()
  48.   : DFF::ContainerProxyFunctor( "ForEach",
  49.     "Proxy used to read legacy ForEach." ) {
  50.   // Declara os portos de entrada /////////////////////////////////////////////
  51.   addInputPort< LookupTableType >( "elements",
  52.     "" );
  53.  
  54.   // Declara os portos de entrada opcionais ///////////////////////////////////
  55.  
  56.   auto skipZeros = addOptionalInputPort< Prim::BooleanValueType >( "skipZeros",
  57.     "" );
  58.   BOOST_ASSERT( skipZeros != nullptr );
  59.   skipZeros->setValue( true );
  60.  
  61.   // Declara os portos de saída internos //////////////////////////////////////
  62.   addInternalOutputPort< Prim::RealValueType >( "step",
  63.     "" );
  64. }
  65.  
  66.  
  67. bool ForEachProxy::isCompatible( const DFF::Functor& functor ) const {
  68.   return true;
  69. }
  70.  
  71.  
  72. std::vector< std::shared_ptr< DFF::Functor > > ForEachProxy::transformFunctor() {
  73.   std::vector< std::shared_ptr< DFF::Functor > > functors;
  74.  
  75.   auto forEach = DFF::FunctorBuilder( "ForEach" )
  76.     .create();
  77.   BOOST_ASSERT( forEach != nullptr );
  78.  
  79.   // Transfere os editores e conexões para o novo functor.
  80.  
  81.   // Como o porto de nome "table" teve o seu tipo alterado, pode ser necessário
  82.   // criar functor "Table" intermediário para manter compatibilidade com a
  83.   // definição prévia. A criação do functor extra é feita quando o porto
  84.   // original possuia editor ou tinha conexão com porto que não provia tabela
  85.   // ou tabela de associação.
  86.  
  87.   bool needsMediation = DFF::ProxyFunctorUtils::isExportedInputPort( *this, "elements" );
  88.  
  89.   if ( !needsMediation ) {
  90.     DFF::InputPort& inputPort = getInputPort( "elements" );
  91.  
  92.     if ( inputPort.isBound() ) {
  93.       DFF::OutputPort *peerPort =
  94.         dynamic_cast< DFF::OutputPort * >( inputPort.getPeer() );
  95.       BOOST_ASSERT( peerPort != nullptr );
  96.  
  97.       std::string peerType = peerPort->getType()->getName();
  98.       needsMediation = peerType != "BaseTable" &&
  99.         peerType != "LookupTable" &&
  100.         peerType != "Table";
  101.  
  102.     } else if ( inputPort.hasDataEditor() )
  103.       needsMediation = true;
  104.   }
  105.  
  106.   DFF::Functor *mediateFunctor = nullptr;
  107.   std::string mediatePortName;
  108.  
  109.   if ( needsMediation ) {
  110.     std::shared_ptr< DFF::Functor > lookupTable =
  111.       DFF::FunctorBuilder( "LookupTable" )
  112.       .extendedComment(
  113.         "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION OF "
  114.         "FOREACH." )
  115.       .create();
  116.     BOOST_ASSERT( lookupTable != nullptr );
  117.     functors.push_back( lookupTable );
  118.  
  119.     lookupTable|"object" >>= forEach|"elements";
  120.  
  121.     functors.push_back( lookupTable );
  122.  
  123.     mediateFunctor = lookupTable.get();
  124.     mediatePortName = "constant";
  125.  
  126.   } else {
  127.     mediateFunctor = forEach.get();
  128.     mediatePortName = "elements";
  129.   }
  130.   BOOST_ASSERT( mediateFunctor != nullptr );
  131.  
  132.   DFF::ProxyFunctorUtils::copyGeneralProperties( *forEach, *this );
  133.  
  134.   DFF::InputPort& skipZeros = getInputPort( "skipZeros" );
  135.  
  136.   auto skipZerosEditor = skipZeros.getDataEditor2< Prim::BooleanValueEditor >();
  137.   if ( ! skipZeros.isBound() &&
  138.     ( skipZerosEditor == nullptr || ! skipZerosEditor->getValue() ) ) {
  139.     // skipZeros não está conectado e não possui editor ou o editor possui
  140.     // valor false.
  141.  
  142.     DFF::ProxyFunctorUtils::exportGhostInputFunctor( functors, *this, "skipZeros" );
  143.  
  144.   } else
  145.     handleSkipZerosInputPort( functors, *forEach, *this );
  146.  
  147.   DFF::ProxyFunctorUtils::copyInputPort( *mediateFunctor, mediatePortName,
  148.     *this, "elements" );
  149.  
  150.   // Faz o tratamento dos functors contidos.
  151.  
  152.   DFF::ProxyFunctorUtils::moveAndExpandChildFunctors( functors, *forEach, *this );
  153.  
  154.   functors.push_back( forEach );
  155.  
  156.   return functors;
  157. }
  158.  
  159.  
  160. namespace {
  161.  
  162.  
  163. void handleSkipZerosInputPort( std::vector< std::shared_ptr< DFF::Functor > >& functors,
  164.   DFF::Functor& targetFunctor, const DFF::Functor& sourceFunctor ) {
  165.   auto group = DFF::FunctorBuilder( "Group" )
  166.     .extendedComment( "Remove zero-value key/value pairs from the lookup table.\n"
  167.       "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
  168.       "OF FOREACH." )
  169.     .collapsed()
  170.     .create();
  171.   BOOST_ASSERT( group != nullptr );
  172.  
  173.   auto lookupTable = DFF::FunctorBuilder( "LookupTable" )
  174.     .extendedComment( "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
  175.       "OF FOREACH." )
  176.     .container( group )
  177.     .create();
  178.   BOOST_ASSERT( lookupTable != nullptr );
  179.  
  180.   auto bool_ = DFF::FunctorBuilder( "BooleanValue" )
  181.     .extendedComment( "Flag used to controls whether zero-value key/value pairs should be "
  182.       "removed from the lookup table.\n"
  183.       "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
  184.       "OF FOREACH." )
  185.     .container( group )
  186.     .create();
  187.   BOOST_ASSERT( bool_ != nullptr );
  188.  
  189.   auto ifThen = DFF::FunctorBuilder( "IfThen" )
  190.     .extendedComment( "Controls whether zero-value key/value pairs should be removed from "
  191.       "the lookup table based on the value of the input flag.\n"
  192.       "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
  193.       "OF FOREACH." )
  194.     .container( group )
  195.     .create();
  196.   BOOST_ASSERT( ifThen != nullptr );
  197.  
  198.   bool_|"object" >>= ifThen|"condition";
  199.  
  200.   auto calculateLookupTable = DFF::FunctorBuilder( "CalculateLookupTable" )
  201.     .extendedComment( "Filter the lookup table removing zero-value key/value pairs.\n"
  202.       "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
  203.       "OF FOREACH." )
  204.     .collapsed()
  205.     .container( ifThen )
  206.     .create();
  207.   BOOST_ASSERT( calculateLookupTable != nullptr );
  208.  
  209.   // Não usa DFF::editValue() pois ImageExpressionEditor não define setValue().
  210.   Prim::ImageExpressionEditor& expressionEditor =
  211.     calculateLookupTable->editPortData< Prim::ImageExpressionEditor >( "expression" );
  212.   expressionEditor.setImageExpressionAsString( "if t1[line] == 0 then null else t1[line]" );
  213.  
  214.   auto numberTable = DFF::FunctorBuilder( "NumberTable" )
  215.     .container( calculateLookupTable )
  216.     .create();
  217.   BOOST_ASSERT( numberTable != nullptr );
  218.  
  219.   DFF::editValue< Prim::PositiveIntegerValueType >( numberTable, "tableNumber", 1 );
  220.  
  221.   lookupTable|"object" >>= numberTable|"table";
  222.  
  223.   auto lookupTableJunction = DFF::FunctorBuilder( "LookupTableJunction" )
  224.     .extendedComment( "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
  225.       "OF FOREACH." )
  226.     .container( group )
  227.     .create();
  228.   BOOST_ASSERT( lookupTableJunction != nullptr );
  229.  
  230.   calculateLookupTable|"result" >>= lookupTableJunction|"possibleTable1";
  231.   lookupTable|"object" >>= lookupTableJunction|"possibleTable2";
  232.   lookupTableJunction|"table" >>= targetFunctor|"elements";
  233.  
  234.   functors.push_back( group );
  235.  
  236.   DFF::ProxyFunctorUtils::copyInputPort( *lookupTable, "constant",
  237.     sourceFunctor, "elements" );
  238.  
  239.   DFF::ProxyFunctorUtils::copyInputPort( *bool_, "constant",
  240.     sourceFunctor, "skipZeros" );
  241. }
  242.  
  243.  
  244. }
  245.  
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement