use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance 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.UnitInstance 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.UnitInstance 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