1 /*
2 
3     This file is part of the Maude 2 interpreter.
4 
5     Copyright 1997-2003 SRI International, Menlo Park, CA 94025, USA.
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20 
21 */
22 
23 //
24 //      Implementation for class ObjectSystemRewritingContext.
25 //
26 
27 //      utility stuff
28 #include "macros.hh"
29 #include "vector.hh"
30 
31 //      forward declarations
32 #include "interface.hh"
33 #include "core.hh"
34 #include "freeTheory.hh"
35 #include "objectSystem.hh"
36 
37 //      interface class definitions
38 #include "symbol.hh"
39 #include "dagNode.hh"
40 #include "term.hh"
41 
42 //	object system class definitions
43 #include "objectSystemRewritingContext.hh"
44 #include "externalObjectManagerSymbol.hh"
45 
~ObjectSystemRewritingContext()46 ObjectSystemRewritingContext::~ObjectSystemRewritingContext()
47 {
48   //DebugAdvisory("~ObjectSystemRewritingContext() called; " << externalObjects.size() << " external objects");
49   if (!(externalObjects.empty()))
50     {
51       FOR_EACH_CONST(i, ObjectMap, externalObjects)
52 	i->second->cleanUp(i->first);
53     }
54 }
55 
56 void
bufferMessage(DagNode * target,DagNode * message)57 ObjectSystemRewritingContext::bufferMessage(DagNode* target, DagNode* message)
58 {
59   incomingMessages[target].push_back(message);
60 }
61 
62 void
addExternalObject(DagNode * name,ExternalObjectManagerSymbol * manager)63 ObjectSystemRewritingContext::addExternalObject(DagNode* name,
64 						ExternalObjectManagerSymbol* manager)
65 {
66   externalObjects.insert(ObjectMap::value_type(name, manager));
67 }
68 
69 void
deleteExternalObject(DagNode * name)70 ObjectSystemRewritingContext::deleteExternalObject(DagNode* name)
71 {
72   externalObjects.erase(name);
73 }
74 
75 bool
getExternalMessages(DagNode * target,list<DagNode * > & messages)76 ObjectSystemRewritingContext::getExternalMessages(DagNode* target, list<DagNode*>& messages)
77 {
78   MessageMap::iterator i = incomingMessages.find(target);
79   if (i != incomingMessages.end())
80     {
81       messages.splice(messages.end(), i->second);
82       incomingMessages.erase(i);
83       return true;
84     }
85   return false;
86 }
87 
88 bool
offerMessageExternally(DagNode * target,DagNode * message)89 ObjectSystemRewritingContext::offerMessageExternally(DagNode* target, DagNode* message)
90 {
91   //cerr << "offerMessageExternally(): saw " << message << endl;
92   ObjectMap::iterator i = externalObjects.find(target);
93   if (i != externalObjects.end())
94     return i->second->handleMessage(message, *this);
95   else if (ExternalObjectManagerSymbol* m = dynamic_cast<ExternalObjectManagerSymbol*>(target->symbol()))
96     return m->handleManagerMessage(message, *this);
97   return false;
98 }
99 
100 void
markReachableNodes()101 ObjectSystemRewritingContext::markReachableNodes()
102 {
103   {
104     FOR_EACH_CONST(i, ObjectMap, externalObjects)
105       i->first->mark();
106   }
107   {
108     FOR_EACH_CONST(i, MessageMap, incomingMessages)
109       {
110 	FOR_EACH_CONST(j, list<DagNode*>, i->second)
111 	  (*j)->mark();
112       }
113   }
114   RewritingContext::markReachableNodes();
115 }
116