-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtomNode.cpp
613 lines (481 loc) · 12.7 KB
/
AtomNode.cpp
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/***************************************************************************
* Copyright (C) 2009 by Mushthofa *
* unintendedchoice@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**
* @file AtomNode.cpp
* @author Roman Schindlauer, Mushthofa
* @date Sat Feb 4 19:09:02 CET 2006
*
* @brief Class representing one atom node in the dependency graph
*
*
*/
#include "AtomNode.h"
unsigned AtomNode::nodeCount = 0;
/*
AtomNode::AtomNode()
: atom(AtomPtr()),
inHead(0),
inBody(0),
nodeId(nodeCount++)
{
}
*/
AtomNode::AtomNode(const AtomPtr& atom /*= AtomPtr()*/)
: atom(atom),inHead(0), inBody(0)
{
//
// TODO: here, we increase the nodecounter and assign it to the node id.
// can we be sure that every time a new node is created - and only then! -
// this constructor is called?
//
nodeId = nodeCount++;
}
void AtomNode::setHead()
{
inHead = 1;
}
void AtomNode::setBody()
{
inBody = 1;
}
bool AtomNode::isHead() const
{
return inHead;
}
bool AtomNode::isBody() const
{
return inBody;
}
void AtomNode::addPreceding(const Dependency& dep)
{
rules.clear(); // start creating rules in AtomNode::getRules
if(dep.isPositive())
pospreceding.insert(dep);
/*
if(dep.getType() == Dependency::DISJUNCTIVE)
disjpreceding.insert(dep);
*/
preceding.insert(dep);
}
void AtomNode::addSucceeding(const Dependency& dep)
{
succeeding.insert(dep);
}
const AtomPtr& AtomNode::getAtom() const
{
return atom;
}
const std::set<Dependency>& AtomNode::getPreceding() const
{
return preceding;
}
const std::set<Dependency>& AtomNode::getPositivePreceding() const
{
return pospreceding;
}
/*
const std::set<Dependency>& AtomNode::getDisjunctivePreceding() const
{
return disjpreceding;
}
*/
const std::set<Dependency>& AtomNode::getSucceeding() const
{
return succeeding;
}
const std::vector<RulePtr>& AtomNode::getRules() const
{
//
// only start the rule-creation machinery if this AtomNode is a
// head-node and the cache is still empty
//
if (this->rules.empty() && isHead()) // we call getRules for the very first time
{
typedef std::set<RulePtr> SetOfRules;
SetOfRules ruleset;
for (std::set<Dependency>::const_iterator d = getPreceding().begin();
d != getPreceding().end(); ++d)
{
Dependency::Type deptype = d->getType();
//
// if deptype is none of DISJ, PREC, or NEG_PREC, we
// skip it and continue our search for rule candidates
// only these types of dependency stem from actual rules
//
if (deptype & ~(Dependency::PRECEDING | Dependency::NEG_PRECEDING))
{
continue; // we only take care of head or body dependencies
}
// try to create a new rule in the ruleset
ruleset.insert(d->getRule());
}
// and now add the fresh rules to our own "rule cache"
std::copy(ruleset.begin(), ruleset.end(), std::inserter(this->rules, this->rules.begin()));
}
return this->rules;
}
unsigned AtomNode::getId() const
{
return nodeId;
}
std::ostream& operator<< (std::ostream& out, const AtomNode& atomnode)
{
out << atomnode.getId() << ": ";
out << *(atomnode.getAtom());
if (atomnode.getPreceding().size() > 0)
{
for (std::set<Dependency>::const_iterator d = atomnode.getPreceding().begin();
d != atomnode.getPreceding().end();
++d)
{
out << std::endl << " depends on: " << *d;
}
}
if (atomnode.getSucceeding().size() > 0)
{
for (std::set<Dependency>::const_iterator d = atomnode.getSucceeding().begin();
d != atomnode.getSucceeding().end();
++d)
{
out << std::endl << " dependents: " << *d;
}
}
if (atomnode.getPositivePreceding().size() > 0)
{
for (std::set<Dependency>::const_iterator d = atomnode.getPositivePreceding().begin();
d != atomnode.getPositivePreceding().end();
++d)
{
out << std::endl << " depends positively on: " << *d;
}
}
/*
* let each dependency dump its rule separately
*
std::vector<Rule*> rules = atomnode.getRules();
if (rules.size() > 0)
out << std::endl << " rules:";
for (std::vector<Rule*>::const_iterator ri = rules.begin(); ri != rules.end(); ++ri)
{
out << " " << *(*ri);
}
*/
return out;
}
Dependency::Dependency()
:type(Dependency::PRECEDING)
{
}
Dependency::Dependency(const Dependency& dep2)
: atomNode(dep2.atomNode), type(dep2.type), rule(dep2.rule)
{
}
Dependency::Dependency(RulePtr r, const AtomNodePtr& an, Type t)
: atomNode(an), type(t), rule(r)
{
}
Dependency::Type Dependency::getType() const
{
return type;
}
const AtomNodePtr& Dependency::getAtomNode() const
{
assert(atomNode);
return atomNode;
}
RulePtr Dependency::getRule() const
{
return rule;
}
void Dependency::addDep(RulePtr rule, const AtomNodePtr& from, const AtomNodePtr& to, Dependency::Type type)
{
Dependency dep1(rule, from, type);
Dependency dep2(rule, to, type);
from->addSucceeding(dep2);
to->addPreceding(dep1);
}
bool Dependency::operator< (const Dependency& dep2) const
{
Rule r1 = *(this->rule);
Rule r2 = *(dep2.rule);
if (r1 < r2)
return true;
if (r1 > r2)
return false;
if (this->atomNode->getId() < dep2.atomNode->getId())
return true;
if (this->atomNode->getId() > dep2.atomNode->getId())
return false;
if (this->type < dep2.type)
return true;
if (this->type > dep2.type)
return false;
return false;
}
std::ostream& operator<< (std::ostream& out, const Dependency& dep)
{
out << *(dep.getAtomNode()->getAtom());
out << " [";
switch (dep.getType())
{
/*
case Dependency::UNIFYING:
out << "unifying";
break;
*/
case Dependency::PRECEDING:
out << "head-body";
break;
case Dependency::NEG_PRECEDING:
out << "head-body NAF";
break;
/*
case Dependency::DISJUNCTIVE:
out << "disjunctive";
break;
*/
default:
assert(0);
break;
}
out << "]";
if (dep.getRule() != 0)
out << " rule: " << *(dep.getRule());
return out;
}
NodeGraph::~NodeGraph()
{
/*
for (std::vector<AtomNodePtr>::const_iterator an = atomNodes.begin();
an != atomNodes.end();
++an)
{
delete *an;
}
*/
}
const std::vector<RulePtr>& NodeGraph::getProgram() const
{
if (this->prog.empty())
{
std::set<RulePtr> ruleset;
for (std::vector<AtomNodePtr>::const_iterator it = atomNodes.begin();
it != atomNodes.end(); ++it)
{
std::vector<RulePtr> rules = (*it)->getRules();
std::copy(rules.begin(), rules.end(), std::inserter(ruleset, ruleset.begin()));
}
std::copy(ruleset.begin(), ruleset.end(), std::inserter(this->prog, this->prog.begin()));
}
return this->prog;
}
const std::vector<AtomNodePtr>& NodeGraph::getNodes() const
{
return atomNodes;
}
void NodeGraph::reset()
{
atomNodes.clear();
prog.clear();
}
/*
const AtomNodePtr
NodeGraph::getNode(unsigned nodeId)
{
for (std::vector<AtomNodePtr>::const_iterator an = atomNodes.begin();
an != atomNodes.end();
++an)
{
if ((*an)->getId() == nodeId)
return *an;
}
}
*/
AtomNodePtr NodeGraph::addNode()
{
//
// create node
//
AtomNodePtr newnode(new AtomNode);
//
// add the new node to the graph
//
atomNodes.push_back(newnode);
return newnode;
}
AtomNodePtr NodeGraph::addUniqueHeadNode(const AtomPtr& atom)
{
//
// does a node with exactly this atom already exist?
// (same predicate, same arguments)
//
//AtomNodePtr newnode = findNode(atom);
AtomNodePtr newnode;
findNode(atom, newnode);
if (newnode.use_count() == 0)
{
//
// no - create node
//
// TODO: still leaks!
newnode = AtomNodePtr(new AtomNode(atom));
//std::cout << "new headnode: " << *(newnode->getAtom()) << std::endl;
//
// add the new node to the graph
//
atomNodes.push_back(newnode);
}
//
// if the node occurred in a head the first time (or was just created - in
// this case it is neither a head nor a body node yet), we have to update
// dependencies by looking through the existing nodes
//
// FFASP: since we only deal with ground atoms at this point,
// we don't need this check
/*
if (!(newnode->isHead()))
{
//std::cout << "headnode: " << *(newnode->getAtom()) << std::endl;
//
// search all existing nodes for an atom that unifies
// with this new one - then we can add the unifying-dependency to both
for (std::vector<AtomNodePtr>::const_iterator oldnode = atomNodes.begin();
oldnode != atomNodes.end();
++oldnode)
{
//
// if the node already existed (as a body atom), we might encounter
// it here again - test for equality
// if equal, take next one
//
if (*oldnode == newnode)
continue;
if ((*oldnode)->getAtom()->unifiesWith(atom))
{
//
// in this function, we only search for existing BODY atoms!
//
if ((*oldnode)->isBody())
{
//
// add only one dependency: from the new node to the
// existing node. The existing node is a body atom and the
// new one is obviously a head atom (otherwise we would not
// be in that function), so the dependency goes from the
// head into the body.
//
///@TODO:
// is this rule-id correct?
Dependency dep1(RulePtr(new Rule()), *oldnode, Dependency::UNIFYING);
Dependency dep2(RulePtr(new Rule()), newnode, Dependency::UNIFYING);
(*oldnode)->addPreceding(dep2);
newnode->addSucceeding(dep1);
//std::cout << " unifies with " << *((*oldnode)->getAtom()) << std::endl;
}
}
}
}
*/
//
// wherever this node occured before - now it is a head node!
//
newnode->setHead();
return newnode;
}
AtomNodePtr NodeGraph::addUniqueBodyNode(const AtomPtr& atom)
{
//
// does a node with exactly this atom already exist?
// (same predicate, same arguments)
//
//std::cout << "==trying to add bodynode: " << *atom << std::endl;
AtomNodePtr newnode;
findNode(atom, newnode);
if (newnode.use_count() == 0)
{
//
// no - create node
//
// TODO: still leaks!
newnode = AtomNodePtr(new AtomNode(atom));
//std::cout << "new bodynode: " << *(newnode->getAtom()) << std::endl;
//
// set this node to be a body node - but only if we just created it!
//
newnode->setBody();
//
// search for all existing nodes if an atom exists that unifies
// with this new one - then we can add the unifying-dependency to both
//// FFASP: since we only deal with ground atoms at this point,
// we don't need this check
/*
for (std::vector<AtomNodePtr>::const_iterator oldnode = atomNodes.begin();
oldnode != atomNodes.end();
++oldnode)
{
if ((*oldnode)->getAtom()->unifiesWith(atom))
{
//
// in this function, we only search for existing HEAD atoms!
//
if ((*oldnode)->isHead())
{
//
// add only one dependency: from the existing node to the
// new node. The existing node is a head atom and the new
// one is obviously a body atom (otherwise we would not be
// in that function), so the dependency goes from the head
// into the body.
//
///@todo is this rule-id correct?
Dependency dep1(RulePtr(new Rule()), *oldnode, Dependency::UNIFYING);
Dependency dep2(RulePtr(new Rule()), newnode, Dependency::UNIFYING);
(*oldnode)->addSucceeding(dep2);
newnode->addPreceding(dep1);
//std::cout << " unifies with " << *((*oldnode)->getAtom()) << std::endl;
}
}
}
*/
//
// add the new node to the graph
//
atomNodes.push_back(newnode);
}
return newnode;
}
void NodeGraph::findNode(const AtomPtr& atom, AtomNodePtr& ptr) const
{
//
// test if atom does already exist as an AtomNode and return its pointer, if
// this is the case
//
for (std::vector<AtomNodePtr>::const_iterator an = atomNodes.begin();
an != atomNodes.end();
++an)
{
if (atom->equals((*an)->getAtom()))
//if (*atom == *(*an)->getAtom())
{
ptr = *an;
return;
}
}
}