edu.cornell.cs.nlp.spf.mr.lambda.mapping.ScopeMapping Java Examples

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.mapping.ScopeMapping. 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: Variable.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected boolean doEquals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	if (!(exp instanceof Variable) || exp instanceof SkolemId) {
		return false;
	}

	final Variable mapValue = mapping.peek(this);
	if (mapValue == exp && mapping.peekValue(mapValue) == this) {
		// Comparison through mapping of variables.
		return true;
	} else if (!mapping.containsValue((Variable) exp)) {
		// Case both are not mapped, do instance comparison for free
		// variables.
		return exp == this;
	} else {
		// Not equal.
		return false;
	}
}
 
Example #2
Source File: GetApplicationArgumentTest.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCreateArg4() {
	final LogicalExpression resultSubExp = TestServices
			.getCategoryServices().readSemantics("(pred:<e,t> $0:e)");
	final LogicalExpression functionSubExp = TestServices
			.getCategoryServices().readSemantics("($1:<e,t> $0:e)");
	final Variable applicationArg = (Variable) ((Literal) functionSubExp)
			.getPredicate();
	final LogicalExpression expected = TestServices.getCategoryServices()
			.readSemantics("pred:<e,t>");
	final ScopeMapping<Variable, Variable> scope = new ScopeMapping<Variable, Variable>();
	scope.push((Variable) ((Literal) functionSubExp).getArg(0),
			(Variable) ((Literal) resultSubExp).getArg(0));
	Assert.assertEquals(expected, GetApplicationArgument.createArgument(
			resultSubExp, functionSubExp, applicationArg, scope));

}
 
Example #3
Source File: SkolemId.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	if (!(exp instanceof SkolemId)) {
		return false;
	}

	final Variable mappedValue = mapping.peek(this);
	if (mappedValue == exp && mapping.peekValue(mappedValue) == this) {
		return true;
	} else if (exp instanceof SkolemIdInstanceWrapper) {
		return exp.equals(this, mapping);
	} else if (!mapping.containsValue((SkolemId) exp)) {
		mapping.push(this, (SkolemId) exp);
		return true;
	} else {
		return false;
	}
}
 
Example #4
Source File: Lambda.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected boolean doEquals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	if (this == exp) {
		// Since skolem IDs from this literal may be used in other parts of
		// the logical form, we need to create a mapping of them. As the
		// instances are identical, we can just update the mapping by
		// creating a mapping from each SkolemId to itself.
		if (!freeVariables.isEmpty()) {
			for (final Variable freeVariable : freeVariables) {
				if (freeVariable instanceof SkolemId) {
					mapping.push(freeVariable, freeVariable);
				}
			}
		}
		return true;
	}
	if (getClass() != exp.getClass()) {
		return false;
	}
	final Lambda other = (Lambda) exp;
	if (!type.equals(other.type)) {
		return false;
	}

	if (argument.getType().equals(other.argument.getType())) {
		// If the types are equal and both are not null, add the mapping for
		// the comparison of the body.
		mapping.push(argument, other.argument);
	} else {
		return false;
	}

	final boolean ret = body.equals(other.body, mapping);

	// Remove mapping.
	mapping.pop(argument);

	return ret;
}
 
Example #5
Source File: SimpleLogicalExpressionReader.java    From UDepLambda with Apache License 2.0 5 votes vote down vote up
public static LogicalExpression read(String string) {
  TypeRepository typeRepository = LogicLanguageServices
      .getTypeRepository();
  ITypeComparator typeComparator = LogicLanguageServices
      .getTypeComparator();
  LogicalExpression exp =
      LogicalExpressionReader.INSTANCE.read(string, new ScopeMapping<>(), typeRepository,
          typeComparator);
  return Simplify.of(exp);
}
 
Example #6
Source File: GetApplicationArgumentTest.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateArg3() {
	final LogicalExpression resultSubExp = TestServices
			.getCategoryServices().readSemantics("(pred:<e,t> boo:e)");
	final LogicalExpression function = TestServices.getCategoryServices()
			.readSemantics("(lambda $0:<e,t> ($0 boo:e))");
	final Variable applicationArg = ((Lambda) function).getArgument();
	final LogicalExpression expected = TestServices.getCategoryServices()
			.readSemantics("pred:<e,t>");
	Assert.assertEquals(expected,
			GetApplicationArgument.createArgument(resultSubExp,
					((Lambda) function).getBody(), applicationArg,
					new ScopeMapping<Variable, Variable>()));

}
 
Example #7
Source File: GetApplicationArgumentTest.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateArg2() {
	final LogicalExpression resultSubExp = TestServices
			.getCategoryServices().readSemantics("boo:t");
	final Variable applicationArg = new Variable(
			LogicLanguageServices.getTypeRepository().getEntityType());
	Assert.assertEquals(null,
			GetApplicationArgument.createArgument(resultSubExp,
					applicationArg, applicationArg,
					new ScopeMapping<Variable, Variable>()));

}
 
