-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotation_fact.cpp
156 lines (136 loc) · 5.16 KB
/
annotation_fact.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
#include <cassert>
#include <vector>
#include "annotation_fact.hpp"
using namespace std;
void annotation_fact::justification_hook() const {
for (const fact_annotatable*annotatable : get_annotatables()) {
annotatable->add_annotation(*this);
}
}
void annotation_fact::unjustification_hook() const {
for (const fact_annotatable*annotatable : get_annotatables()) {
annotatable->remove_annotation(*this);
}
}
annotation_fact::operator bool() const {
return get_annotatables().front()->has_annotation(*this);
}
void annotation_fact::justify() const {
for (const fact_annotatable*annotatable : get_annotatables()) {
if (annotatable->has_been_predeleted()) {
return;
}
}
fact::justify();
}
void negative_annotation_fact::justification_hook() const {
for (const fact_annotatable*annotatable : get_annotatables()) {
annotatable->remove_annotation(*this);
}
}
void negative_annotation_fact::unjustification_hook() const {
surreptitiously_make_false();
}
negative_annotation_fact::operator bool() const {
return !get_annotatables().front()->has_annotation(*this);
}
void negative_annotation_fact::surreptitiously_make_false() const {
for (const fact_annotatable*annotatable : get_annotatables()) {
annotatable->add_annotation(*this);
}
}
// These two look like they might be dangerous for reentrancy reasons, but in
// fact are safe because combined annotation facts are always observations, so
// the only possible callers are justify() and unjustify().
void combined_annotation_fact::justification_hook() const {
annotation_fact::justification_hook();
combined_annotation_fact*counterpart = const_cast<combined_annotation_fact*>(dynamic_cast<const combined_annotation_fact*>(clone()));
counterpart->in_the_positive_sense = !in_the_positive_sense;
if (*counterpart) {
counterpart->annotation_fact::unjustify();
}
counterpart->free_as_clone();
}
void combined_annotation_fact::unjustification_hook() const {
annotation_fact::unjustification_hook();
combined_annotation_fact*counterpart = const_cast<combined_annotation_fact*>(dynamic_cast<const combined_annotation_fact*>(clone()));
counterpart->in_the_positive_sense = !in_the_positive_sense;
if (!*counterpart) {
counterpart->annotation_fact::justify();
}
counterpart->free_as_clone();
}
bool combined_annotation_fact::is_in_the_positive_sense() const {
return in_the_positive_sense;
}
fact_annotatable::fact_annotatable() :
predeleted(false) {}
void fact_annotatable::add_annotation(const ::annotation&annotation) const {
annotatable::add_annotation(annotation);
const ::negative_annotation_fact*negative_annotation_fact = dynamic_cast<const ::negative_annotation_fact*>(&annotation);
if (negative_annotation_fact && negative_annotation_fact->is_observation()) {
annotations_iterator i = justified_negative_annotation_facts.find(type_index{typeid(annotation)});
if (i == justified_negative_annotation_facts.end()) {
return;
}
const_specific_annotations_iterator j = i->second.find(annotation);
if (j != i->second.end()) {
i->second.erase(j);
if (i->second.empty()) {
justified_negative_annotation_facts.erase(i);
}
}
}
}
void fact_annotatable::remove_annotation(const ::annotation&annotation) const {
const ::negative_annotation_fact*negative_annotation_fact = dynamic_cast<const ::negative_annotation_fact*>(&annotation);
if (negative_annotation_fact && negative_annotation_fact->is_observation()) {
justified_negative_annotation_facts[type_index{typeid(annotation)}].insert(annotation);
}
annotatable::remove_annotation(annotation);
}
bool fact_annotatable::has_been_predeleted() const {
return predeleted;
}
void fact_annotatable::predelete() {
assert(!predeleted);
predeleted = true;
vector<const annotation_fact*>positive_accumulator;
vector<const negative_annotation_fact*>negative_accumulator;
for (auto&i : annotations) {
for (const annotation_wrapper&j : i.second) {
const annotation_fact*fact = dynamic_cast<const annotation_fact*>(&static_cast<const annotation&>(j));
if (!fact || !fact->is_observation()) {
// Continue the outer loop, effectively advancing to the next type.
break;
}
positive_accumulator.push_back(dynamic_cast<const annotation_fact*>(fact->clone()));
}
}
for (auto&i : justified_negative_annotation_facts) {
for (const annotation_wrapper&j : i.second) {
const negative_annotation_fact*fact = dynamic_cast<const negative_annotation_fact*>(&static_cast<const annotation&>(j));
assert(fact);
negative_accumulator.push_back(dynamic_cast<const negative_annotation_fact*>(fact->clone()));
}
}
for (const annotation_fact*fact : positive_accumulator) {
fact->unjustify();
fact->free_as_clone();
}
for (const negative_annotation_fact*fact : negative_accumulator) {
fact->unjustify();
fact->free_as_clone();
}
}
ostream&fact_annotatable::dump(ostream&out) const {
for (auto&i : annotations) {
for (const annotation_wrapper&j : i.second) {
const annotation_fact*fact = dynamic_cast<const annotation_fact*>(&static_cast<const annotation&>(j));
if (fact) {
out << " " << *fact << endl;
}
}
}
return out;
}