summaryrefslogtreecommitdiff
path: root/src/TFunction/TFunction_IFunction.cxx
blob: decff15810369d40de6f0f37ef90bd6437a7cca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// File:	TFunction_IFunction.cxx
// Created:	Mon Jun 21 14:55:45 2008
// Author:	Vladislav ROMASHKO
//		<vladislav.romashko@opencascade.com>


#include <TFunction_IFunction.ixx>
#include <TFunction_Function.hxx>
#include <TFunction_GraphNode.hxx>
#include <TFunction_DriverTable.hxx>
#include <TFunction_Scope.hxx>
#include <TFunction_DataMapOfLabelListOfLabel.hxx>
#include <TFunction_DataMapIteratorOfDataMapOfLabelListOfLabel.hxx>
#include <TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel.hxx>

#include <TDF_LabelList.hxx>
#include <TDF_ListIteratorOfLabelList.hxx>
#include <TDF_MapIteratorOfLabelMap.hxx>

#include <Standard_NoSuchObject.hxx>
#include <TColStd_MapIteratorOfMapOfInteger.hxx>

#ifdef DEB
#include <TDataStd_Name.hxx>
#endif // DEB

//=======================================================================
//function : NewFunction
//purpose  : Static method to create a new function.
//=======================================================================

Standard_Boolean TFunction_IFunction::NewFunction(const TDF_Label& L, const Standard_GUID& ID) 
{ 
  // Set Function (ID, code of failure)
  TFunction_Function::Set(L, ID)->SetFailure(0);

  // Set graph node (dependencies, status)
  Handle(TFunction_GraphNode) graphNode = TFunction_GraphNode::Set(L);
  graphNode->RemoveAllPrevious();
  graphNode->RemoveAllNext();
  graphNode->SetStatus(TFunction_ES_WrongDefinition);

  // Check presence of the function in the current scope
  TFunction_Scope::Set(L)->AddFunction(L);

  return TFunction_DriverTable::Get()->HasDriver(ID);
}

//=======================================================================
//function : DeleteFunction
//purpose  : Static method to delete a function.
//=======================================================================

Standard_Boolean TFunction_IFunction::DeleteFunction(const TDF_Label& L) 
{ 
  // Delete Function
  Handle(TFunction_Function) func;
  if (L.FindAttribute(TFunction_Function::GetID(), func))
    L.ForgetAttribute(func);

  // Take the scope of functions
  Handle(TFunction_Scope) scope = TFunction_Scope::Set(L);
  const Standard_Integer funcID = scope->GetFunctions().Find2(L);

  // Delete graph node
  Handle(TFunction_GraphNode) graphNode;
  if (L.FindAttribute(TFunction_GraphNode::GetID(), graphNode))
  {
    const TColStd_MapOfInteger& prev = graphNode->GetPrevious();
    const TColStd_MapOfInteger& next = graphNode->GetNext();
    // Disconnect previous functions
    TColStd_MapIteratorOfMapOfInteger itrm(prev);
    for (; itrm.More(); itrm.Next())
    {
      const Standard_Integer ID = itrm.Key();
      const TDF_Label& La = scope->GetFunctions().Find1(ID);
      Handle(TFunction_GraphNode) G;
      if (La.FindAttribute(TFunction_GraphNode::GetID(), G))
      {
	G->RemoveNext(funcID);
      }
    }
    // Disconnect next functions
    for (itrm.Initialize(next); itrm.More(); itrm.Next())
    {
      const Standard_Integer ID = itrm.Key();
      const TDF_Label& La = scope->GetFunctions().Find1(ID);
      Handle(TFunction_GraphNode) G;
      if (La.FindAttribute(TFunction_GraphNode::GetID(), G))
      {
	G->RemovePrevious(funcID);
      }
    }

    L.ForgetAttribute(graphNode);
  }

  // Delete the function from the current scope of functions.
  scope->RemoveFunction(L);

  return Standard_True;
}

//=======================================================================
//function : UpdateDependencies
//purpose  : Updates the dependencies of all functions.
//=======================================================================

