use of org.finos.legend.pure.m3.navigation.multiplicity.MultiplicityMatch in project legend-pure by finos.
the class TypeMatch method newFunctionTypeMatch.
private static TypeMatch newFunctionTypeMatch(CoreInstance targetType, CoreInstance valueType, boolean covariant, NullMatchBehavior valueNullMatchBehavior, ParameterMatchBehavior targetParameterMatchBehavior, ParameterMatchBehavior valueParameterMatchBehavior, ProcessorSupport processorSupport) {
ListIterable<? extends CoreInstance> targetParameters = targetType.getValueForMetaPropertyToMany(M3Properties.parameters);
ListIterable<? extends CoreInstance> valueParameters = valueType.getValueForMetaPropertyToMany(M3Properties.parameters);
int parameterCount = targetParameters.size();
if (parameterCount != valueParameters.size()) {
return null;
}
MutableList<GenericTypeMatch> paramTypeMatches = FastList.newList(parameterCount);
MutableList<MultiplicityMatch> paramMultMatches = FastList.newList(parameterCount);
for (int i = 0; i < parameterCount; i++) {
CoreInstance paramParam = targetParameters.get(i);
CoreInstance valueParam = valueParameters.get(i);
CoreInstance paramParamType = paramParam.getValueForMetaPropertyToOne(M3Properties.genericType);
CoreInstance valueParamType = valueParam.getValueForMetaPropertyToOne(M3Properties.genericType);
GenericTypeMatch paramTypeMatch = GenericTypeMatch.newGenericTypeMatch(paramParamType, valueParamType, !covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior, processorSupport);
if (paramTypeMatch == null) {
return null;
}
paramTypeMatches.add(paramTypeMatch);
CoreInstance paramParamMult = Instance.getValueForMetaPropertyToOneResolved(paramParam, M3Properties.multiplicity, processorSupport);
CoreInstance valueParamMult = Instance.getValueForMetaPropertyToOneResolved(valueParam, M3Properties.multiplicity, processorSupport);
MultiplicityMatch paramMultMatch = MultiplicityMatch.newMultiplicityMatch(paramParamMult, valueParamMult, !covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
if (paramMultMatch == null) {
return null;
}
paramMultMatches.add(paramMultMatch);
}
CoreInstance targetReturnType = targetType.getValueForMetaPropertyToOne(M3Properties.returnType);
CoreInstance valueReturnType = valueType.getValueForMetaPropertyToOne(M3Properties.returnType);
GenericTypeMatch returnTypeMatch = GenericTypeMatch.newGenericTypeMatch(targetReturnType, valueReturnType, covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior, processorSupport);
if (returnTypeMatch == null) {
return null;
}
CoreInstance targetReturnMult = Instance.getValueForMetaPropertyToOneResolved(targetType, M3Properties.returnMultiplicity, processorSupport);
CoreInstance valueReturnMult = Instance.getValueForMetaPropertyToOneResolved(valueType, M3Properties.returnMultiplicity, processorSupport);
MultiplicityMatch returnMultMatch = MultiplicityMatch.newMultiplicityMatch(targetReturnMult, valueReturnMult, covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
if (returnMultMatch == null) {
return null;
}
return new FunctionTypeMatch(paramTypeMatches, paramMultMatches, returnTypeMatch, returnMultMatch);
}
use of org.finos.legend.pure.m3.navigation.multiplicity.MultiplicityMatch in project legend-pure by finos.
the class TestMultiplicityMatch method testMatchOrderingZeroManyTargetCovariant.
@Test
public void testMatchOrderingZeroManyTargetCovariant() {
NullMatchBehavior valueNullMatchBehavior = NullMatchBehavior.MATCH_ANYTHING;
ParameterMatchBehavior targetParameterMatchBehavior = ParameterMatchBehavior.MATCH_ANYTHING;
ParameterMatchBehavior valueParameterMatchBehavior = ParameterMatchBehavior.MATCH_CAUTIOUSLY;
MultiplicityMatch zeroManyMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, zeroMany, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch oneManyMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, oneMany, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch zeroOneMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, zeroOne, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch oneMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, one, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch mMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, m, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch nMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, n, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch nullMatch = MultiplicityMatch.newMultiplicityMatch(zeroMany, null, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
Assert.assertNotNull(zeroManyMatch);
Assert.assertNotNull(oneManyMatch);
Assert.assertNotNull(zeroOneMatch);
Assert.assertNotNull(oneMatch);
Assert.assertNotNull(mMatch);
Assert.assertNotNull(nMatch);
Assert.assertNotNull(nullMatch);
assertComparesEqual(zeroManyMatch, zeroManyMatch);
assertComparesLessThan(zeroManyMatch, oneManyMatch);
assertComparesLessThan(zeroManyMatch, zeroOneMatch);
assertComparesLessThan(zeroManyMatch, oneMatch);
assertComparesLessThan(zeroManyMatch, mMatch);
assertComparesLessThan(zeroManyMatch, nMatch);
assertComparesLessThan(zeroManyMatch, nullMatch);
assertComparesGreaterThan(oneManyMatch, zeroManyMatch);
assertComparesEqual(oneManyMatch, oneManyMatch);
assertComparesLessThan(oneManyMatch, zeroOneMatch);
assertComparesLessThan(oneManyMatch, oneMatch);
assertComparesLessThan(oneManyMatch, mMatch);
assertComparesLessThan(oneManyMatch, nMatch);
assertComparesLessThan(oneManyMatch, nullMatch);
assertComparesGreaterThan(zeroOneMatch, zeroManyMatch);
assertComparesGreaterThan(zeroOneMatch, oneManyMatch);
assertComparesEqual(zeroOneMatch, zeroOneMatch);
assertComparesLessThan(zeroOneMatch, oneMatch);
assertComparesLessThan(zeroOneMatch, mMatch);
assertComparesLessThan(zeroOneMatch, nMatch);
assertComparesLessThan(zeroOneMatch, nullMatch);
assertComparesGreaterThan(oneMatch, zeroManyMatch);
assertComparesGreaterThan(oneMatch, oneManyMatch);
assertComparesGreaterThan(oneMatch, zeroOneMatch);
assertComparesEqual(oneMatch, oneMatch);
assertComparesLessThan(oneMatch, mMatch);
assertComparesLessThan(oneMatch, nMatch);
assertComparesLessThan(oneMatch, nullMatch);
assertComparesGreaterThan(mMatch, zeroManyMatch);
assertComparesGreaterThan(mMatch, oneManyMatch);
assertComparesGreaterThan(mMatch, zeroOneMatch);
assertComparesGreaterThan(mMatch, oneMatch);
assertComparesEqual(mMatch, mMatch);
assertComparesEqual(mMatch, nMatch);
assertComparesLessThan(mMatch, nullMatch);
assertComparesGreaterThan(nMatch, zeroManyMatch);
assertComparesGreaterThan(nMatch, oneManyMatch);
assertComparesGreaterThan(nMatch, zeroOneMatch);
assertComparesGreaterThan(nMatch, oneMatch);
assertComparesEqual(nMatch, mMatch);
assertComparesEqual(nMatch, nMatch);
assertComparesLessThan(nMatch, nullMatch);
assertComparesGreaterThan(nullMatch, zeroManyMatch);
assertComparesGreaterThan(nullMatch, oneManyMatch);
assertComparesGreaterThan(nullMatch, zeroOneMatch);
assertComparesGreaterThan(nullMatch, oneMatch);
assertComparesGreaterThan(nullMatch, mMatch);
assertComparesGreaterThan(nullMatch, nMatch);
assertComparesEqual(nullMatch, nullMatch);
}
use of org.finos.legend.pure.m3.navigation.multiplicity.MultiplicityMatch in project legend-pure by finos.
the class GenericTypeMatch method newGenericTypeMatch.
public static GenericTypeMatch newGenericTypeMatch(CoreInstance targetGenericType, CoreInstance valueGenericType, boolean covariant, NullMatchBehavior valueNullMatchBehavior, ParameterMatchBehavior targetParameterMatchBehavior, ParameterMatchBehavior valueParameterMatchBehavior, ProcessorSupport processorSupport) {
// Null checks
if (targetGenericType == null) {
throw new IllegalArgumentException("Target generic type may not be null");
}
if (valueGenericType == null) {
switch(getNullMatchBehavior(valueNullMatchBehavior)) {
case MATCH_ANYTHING:
{
return NULL_MATCH;
}
case MATCH_NOTHING:
{
return null;
}
case ERROR:
{
throw new RuntimeException("Value generic type may not be null");
}
}
}
// Identical generic types
if (GenericType.genericTypesEqual(targetGenericType, valueGenericType, processorSupport)) {
return EXACT_MATCH;
}
CoreInstance targetRawType = Instance.getValueForMetaPropertyToOneResolved(targetGenericType, M3Properties.rawType, processorSupport);
CoreInstance valueRawType = Instance.getValueForMetaPropertyToOneResolved(valueGenericType, M3Properties.rawType, processorSupport);
// Target generic type is not concrete (i.e., targetRawType == null)
if (targetRawType == null) {
switch(getParameterMatchBehavior(targetParameterMatchBehavior)) {
case MATCH_ANYTHING:
{
return NON_CONCRETE_MATCH;
}
case MATCH_CAUTIOUSLY:
{
if (valueRawType == null) {
String targetTypeParameterName = GenericType.getTypeParameterName(targetGenericType);
String valueTypeParameterName = GenericType.getTypeParameterName(valueGenericType);
return (targetTypeParameterName != null) && targetTypeParameterName.equals(valueTypeParameterName) ? NON_CONCRETE_MATCH : null;
}
boolean willMatchParameterBinding = covariant ? Type.isBottomType(valueRawType, processorSupport) : Type.isTopType(valueRawType, processorSupport);
return willMatchParameterBinding ? NON_CONCRETE_MATCH : null;
}
case MATCH_NOTHING:
{
return null;
}
case ERROR:
{
throw new RuntimeException("Target generic type must be concrete, got: " + GenericType.print(targetGenericType, processorSupport));
}
default:
{
throw new RuntimeException("Should not be possible");
}
}
}
// If the value generic type is not concrete (i.e., valueRawType == null), then it only matches Any (for covariant) or Nil (for contravariant)
if (valueRawType == null) {
switch(getParameterMatchBehavior(valueParameterMatchBehavior)) {
case MATCH_ANYTHING:
{
return NON_CONCRETE_MATCH;
}
case MATCH_CAUTIOUSLY:
{
boolean anyParameterBindingWillMatch = covariant ? Type.isTopType(targetRawType, processorSupport) : Type.isBottomType(targetRawType, processorSupport);
return anyParameterBindingWillMatch ? NON_CONCRETE_MATCH : null;
}
case MATCH_NOTHING:
{
return null;
}
case ERROR:
{
throw new RuntimeException("Value generic type must be concrete, got: " + GenericType.print(valueGenericType, processorSupport));
}
}
}
// Both generic types are concrete, so we calculate a match between the raw types
TypeMatch rawTypeMatch = TypeMatch.newTypeMatch(targetRawType, valueRawType, covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior, processorSupport);
if (rawTypeMatch == null) {
return null;
}
if (Type.isBottomType(covariant ? valueRawType : targetRawType, processorSupport) || Type.isTopType(covariant ? targetRawType : valueRawType, processorSupport)) {
return new GenericTypeMatch(rawTypeMatch);
}
ListIterable<? extends CoreInstance> targetTypeArguments = targetGenericType.getValueForMetaPropertyToMany(M3Properties.typeArguments);
int typeParameterCount = targetTypeArguments.size();
MutableList<GenericTypeMatch> typeArgumentMatches = FastList.newList(typeParameterCount);
if (typeParameterCount > 0) {
GenericTypeWithXArguments homogenizedTypeArguments = GenericType.resolveClassTypeParameterUsingInheritance(valueGenericType, targetGenericType, processorSupport);
if (homogenizedTypeArguments == null) {
return null;
}
ImmutableMap<String, CoreInstance> valueTypeArgumentsByParam = homogenizedTypeArguments.getArgumentsByParameterName();
if (typeParameterCount != valueTypeArgumentsByParam.size()) {
return null;
}
ListIterable<? extends CoreInstance> typeParameters = targetRawType.getValueForMetaPropertyToMany(M3Properties.typeParameters);
for (int i = 0; i < typeParameterCount; i++) {
CoreInstance typeParameter = typeParameters.get(i);
boolean contravariant = PrimitiveUtilities.getBooleanValue(typeParameter.getValueForMetaPropertyToOne(M3Properties.contravariant), false);
boolean paramCovariant = contravariant ? !covariant : covariant;
CoreInstance valueTypeArgument = valueTypeArgumentsByParam.get(PrimitiveUtilities.getStringValue(typeParameter.getValueForMetaPropertyToOne(M3Properties.name)));
GenericTypeMatch typeArgumentMatch = newGenericTypeMatch(targetTypeArguments.get(i), valueTypeArgument, paramCovariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior, processorSupport);
if (typeArgumentMatch == null) {
return null;
}
typeArgumentMatches.add(typeArgumentMatch);
}
}
ListIterable<? extends CoreInstance> targetMultArguments = Instance.getValueForMetaPropertyToManyResolved(targetGenericType, M3Properties.multiplicityArguments, processorSupport);
int multParameterCount = targetMultArguments.size();
MutableList<MultiplicityMatch> multArgumentMatches = FastList.newList(multParameterCount);
if (multParameterCount > 0) {
GenericTypeWithXArguments homogenizedMultArguments = GenericType.resolveClassMultiplicityParameterUsingInheritance(valueGenericType, targetRawType, processorSupport);
if (homogenizedMultArguments == null) {
return null;
}
ListIterable<CoreInstance> valueMultArguments = homogenizedMultArguments.extractArgumentsAsMultiplicityParameters(processorSupport);
if (multParameterCount != valueMultArguments.size()) {
return null;
}
for (int i = 0; i < multParameterCount; i++) {
MultiplicityMatch multArgumentMatch = MultiplicityMatch.newMultiplicityMatch(targetMultArguments.get(i), valueMultArguments.get(i), covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
if (multArgumentMatch == null) {
return null;
}
multArgumentMatches.add(multArgumentMatch);
}
}
return new GenericTypeMatch(rawTypeMatch, typeArgumentMatches, multArgumentMatches);
}
use of org.finos.legend.pure.m3.navigation.multiplicity.MultiplicityMatch in project legend-pure by finos.
the class TestMultiplicityMatch method testExactMultiplicityMatch.
@Test
public void testExactMultiplicityMatch() {
NullMatchBehavior valueNullMatchBehavior = NullMatchBehavior.ERROR;
ParameterMatchBehavior targetParameterMatchBehavior = ParameterMatchBehavior.ERROR;
ParameterMatchBehavior valueParameterMatchBehavior = ParameterMatchBehavior.ERROR;
MultiplicityMatch exactMatchZeroMany = MultiplicityMatch.newMultiplicityMatch(zeroMany, zeroMany, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch exactMatchOneMany = MultiplicityMatch.newMultiplicityMatch(oneMany, oneMany, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
Assert.assertEquals(exactMatchZeroMany, exactMatchOneMany);
Assert.assertEquals(0, exactMatchOneMany.compareTo(exactMatchZeroMany));
Assert.assertEquals(0, exactMatchZeroMany.compareTo(exactMatchOneMany));
MultiplicityMatch exactMatchOneSix = MultiplicityMatch.newMultiplicityMatch(oneSix, oneSix, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
Assert.assertEquals(exactMatchZeroMany, exactMatchOneSix);
Assert.assertEquals(0, exactMatchOneMany.compareTo(exactMatchOneSix));
}
use of org.finos.legend.pure.m3.navigation.multiplicity.MultiplicityMatch in project legend-pure by finos.
the class TestMultiplicityMatch method testMatchOrderingNonConcreteTargetCovariant.
@Test
public void testMatchOrderingNonConcreteTargetCovariant() {
NullMatchBehavior valueNullMatchBehavior = NullMatchBehavior.MATCH_ANYTHING;
ParameterMatchBehavior targetParameterMatchBehavior = ParameterMatchBehavior.MATCH_ANYTHING;
ParameterMatchBehavior valueParameterMatchBehavior = ParameterMatchBehavior.ERROR;
MultiplicityMatch zeroManyMatch = MultiplicityMatch.newMultiplicityMatch(m, zeroMany, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch oneManyMatch = MultiplicityMatch.newMultiplicityMatch(m, oneMany, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch zeroOneMatch = MultiplicityMatch.newMultiplicityMatch(m, zeroOne, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch oneMatch = MultiplicityMatch.newMultiplicityMatch(m, one, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch mMatch = MultiplicityMatch.newMultiplicityMatch(m, newMultiplicity("m"), true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch nMatch = MultiplicityMatch.newMultiplicityMatch(m, n, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
MultiplicityMatch nullMatch = MultiplicityMatch.newMultiplicityMatch(m, null, true, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
Assert.assertNotNull(zeroManyMatch);
Assert.assertNotNull(oneManyMatch);
Assert.assertNotNull(zeroOneMatch);
Assert.assertNotNull(oneMatch);
Assert.assertNotNull(mMatch);
Assert.assertNotNull(nMatch);
Assert.assertNotNull(nullMatch);
assertComparesEqual(zeroManyMatch, zeroManyMatch);
assertComparesEqual(zeroManyMatch, oneManyMatch);
assertComparesEqual(zeroManyMatch, zeroOneMatch);
assertComparesEqual(zeroManyMatch, oneMatch);
assertComparesEqual(zeroManyMatch, mMatch);
assertComparesEqual(zeroManyMatch, nMatch);
assertComparesLessThan(zeroManyMatch, nullMatch);
assertComparesEqual(oneManyMatch, zeroManyMatch);
assertComparesEqual(oneManyMatch, oneManyMatch);
assertComparesEqual(oneManyMatch, zeroOneMatch);
assertComparesEqual(oneManyMatch, oneMatch);
assertComparesEqual(oneManyMatch, mMatch);
assertComparesEqual(oneManyMatch, nMatch);
assertComparesLessThan(oneManyMatch, nullMatch);
assertComparesEqual(zeroOneMatch, zeroManyMatch);
assertComparesEqual(zeroOneMatch, oneManyMatch);
assertComparesEqual(zeroOneMatch, zeroOneMatch);
assertComparesEqual(zeroOneMatch, oneMatch);
assertComparesEqual(zeroOneMatch, mMatch);
assertComparesEqual(zeroOneMatch, nMatch);
assertComparesLessThan(zeroOneMatch, nullMatch);
assertComparesEqual(oneMatch, zeroManyMatch);
assertComparesEqual(oneMatch, oneManyMatch);
assertComparesEqual(oneMatch, zeroOneMatch);
assertComparesEqual(oneMatch, oneMatch);
assertComparesEqual(oneMatch, mMatch);
assertComparesEqual(oneMatch, nMatch);
assertComparesLessThan(oneMatch, nullMatch);
assertComparesEqual(mMatch, zeroManyMatch);
assertComparesEqual(mMatch, oneManyMatch);
assertComparesEqual(mMatch, zeroOneMatch);
assertComparesEqual(mMatch, oneMatch);
assertComparesEqual(mMatch, mMatch);
assertComparesEqual(mMatch, nMatch);
assertComparesLessThan(mMatch, nullMatch);
assertComparesEqual(nMatch, zeroManyMatch);
assertComparesEqual(nMatch, oneManyMatch);
assertComparesEqual(nMatch, zeroOneMatch);
assertComparesEqual(nMatch, oneMatch);
assertComparesEqual(nMatch, mMatch);
assertComparesEqual(nMatch, nMatch);
assertComparesLessThan(nMatch, nullMatch);
assertComparesGreaterThan(nullMatch, zeroManyMatch);
assertComparesGreaterThan(nullMatch, oneManyMatch);
assertComparesGreaterThan(nullMatch, zeroOneMatch);
assertComparesGreaterThan(nullMatch, oneMatch);
assertComparesGreaterThan(nullMatch, mMatch);
assertComparesGreaterThan(nullMatch, nMatch);
assertComparesEqual(nullMatch, nullMatch);
}
Aggregations