Java Code Examples for edu.cornell.cs.nlp.spf.mr.lambda.Lambda#getBody()

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.Lambda#getBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetMapping.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(LambdaNode node) {
	final Lambda resultLambda = (Lambda) currentResult;

	// Visit the argument.
	currentResult = resultLambda.getArgument();
	node.getArgument().accept(this);

	if (fail) {
		return;
	}

	// Visit the body.
	currentResult = resultLambda.getBody();
	node.getBody().accept(this);
}
 
Example 2
Source File: DummyEntityServices.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a lambda term (e.g., (lambda $0:e (and:<t*,t> ...))), return 'true'
 * iff its body contains relation literals and at least one of them has a
 * dummy entity.
 */
public static boolean hasDummyEntity(Lambda lambda) {
	if (lambda.getBody() instanceof Literal) {
		final Literal bodyLiteral = (Literal) lambda.getBody();
		if (LogicLanguageServices.getConjunctionPredicate().equals(
				bodyLiteral.getPredicate())) {
			final int len = bodyLiteral.numArgs();
			for (int i = 0; i < len; ++i) {
				final LogicalExpression arg = bodyLiteral.getArg(i);
				if (arg instanceof Literal) {
					if (isRelationWithDummy((Literal) arg)) {
						return true;
					}
				}
			}
		} else if (isRelationWithDummy(bodyLiteral)) {
			return true;
		}
	}
	return false;

}
 
Example 3
Source File: Evaluation.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decomposes a logical expression as a SELECT query.
 *
 * @param exp
 * @return Pair of queried variables and SELECT body. If not a SELECT query,
 *         returns null.
 */
private static Pair<List<Variable>, LogicalExpression> decomposeLogicalExpressionAsSelect(
		LogicalExpression exp) {
	LogicalExpression currentBody = exp;
	final List<Variable> queryVariables = new LinkedList<Variable>();
	while (currentBody instanceof Lambda) {
		final Lambda lambda = (Lambda) currentBody;
		if (lambda.getArgument().getType().isComplex()) {
			// Case argument is complex
			return null;
		} else {
			queryVariables.add(lambda.getArgument());
			currentBody = lambda.getBody();
		}
	}

	if (currentBody.getType().isComplex()) {
		return null;
	} else {
		return Pair.of(queryVariables, currentBody);
	}
}
 
Example 4
Source File: FactoringServices.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(Lambda lambda) {
	// not visiting argument, since we are only abstracting constants.
	lambda.getBody().accept(this);
	final ListIterator<Pair<Placeholders, ? extends LogicalExpression>> iterator = tempReturn
			.listIterator();
	while (iterator.hasNext()) {
		final Pair<Placeholders, ? extends LogicalExpression> pair = iterator
				.next();
		if (pair.second() != null) {
			final LogicalExpression newBody = pair.second();
			if (newBody == lambda.getBody()) {
				iterator.set(Pair.of(pair.first(), lambda));
			} else {
				iterator.set(Pair.of(pair.first(),
						new Lambda(lambda.getArgument(), newBody)));
			}

		}
	}
}
 
Example 5
Source File: AMRServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the unary typing predicate (if one exists) from a <e,t>-typed lambda
 * term (the body of a skolem term).
 */
public static LogicalConstant getTypingPredicate(Lambda set) {
	if (set.getBody() instanceof Literal) {
		final Literal bodyLiteral = (Literal) set.getBody();
		final int len = bodyLiteral.numArgs();
		if (bodyLiteral.getPredicate()
				.equals(LogicLanguageServices.getConjunctionPredicate())) {
			return getTypingPredicateFromConjunction(bodyLiteral);
		} else if (len == 1
				&& bodyLiteral.getPredicate() instanceof LogicalConstant) {
			return (LogicalConstant) bodyLiteral.getPredicate();
		}
	}
	return null;
}
 
Example 6
Source File: AMRServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isNamedEntityBody(Lambda entityBody) {
	if (entityBody.getComplexType().getDomain()
			.equals(LogicLanguageServices.getTypeRepository()
					.getEntityType())
			&& entityBody.getComplexType().getRange()
					.equals(LogicLanguageServices.getTypeRepository()
							.getTruthValueType())
			&& entityBody.getBody() instanceof Literal) {
		final Literal bodyLiteral = (Literal) entityBody.getBody();
		if (bodyLiteral.getPredicate()
				.equals(LogicLanguageServices.getConjunctionPredicate())) {
			final int len = bodyLiteral.numArgs();
			for (int i = 0; i < len; ++i) {
				final LogicalExpression arg = bodyLiteral.getArg(i);
				if (arg instanceof Literal && ((Literal) arg).numArgs() == 2
						&& ((Literal) arg).getArg(1) instanceof Literal
						&& isSkolemTerm(
								(Literal) ((Literal) arg).getArg(1))) {
					if (INSTANCE.nameInstancePredicate
							.equals(getTypingPredicate(
									(Literal) ((Literal) arg).getArg(1)))) {
						return true;
					}
				}
			}
		}
	}
	return false;
}
 
Example 7
Source File: AToExists.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Lambda lambda) {
	lambda.getBody().accept(this);
	if (result.first() != lambda.getBody()) {
		// Case body changed
		result = Pair.of(new Lambda(lambda.getArgument(), result.first()),
				result.second());
	} else {
		result = Pair.of(lambda, result.second());
	}
}
 
Example 8
Source File: GetApplicationFunction.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Lambda lambda) {
	if (isDirectlyMatched(lambda)) {
		return;
	}

	if (!(argument instanceof Lambda)) {
		result = false;
		return;
	}

	final Lambda argLambda = (Lambda) argument;

	// Update the variable scope mapping.
	scope.push(lambda.getArgument(), argLambda.getArgument());

	// If the variable of argLambda is used in the stripped variables
	// mapping, we need to remove it from there, since that mapping
	// relies
	// on an unscoped instance.
	final boolean removedFromStrippedVariables;
	final LogicalExpression storedValue;
	if (externalVariableMapping.containsKey(argLambda.getArgument())) {
		removedFromStrippedVariables = true;
		storedValue = externalVariableMapping.get(argLambda
				.getArgument());
	} else {
		removedFromStrippedVariables = false;
		storedValue = null;
	}

	// Visit the body.
	argument = argLambda.getBody();
	lambda.getBody().accept(this);

	if (removedFromStrippedVariables) {
		// Restore mapping, if changed.
		externalVariableMapping.put(argLambda.getArgument(),
				storedValue);
	}
}