* concepts
*/
public static ATermAppl nnf(ATermAppl term) {
ATermAppl newterm = null;
AFun af = term.getAFun();
if( af.equals( ATermUtils.NOTFUN ) ) { // Function is a NOT
// Take the first argument to the NOT, then check
// the type of that argument to determine what needs to be done.
ATermUtils.assertTrue( af.getArity() == 1 );
ATermAppl arg = (ATermAppl) term.getArgument( 0 );
af = arg.getAFun();
if( arg.getArity() == 0 ) {
newterm = term; // Negation is in as far as it can go
}
else if( af.equals( ATermUtils.NOTFUN ) ) { // Double negation.
newterm = nnf( (ATermAppl) arg.getArgument( 0 ) );
}
else if( af.equals( ATermUtils.VALUEFUN ) || af.equals( ATermUtils.SELFFUN ) || af.equals( ATermUtils.RESTRDATATYPEFUN ) ) {
newterm = term;
}
else if( af.equals( ATermUtils.MAXFUN ) ) {
ATermInt n = (ATermInt) arg.getArgument( 1 );
newterm = ATermUtils.makeMin( arg.getArgument( 0 ), n.getInt() + 1, nnf( arg
.getArgument( 2 ) ) );
}
else if( af.equals( ATermUtils.MINFUN ) ) {
ATermInt n = (ATermInt) arg.getArgument( 1 );
if( n.getInt() == 0 ) {
newterm = ATermUtils.BOTTOM;
}
else {
newterm = ATermUtils.makeMax( arg.getArgument( 0 ), n.getInt() - 1, nnf( arg
.getArgument( 2 ) ) );
}
}
else if( af.equals( ATermUtils.CARDFUN ) ) {
newterm = nnf( makeNot( makeExactCard( arg.getArgument( 0 ), ((ATermInt) arg
.getArgument( 1 )), arg.getArgument( 2 ) ) ) );
}
else if( af.equals( ATermUtils.ANDFUN ) ) {
newterm = ATermUtils.makeOr( nnf( negate( (ATermList) arg.getArgument( 0 ) ) ) );
}
else if( af.equals( ATermUtils.ORFUN ) ) {
newterm = ATermUtils.makeAnd( nnf( negate( (ATermList) arg.getArgument( 0 ) ) ) );
}
else if( af.equals( ATermUtils.SOMEFUN ) ) {
ATerm p = arg.getArgument( 0 );
ATerm c = arg.getArgument( 1 );
newterm = ATermUtils.makeAllValues( p, nnf( makeNot( c ) ) );
}
else if( af.equals( ATermUtils.ALLFUN ) ) {
ATerm p = arg.getArgument( 0 );
ATerm c = arg.getArgument( 1 );
newterm = ATermUtils.makeSomeValues( p, nnf( makeNot( c ) ) );
}
else {
throw new InternalReasonerException( "Unknown term type: " + term );
}
}
else if( af.equals( ATermUtils.MINFUN ) || af.equals( ATermUtils.MAXFUN )
|| af.equals( ATermUtils.SELFFUN ) ) {
newterm = term;
}
else if( af.equals( ATermUtils.CARDFUN ) ) {
newterm = nnf( makeExactCard( term.getArgument( 0 ),
((ATermInt) term.getArgument( 1 )), term.getArgument( 2 ) ) );
}
else {
// Return the term with all of its arguments in nnf