Skip to content

Commit

Permalink
Added AF-adapting functions
Browse files Browse the repository at this point in the history
+ Added adaptTo for SetArguments
+ Added adaptTo for Labelling
~ Changed temporary prefSAT solution with a call to adaptTo
+ Added calls to adaptTo in places where it should be useful
  • Loading branch information
Denaun committed Jul 7, 2013
1 parent 3f49b8b commit 057dc9e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
32 changes: 32 additions & 0 deletions src/Labelling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@ bool Labelling::empty()
return this->labelling.empty() && this->in.empty() && this->out.empty() && this->undec.empty();
}

/**
* @brief Adapts the labels of `this` Labelling to the AF.
* #details
* Substitutes the elements matching by name and not by number.
*
* @param[in] other The AF source of the substitution arguments.
*/
void Labelling::adaptTo( AF* other )
{
// Have to iterate over the map
// because there's no way to access our elements otherwise
for ( map<Argument*, Label>::iterator it = this->labelling.begin();
it != this->labelling.end(); ++it )
{
// No try-catches because every element of the labelling should be inside the new AF
Argument* victim = other->getArgumentByName( it->first->getName() );

// Substitute the Label if the victim is changed
if ( it->first != victim )
{
swap( this->labelling[ victim ], it->second );

this->labelling.erase( it );
}
}

// Also adapt the internal SetArguments
in.adaptTo( other );
out.adaptTo( other );
undec.adaptTo( other );
}

/**
* @brief Clone this labelling into a new one
* @param[out] other A pointer to a Labelling which will be the clone of this one
Expand Down
1 change: 1 addition & 0 deletions src/Labelling.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Labelling
SetArguments *outargs();
SetArguments *undecargs();
bool empty();
void adaptTo( AF* );
void clone(Labelling *);
};

Expand Down
31 changes: 9 additions & 22 deletions src/Preferred_pref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void Preferred::pref( AF* theAF, SetArguments* theC )
Grounded( &e, &I );

if ( stages )
cerr << "e: " << e << endl << "I: " << I << endl;
cerr << "\te: " << e << endl << "\tI: " << I << endl;

// Convert Grounded's output into a Labelling
Labelling first = Labelling();
Expand Down Expand Up @@ -88,7 +88,7 @@ void Preferred::pref( AF* theAF, SetArguments* theC )
aLabelling != this->labellings.end(); ++aLabelling )
{
SetArguments O = SetArguments();
I = SetArguments(); // I already exists..
I = SetArguments(); // I already exists..

boundcond( *aSCC, (*aLabelling).inargs(), &O, &I );

Expand All @@ -103,25 +103,10 @@ void Preferred::pref( AF* theAF, SetArguments* theC )
AF restricted = AF();
this->af->restrictTo( *aSCC, &restricted );

// TODO: prefSAT problem: the nodes in I are different from
// prefSAT problem: the nodes in I are different from
// the nodes in the restricted AF.
// Temporary solution: rebuild I.
for ( SetArgumentsIterator it = restricted.begin(); it != restricted.end(); ++it )
{
try
{
Argument* victim = I.getArgumentByName( (*it)->getName() );

// No exception: the Argument is inside I
// Remove it and substitute it with the new one
I.remove( victim );
I.add_Argument( *it );
}
catch ( const std::out_of_range& oor )
{
// The Argument is outside of I. Nothing to do.
}
}
I.adaptTo( &restricted );

// Should be done iff I != Ø (doesn't prefSAT return Ø otherwise..?)
p.prefSAT( &restricted, &I );
Expand All @@ -135,7 +120,9 @@ void Preferred::pref( AF* theAF, SetArguments* theC )
(*aSCC)->setminus( &O, &restriction );
AF restricted = AF();
this->af->restrictTo( &restriction, &restricted );
// TODO? the same as above for prefSAT.

// The same as above for prefSAT.
I.adaptTo( &restricted );

p.pref( &restricted, &I );
}
Expand All @@ -151,9 +138,9 @@ void Preferred::pref( AF* theAF, SetArguments* theC )
if ( debug )
cerr << "\t\tFound " << *((*EStar).inargs()) << endl;

// TODO: Rebuild the Labelling using the original Arguments
// Rebuild the Labelling using the original Arguments
// Not doing so could cause problems to next boundconds
// Currently not possible due to the structure of Labelling
(*EStar).adaptTo( theAF );

(*aLabelling).clone( &(*EStar) );

Expand Down
27 changes: 27 additions & 0 deletions src/SetArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,33 @@ void SetArguments::intersect(SetArguments *other, SetArguments *res)
}
}

/**
* @brief Adapts the elements of `this` set to the AF, substituting the matching elements.
* @details
* *NOTE*: This method changes the structure of the SetArguments on which it is called.
*
* @param[in] other The AF source of the substitution arguments.
*/
void SetArguments::adaptTo( AF* other )
{
for ( SetArgumentsIterator it = other->begin(); it != other->end(); ++it )
{
try
{
Argument* victim = this->getArgumentByName( (*it)->getName() );

// No exception: the Argument is inside this set
// Remove it and substitute it with the new one
this->remove( victim );
this->add_Argument( *it );
}
catch ( const out_of_range& oor )
{
// The Argument is outside this set. Nothing to do.
}
}
}

/**
* @brief Overloading of the == operator
* @param[in] other The other term of the comparison
Expand Down
2 changes: 2 additions & 0 deletions src/SetArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <iterator>
#include <algorithm>
#include <stdexcept>
using namespace std;

#include "Argument.h"
Expand Down Expand Up @@ -50,6 +51,7 @@ class SetArguments
bool is_subset(SetArguments *);
void intersect(SetArguments *, SetArguments *);
void setminus(SetArguments *, SetArguments *);
void adaptTo( AF* );
void remove(Argument *);
void clone(SetArguments *);
bool operator==(const SetArguments &other) const;
Expand Down

0 comments on commit 057dc9e

Please sign in to comment.