use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.MeasureInstance 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.type.MeasureInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method measureParser.
/**
* Parses the measure given its definition context.
* Returns the parsed measure as a CoreInstance.
*/
private CoreInstance measureParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext ctx, ImportGroup importId, boolean addLines) throws PureParserException {
UnitInstance canonicalUnit;
MutableList<UnitInstance> nonCanonicalUnits;
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
MutableList<String> typeParameterNames = Lists.mutable.empty();
MutableList<Boolean> contravariants = Lists.mutable.empty();
MutableList<String> multiplicityParameterNames = Lists.mutable.empty();
ListIterable<CoreInstance> stereotypes = Lists.mutable.empty();
ListIterable<TaggedValue> tags = Lists.mutable.empty();
MeasureInstance measureInstance;
String measureName = ctx.qualifiedName().identifier().getText();
measureInstance = MeasureInstance.createPersistent(this.repository, measureName);
PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
measureInstance._package(packageInstance);
packageInstance._childrenAdd(measureInstance);
String fullName = this.getQualifiedNameString(ctx.qualifiedName());
measureInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
if (superTypesGenericTypes.isEmpty()) {
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
superTypesGenericTypes.add(genericTypeInstance);
}
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance measureType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Measure);
classifierGT._rawTypeCoreInstance(measureType);
measureInstance._classifierGenericType(classifierGT);
if (!typeParameterNames.isEmpty()) {
MutableList<TypeParameter> typeParameters = Lists.mutable.of();
MutableList<Pair<String, Boolean>> tps = typeParameterNames.zip(contravariants);
for (Pair<String, Boolean> typeParam : tps) {
TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParam.getOne());
tp._contravariant(typeParam.getTwo());
typeParameters.add(tp);
}
MutableList<GenericType> typeArgs = Lists.mutable.of();
for (String typeParamName : typeParameterNames) {
TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParamName);
GenericTypeInstance gt = GenericTypeInstance.createPersistent(this.repository);
gt._typeParameter(tp);
typeArgs.add(gt);
}
}
if (!multiplicityParameterNames.isEmpty()) {
MutableList<Multiplicity> multParameters = Lists.mutable.of();
for (String multiplicityParam : multiplicityParameterNames) {
MultiplicityInstance mult = MultiplicityInstance.createPersistent(this.repository, null, null);
mult._multiplicityParameter(multiplicityParam);
multParameters.add(mult);
}
}
measureInstance._name(ctx.qualifiedName().identifier().getText());
if (!stereotypes.isEmpty()) {
measureInstance._stereotypesCoreInstance(stereotypes);
}
if (!tags.isEmpty()) {
measureInstance._taggedValues(tags);
}
MutableList<Generalization> generalizations = Lists.mutable.empty();
for (GenericType superType : superTypesGenericTypes) {
GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, measureInstance);
generalizations.add(generalizationInstance);
}
measureInstance._generalizations(generalizations);
if (null != ctx.measureBody().canonicalExpr()) {
// traditional canonical unit pattern
canonicalUnit = this.canonicalUnitParser(ctx.measureBody().canonicalExpr(), importId, measureInstance, ctx);
measureInstance._canonicalUnit(canonicalUnit);
nonCanonicalUnits = this.nonCanonicalUnitsParser(ctx.measureBody().measureExpr(), importId, measureInstance, ctx);
if (nonCanonicalUnits.notEmpty()) {
measureInstance._nonCanonicalUnits(nonCanonicalUnits);
}
} else {
// non-convertible unit pattern
MutableList<UnitInstance> nonConvertibleUnits = Lists.mutable.empty();
for (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NonConvertibleMeasureExprContext ncctx : ctx.measureBody().nonConvertibleMeasureExpr()) {
UnitInstance currentUnit = this.nonConvertibleUnitParser(ncctx, importId, measureInstance, ctx);
nonConvertibleUnits.add(currentUnit);
}
measureInstance._canonicalUnit(nonConvertibleUnits.get(0));
if (nonConvertibleUnits.size() > 1) {
measureInstance._nonCanonicalUnits(nonConvertibleUnits.subList(1, nonConvertibleUnits.size()));
}
}
return measureInstance;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.MeasureInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method nonConvertibleUnitParser.
private UnitInstance nonConvertibleUnitParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NonConvertibleMeasureExprContext ctx, ImportGroup importId, MeasureInstance measureInstance, org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext mctx) throws PureParserException {
UnitInstance unitInstance;
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
String unitName = mctx.qualifiedName().identifier().getText().concat(this.tilde).concat(ctx.qualifiedName().identifier().getText());
unitInstance = UnitInstance.createPersistent(this.repository, unitName);
PackageInstance packageInstance = buildPackage(Lists.mutable.withAll(mctx.qualifiedName().packagePath() == null ? Lists.mutable.empty() : ListAdapter.adapt(mctx.qualifiedName().packagePath().identifier())));
unitInstance._package(packageInstance);
packageInstance._childrenAdd(unitInstance);
unitInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance unitType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Unit);
classifierGT._rawTypeCoreInstance(unitType);
unitInstance._classifierGenericType(classifierGT);
unitInstance._name(unitName);
if (superTypesGenericTypes.isEmpty()) {
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstance._rawTypeCoreInstance(measureInstance);
superTypesGenericTypes.add(genericTypeInstance);
}
unitInstance._generalizations(superTypesGenericTypes.collect(superType -> GeneralizationInstance.createPersistent(this.repository, superType, unitInstance)));
unitInstance._measure(measureInstance);
return unitInstance;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.MeasureInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method unitParser.
/**
* Helps build the unitInstance for any canonical and non-canonical units and returns the parsed unitInstance.
*/
private UnitInstance unitParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureExprContext ctx, ImportGroup importId, MeasureInstance measureInstance, org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext mctx) throws PureParserException {
UnitInstance unitInstance;
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
String unitName = mctx.qualifiedName().identifier().getText().concat(this.tilde).concat(ctx.qualifiedName().identifier().getText());
unitInstance = UnitInstance.createPersistent(this.repository, unitName);
PackageInstance packageInstance = buildPackage(Lists.mutable.withAll(mctx.qualifiedName().packagePath() == null ? Lists.mutable.empty() : ListAdapter.adapt(mctx.qualifiedName().packagePath().identifier())));
unitInstance._package(packageInstance);
packageInstance._childrenAdd(unitInstance);
unitInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance unitType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Unit);
classifierGT._rawTypeCoreInstance(unitType);
unitInstance._classifierGenericType(classifierGT);
unitInstance._name(unitName);
if (superTypesGenericTypes.isEmpty()) {
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
// set unit super type to be its measure (Kilogram -> Mass)
genericTypeInstance._rawTypeCoreInstance(measureInstance);
superTypesGenericTypes.add(genericTypeInstance);
}
MutableList<Generalization> generalizations = Lists.mutable.empty();
for (GenericType superType : superTypesGenericTypes) {
GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, unitInstance);
generalizations.add(generalizationInstance);
}
unitInstance._generalizations(generalizations);
unitInstance._measure(measureInstance);
// prepare lambda instance for the conversion function
String fullName = this.getQualifiedNameString(ctx.qualifiedName());
LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
MutableList<String> typeParametersNames = Lists.mutable.empty();
FunctionTypeInstance signature = FunctionTypeInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.unitExpr().getStart(), ctx.unitExpr().getStart(), ctx.unitExpr().getStop()), null, null);
// prepare params
VariableExpression expr = this.lambdaParam(null, ctx.unitExpr().identifier(), typeParametersNames, "", importId);
expr._multiplicity(this.getPureOne());
expr._functionTypeOwner(signature);
GenericTypeInstance paramGenericType = GenericTypeInstance.createPersistent(this.repository);
CoreInstance paramType = this.processorSupport.package_getByUserPath(M3Paths.Number);
paramGenericType._rawTypeCoreInstance(paramType);
expr._genericType(paramGenericType);
signature._parameters(Lists.mutable.with(expr));
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
Type type = (Type) this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
genericTypeInstance._rawTypeCoreInstance(type);
GenericTypeInstance genericTypeInstanceTa = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstanceTa._rawTypeCoreInstance(signature);
genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(genericTypeInstanceTa));
LambdaFunctionInstance lambdaFunctionInstance = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), this.sourceInformation.getPureSourceInformation(ctx.unitExpr().ARROW().getSymbol()));
lambdaFunctionInstance._classifierGenericType(genericTypeInstance);
signature._functionAdd(lambdaFunctionInstance);
ListIterable<ValueSpecification> block = this.codeBlock(ctx.unitExpr().codeBlock(), typeParametersNames, importId, lambdaContext, addLines, tabs(6));
lambdaFunctionInstance._expressionSequence(block);
unitInstance._conversionFunction(lambdaFunctionInstance);
return unitInstance;
}
Aggregations