MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ShellCopy.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "header.h"
11 #include "OneToAllMsg.h"
12 #include "Shell.h"
13 #include "../scheduling/Clock.h"
14 
16 Id Shell::doCopy( Id orig, ObjId newParent, string newName,
17  unsigned int n, bool toGlobal, bool copyExtMsg )
18 {
19  if ( newName.length() > 0 && !isNameValid( newName ) ) {
20  cout << "Error: Shell::doCopy: Illegal name for copy.\n";
21  return Id();
22  }
23 
24  if ( Neutral::isDescendant( newParent, orig ) ) {
25  cout << "Error: Shell::doCopy: Cannot copy object to descendant in tree\n";
26  return Id();
27  }
28  if ( n < 1 ) {
29  cout << "Warning: Shell::doCopy( " << orig.path() << " to " <<
30  newParent.path() << " ) : numCopies must be > 0, using 1 \n";
31  return Id();
32  }
33  if ( Neutral::child( newParent.eref(), newName ) != Id() ) {
34  cout << "Error: Shell::doCopy: Cannot copy object '" << newName <<
35  "' onto '" << newParent.path() <<
36  "' since object with same name already present.\n";
37  return Id();
38  }
39 
40  Eref sheller( shelle_, 0 );
41  Id newElm = Id::nextId();
42  vector< ObjId > args;
43  args.push_back( orig );
44  args.push_back( newParent );
45  args.push_back( newElm );
46  SetGet5< vector < ObjId >, string, unsigned int, bool, bool >::set(
47  ObjId(), "copy",
48  args, newName, n, toGlobal, copyExtMsg );
49  /*
50  if ( innerCopy( args, newName, n, toGlobal, copyExtMsg ) )
51  return newElm;
52  else
53  return Id();
54  */
55  return newElm;
56 }
57 
62 Element* innerCopyElements( Id orig, ObjId newParent, Id newId,
63  unsigned int n, bool toGlobal, map< Id, Id >& tree )
64 {
65  // Element* e = new Element( newId, orig.element(), n, toGlobal );
66  unsigned int newNumData = orig.element()->numData() * n;
67  Element* e = orig.element()->copyElement(
68  newParent, newId, newNumData, toGlobal );
69  assert( e );
70  Shell::adopt( newParent, newId, 0 );
71  e->setTick( Clock::lookupDefaultTick( e->cinfo()->name() ) );
72 
73  // cout << Shell::myNode() << ": Copy: orig= " << orig << ", newParent = " << newParent << ", newId = " << newId << endl;
74  tree[ orig ] = e->id();
75 
76  // cout << Shell::myNode() << ": ice, pa = " << newParent << ", pair= (" << orig << "," << e->id() << ")\n";
77  vector< Id > kids;
78  Neutral::children( orig.eref(), kids );
79 
80  for ( vector< Id >::iterator i = kids.begin(); i != kids.end(); ++i ) {
81  // Needed in case parent is not on zero dataIndex.
82  ObjId pa = Neutral::parent( *i );
83  ObjId newParent( e->id(), pa.dataIndex );
84  innerCopyElements( *i, newParent, Id::nextId(), n, toGlobal, tree );
85  }
86  return e;
87 }
88 
89 void innerCopyMsgs( map< Id, Id >& tree, unsigned int n, bool copyExtMsgs )
90 {
91  static const Finfo* cf = Neutral::initCinfo()->findFinfo( "childOut" );
92  static const SrcFinfo1< int >* cf2 =
93  dynamic_cast< const SrcFinfo1< int >* >( cf );
94  assert( cf );
95  assert( cf2 );
96 
97  /*
98  cout << endl << Shell::myNode() << ": innerCopyMsg ";
99  for ( map< Id, Id >::const_iterator i = tree.begin();
100  i != tree.end(); ++i ) {
101  cout << " (" << i->first << "," << i->second << ") ";
102  }
103  cout << endl;
104  */
105  for ( map< Id, Id >::const_iterator i = tree.begin();
106  i != tree.end(); ++i ) {
107  Element *e = i->first.element();
108  unsigned int j = 0;
109  const vector< MsgFuncBinding >* b = e->getMsgAndFunc( j );
110  while ( b ) {
111  if ( j != cf2->getBindIndex() ) {
112  for ( vector< MsgFuncBinding >::const_iterator k =
113  b->begin();
114  k != b->end(); ++k ) {
115  ObjId mid = k->mid;
116  const Msg* m = Msg::getMsg( mid );
117  assert( m );
118  /*
119  cout << endl << Shell::myNode() << ": innerCopyMsg orig = (" <<
120  e->id() << ", " << e->getName() << "), e1 = (" <<
121  m->e1()->id() << ", " << m->e1()->getName() << "), e2 = (" <<
122  m->e2()->id() << ", " << m->e2()->getName() << "), fid = " <<
123  k->fid << ", mid = " << k->mid << endl;
124  */
125  map< Id, Id >::const_iterator tgt;
126  if ( m->e1() == e ) {
127  tgt = tree.find( m->e2()->id() );
128  } else if ( m->e2() == e ) {
129  tgt = tree.find( m->e1()->id() );
130  } else {
131  assert( 0 );
132  }
133  if ( tgt != tree.end() )
134  m->copy( e->id(), i->second, tgt->second,
135  k->fid, j, n );
136  }
137  }
138  b = e->getMsgAndFunc( ++j );
139  }
140  }
141 }
142 
143 
144 bool Shell::innerCopy( const vector< ObjId >& args, const string& newName,
145  unsigned int n, bool toGlobal, bool copyExtMsgs )
146 {
147  map< Id, Id > tree;
148  // args are orig, newParent, newElm.
149  assert( args.size() == 3 );
150  Element* e = innerCopyElements( args[0], args[1], args[2],
151  n, toGlobal, tree );
152  if ( !e ) {
153  return 0;
154  }
155  if ( newName != "" )
156  e->setName( newName );
157  //innerCopyData( orig, newParent );
158  innerCopyMsgs( tree, n, copyExtMsgs );
159  return 1;
160 }
161 
162 void Shell::handleCopy( const Eref& er, vector< ObjId > args,
163  string newName, unsigned int n, bool toGlobal, bool copyExtMsgs )
164 {
165  if ( !innerCopy( args, newName, n, toGlobal, copyExtMsgs ) ) {
166  cout << "Error on Shell::myNode()::Shell::handleCopy for " <<
167  newName << ", " << n << endl;
168  }
169  /*
170  static const Finfo* ackf =
171  Shell::initCinfo()->findFinfo( "ack" );
172  static const SrcFinfo2< unsigned int, unsigned int >*
173  ack = dynamic_cast< const SrcFinfo2< unsigned int, unsigned int >* >( ackf );
174  assert( ackf );
175  assert( ack );
176 
177  if ( innerCopy( args, newName, n, toGlobal, copyExtMsgs ) )
178  ack->send( Eref( shelle_, 0 ), Shell::myNode(), ErrorStatus );
179  else
180  ack->send( Eref( shelle_, 0 ), Shell::myNode(), OkStatus );
181  */
182 }
Element * e2() const
Definition: Msg.h:68
static ObjId parent(const Eref &e)
Definition: Neutral.cpp:701
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
BindIndex getBindIndex() const
Definition: SrcFinfo.cpp:28
std::string path(const std::string &separator="/") const
Definition: Id.cpp:76
Id doCopy(Id orig, ObjId newParent, string newName, unsigned int n, bool toGlobal, bool copyExtMsgs)
Returns the Id of the root of the copied tree upon success.
Definition: ShellCopy.cpp:16
static Id child(const Eref &e, const string &name)
Definition: Neutral.cpp:665
Definition: ObjId.h:20
Eref eref() const
Definition: Id.cpp:125
static void children(const Eref &e, vector< Id > &ret)
Definition: Neutral.cpp:342
void handleCopy(const Eref &e, vector< ObjId > args, string newName, unsigned int n, bool toGlobal, bool copyExtMsgs)
Definition: ShellCopy.cpp:162
void innerCopyMsgs(map< Id, Id > &tree, unsigned int n, bool copyExtMsgs)
Definition: ShellCopy.cpp:89
Id id() const
Definition: Element.cpp:71
virtual Element * copyElement(Id newParent, Id newId, unsigned int n, bool toGlobal) const =0
const std::string & name() const
Definition: Cinfo.cpp:260
static Id nextId()
Definition: Id.cpp:132
static bool isDescendant(Id me, Id ancestor)
Definition: Neutral.cpp:647
string path() const
Definition: ObjId.cpp:119
Element * innerCopyElements(Id orig, ObjId newParent, Id newId, unsigned int n, bool toGlobal, map< Id, Id > &tree)
Definition: ShellCopy.cpp:62
Definition: Eref.h:26
static unsigned int lookupDefaultTick(const string &className)
Definition: Clock.cpp:1035
const vector< MsgFuncBinding > * getMsgAndFunc(BindIndex b) const
Definition: Element.cpp:300
const Cinfo * cinfo() const
Definition: Element.cpp:66
virtual unsigned int numData() const =0
Returns number of data entries across all nodes.
Definition: Msg.h:18
Element * e1() const
Definition: Msg.h:61
Eref eref() const
Definition: ObjId.cpp:66
static bool adopt(ObjId parent, Id child, unsigned int msgIndex)
Definition: Shell.cpp:654
Definition: Id.h:17
void setTick(int t)
Definition: Element.cpp:251
bool innerCopy(const vector< ObjId > &args, const string &newName, unsigned int n, bool toGlobal, bool copyExtMsgs)
Does actual work of copying. Returns true on success.
Definition: ShellCopy.cpp:144
static const Msg * getMsg(ObjId m)
Definition: Msg.cpp:59
virtual Msg * copy(Id origSrc, Id newSrc, Id newTgt, FuncId fid, unsigned int b, unsigned int n) const =0
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
Element * shelle_
Definition: Shell.h:551
static bool isNameValid(const string &name)
Definition: Shell.cpp:499
const Finfo * findFinfo(const string &name) const
Definition: Cinfo.cpp:224
unsigned int dataIndex
Definition: ObjId.h:99
Definition: Finfo.h:12