use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement in project legend-pure by finos.
the class TestIdBuilder method testIdUniqueness.
private void testIdUniqueness(IdBuilder idBuilder) {
MutableSet<CoreInstance> excludedClassifiers = PrimitiveUtilities.getPrimitiveTypes(processorSupport).toSet();
MutableMap<CoreInstance, MutableSet<String>> classifierIds = Maps.mutable.empty();
MutableMap<CoreInstance, MutableObjectIntMap<String>> idConflicts = Maps.mutable.empty();
GraphNodeIterable.fromModelRepository(runtime.getModelRepository()).forEach(instance -> {
CoreInstance classifier = instance.getClassifier();
if (!excludedClassifiers.contains(classifier)) {
String id = idBuilder.buildId(instance);
if (!classifierIds.getIfAbsentPut(classifier, Sets.mutable::empty).add(id)) {
idConflicts.getIfAbsentPut(classifier, ObjectIntMaps.mutable::empty).updateValue(id, 1, n -> n + 1);
}
}
});
if (idConflicts.notEmpty()) {
MutableMap<CoreInstance, String> classifierPaths = idConflicts.keysView().toMap(c -> c, PackageableElement::getUserPathForPackageableElement);
StringBuilder builder = new StringBuilder("The following ids have conflicts:");
idConflicts.keysView().toSortedListBy(classifierPaths::get).forEach(classifier -> {
builder.append("\n\t").append(classifierPaths.get(classifier));
ObjectIntMap<String> classifierIdConflicts = idConflicts.get(classifier);
classifierIdConflicts.forEachKeyValue((id, count) -> builder.append("\n\t\t").append(id).append(": ").append(count).append(" instances"));
});
Assert.fail(builder.toString());
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement in project legend-pure by finos.
the class PackageableElementUnloaderWalk method run.
@Override
public void run(CoreInstance element, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
PackageableElement packageableElement = PackageableElementCoreInstanceWrapper.toPackageableElement(element);
for (ReferenceUsage referenceUsage : packageableElement._referenceUsages()) {
matcher.fullMatch(referenceUsage._ownerCoreInstance(), state);
}
Package pkg = packageableElement._package();
if ((pkg != null) && (pkg.getSourceInformation() == null)) {
matcher.fullMatch(pkg, state);
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method definition.
public CoreInstance definition(DefinitionContext ctx, boolean useImportStubsInInstanceParser) throws PureParserException {
CoreInstance result = null;
ImportGroup importId = null;
if (ctx.imports() != null) {
importId = this.imports(ctx.imports());
if (this.oldState == null) {
this.hasImportChanged = true;
} else {
String importGroupID = importId._name();
RichIterable<? extends PackageableElement> oldImportGroups = this.oldState.getImportGroups().select(e -> importGroupID.equals(e._name()));
if (oldImportGroups.size() == 1) {
ImportGroup oldImportGroup = (ImportGroup) oldImportGroups.toList().get(0);
MutableSet<String> oldPaths = oldImportGroup._imports().collect(Import::_path, Sets.mutable.empty());
MutableSet<String> newPaths = importId._imports().collect(Import::_path, Sets.mutable.empty());
if (oldPaths.equals(newPaths)) {
this.hasImportChanged = false;
PackageInstance parent = (PackageInstance) this.processorSupport.package_getByUserPath("system::imports");
parent._children(parent._children().reject(e -> importGroupID.equals(e._name())));
parent._childrenAdd(oldImportGroup);
oldImportGroup._package(parent);
oldImportGroup.setSourceInformation(importId.getSourceInformation());
oldImportGroup._imports(importId._imports());
importId = oldImportGroup;
} else {
this.hasImportChanged = true;
}
} else {
this.hasImportChanged = true;
}
}
}
if (ctx.profile() != null) {
for (ProfileContext pCtx : ctx.profile()) {
if (this.hasImportChanged) {
result = this.profile(pCtx);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(pCtx.start.getInputStream().getText(new Interval(pCtx.start.getStartIndex(), pCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
MutableList<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
if (oldInstances.size() == 1) {
CoreInstance thisInstance = oldInstances.get(0);
this.oldInstances.remove(thisInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(pCtx.getStart(), pCtx.qualifiedName().getStop(), pCtx.getStop());
if (thisInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
this.offsetSourceInformationForInstanceAndChildren(thisInstance, newSourceInfo.getStartLine() - thisInstance.getSourceInformation().getStartLine());
PackageInstance packageInstance = this.buildPackage(pCtx.qualifiedName().packagePath());
((PackageableElement) thisInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) thisInstance);
result = thisInstance;
} else {
result = this.profile(pCtx);
}
} else {
result = this.profile(pCtx);
}
}
this.coreInstancesResult.add(result);
}
}
if (ctx.classDefinition() != null) {
for (ClassDefinitionContext dCtx : ctx.classDefinition()) {
if (this.hasImportChanged) {
result = this.classParser(dCtx, importId, this.addLines);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(dCtx.start.getInputStream().getText(new Interval(dCtx.start.getStartIndex(), dCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
List<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
if (oldInstances.size() == 1) {
CoreInstance thisInstance = oldInstances.get(0);
this.oldInstances.remove(thisInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(dCtx.getStart(), dCtx.qualifiedName().identifier().getStart(), dCtx.getStop());
if (thisInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
this.offsetSourceInformationForInstanceAndChildren(thisInstance, newSourceInfo.getStartLine() - thisInstance.getSourceInformation().getStartLine());
PackageInstance packageInstance = this.buildPackage(dCtx.qualifiedName().packagePath());
((PackageableElement) thisInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) thisInstance);
result = thisInstance;
} else {
result = this.classParser(dCtx, importId, this.addLines);
}
} else {
result = this.classParser(dCtx, importId, this.addLines);
}
}
this.coreInstancesResult.add(result);
}
}
if (ctx.measureDefinition() != null) {
for (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext dCtx : ctx.measureDefinition()) {
if (this.hasImportChanged) {
result = this.measureParser(dCtx, importId, this.addLines);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(dCtx.start.getInputStream().getText(new Interval(dCtx.start.getStartIndex(), dCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
List<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
Predicate2<CoreInstance, SourceInformation> matchesOldInstance = (oldMeasureInstance, newSourceInfo) -> {
if (newSourceInfo == null || oldMeasureInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
PackageInstance packageInstance;
if (newSourceInfo != null) {
offsetSourceInformationForInstanceAndChildren(oldMeasureInstance, newSourceInfo.getStartLine() - oldMeasureInstance.getSourceInformation().getStartLine());
}
Function<String, IdentifierContext> newIdentifierContext = s -> new IdentifierContext(null, 1) {
@Override
public String getText() {
return s;
}
};
packageInstance = buildPackage(Lists.mutable.with(newIdentifierContext.valueOf(oldMeasureInstance.getValueForMetaPropertyToOne(M3Properties._package).getName())));
((PackageableElement) oldMeasureInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) oldMeasureInstance);
return true;
}
return false;
};
CoreInstance oldMeasureInstance = ListIterate.detect(oldInstances, i -> Instance.instanceOf(i, M3Paths.Measure, this.processorSupport));
if (oldMeasureInstance != null) {
this.oldInstances.remove(oldMeasureInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(dCtx.getStart(), dCtx.qualifiedName().identifier().getStart(), dCtx.getStop());
if (matchesOldInstance.accept(oldMeasureInstance, newSourceInfo)) {
MeasureInstance mI = (MeasureInstance) oldMeasureInstance;
result = mI;
MutableList<Unit> units = Lists.mutable.with(mI._canonicalUnit()).withAll(mI._nonCanonicalUnits());
for (Unit unit : units) {
PackageInstance packageInstance = buildPackage(Lists.mutable.withAll(dCtx.qualifiedName().packagePath() == null ? Lists.mutable.empty() : ListAdapter.adapt(dCtx.qualifiedName().packagePath().identifier())));
unit._package(packageInstance);
packageInstance._childrenAdd(unit);
}
} else {
result = this.measureParser(dCtx, importId, this.addLines);
}
} else {
result = this.measureParser(dCtx, importId, this.addLines);
}
}
MeasureInstance mI = (MeasureInstance) result;
List<Unit> allMeasureUnitInstances = Lists.mutable.<Unit>withAll(mI._nonCanonicalUnits()).with(mI._canonicalUnit()).toList();
this.coreInstancesResult.add(result);
this.coreInstancesResult.addAll(allMeasureUnitInstances);
}
}
if (ctx.association() != null) {
for (AssociationContext pCtx : ctx.association()) {
if (this.hasImportChanged) {
result = this.associationParser(pCtx, importId);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(pCtx.start.getInputStream().getText(new Interval(pCtx.start.getStartIndex(), pCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
List<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
if (oldInstances.size() == 1) {
CoreInstance thisInstance = oldInstances.get(0);
this.oldInstances.remove(thisInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(pCtx.ASSOCIATION().getSymbol(), pCtx.qualifiedName().identifier().getStart(), pCtx.getStop());
if (thisInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
this.offsetSourceInformationForInstanceAndChildren(thisInstance, newSourceInfo.getStartLine() - thisInstance.getSourceInformation().getStartLine());
PackageInstance packageInstance = this.buildPackage(pCtx.qualifiedName().packagePath());
((PackageableElement) thisInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) thisInstance);
result = thisInstance;
} else {
result = this.associationParser(pCtx, importId);
}
} else {
result = this.associationParser(pCtx, importId);
}
}
this.coreInstancesResult.add(result);
}
}
if (ctx.enumDefinition() != null) {
for (EnumDefinitionContext pCtx : ctx.enumDefinition()) {
if (this.hasImportChanged) {
result = this.enumParser(pCtx, importId);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(pCtx.start.getInputStream().getText(new Interval(pCtx.start.getStartIndex(), pCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
List<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
if (oldInstances.size() == 1) {
CoreInstance thisInstance = oldInstances.get(0);
this.oldInstances.remove(thisInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(pCtx.getStart(), pCtx.qualifiedName().identifier().getStart(), pCtx.getStop());
if (thisInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
this.offsetSourceInformationForInstanceAndChildren(thisInstance, newSourceInfo.getStartLine() - thisInstance.getSourceInformation().getStartLine());
PackageInstance packageInstance = this.buildPackage(pCtx.qualifiedName().packagePath());
((PackageableElement) thisInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) thisInstance);
result = thisInstance;
} else {
result = this.enumParser(pCtx, importId);
}
} else {
result = this.enumParser(pCtx, importId);
}
}
this.coreInstancesResult.add(result);
}
}
if (ctx.nativeFunction() != null) {
for (NativeFunctionContext pCtx : ctx.nativeFunction()) {
if (this.hasImportChanged) {
this.nativeFunction(pCtx, importId, "", this.coreInstancesResult);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(pCtx.start.getInputStream().getText(new Interval(pCtx.start.getStartIndex(), pCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
List<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
if (oldInstances.size() == 1) {
CoreInstance thisInstance = oldInstances.get(0);
this.oldInstances.remove(thisInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(pCtx.NATIVE().getSymbol(), pCtx.qualifiedName().identifier().getStart(), pCtx.END_LINE().getSymbol());
if (thisInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
this.functionCounter++;
this.offsetSourceInformationForInstanceAndChildren(thisInstance, newSourceInfo.getStartLine() - thisInstance.getSourceInformation().getStartLine());
PackageInstance packageInstance = this.buildPackage(pCtx.qualifiedName().packagePath());
((PackageableElement) thisInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) thisInstance);
this.coreInstancesResult.add(thisInstance);
} else {
this.nativeFunction(pCtx, importId, "", this.coreInstancesResult);
}
} else {
this.nativeFunction(pCtx, importId, "", this.coreInstancesResult);
}
}
}
}
if (ctx.functionDefinition() != null) {
for (FunctionDefinitionContext pCtx : ctx.functionDefinition()) {
if (this.hasImportChanged) {
result = this.concreteFunctionDefinition(pCtx, importId, true, "", this.coreInstancesResult);
} else {
String importGroupID = importId._name();
String newContent = Lists.mutable.with(pCtx.start.getInputStream().getText(new Interval(pCtx.start.getStartIndex(), pCtx.stop.getStopIndex())).split("\\r?\\n")).makeString("", System.lineSeparator(), System.lineSeparator());
List<CoreInstance> oldInstances = this.oldInstances.select(i -> (this.oldState != null) && this.oldState.instanceImportGroupInSourceEqualsNewImportGroup(i, importGroupID) && this.oldState.instanceContentInSourceEqualsNewContent(i, newContent), Lists.mutable.empty());
if (oldInstances.size() == 1) {
CoreInstance thisInstance = oldInstances.get(0);
this.oldInstances.remove(thisInstance);
SourceInformation newSourceInfo = this.sourceInformation.getPureSourceInformation(pCtx.FUNCTION().getSymbol(), pCtx.qualifiedName().identifier().getStart(), pCtx.getStop());
if (thisInstance.getSourceInformation().getStartColumn() == newSourceInfo.getStartColumn()) {
this.functionCounter++;
this.offsetSourceInformationForInstanceAndChildren(thisInstance, newSourceInfo.getStartLine() - thisInstance.getSourceInformation().getStartLine());
PackageInstance packageInstance = this.buildPackage(pCtx.qualifiedName().packagePath());
((PackageableElement) thisInstance)._package(packageInstance);
packageInstance._childrenAdd((PackageableElement) thisInstance);
this.coreInstancesResult.add(thisInstance);
result = thisInstance;
} else {
result = this.concreteFunctionDefinition(pCtx, importId, true, "", this.coreInstancesResult);
}
} else {
result = this.concreteFunctionDefinition(pCtx, importId, true, "", this.coreInstancesResult);
}
}
}
}
if (ctx.instance() != null) {
for (InstanceContext pCtx : ctx.instance()) {
result = this.instanceParser(pCtx, true, importId, this.addLines, "", true, useImportStubsInInstanceParser);
this.coreInstancesResult.add(result);
}
}
this.newSourceInfoMap.forEachKeyValue(CoreInstance::setSourceInformation);
return result;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement in project legend-pure by finos.
the class TestIdBuilderCore method testIdUniqueness.
private void testIdUniqueness(IdBuilder idBuilder) {
// ImportGroup is excluded because of a known issue with conflicts with ImportGroup ids independent of IdBuilder
MutableSet<CoreInstance> excludedClassifiers = PrimitiveUtilities.getPrimitiveTypes(processorSupport).toSet().with(processorSupport.package_getByUserPath(M3Paths.ImportGroup));
MutableMap<CoreInstance, MutableSet<String>> classifierIds = Maps.mutable.empty();
MutableMap<CoreInstance, MutableObjectIntMap<String>> idConflicts = Maps.mutable.empty();
GraphNodeIterable.fromModelRepository(runtime.getModelRepository()).forEach(instance -> {
CoreInstance classifier = instance.getClassifier();
if (!excludedClassifiers.contains(classifier)) {
String id = idBuilder.buildId(instance);
if (!classifierIds.getIfAbsentPut(classifier, Sets.mutable::empty).add(id)) {
idConflicts.getIfAbsentPut(classifier, ObjectIntMaps.mutable::empty).updateValue(id, 1, n -> n + 1);
}
}
});
if (idConflicts.notEmpty()) {
MutableMap<CoreInstance, String> classifierPaths = idConflicts.keysView().toMap(c -> c, PackageableElement::getUserPathForPackageableElement);
StringBuilder builder = new StringBuilder("The following ids have conflicts:");
idConflicts.keysView().toSortedListBy(classifierPaths::get).forEach(classifier -> {
builder.append("\n\t").append(classifierPaths.get(classifier));
ObjectIntMap<String> classifierIdConflicts = idConflicts.get(classifier);
classifierIdConflicts.forEachKeyValue((id, count) -> builder.append("\n\t\t").append(id).append(": ").append(count).append(" instances"));
});
Assert.fail(builder.toString());
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement in project legend-pure by finos.
the class ImportStub method resolvePackageableElement.
private static CoreInstance resolvePackageableElement(String idOrPath, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStub importStubNode, ModelRepository repository, ProcessorSupport processorSupport) {
// Try the Special Types (not user-defined top level types!!!)
if (_Package.SPECIAL_TYPES.contains(idOrPath)) {
return repository.getTopLevel(idOrPath);
}
ImportGroup importGroup = importStubNode._importGroup();
// Check if a package is specified
int lastIndex = idOrPath.lastIndexOf(':');
if (lastIndex != -1) {
CoreInstance node = processorSupport.package_getByUserPath(idOrPath);
if (node == null) {
String id = idOrPath.substring(lastIndex + 1);
throw new PureUnresolvedIdentifierException(importStubNode.getSourceInformation(), idOrPath, id, repository, processorSupport, importStubNode, importGroup);
}
return node;
}
// Look in the imported packages
MutableSet<CoreInstance> results = Sets.mutable.with();
Imports.getImportGroupPackages(importGroup, processorSupport).forEach(pkg -> {
CoreInstance found = _Package.findInPackage(pkg, idOrPath);
if (found != null) {
results.add(found);
}
});
switch(results.size()) {
case 0:
{
// Try user defined top elements (important to do that last ... as doing it earlier could create conflicts...)
CoreInstance node = processorSupport.package_getByUserPath(idOrPath);
if (node == null) {
throw new PureUnresolvedIdentifierException(importStubNode.getSourceInformation(), idOrPath, idOrPath, repository, processorSupport, importStubNode, importGroup);
}
return node;
}
case 1:
{
return results.getAny();
}
default:
{
throw new PureCompilationException(importStubNode.getSourceInformation(), results.collect(PackageableElement::getUserPathForPackageableElement, Lists.mutable.ofInitialCapacity(results.size())).sortThis().makeString(idOrPath + " has been found more than one time in the imports: [", ", ", "]"));
}
}
}
Aggregations