Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- \file ForEachProxyFunctor.cpp
- \brief Implementação do proxy responsável pela leitura da interface
- legada do functor ForEach.
- Copyright© 1998-2012 Centro de Sensoriamento Remoto / Universidade
- Federal de Minas Gerais - Brazil.
- All Rights Reserved.
- \author Hermann Oliveira Rodrigues
- \author Gustavo Coutinho Cerqueira
- */
- #include "Config.h"
- #define _DFF_CPP_LIBRARY_ TableLib
- #include <memory>
- #include <boost/assert.hpp>
- #include "BooleanValueType.h"
- #include "ContainerProxyFunctor.h"
- #include "RealValueType.h"
- #include "ForEachProxyFunctor.h"
- #include "FunctorBuilderDSL.h"
- #include "FunctorBuilderDSL.h"
- #include "ImageExpressionEditor.h"
- #include "Log.h"
- #include "LookupTableType.h"
- #include "PositiveIntegerValueType.h"
- #include "ProxyFunctorUtils.h"
- namespace Table {
- namespace {
- void handleSkipZerosInputPort( std::vector< std::shared_ptr< DFF::Functor > >& functors,
- DFF::Functor& targetFunctor, const DFF::Functor& sourceFunctor );
- }
- ForEachProxy::ForEachProxy()
- : DFF::ContainerProxyFunctor( "ForEach",
- "Proxy used to read legacy ForEach." ) {
- // Declara os portos de entrada /////////////////////////////////////////////
- addInputPort< LookupTableType >( "elements",
- "" );
- // Declara os portos de entrada opcionais ///////////////////////////////////
- auto skipZeros = addOptionalInputPort< Prim::BooleanValueType >( "skipZeros",
- "" );
- BOOST_ASSERT( skipZeros != nullptr );
- skipZeros->setValue( true );
- // Declara os portos de saída internos //////////////////////////////////////
- addInternalOutputPort< Prim::RealValueType >( "step",
- "" );
- }
- bool ForEachProxy::isCompatible( const DFF::Functor& functor ) const {
- return true;
- }
- std::vector< std::shared_ptr< DFF::Functor > > ForEachProxy::transformFunctor() {
- std::vector< std::shared_ptr< DFF::Functor > > functors;
- auto forEach = DFF::FunctorBuilder( "ForEach" )
- .create();
- BOOST_ASSERT( forEach != nullptr );
- // Transfere os editores e conexões para o novo functor.
- // Como o porto de nome "table" teve o seu tipo alterado, pode ser necessário
- // criar functor "Table" intermediário para manter compatibilidade com a
- // definição prévia. A criação do functor extra é feita quando o porto
- // original possuia editor ou tinha conexão com porto que não provia tabela
- // ou tabela de associação.
- bool needsMediation = DFF::ProxyFunctorUtils::isExportedInputPort( *this, "elements" );
- if ( !needsMediation ) {
- DFF::InputPort& inputPort = getInputPort( "elements" );
- if ( inputPort.isBound() ) {
- DFF::OutputPort *peerPort =
- dynamic_cast< DFF::OutputPort * >( inputPort.getPeer() );
- BOOST_ASSERT( peerPort != nullptr );
- std::string peerType = peerPort->getType()->getName();
- needsMediation = peerType != "BaseTable" &&
- peerType != "LookupTable" &&
- peerType != "Table";
- } else if ( inputPort.hasDataEditor() )
- needsMediation = true;
- }
- DFF::Functor *mediateFunctor = nullptr;
- std::string mediatePortName;
- if ( needsMediation ) {
- std::shared_ptr< DFF::Functor > lookupTable =
- DFF::FunctorBuilder( "LookupTable" )
- .extendedComment(
- "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION OF "
- "FOREACH." )
- .create();
- BOOST_ASSERT( lookupTable != nullptr );
- functors.push_back( lookupTable );
- lookupTable|"object" >>= forEach|"elements";
- functors.push_back( lookupTable );
- mediateFunctor = lookupTable.get();
- mediatePortName = "constant";
- } else {
- mediateFunctor = forEach.get();
- mediatePortName = "elements";
- }
- BOOST_ASSERT( mediateFunctor != nullptr );
- DFF::ProxyFunctorUtils::copyGeneralProperties( *forEach, *this );
- DFF::InputPort& skipZeros = getInputPort( "skipZeros" );
- auto skipZerosEditor = skipZeros.getDataEditor2< Prim::BooleanValueEditor >();
- if ( ! skipZeros.isBound() &&
- ( skipZerosEditor == nullptr || ! skipZerosEditor->getValue() ) ) {
- // skipZeros não está conectado e não possui editor ou o editor possui
- // valor false.
- DFF::ProxyFunctorUtils::exportGhostInputFunctor( functors, *this, "skipZeros" );
- } else
- handleSkipZerosInputPort( functors, *forEach, *this );
- DFF::ProxyFunctorUtils::copyInputPort( *mediateFunctor, mediatePortName,
- *this, "elements" );
- // Faz o tratamento dos functors contidos.
- DFF::ProxyFunctorUtils::moveAndExpandChildFunctors( functors, *forEach, *this );
- functors.push_back( forEach );
- return functors;
- }
- namespace {
- void handleSkipZerosInputPort( std::vector< std::shared_ptr< DFF::Functor > >& functors,
- DFF::Functor& targetFunctor, const DFF::Functor& sourceFunctor ) {
- auto group = DFF::FunctorBuilder( "Group" )
- .extendedComment( "Remove zero-value key/value pairs from the lookup table.\n"
- "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
- "OF FOREACH." )
- .collapsed()
- .create();
- BOOST_ASSERT( group != nullptr );
- auto lookupTable = DFF::FunctorBuilder( "LookupTable" )
- .extendedComment( "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
- "OF FOREACH." )
- .container( group )
- .create();
- BOOST_ASSERT( lookupTable != nullptr );
- auto bool_ = DFF::FunctorBuilder( "BooleanValue" )
- .extendedComment( "Flag used to controls whether zero-value key/value pairs should be "
- "removed from the lookup table.\n"
- "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
- "OF FOREACH." )
- .container( group )
- .create();
- BOOST_ASSERT( bool_ != nullptr );
- auto ifThen = DFF::FunctorBuilder( "IfThen" )
- .extendedComment( "Controls whether zero-value key/value pairs should be removed from "
- "the lookup table based on the value of the input flag.\n"
- "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
- "OF FOREACH." )
- .container( group )
- .create();
- BOOST_ASSERT( ifThen != nullptr );
- bool_|"object" >>= ifThen|"condition";
- auto calculateLookupTable = DFF::FunctorBuilder( "CalculateLookupTable" )
- .extendedComment( "Filter the lookup table removing zero-value key/value pairs.\n"
- "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
- "OF FOREACH." )
- .collapsed()
- .container( ifThen )
- .create();
- BOOST_ASSERT( calculateLookupTable != nullptr );
- // Não usa DFF::editValue() pois ImageExpressionEditor não define setValue().
- Prim::ImageExpressionEditor& expressionEditor =
- calculateLookupTable->editPortData< Prim::ImageExpressionEditor >( "expression" );
- expressionEditor.setImageExpressionAsString( "if t1[line] == 0 then null else t1[line]" );
- auto numberTable = DFF::FunctorBuilder( "NumberTable" )
- .container( calculateLookupTable )
- .create();
- BOOST_ASSERT( numberTable != nullptr );
- DFF::editValue< Prim::PositiveIntegerValueType >( numberTable, "tableNumber", 1 );
- lookupTable|"object" >>= numberTable|"table";
- auto lookupTableJunction = DFF::FunctorBuilder( "LookupTableJunction" )
- .extendedComment( "ADDED AUTOMATICALLY TO KEEP COMPATIBILITY WITH THE PREVIOUS VERSION "
- "OF FOREACH." )
- .container( group )
- .create();
- BOOST_ASSERT( lookupTableJunction != nullptr );
- calculateLookupTable|"result" >>= lookupTableJunction|"possibleTable1";
- lookupTable|"object" >>= lookupTableJunction|"possibleTable2";
- lookupTableJunction|"table" >>= targetFunctor|"elements";
- functors.push_back( group );
- DFF::ProxyFunctorUtils::copyInputPort( *lookupTable, "constant",
- sourceFunctor, "elements" );
- DFF::ProxyFunctorUtils::copyInputPort( *bool_, "constant",
- sourceFunctor, "skipZeros" );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement