diff --git a/src/Preferred_pref.cpp b/src/Preferred_pref.cpp index 34b0dc5..b37fdcc 100644 --- a/src/Preferred_pref.cpp +++ b/src/Preferred_pref.cpp @@ -33,10 +33,22 @@ void Preferred::pref( AF* theAF, SetArguments* theC ) SetArguments e = SetArguments(), I = SetArguments(); - Grounded( &e, &I ); + // Avoid calling Grounded if useless + if ( theC->cardinality() <= 1 && *theC == *(theAF->get_arguments()) ) + { + if ( debug ) + cerr << "\tNo need to call Grounded.\n"; + + // Empty: no extensions => null iteration + // Singlet: still have to iterate over I + for ( SetArgumentsIterator it = theC->begin(); it != theC->end(); ++it ) + e.add_Argument( *it ); + } + else + 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(); @@ -88,7 +100,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 ); @@ -98,46 +110,93 @@ void Preferred::pref( AF* theAF, SetArguments* theC ) if ( O.empty() ) { if ( debug ) - cerr << "\tCalling prefSAT.\n"; - - AF restricted = AF(); - this->af->restrictTo( *aSCC, &restricted ); - - // TODO: 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 ) + cerr << "\tGoing to call prefSAT.\n"; + + // Avoid calling prefSAT if the two parameters are the same + // and the size is 2 or less: + // 0: out = { {} } + // 1: out = { {singlet} } + // 2: out = { {singlet}, {singlet} } + if ( (*aSCC)->cardinality() <= 2 && **aSCC == I ) { - try + if ( debug ) + cerr << "\t\tNo need to call prefSAT.\n"; + + // Empty: no extensions => null iteration + // Singlet: still have to iterate over I + // Two element: Mutually exclusive arguments + for ( SetArgumentsIterator it = (*aSCC)->begin(); + it != (*aSCC)->end(); ++it ) { - 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 ); + Labelling* temp = new Labelling(); + temp->add_label( *it, Labelling::lab_in ); + p.labellings.push_back( *temp ); } - catch ( const std::out_of_range& oor ) + } + else + { + AF restricted = AF(); + this->af->restrictTo( *aSCC, &restricted ); + + // TODO: 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 ) { - // The Argument is outside of I. Nothing to do. + 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. + } } - } - // Should be done iff I != Ø (doesn't prefSAT return Ø otherwise..?) - p.prefSAT( &restricted, &I ); + if ( debug ) + cerr << "\t\tCalling prefSAT.\n"; + + p.prefSAT( &restricted, &I ); + } } else { if ( debug ) - cerr << "\tCalling pref.\n"; + cerr << "\tGoing to call pref.\n"; SetArguments restriction = SetArguments(); (*aSCC)->setminus( &O, &restriction ); - AF restricted = AF(); - this->af->restrictTo( &restriction, &restricted ); - // TODO? the same as above for prefSAT. + if ( restriction.cardinality() <= 1 && restriction == I ) + { + if ( debug ) + cerr << "\t\tNo need to call pref.\n"; + + // Empty: no extensions => null iteration + // Singlet: still have to iterate over I + for ( SetArgumentsIterator it = I.begin(); + it != I.end(); ++it ) + { + Labelling* temp = new Labelling(); + temp->add_label( *it, Labelling::lab_in ); + p.labellings.push_back( *temp ); + } + } + else + { + if ( debug ) + cerr << "\tCalling pref.\n"; - p.pref( &restricted, &I ); + AF restricted = AF(); + this->af->restrictTo( &restriction, &restricted ); + // TODO? the same as above for prefSAT. + + p.pref( &restricted, &I ); + } } // prefSAT doesn't put newline at the end of its output...