Standard_Boolean TFunction_IFunction::UpdateDependencies(const TDF_Label& Access)
{
  // Take the scope of functions.
  Handle(TFunction_Scope) scope = TFunction_Scope::Set(Access);

  // Make a data map of function - results.
  TFunction_DataMapOfLabelListOfLabel table;
  TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel itrm(scope->GetFunctions());
  for (; itrm.More(); itrm.Next())
  {
    // Label of the function
    const TDF_Label& L = itrm.Key2();
    TFunction_IFunction iFunction(L);

#ifdef DEB
    /*
    TCollection_AsciiString fname;
    Handle(TDataStd_Name) name;
    if (L.FindAttribute(TDataStd_Name::GetID(), name))
    {
      fname = name->Get();
    }
    else if (L.Father().FindAttribute(TDataStd_Name::GetID(), name))
    {
      fname = name->Get();
    }
    */
#endif // DEB
    
    // Take the driver.
    Handle(TFunction_Driver) driver = iFunction.GetDriver();

    // Take the results.
    TDF_LabelList res;
    driver->Results(res);

    // Fill-in the table
    table.Bind(L, res);
    
    // Clean the graph node
    Handle(TFunction_GraphNode) graphNode = iFunction.GetGraphNode();
    graphNode->RemoveAllPrevious();
    graphNode->RemoveAllNext();
  }

  // Update previous and next functions for each function of the scope
  TFunction_DataMapIteratorOfDataMapOfLabelListOfLabel itrd;
  for (itrm.Initialize(scope->GetFunctions()); itrm.More(); itrm.Next())
  {
    // Label of the functions
    const TDF_Label& L = itrm.Key2();
    TFunction_IFunction iFunction(L);

#ifdef DEB
    /*
    TCollection_AsciiString fname;
    Handle(TDataStd_Name) name;
    if (L.FindAttribute(TDataStd_Name::GetID(), name))
    {
      fname = name->Get();
    }
    else if (L.Father().FindAttribute(TDataStd_Name::GetID(), name))
    {
      fname = name->Get();
    }
    */
#endif // DEB

    // Take the driver.
    Handle(TFunction_Driver) driver = iFunction.GetDriver();

    // Take the arguments.
    TDF_LabelList args;
    driver->Arguments(args);
    
    // Make a map of arguments
    TDF_LabelMap argsMap;
    TDF_ListIteratorOfLabelList itrl(args);
    for (; itrl.More(); itrl.Next())
      argsMap.Add(itrl.Value());

    // ID of the function
    const Standard_Integer funcID = itrm.Key1();

    // Find the functions, which produce the arguments of this function.
    for (itrd.Initialize(table); itrd.More(); itrd.Next())
    {
      const TDF_Label& anotherL = itrd.Key();
      if (L == anotherL)
	continue;
      const TDF_LabelList& anotherRes = itrd.Value();

#ifdef DEB
      /*
      TCollection_AsciiString afname;
      Handle(TDataStd_Name) aname;
      if (anotherL.FindAttribute(TDataStd_Name::GetID(), aname))
      {
	afname = aname->Get();
      }
      else if (anotherL.Father().FindAttribute(TDataStd_Name::GetID(), aname))
      {
	afname = aname->Get();
      }
      */
#endif // DEB

      for (itrl.Initialize(anotherRes); itrl.More(); itrl.Next())
      {
	if (argsMap.Contains(itrl.Value()))
	{
	  iFunction.GetGraphNode()->AddPrevious(anotherL);

	  TFunction_IFunction iAnotherFunction(anotherL);
	  iAnotherFunction.GetGraphNode()->AddNext(funcID);
	}
      }
    }
  }

  return Standard_True;
}

//=======================================================================
//function : Create
//purpose  : Constructor
//=======================================================================

TFunction_IFunction::TFunction_IFunction() 
{  

}

//=======================================================================
//function : Create
//purpose  : Constructor
//=======================================================================

TFunction_IFunction::TFunction_IFunction(const TDF_Label& L)
{
  Init(L);
}

//=======================================================================
//function : Init
//purpose  : Initializes the interface.
//=======================================================================

void TFunction_IFunction::Init(const TDF_Label& L)
{
  myLabel = L;
}

//=======================================================================
//function : Label
//purpose  : Returns the label of the interface.
//=======================================================================

const TDF_Label& TFunction_IFunction::Label() const
{
  return myLabel;
}

//=======================================================================
//function : UpdateDependencies
//purpose  : Updates the dependencies of this function only.
//=======================================================================

Standard_Boolean TFunction_IFunction::UpdateDependencies() const
{
  // Take the arguments & results of the functions
  TDF_LabelList args, res;
  Handle(TFunction_Driver) D = GetDriver();
  D->Arguments(args);
  D->Results(res);

  // Insert the arguments and results into maps for fast searching.
  TDF_LabelMap argsMap, resMap;
  TDF_ListIteratorOfLabelList itrl(args);
  for (; itrl.More(); itrl.Next())
  {
    argsMap.Add(itrl.Value());
  }
  for (itrl.Initialize(res); itrl.More(); itrl.Next())
  {
    resMap.Add(itrl.Value());
  }

  // Consider all other functions checking their attitude to this function.
  Handle(TFunction_Scope) scope = TFunction_Scope::Set(myLabel);
  TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel itrm(scope->GetFunctions());
  for (; itrm.More(); itrm.Next())
  {
    const TDF_Label& L = itrm.Key2();
    if (L == myLabel)
      continue;
    TFunction_IFunction iFunc(L);
    D = iFunc.GetDriver();

    // Arguments of another function
    args.Clear();
    D->Arguments(args);

    // Check presence of the arguments in results of our function
    for (itrl.Initialize(args); itrl.More(); itrl.Next())
    {
      if (resMap.Contains(itrl.Value()))
      {
	// Our function is a previous one for this function.
	GetGraphNode()->AddNext(scope->GetFunctions().Find2(L));
	iFunc.GetGraphNode()->AddPrevious(scope->GetFunctions().Find2(myLabel));
      }
    }

    // Results of another function
    res.Clear();
    D->Results(res);

    // Check presence of the results in arguments of our function
    for (itrl.Initialize(res); itrl.More(); itrl.Next())
    {
      if (argsMap.Contains(itrl.Value()))
      {
	// Our function is a next one for this function.
	GetGraphNode()->AddPrevious(scope->GetFunctions().Find2(L));
	iFunc.GetGraphNode()->AddNext(scope->GetFunctions().Find2(myLabel));
      }
    }
  }

  return Standard_True;
}