Example #8
Source File: GetApplicationArgumentTest.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateArg1() {
	final LogicalExpression resultSubExp = TestServices
			.getCategoryServices().readSemantics("boo:e");
	final Variable applicationArg = new Variable(
			LogicLanguageServices.getTypeRepository().getEntityType());
	Assert.assertEquals(resultSubExp,
			GetApplicationArgument.createArgument(resultSubExp,
					applicationArg, applicationArg,
					new ScopeMapping<Variable, Variable>()));

}
 
Example #9
Source File: SkolemId.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SkolemId read(String string,
		ScopeMapping<String, LogicalExpression> mapping,
		TypeRepository typeRepository, ITypeComparator typeComparator,
		LogicalExpressionReader reader) {
	if (!mapping.containsKey(string)) {
		mapping.push(string, new SkolemId());
	}
	return (SkolemId) mapping.peek(string);
}
 
Example #10
Source File: Variable.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Variable read(String string,
		ScopeMapping<String, LogicalExpression> mapping,
		TypeRepository typeRepository, ITypeComparator typeComparator,
		LogicalExpressionReader reader) {

	try {
		final Pair<String, Variable> defintion = readVariableDefintion(
				string, typeRepository);
		if (defintion != null && !mapping.containsKey(string)) {
			mapping.push(defintion.first(), defintion.second());
			return defintion.second();
		} else if (defintion != null) {
			throw new LogicalExpressionRuntimeException(
					"Re-define a global variable: " + string);
		} else {
			// Case variable reference.
			if (mapping.containsKey(string)) {
				return (Variable) mapping.peek(string);
			} else {
				throw new LogicalExpressionRuntimeException(
						"Undefined variable reference: " + string);
			}
		}
	} catch (final RuntimeException e) {
		LOG.error("Variable error: %s", string);
		throw e;
	}

}
 
Example #11
Source File: LogicalExpressionReader.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read a logical expression from a string.
 *
 * @param string
 *            LISP formatted string.
 * @param mapping
 *            Mapping of labels to logical expressions created during
 *            parsing (for example, so we could re-use variables).
 */
protected LogicalExpression read(String string,
		ScopeMapping<String, LogicalExpression> mapping,
		TypeRepository typeRepository, ITypeComparator typeComparator) {
	for (final IReader<? extends LogicalExpression> reader : readers) {
		if (reader.test(string)) {
			return reader.read(string, mapping, typeRepository,
					typeComparator, this);
		}
	}
	throw new IllegalArgumentException(
			"Invalid logical expression syntax: " + string);
}
 
Example #12
Source File: LogicalExpressionReader.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
/** {@see #read(String)} */
private LogicalExpression read(String string,
		TypeRepository typeRepository, ITypeComparator typeComparator) {
	// Flatten the string. Replace all white space sequences with a single
	// space.
	final String flatString = WHITE_SPACE_REPLACER.replace(string);
	try {
		return LambdaWrapped.of(read(flatString,
				new ScopeMapping<String, LogicalExpression>(),
				typeRepository, typeComparator));
	} catch (final RuntimeException e) {
		LOG.error("Logical expression syntax error: %s", flatString);
		throw e;
	}
}
 
Example #13
Source File: SkolemIdInstanceWrapper.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	if (exp instanceof SkolemIdInstanceWrapper) {
		return base == ((SkolemIdInstanceWrapper) exp).base;
	} else {
		return base == exp;
	}
}
 
Example #14
Source File: LogicalConstant.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public LogicalConstant read(String string,
		ScopeMapping<String, LogicalExpression> mapping,
		TypeRepository typeRepository, ITypeComparator typeComparator,
		LogicalExpressionReader reader) {
	return LogicalConstant.read(string, typeRepository);
}
 
Example #15
Source File: Literal.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Literal read(String string,
		ScopeMapping<String, LogicalExpression> mapping,
		TypeRepository typeRepository, ITypeComparator typeComparator,
		LogicalExpressionReader reader) {
	try {
		final LispReader lispReader = new LispReader(new StringReader(
				string));

		// First is the literal predicate. Get its signature and verify
		// it
		// exists
		final String predicateString = lispReader.next();
		final LogicalExpression predicate = reader.read(
				predicateString, mapping, typeRepository,
				typeComparator);

		// The rest of the elements are the arguments
		final List<LogicalExpression> arguments = new ArrayList<LogicalExpression>();
		while (lispReader.hasNext()) {
			final String stringElement = lispReader.next();
			final LogicalExpression argument = reader.read(
					stringElement, mapping, typeRepository,
					typeComparator);
			arguments.add(argument);
		}

		// Create the literal, all checks are done within the
		// constructor
		return new Literal(predicate,
				arguments.toArray(new LogicalExpression[arguments
						.size()]), typeComparator, typeRepository);
	} catch (final RuntimeException e) {
		LOG.error("Literal syntax error: %s", string);
		throw e;
	}
}
 
