void copyDataflows( ESMoL::ModelsFolder inputModelsFolder, ESMoL::ModelsFolder outputModelsFolder ) { getPortList().clear(); getPortMap().clear(); DataflowVector dataflowVector = inputModelsFolder.Dataflow_kind_children(); for( DataflowVector::iterator dfvItr = dataflowVector.begin() ; dfvItr != dataflowVector.end() ; ++dfvItr ) { ESMoL::Dataflow inputDataflow = *dfvItr; ESMoL::Dataflow outputDataflow = ESMoL::Dataflow::Create( outputModelsFolder ); outputDataflow.name() = inputDataflow.name(); copySubsystems_flatten( inputDataflow, outputDataflow ); } for( PortList::iterator ptlItr = getPortList().begin() ; ptlItr != getPortList().end() ; ++ptlItr ) { ESMoL::Port inputDstPort = *ptlItr; LineSet lineSet = inputDstPort.srcLine(); if ( lineSet.empty() ) continue; ESMoL::Line line = *lineSet.begin(); ESMoL::Port inputSrcPort = line.srcLine_end(); lineSet = inputSrcPort.srcLine(); while( getPortMap().find( inputSrcPort ) == getPortMap().end() && !lineSet.empty() ) { line = *lineSet.begin(); inputSrcPort = line.srcLine_end(); lineSet = inputSrcPort.srcLine(); } PortMap::iterator ptmItr = getPortMap().find( inputDstPort ); if ( ptmItr == getPortMap().end() ) { std::cerr << "Warning: port not in PortMap" << std::endl; continue; } ESMoL::Port outputDstPort = ptmItr->second; ptmItr = getPortMap().find( inputSrcPort ); if ( ptmItr == getPortMap().end() ) { std::cerr << "Warning: port not in PortMap" << std::endl; continue; } ESMoL::Port outputSrcPort = ptmItr->second; Udm::Object lineParent = outputSrcPort.GetParent(); if ( Udm::IsDerivedFrom( outputSrcPort.type(), ESMoL::OutPort::meta ) ) lineParent = lineParent.GetParent(); ESMoL::Line outputLine = ESMoL::Line::Create( lineParent ); outputLine.srcLine_end() = outputSrcPort; outputLine.dstLine_end() = outputDstPort; } }
bool isConditionActionSubsystem( ESMoL::Subsystem subsystem ) { ActionPortVector actionPortVector = subsystem.ActionPort_kind_children(); if ( actionPortVector.size() != 1 ) return false; ESMoL::Port port = actionPortVector.front(); LineSet lineSet = port.srcLine(); if ( lineSet.size() != 1 ) return false; ESMoL::Line line = *lineSet.begin(); ESMoL::Port srcPort = line.srcLine_end(); ESMoL::Block block = ESMoL::Block::Cast( srcPort.GetParent() ); return block.BlockType() == "If" || block.BlockType() == "SwitchCase"; }
int main(int argc, char ** argv) { // load the line data typedef utils::Directory::FilenamesType FilenamesType; FilenamesType filenames = utils::Directory::GetFiles(argv[1], ".json"); for(unsigned int i = 0; i < filenames.size(); i++) { std::string filename = filenames[i]; std::cout << filename << std::endl; QString inFilename = QString::fromStdString(filename); inFilename = inFilename.replace(QString::fromStdString(argv[1]),""); inFilename = inFilename.replace("/",""); inFilename = inFilename.replace(".json",""); //std::cout << inFilename.toStdString() << std::endl; vt::JsonReader::Pointer reader = vt::JsonReader::New(); reader->SetFilename(filename); typedef vt::JsonReader::LineSet LineSet; LineSet data = reader->Read(); std::string partName = inFilename.toStdString(); LineSet::iterator lineIt = data.begin(); while(lineIt != data.end()) { QString type = QString::fromStdString(lineIt->first); type = type.replace(":","-"); QString output = QString::fromStdString(argv[2]); QDir outputDir(output); QString outputPath = outputDir.absoluteFilePath(type); //std::cout << outputPath.toStdString() << std::endl; if(!outputDir.exists(outputPath)) outputDir.mkdir(outputPath); QString fileName = QDir(outputPath).absoluteFilePath(QString::fromStdString(partName)); vt::JsonConverter::Pointer converter = vt::JsonConverter::New(); converter->SetInput(lineIt->second); converter->Convert(); vt::ValveSequenceWriter<3>::Pointer writer = vt::ValveSequenceWriter<3>::New(); writer->SetInput(converter->GetOutput()); writer->SetFileName(fileName.toStdString()); writer->Write(); std::cout << fileName.toStdString() << " " << converter->GetOutput()->GetNumberOfLines() << std::endl; ++lineIt; } } return 0; }
void SFUtils::DoTopologicalSort( SLSF::Subsystem subsystem ) { int vertexIndex = 0; VertexIndexBlockMap vertexIndexBlockMap; BlockVertexIndexMap blockVertexIndexMap; BlockVector blockVector = subsystem.Block_kind_children(); for( BlockVector::iterator blvItr = blockVector.begin(); blvItr != blockVector.end(); ++blvItr, ++vertexIndex ) { SLSF::Block block = *blvItr; vertexIndexBlockMap[ vertexIndex ] = block; blockVertexIndexMap[ block ] = vertexIndex; std::string blockType = block.BlockType(); if ( blockType == "UnitDelay" ) { // check on other delay blocks as well ... ++vertexIndex; // UnitDelay is vertexed twice - one for outputs (timestep n-1), and one for inputs: vertexIndex is as destination, vertexIndex + 1 is as source } } Graph graph( vertexIndex ); LineSet lineSet = subsystem.Line_kind_children(); for( LineSet::iterator lnsItr = lineSet.begin(); lnsItr != lineSet.end() ; ++lnsItr ) { SLSF::Line line = *lnsItr; SLSF::Port sourcePort = line.srcLine_end(); SLSF::Port destinationPort = line.dstLine_end(); SLSF::Block sourceBlock = sourcePort.Block_parent(); SLSF::Block destinationBlock = destinationPort.Block_parent(); if ( sourceBlock == subsystem || destinationBlock == subsystem ) continue; int sourceBlockVertexIndex = blockVertexIndexMap[ sourceBlock ]; if ( static_cast< std::string >( sourceBlock.BlockType() ) == "UnitDelay" ) { ++sourceBlockVertexIndex; } int destinationBlockVertexIndex = blockVertexIndexMap[ destinationBlock ]; boost::add_edge( sourceBlockVertexIndex, destinationBlockVertexIndex, graph ); } LoopDetector loopDetector( graph ); if ( loopDetector.check() ) { // TODO: add support for loops involving integrator and other stateful blocks // Determine what Blocks caused the loop typedef std::map< Vertex, int > VertexStrongComponentIndexMap; VertexStrongComponentIndexMap vertexStrongComponentIndexMap; boost::associative_property_map< VertexStrongComponentIndexMap > apmVertexStrongComponentIndexMap( vertexStrongComponentIndexMap ); strong_components( graph, apmVertexStrongComponentIndexMap ); typedef std::vector< Vertex > VertexVector; typedef std::map< int, VertexVector > StrongComponentIndexVertexGroupMap; StrongComponentIndexVertexGroupMap strongComponentIndexVertexGroupMap; for( VertexStrongComponentIndexMap::iterator vsmItr = vertexStrongComponentIndexMap.begin(); vsmItr != vertexStrongComponentIndexMap.end(); ++vsmItr ) { strongComponentIndexVertexGroupMap[ vsmItr->second ].push_back( vsmItr->first ); } std::string error( "Dataflow Graph '" + static_cast< std::string >( subsystem.Name() ) + "' has unhandled loops: " ); for( StrongComponentIndexVertexGroupMap::iterator svmItr = strongComponentIndexVertexGroupMap.begin(); svmItr != strongComponentIndexVertexGroupMap.end(); ++svmItr ) { VertexVector vertexVector = svmItr->second; if ( vertexVector.size() <= 1 ) continue; error.append( "\n" ); for( VertexVector::iterator vtvItr = vertexVector.begin(); vtvItr != vertexVector.end(); ++vtvItr ) { error.append( blockVector[ *vtvItr ].getPath("/") ); error.append( ", " ); } error.erase( error.size() - 2 ); } throw udm_exception(error); } typedef std::set< Vertex > VertexSet; typedef std::map< int, VertexSet > PriorityVertexSetMap; PriorityVertexSetMap priorityVertexSetMap; for( BlockVector::iterator blvItr = blockVector.begin() ; blvItr != blockVector.end() ; ++blvItr ) { SLSF::Block block = *blvItr; int priority = block.Priority(); if ( priority == 0 ) continue; Vertex vertex = blockVertexIndexMap[ block ]; priorityVertexSetMap[ priority ].insert( vertex ); } if ( priorityVertexSetMap.size() > 1 ) { PriorityVertexSetMap::iterator lstPvmItr = priorityVertexSetMap.end(); --lstPvmItr; for( PriorityVertexSetMap::iterator pvmItr = priorityVertexSetMap.begin() ; pvmItr != lstPvmItr ; ) { PriorityVertexSetMap::iterator nxtPvmItr = pvmItr; ++nxtPvmItr; VertexSet &higherPriorityVertexSet = pvmItr->second; VertexSet &lowerPriorityVertexSet = nxtPvmItr->second; for( VertexSet::iterator hvsItr = higherPriorityVertexSet.begin() ; hvsItr != higherPriorityVertexSet.end() ; ++hvsItr ) { for( VertexSet::iterator lvsItr = lowerPriorityVertexSet.begin() ; lvsItr != lowerPriorityVertexSet.end() ; ++lvsItr ) { boost::add_edge( *hvsItr, *lvsItr, graph ); LoopDetector loopDetector( graph ); if ( loopDetector.check( *hvsItr ) ) { SLSF::Block higherPriorityBlock = vertexIndexBlockMap[ *hvsItr ]; SLSF::Block lowerPriorityBlock = vertexIndexBlockMap[ *lvsItr ]; std::cerr << "WARNING: Cannot implement priority difference between block \"" << higherPriorityBlock.getPath( "/" ) << "\" (Priority = " << *hvsItr << ") and " << std::endl; std::cerr << " block \"" << lowerPriorityBlock.getPath( "/" ) << "\" (Priority = " << *lvsItr << "): contradicts topology of subsystem or other implemented block priority order." << std::endl; boost::remove_edge( *hvsItr, *lvsItr, graph ); } } } pvmItr = nxtPvmItr; } } VertexList vertexList; boost::topological_sort( graph, std::back_inserter( vertexList ) ); /* PUT ALL "DataStoreMemory" BLOCKS AT END OF "C" SO THEY HAVE HIGHEST PRIORITY */ VertexList::reverse_iterator vtlRit = vertexList.rbegin(); while( vtlRit != vertexList.rend() ) { int index = *vtlRit; SLSF::Block block = vertexIndexBlockMap[ index ]; (void)++vtlRit; if ( block != Udm::null && static_cast< std::string >( block.BlockType() ) == "DataStoreMemory" ) { VertexList::reverse_iterator vtlRit2 = vtlRit; vertexList.splice( vertexList.end(), vertexList, vtlRit2.base() ); } } int priority = 0; for( VertexList::reverse_iterator vtlRit = vertexList.rbegin() ; vtlRit != vertexList.rend() ; ++vtlRit ) { int index = *vtlRit; SLSF::Block block = vertexIndexBlockMap[ index ]; if ( block == Udm::null ) { // unit delay as source is not registered - we will invoke it initially, and invoke it as destination in the priority order // const std::string& bt = blk.BlockType(); // assert(bt.compare("UnitDelay") == 0); /* Unit Delay Block as destination */ continue; } block.Priority() = priority++; } }