//=======================================================================
//function : Arguments
//purpose  : The method fills-in the list by labels, 
//           where the arguments of the function are located.
//=======================================================================

void TFunction_IFunction::Arguments(TDF_LabelList& args) const
{
  Handle(TFunction_Driver) driver = GetDriver();
  driver->Arguments(args);
}

//=======================================================================
//function : Results
//purpose  : The method fills-in the list by labels,
//           where the results of the function are located.
//=======================================================================

void TFunction_IFunction::Results(TDF_LabelList& res) const
{
  Handle(TFunction_Driver) driver = GetDriver();
  driver->Results(res);
}

//=======================================================================
//function : GetPrevious
//purpose  : Returns a list of previous functions.
//=======================================================================

void TFunction_IFunction::GetPrevious(TDF_LabelList& prev) const
{
  Handle(TFunction_GraphNode) graph = GetGraphNode();
  const TColStd_MapOfInteger& map = graph->GetPrevious();
  Handle(TFunction_Scope) scope = TFunction_Scope::Set(myLabel);

  TColStd_MapIteratorOfMapOfInteger itrm(map);
  for (; itrm.More(); itrm.Next())
  {
    const Standard_Integer funcID = itrm.Key();
    if (scope->GetFunctions().IsBound1(funcID))
    {
      prev.Append(scope->GetFunctions().Find1(funcID));
    }
  }
}

//=======================================================================
//function : GetNext
//purpose  : Returns a list of next functions.
//=======================================================================

void TFunction_IFunction::GetNext(TDF_LabelList& next) const
{
  Handle(TFunction_GraphNode) graph = GetGraphNode();
  const TColStd_MapOfInteger& map = graph->GetNext();
  Handle(TFunction_Scope) scope = TFunction_Scope::Set(myLabel);

  TColStd_MapIteratorOfMapOfInteger itrm(map);
  for (; itrm.More(); itrm.Next())
  {
    const Standard_Integer funcID = itrm.Key();
    if (scope->GetFunctions().IsBound1(funcID))
    {
      next.Append(scope->GetFunctions().Find1(funcID));
    }
  }
}

//=======================================================================
//function : GetStatus
//purpose  : Returns the execution status of the function.
//=======================================================================

TFunction_ExecutionStatus TFunction_IFunction::GetStatus() const
{
  Handle(TFunction_GraphNode) graph = GetGraphNode();
  return graph->GetStatus();
}

//=======================================================================
//function : SetStatus
//purpose  : Defines an execution status for a function.
//=======================================================================

void TFunction_IFunction::SetStatus(const TFunction_ExecutionStatus status) const
{
  Handle(TFunction_GraphNode) graph = GetGraphNode();
  graph->SetStatus(status);
}

//=======================================================================
//function : GetFunctions
//purpose  : Returns the scope of functions.
//=======================================================================

const TFunction_DoubleMapOfIntegerLabel& TFunction_IFunction::GetAllFunctions() const
{
  return TFunction_Scope::Set(myLabel)->GetFunctions();
}

//=======================================================================
//function : GetLogbook
//purpose  : Returns the Logbook.
//=======================================================================

TFunction_Logbook& TFunction_IFunction::GetLogbook() const
{
  return TFunction_Scope::Set(myLabel)->GetLogbook();
}

//=======================================================================
//function : GetDriver
//purpose  : Returns the function driver.
//=======================================================================

Handle(TFunction_Driver) TFunction_IFunction::GetDriver(const Standard_Integer thread) const
{
  Handle(TFunction_Driver) driver;
  Handle(TFunction_Function) func;
  if (!myLabel.FindAttribute(TFunction_Function::GetID(), func))
    Standard_NoSuchObject::Raise("TFunction_IFunction::GetDriver(): A Function is not found attached to this label");
  if (!TFunction_DriverTable::Get()->FindDriver(func->GetDriverGUID(), driver, thread))
    Standard_NoSuchObject::Raise("TFunction_IFunction::GetDriver(): A driver is not found for this ID");
  driver->Init(myLabel);
  return driver;
}

//=======================================================================
//function : GetGraphNode
//purpose  : Returns a graph node of the function.
//=======================================================================

Handle(TFunction_GraphNode) TFunction_IFunction::GetGraphNode() const
{
  Handle(TFunction_GraphNode) graphNode;
  if (!myLabel.FindAttribute(TFunction_GraphNode::GetID(), graphNode))
    Standard_NoSuchObject::Raise("TFunction_IFunction::GetStatus(): A graph node is not found attached to this label");
  return graphNode;
}