-
Notifications
You must be signed in to change notification settings - Fork 0
/
AF.cpp
226 lines (195 loc) · 6.44 KB
/
AF.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
/**
* @file AF.cpp
* @class AF
* @brief Class encompassing an Argumentation Framework
* @author Mauro Vallati <[email protected]>
* @copyright GNU Public License v2
*/
#include "AF.h"
/**
* @brief Simple constructor
*/
AF::AF()
{
this->arguments = new SetArguments();
//this->raw_attacks = map<int, int >();
//this->attacks = map<Argument *, SetArguments *>();
//this->attackers = map<Argument *, SetArguments *>();
}
AF::~AF()
{
// TODO Auto-generated destructor stub
}
/**
* @brief Method for parsing a ASPARTIX compliant file
* @details It uses part of the code from Samer Nofal's, University of Liverpool
* @param[in] file A string representing the (relative/absolute) path to the ASPARTIX compliant file
* @retval bool It returns `True` if it can correctly parse the file, `False` otherwise
*/
bool AF::readFile(string file)
{
string inLine;
ifstream infile;
infile.open(file.c_str());
if (!infile.good())
return false;
infile >> inLine;
while (inLine.find("arg") != string::npos && !infile.eof())
{
this->arguments->add_Argument(new Argument(
inLine.substr(4, inLine.find_last_of(")") - 4),
this->numArgs(),
this
));
infile >> inLine;
}
while (!infile.eof())
{
if (inLine.find("att") != string::npos)
{
Argument *source = this->getArgumentByName(
(inLine.substr(4,
inLine.find_last_of(",") - 4)));
Argument *target = this->getArgumentByName(
(inLine.substr(inLine.find_last_of(",") + 1,
inLine.find_last_of(")")
- inLine.find_last_of(",")
- 1)));
source->add_attacks(target);
target->add_attackers(source);
// this->raw_attacks.push_back(
// pair<int, int>(
// this->argNameToCode(
// (inLine.substr(4,
// inLine.find_last_of(",") - 4))),
// this->argNameToCode(
// (inLine.substr(inLine.find_last_of(",") + 1,
// inLine.find_last_of(")")
// - inLine.find_last_of(",")
// - 1)))));
}
infile >> inLine;
}
infile.close();
return true;
}
/**
* @brief This method returns the pointer to the Argument whose name is given as parameter
* @param[in] name The name of the argument
* @retval Argument* The pointer to the Argument object, or NULL if not found
*/
Argument *AF::getArgumentByName(string name)
{
return this->arguments->getArgumentByName(name);
}
/**
* @brief Returns the number of arguments
* @retval int
*/
int AF::numArgs()
{
return this->arguments->cardinality();
}
/**
* @brief This method returns the pointer to the Argument whose identification number is given as parameter
* @param[in] num The name of the argument
* @retval Argument* The pointer to the Argument object, or NULL if not found
*/
Argument *AF::getArgumentByNumber(int num)
{
return this->arguments->getArgumentByNumber(num);
}
/**
* @brief Begin of the iterator for the set of arguments
* @retval SetArgumentsIterator An iterator pointing at the first of the elements of the set of arguments
*/
SetArgumentsIterator AF::begin()
{
return this->arguments->begin();
}
/**
* @brief End of the iterator for the set of arguments
* @retval SetArgumentsIterator An iterator pointing at the last of the elements of the set of arguments
*/
SetArgumentsIterator AF::end()
{
return this->arguments->end();
}
/**
* @brief Returns a pointer to the set of arguments
* @retval SetArguments*
*/
SetArguments *AF::get_arguments() const
{
return this->arguments;
}
/**
* @brief Create a new AF, with a copy of SetArguments of the AF in input
* @param[in] AF to copy
*/
AF::AF(const AF& gamma){
this->arguments=new SetArguments(*gamma.get_arguments());
}
/**
* @brief Method for reduce an AF on a Set of Arguments
* @details The method reduce the SetArguments of this AF, considering only the Arguments which are in the SetArguments I.
The suspended attacks are removed from the reduced AF
* @param[in] The SetArguments which is used to reduce the AF
* @retval Returns a new AF, the reduced one
*/
AF AF::reduceAF(SetArguments I){
int index=0;
AF *gamma_reduced = new AF();
gamma_reduced->arguments = new SetArguments();
SetArgumentsIterator yt = gamma_reduced->arguments->begin();
// Iteration on I
for(SetArgumentsIterator it = I.begin(); it != I.end(); it++, yt++){
// New Argument, which we have to insert in gamma_reduced
Argument *toAdd=new Argument(**it, gamma_reduced, index);
//cout << "current argument: "<<(*it)->getName()<<endl<<"old af: "<<*(*it)->get_af() <<endl;
++index;
// attacks is the SetArguments which we use to iterate, we remove the suspended attacks from attacks1
SetArguments * attacks = new SetArguments(*(*it)->get_attacks());
SetArguments * attacks1 = new SetArguments(*(*it)->get_attacks());
for(SetArgumentsIterator jt = attacks->begin(); jt != attacks->end(); jt++){
if(!I.exists(*jt))
attacks1->remove(*jt);
}
// set attacks1 as the set of attacks of the argument to add in the reducedAF
toAdd->set_attacks(attacks1);
// the same of attacks
SetArguments * attackers = new SetArguments(*(*it)->get_attackers());
SetArguments * attackers1 = new SetArguments(*(*it)->get_attackers());
for(SetArgumentsIterator jt = attackers->begin(); jt != attackers->end(); jt++){
if(!I.exists(*jt))
attackers1->remove(*jt);
}
toAdd->set_attackers(attackers1);
// add of the new argument in the reducedAF
gamma_reduced->arguments->add_Argument(toAdd);
}
// iterate on the reduced_AF, necessary to adjust the index number of the new set of Argument
SetArguments * args_gamma = gamma_reduced->get_arguments();
for(SetArgumentsIterator it = args_gamma->begin(); it != args_gamma->end(); it++){
// take the set of attacks based on the old Argument and adjust it with the new Arguments which stay in gamma_reduced
SetArguments * attacks = (*it)->get_attacks();
SetArguments * new_attacks = attacks->adjust_set(args_gamma);
(*it)->set_attacks(new_attacks);
// the same of attacks
SetArguments * attackers = (*it)->get_attackers();
SetArguments * new_attackers = attackers->adjust_set(args_gamma);
(*it)->set_attackers(new_attackers);
}
return *gamma_reduced;
}
/**
* @brief Method for printing an AF
*/
ostream& operator<<(ostream& out, const AF& framework){
SetArguments * argomenti=framework.get_arguments();
out <<*argomenti<<endl;
for(SetArgumentsIterator it=argomenti->begin();it!=argomenti->end();it++){
out << (*it)->getName() << " attacks: " << *(*it)->get_attacks() << " attackers: "<< *(*it)->get_attackers()<<endl;
}
return out;
}