Example #16
Source File: GetApplicationFunction.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Literal literal) {
	if (isDirectlyMatched(literal)) {
		return;
	}

	final int len = literal.numArgs();
	if (!(argument instanceof Literal)
			|| ((Literal) argument).numArgs() != len) {
		result = false;
		return;
	}

	final Literal argLiteral = (Literal) argument;

	if (literal.getPredicateType().isOrderSensitive()) {
		// Case order sensitive literal.
		argument = argLiteral.getPredicate();
		literal.getPredicate().accept(this);
		if (!result) {
			return;
		}
		for (int i = 0; i < len; ++i) {
			argument = argLiteral.getArg(i);
			literal.getArg(i).accept(this);
			if (!result) {
				return;
			}
		}
	} else {
		// Case order insensitive literal. Similar to how we compare
		// Literal objects.

		final ScopeMapping<Variable, Variable> originalScopeMapping = scope;
		final Map<Variable, LogicalExpression> originalExternalVariableMapping = externalVariableMapping;

		final LogicalExpression[] otherArgsCopy = argLiteral
				.argumentCopy();
		for (int j = 0; j < len; ++j) {
			boolean found = false;
			for (int i = 0; i < len; ++i) {
				if (otherArgsCopy[i] != null) {
					scope = new ScopeMappingOverlay<Variable, Variable>(
							originalScopeMapping,
							new IdentityFastStackMap<Variable, Variable>(),
							new IdentityFastStackMap<Variable, Variable>());
					final HashMap<Variable, LogicalExpression> temporaryMap = new HashMap<Variable, LogicalExpression>(
							originalExternalVariableMapping);
					externalVariableMapping = temporaryMap;
					argument = otherArgsCopy[i];
					literal.getArg(j).accept(this);
					externalVariableMapping = originalExternalVariableMapping;
					if (result) {
						found = true;
						otherArgsCopy[i] = null;
						((ScopeMappingOverlay<Variable, Variable>) scope)
								.applyToBase();
						originalExternalVariableMapping
								.putAll(temporaryMap);
						break;
					} else {
						// Reset the result.
						result = true;
					}
				}
			}
			if (!found) {
				result = false;
				return;
			}
		}
		scope = originalScopeMapping;
	}
}
 
Example #17
Source File: LogicalExpressionReader.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
LOGEXP read(String string,
ScopeMapping<String, LogicalExpression> mapping,
TypeRepository typeRepository, ITypeComparator typeComparator,
LogicalExpressionReader reader);
 
Example #18
Source File: SkolemId.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected boolean doEquals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	return equals(exp, mapping);
}
 
Example #19
Source File: LogicalConstant.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected boolean doEquals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	// Variable mapping is irrelevant for constants mapping.
	return equals(exp);
}
 
Example #20
Source File: LogicalConstant.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean equals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	// Variable mapping is irrelevant for constants mapping.
	return equals(exp);
}
 
Example #21
Source File: LogicalExpressionComparator.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean compare(LogicalExpression o1, LogicalExpression o2) {
	return o1.doEquals(o2, new ScopeMapping<Variable, Variable>(
			new IdentityFastStackMap<Variable, Variable>(),
			new IdentityFastStackMap<Variable, Variable>()));
}
 
Example #22
Source File: Lambda.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Lambda read(String string,
		ScopeMapping<String, LogicalExpression> mapping,
		TypeRepository typeRepository, ITypeComparator typeComparator,
		LogicalExpressionReader reader) {

	try {
		final LispReader lispReader = new LispReader(new StringReader(
				string));

		// The first argument is the 'lambda' keyword. We just ignore
		// it.
		lispReader.next();

		// The second argument is the variable definition.
		final Pair<String, Variable> variableDef = Variable
				.readVariableDefintion(lispReader.next(),
						typeRepository);

		if (variableDef == null) {
			throw new LogicalExpressionRuntimeException(
					"Invalid lambda argument: " + string);
		}

		// Update the scope mapping.
		mapping.push(variableDef.first(), variableDef.second());

		// The next argument is the body expression.
		final LogicalExpression lambdaBody = reader.read(
				lispReader.next(), mapping, typeRepository,
				typeComparator);

		// Verify that we don't have any more elements.
		if (lispReader.hasNext()) {
			throw new LogicalExpressionRuntimeException(String.format(
					"Invalid lambda expression: %s", string));
		}

		// Remove the variable from the mapping.
		if (mapping.pop(variableDef.first()) == null) {
			throw new LogicalExpressionRuntimeException(
					"Failed to remove variable from mapping. Something werid is happening: "
							+ string);
		}

		return new Lambda(variableDef.second(), lambdaBody);
	} catch (final RuntimeException e) {
		LOG.error("Lambda syntax error: %s", string);
		throw e;
	}

}
 
Example #23
Source File: LogicalExpression.java    From spf with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Comparison with existing mapping and a hashcode short-circuiting.
 *
 * @param exp
 *            Compared object.
 * @param mapping
 *            Existing logical expression mapping between this logical
 *            expression and the target.
 */
public boolean equals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping) {
	return exp != null && exp.hashCode() == hashCode()
			&& doEquals(exp, mapping);
}
 
Example #24
Source File: LogicalExpression.java    From spf with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Comparison with mapping.
 *
 * @param exp
 *            Compared object.
 * @param mapping
 *            Map of logical expressions from source expression to target.
 */
protected abstract boolean doEquals(LogicalExpression exp,
		ScopeMapping<Variable, Variable> mapping);