Skip to content

Commit

Permalink
Fix source info for reference usage owners (#920)
Browse files Browse the repository at this point in the history
* Fix source info for generalizations, classifierGenericType, and more in AntlrContextToM3CoreInstance

* Copy source info when copying generic type for "this" param in qualified properties from associations

* Improve source info management for generated milestoning properties

* Add compiled state integrity test for ReferenceUsage owner source info

* Fix source info for m2m and x-store mappings

* Set source info for relational milestoning property mappings

* Add source info for SubstituteStore

* Add source info for AggregationAwarePropertyMappings

* Fix source info for class and association projection properties

* Add source info for graph tree parameter values
  • Loading branch information
kevin-m-knight-gs authored Jan 16, 2025
1 parent 8da9c6c commit 71fabb2
Show file tree
Hide file tree
Showing 17 changed files with 896 additions and 836 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,30 @@ private static void preProcessAssociationProjection(AssociationProjection associ
Class<?> leftProjectedRawType = (Class<?>) ImportStub.withImportStubByPass(firstProjection._projectionSpecification()._type()._rawTypeCoreInstance(), processorSupport);
Class<?> rightProjectedRawType = (Class<?>) ImportStub.withImportStubByPass(lastProjection._projectionSpecification()._type()._rawTypeCoreInstance(), processorSupport);

MutableList<Property<?, ?>> projectedPropertiesCopy = Lists.mutable.ofInitialCapacity(2);

ClassProjection<?> leftProjection = findProjectionTypeMatch(leftProjectedRawType, rightProjectedRawType, projectedPropertyLeftRawType, firstProjection, lastProjection, processorSupport);
if (leftProjection == null)
{
throwInvalidProjectionException(associationProjection, rightProperty);
}
Property<?, ?> leftPropertyCopy = (Property<?, ?>) ProjectionUtil.createPropertyCopy(leftProperty, associationProjection, modelRepository, processorSupport);
leftPropertyCopy._owner(null);
GenericType leftProjectionGTCopy = (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(leftProjection, processorSupport);
projectedPropertiesCopy.add(leftPropertyCopy);

ClassProjection<?> rightProjection = findProjectionTypeMatch(leftProjectedRawType, rightProjectedRawType, projectedPropertyRightRawType, (ClassProjection<?>) projections.getFirst(), (ClassProjection<?>) projections.getLast(), processorSupport);
if (rightProjection == null)
{
throwInvalidProjectionException(associationProjection, leftProperty);
}

Property<?, ?> rightPropertyCopy = (Property<?, ?>) ProjectionUtil.createPropertyCopy(rightProperty, associationProjection, modelRepository, processorSupport);
rightPropertyCopy._owner(null);
GenericType rightProjectionGTCopy = (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(rightProjection, processorSupport);
projectedPropertiesCopy.add(rightPropertyCopy);

replacePropertyGenericType(leftPropertyCopy, rightProjectionGTCopy);
replacePropertyReturnType(leftPropertyCopy, rightProjectionGTCopy);

replacePropertyGenericType(rightPropertyCopy, leftProjectionGTCopy);
replacePropertyReturnType(rightPropertyCopy, leftProjectionGTCopy);
Property<?, ?> leftPropertyCopy = (Property<?, ?>) ProjectionUtil.createPropertyCopy(leftProperty, associationProjection, modelRepository, processorSupport)
._owner(null);
GenericType leftPropertyCopyGT = (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(rightProjection, leftPropertyCopy.getSourceInformation(), processorSupport);
replacePropertyGenericType(leftPropertyCopy, leftPropertyCopyGT);
replacePropertyReturnType(leftPropertyCopy, leftPropertyCopyGT);

Property<?, ?> rightPropertyCopy = (Property<?, ?>) ProjectionUtil.createPropertyCopy(rightProperty, associationProjection, modelRepository, processorSupport)
._owner(null);
GenericType rightPropertyCopyGT = (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(leftProjection, rightPropertyCopy.getSourceInformation(), processorSupport);
replacePropertyGenericType(rightPropertyCopy, rightPropertyCopyGT);
replacePropertyReturnType(rightPropertyCopy, rightPropertyCopyGT);

associationProjection._propertiesCoreInstance(Lists.immutable.<Property<?, ?>>ofAll(associationProjection._properties()).newWithAll(projectedPropertiesCopy));
associationProjection._propertiesAddAll(Lists.immutable.with(leftPropertyCopy, rightPropertyCopy));
}

private static void checkForValidProjectionType(AssociationProjection associationProjection, CoreInstance firstProjection)
Expand Down Expand Up @@ -297,13 +290,13 @@ private static void updateClassifierGenericTypeForQualifiedPropertiesThisVarExpr
Property<?, ?> leftSideOfFilterOtherProperty = assnProperties.detect(p -> !leftSideOfFilterProperty.equals(p));
GenericType leftSideOfFilterOtherPropertyGenericType = leftSideOfFilterOtherProperty._genericType();

FunctionType functionType = (FunctionType) qualifiedProperty._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance();
FunctionType functionType = (FunctionType) qualifiedProperty._classifierGenericType()._typeArguments().getOnly()._rawTypeCoreInstance();
Iterable<? extends VariableExpression> functionTypeParams = functionType._parameters();
for (VariableExpression functionTypeParam : functionTypeParams)
{
if (functionTypeParam != null && "this".equals(functionTypeParam._name()))
{
GenericType genericTypeCopy = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericType(leftSideOfFilterOtherPropertyGenericType, processorSupport);
GenericType genericTypeCopy = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericType(leftSideOfFilterOtherPropertyGenericType, true, processorSupport);
functionTypeParam._genericType(genericTypeCopy);
context.update(functionTypeParam);
if (functionTypeParam.hasBeenValidated())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType;
import org.finos.legend.pure.m3.navigation.M3Properties;
import org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement;
Expand All @@ -33,9 +34,14 @@
import org.finos.legend.pure.m3.navigation.importstub.ImportStub;
import org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity;
import org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser;
import org.finos.legend.pure.m3.tools.ListHelper;
import org.finos.legend.pure.m4.ModelRepository;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.coreinstance.SourceInformation;
import org.finos.legend.pure.m4.tools.GraphNodeIterable;
import org.finos.legend.pure.m4.tools.GraphWalkFilterResult;

import java.util.UUID;

public class PropertyInstanceBuilder
{
Expand Down Expand Up @@ -93,14 +99,15 @@ private static ListIterable<AbstractProperty<?>> createM3MilestonedProperties(Co
private static AbstractProperty<?> createM3Property(ImportGroup importId, PropertyOwner propertyOwner, MilestonePropertyCodeBlock propertyCodeBlock, ModelRepository modelRepository, Context context, ProcessorSupport processorSupport, int startingQualifiedPropertyIndex)
{
ImportStubInstance typeOwner = ImportStubInstance.createPersistent(modelRepository, null, propertyOwner.getSourceInformation(), PackageableElement.getUserPathForPackageableElement(propertyOwner), importId);
String fileName = propertyOwner.getSourceInformation().getSourceId();
// we create a unique filename for this, which we will use to replace source info after parsing
String fileName = UUID.randomUUID() + propertyOwner.getSourceInformation().getSourceId();
MutableList<QualifiedProperty<? extends CoreInstance>> qps = Lists.mutable.empty();
MutableList<Property<? extends CoreInstance, ?>> ps = Lists.mutable.empty();
new M3AntlrParser().parseProperties(propertyCodeBlock.getCodeBlock(), fileName, ps, qps, typeOwner, importId, false, modelRepository, context, startingQualifiedPropertyIndex);
new M3AntlrParser().parseProperties(propertyCodeBlock.getCodeBlock(), fileName, ps, qps, typeOwner, importId, true, modelRepository, context, startingQualifiedPropertyIndex);
AbstractProperty<?> property = ps.isEmpty() ? qps.getLast() : ps.getLast();
if (property != null)
{
updatePropertySourceInformation(propertyCodeBlock, property, qps.notEmpty());
updatePropertySourceInformation(propertyCodeBlock, fileName, property, qps.notEmpty());
property._owner(propertyOwner);
property._stereotypesAddAll(propertyCodeBlock.getNonMilestonedStereotypes(processorSupport));
property._taggedValuesAddAll(propertyCodeBlock.getTaggedValues());
Expand All @@ -114,24 +121,49 @@ private static AbstractProperty<?> createM3Property(ImportGroup importId, Proper
return property;
}

private static void updatePropertySourceInformation(MilestonePropertyCodeBlock propertyCodeBlock, AbstractProperty<?> property, boolean isQualifiedProperty)
private static void updatePropertySourceInformation(MilestonePropertyCodeBlock propertyCodeBlock, String fileName, AbstractProperty<?> property, boolean isQualifiedProperty)
{
property.setSourceInformation(propertyCodeBlock.getPropertySourceInformation());
GraphNodeIterable.builder()
.withStartingNode(property)
.withNodeFilter(node ->
{
SourceInformation sourceInfo = node.getSourceInformation();
if (sourceInfo == null)
{
return GraphWalkFilterResult.REJECT_AND_CONTINUE;
}
if (fileName.equals(sourceInfo.getSourceId()))
{
return GraphWalkFilterResult.ACCEPT_AND_CONTINUE;
}
return GraphWalkFilterResult.REJECT_AND_STOP;
})
.build()
.forEach(node -> node.setSourceInformation(propertyCodeBlock.getPropertySourceInformation()));

if (propertyCodeBlock.isPropertyGenericTypeSourceInformationIsAvailable())
{
GenericType genericType = property._genericType();
CoreInstance rawType = genericType._rawTypeCoreInstance();
genericType.setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
genericType._rawTypeCoreInstance().setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
GenericType classifierGenericType = property._classifierGenericType();
if (!isQualifiedProperty && classifierGenericType != null)
if (classifierGenericType != null)
{
ListIterable<? extends GenericType> typeArguments = classifierGenericType._typeArguments().toList();
if (typeArguments.size() == 2)
if (isQualifiedProperty)
{
typeArguments.get(1)._rawTypeCoreInstance().setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
GenericType typeArgument = classifierGenericType._typeArguments().getAny();
FunctionType functionType = (FunctionType) typeArgument._rawType();
functionType._returnType().setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
}
else
{
ListIterable<? extends GenericType> typeArguments = ListHelper.wrapListIterable(classifierGenericType._typeArguments());
if (typeArguments.size() == 2)
{
typeArguments.get(1)._rawTypeCoreInstance().setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
}
}
}
genericType.setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
rawType.setSourceInformation(propertyCodeBlock.getPropertyGenericTypeSourceInformation());
}
}
}
Loading

0 comments on commit 71fabb2

Please sign in to comment.