src/PredFinder/macros/RoleParameter.cpp
/**
 * Reads the role parameter in some operator
 * context from the specified S-expression.
 *
 * @param sexp The read S-expression.
 *
 * @author nward@bbn.com
 * @date 2010.10.06
 **/
Reads the role parameter in some operator
context from the specified S-expression.

@param sexp The read S-expression.


Reads the role parameter in some operator context from the specified S - expression . @param sexp The read S - expression .
RoleParameter::RoleParameter(Sexp* sexp) : ReadingMacroParameter(sexp) {
	std::stringstream error;
	if (sexp->getNumChildren() < 2 || sexp->getNumChildren() > 5) {
		error << "Ill-formed role parameter: need 2-5 top-level nodes; got " << sexp->getNumChildren();
		error << " " << sexp->to_debug_string();
		throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
	}
	if (sexp->getFirstChild()->isList() || sexp->getFirstChild()->getValue() !=  ReadingMacroParameter::ROLE_SYM) {
		error << "Ill-formed role parameter: need role expression; got";
		error << " " << sexp->getFirstChild()->to_debug_string();
		throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
	}
	int child = 1;
	if (_shortcut_name != Symbol())
		child++;
	if (sexp->getNthChild(child)->isAtom()) {
		_input_role_name = sexp->getNthChild(child)->getValue().to_string();
	} else {
		error << "Ill-formed role parameter: need predicate input role atom; got ";
		error << " " << sexp->getNthChild(child)->to_debug_string();
		throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
	}
	_optional = false;
	_delete = false;
	for (child++; child < sexp->getNumChildren(); child++) {
		if ((child == sexp->getNumChildren() - 2 && sexp->getNthChild(sexp->getNumChildren() - 1)->isList()) ||
			(child == sexp->getNumChildren() - 1 && sexp->getNthChild(sexp->getNumChildren() - 1)->isAtom())) {
			if (sexp->getNthChild(child)->isAtom()) {
				_output_role_name = sexp->getNthChild(child)->getValue().to_string();
			} else {
				error << "Ill-formed role parameter: need predicate output role atom; got ";
				error << " " << sexp->getNthChild(child)->to_debug_string();
				throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
			}
		} else if (child == sexp->getNumChildren() - 1 && sexp->getNthChild(child)->isList()) {
			Sexp* skolem_sexp = sexp->getNthChild(child);
			bool is_valid(false);
			if (skolem_sexp->getNumChildren() == 1 && skolem_sexp->getFirstChild()->isAtom()) {
				if (skolem_sexp->getFirstChild()->getValue() == OPTIONAL_SYM) {
					_optional = true;
					is_valid = true;
				} else if (skolem_sexp->getFirstChild()->getValue() == DELETE_SYM) {
					_delete = true;
					is_valid = true;
				}
			} else if (skolem_sexp->getNumChildren() == 2 && skolem_sexp->getFirstChild()->isAtom() && skolem_sexp->getFirstChild()->getValue() == SKOLEM_SYM) {
				if (skolem_sexp->getSecondChild()->isAtom()) {
					_optional = true;
					_skolem_type = skolem_sexp->getSecondChild()->getValue().to_string();
					is_valid = true;
				} else {
					error << "Ill-formed role parameter: need skolem type atom; got ";
					error << " " << skolem_sexp->to_debug_string();
					throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
				}
			} 
			if (!is_valid) {
				error << "Ill-formed role parameter: need 1-node 'optional'/'delete' or 2-node 'skolem' expression; "
				      << "got " << skolem_sexp->getNumChildren();
				error << " " << skolem_sexp->to_debug_string();
				throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
			}
		} else {
			error << "Ill-formed role parameter: need predicate output role atom and/or 'optional'/'delete'/'skolem' expression; got ";
			error << " " << sexp->getNthChild(child)->to_debug_string();
			throw UnexpectedInputException("RoleParameter::RoleParameter(Sexp*)", error.str().c_str());
		}
	}
	if (_output_role_name == L"")
		_output_role_name = _input_role_name;
}
RoleParameter :: RoleParameter ( Sexp * sexp ) : ReadingMacroParameter ( sexp ) { std :: stringstream error ; if ( sexp -> getNumChildren ( ) < 2 || sexp -> getNumChildren ( ) > 5 ) { error << " " << sexp -> getNumChildren ( ) ; error << " " << sexp -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } if ( sexp -> getFirstChild ( ) -> isList ( ) || sexp -> getFirstChild ( ) -> getValue ( ) != ReadingMacroParameter :: ROLE_SYM ) { error << " " ; error << " " << sexp -> getFirstChild ( ) -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } int child = 1 ; if ( _shortcut_name != Symbol ( ) ) child ++ ; if ( sexp -> getNthChild ( child ) -> isAtom ( ) ) { _input_role_name = sexp -> getNthChild ( child ) -> getValue ( ) . to_string ( ) ; } else { error << " " ; error << " " << sexp -> getNthChild ( child ) -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } _optional = false ; _delete = false ; for ( child ++ ; child < sexp -> getNumChildren ( ) ; child ++ ) { if ( ( child == sexp -> getNumChildren ( ) - 2 && sexp -> getNthChild ( sexp -> getNumChildren ( ) - 1 ) -> isList ( ) ) || ( child == sexp -> getNumChildren ( ) - 1 && sexp -> getNthChild ( sexp -> getNumChildren ( ) - 1 ) -> isAtom ( ) ) ) { if ( sexp -> getNthChild ( child ) -> isAtom ( ) ) { _output_role_name = sexp -> getNthChild ( child ) -> getValue ( ) . to_string ( ) ; } else { error << " " ; error << " " << sexp -> getNthChild ( child ) -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } } else if ( child == sexp -> getNumChildren ( ) - 1 && sexp -> getNthChild ( child ) -> isList ( ) ) { Sexp * skolem_sexp = sexp -> getNthChild ( child ) ; bool is_valid ( false ) ; if ( skolem_sexp -> getNumChildren ( ) == 1 && skolem_sexp -> getFirstChild ( ) -> isAtom ( ) ) { if ( skolem_sexp -> getFirstChild ( ) -> getValue ( ) == OPTIONAL_SYM ) { _optional = true ; is_valid = true ; } else if ( skolem_sexp -> getFirstChild ( ) -> getValue ( ) == DELETE_SYM ) { _delete = true ; is_valid = true ; } } else if ( skolem_sexp -> getNumChildren ( ) == 2 && skolem_sexp -> getFirstChild ( ) -> isAtom ( ) && skolem_sexp -> getFirstChild ( ) -> getValue ( ) == SKOLEM_SYM ) { if ( skolem_sexp -> getSecondChild ( ) -> isAtom ( ) ) { _optional = true ; _skolem_type = skolem_sexp -> getSecondChild ( ) -> getValue ( ) . to_string ( ) ; is_valid = true ; } else { error << " " ; error << " " << skolem_sexp -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } } if ( ! is_valid ) { error << " " << " " << skolem_sexp -> getNumChildren ( ) ; error << " " << skolem_sexp -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } } else { error << " " ; error << " " << sexp -> getNthChild ( child ) -> to_debug_string ( ) ; throw UnexpectedInputException ( " " , error . str ( ) . c_str ( ) ) ; } } if ( _output_role_name == L" " ) _output_role_name = _input_role_name ; }
Reads the role parameter in some operator
 context from the specified S-expression.
Reads the role parameter in some operator context from the specified S - expression .
// Local error messages
 // Make sure we have an S-expression worth reading
 // Make sure this is a role expression
 // Check if we need to start reading subexpressions after a shortcut expression
 // Get the input role name specification
 // Not optional unless we see otherwise
 // Do not delete unless we see otherwise
 // Loop over the remaining children
 //   This reduces duplicated code, but makes the if checks for each "slot" kinda weird
 // Check if we're looking for an output role
 // Make sure this is a valid 'optional'/'delete'/'skolem' type specification
 // Input role is optional
 // Generate skolem with specified type if input role not present
 // Set the output role to identity if there was no rename
