use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method simpleProperty.
private void simpleProperty(PropertyContext ctx, MutableList<Property<? extends CoreInstance, ?>> properties, MutableList<String> typeParameterNames, MutableList<String> multiplicityParameterNames, ImportStub isOwner, ImportGroup importId, boolean addLines) {
ListIterable<CoreInstance> stereotypes = null;
ListIterable<TaggedValue> tags = null;
DefaultValue defaultValue = null;
GenericType genericType;
Multiplicity multiplicity;
String aggregation;
String propertyName = ctx.identifier().getText();
if (ctx.stereotypes() != null) {
stereotypes = this.stereotypes(ctx.stereotypes(), importId);
}
if (ctx.taggedValues() != null) {
tags = this.taggedValues(ctx.taggedValues(), importId);
}
if (ctx.aggregation() != null) {
if ("(composite)".equals(ctx.aggregation().getText())) {
aggregation = "Composite";
} else if ("(shared)".equals(ctx.aggregation().getText())) {
aggregation = "Shared";
} else {
aggregation = "None";
}
} else {
aggregation = "None";
}
if (ctx.defaultValue() != null) {
defaultValue = defaultValue(ctx.defaultValue(), importId, propertyName);
}
genericType = this.type(ctx.propertyReturnType().type(), typeParameterNames, "", importId, addLines);
multiplicity = this.buildMultiplicity(ctx.propertyReturnType().multiplicity().multiplicityArgument());
Enumeration<?> agg = (Enumeration<?>) this.processorSupport.package_getByUserPath(M3Paths.AggregationKind);
Enum aggKind = (Enum) agg._values().detect(v -> aggregation.equals(((Enum) v).getName()));
SourceInformation propertySourceInfo = this.sourceInformation.getPureSourceInformation(ctx.identifier().getStart(), ctx.identifier().getStart(), ctx.getStop());
PropertyInstance propertyInstance = PropertyInstance.createPersistent(this.repository, propertyName, propertySourceInfo, aggKind, genericType, multiplicity, null);
propertyInstance._stereotypesCoreInstance(stereotypes);
propertyInstance._taggedValues(tags);
propertyInstance._name(propertyName);
propertyInstance._defaultValue(defaultValue);
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository, propertySourceInfo);
ClassInstance propertyType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Property);
classifierGT._rawTypeCoreInstance(propertyType);
classifierGT._multiplicityArguments(Lists.mutable.of(multiplicity));
GenericTypeInstance classifierGTTA = GenericTypeInstance.createPersistent(this.repository);
classifierGTTA._rawTypeCoreInstance(isOwner);
if (!typeParameterNames.isEmpty()) {
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);
}
classifierGTTA._typeArguments(typeArgs);
}
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);
}
classifierGTTA._multiplicityArguments(multParameters);
}
// Clone generic type
// TODO - do we need a deep clone?
GenericTypeInstance ngt = GenericTypeInstance.createPersistent(this.repository, genericType.getSourceInformation());
CoreInstance rawType = genericType._rawTypeCoreInstance();
if (rawType != null) {
if (rawType instanceof ImportStub) {
ImportStub gtis = (ImportStub) rawType;
ImportStubInstance is = ImportStubInstance.createPersistent(this.repository, gtis.getSourceInformation(), gtis._idOrPath(), gtis._importGroup());
// ImportStubInstance is = ImportStubInstance.createPersistent(this.repository, gtis.getSourceInformation(), ((ImportStubInstance)gtis)._idOrPathAsCoreInstance().getName(), (ImportGroup)gtis._importGroup());
ngt._rawTypeCoreInstance(is);
} else {
ngt._rawTypeCoreInstance(rawType);
}
}
if (!genericType._typeArguments().isEmpty()) {
ngt._typeArguments(genericType._typeArguments());
}
ngt._typeParameter(genericType._typeParameter());
if (!genericType._multiplicityArguments().isEmpty()) {
ngt._multiplicityArguments(genericType._multiplicityArguments());
}
classifierGT._typeArguments(Lists.mutable.<GenericType>of(classifierGTTA, ngt));
propertyInstance._classifierGenericType(classifierGT);
properties.add(propertyInstance);
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method classParser.
private CoreInstance classParser(ClassDefinitionContext ctx, ImportGroup importId, boolean addLines) throws PureParserException {
MutableList<Property<? extends CoreInstance, ?>> properties = Lists.mutable.empty();
MutableList<QualifiedProperty<? extends CoreInstance>> qualifiedProperties = Lists.mutable.empty();
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
boolean isDirectSubTypeofAny = false;
MutableList<String> typeParameterNames = Lists.mutable.empty();
MutableList<Boolean> contravariants = Lists.mutable.empty();
MutableList<String> multiplicityParameterNames = Lists.mutable.empty();
ImportStubInstance ownerType;
ListIterable<CoreInstance> stereotypes = null;
ListIterable<TaggedValue> tags = null;
ClassInstance classInstance;
if (ctx.stereotypes() != null) {
stereotypes = this.stereotypes(ctx.stereotypes(), importId);
}
if (ctx.taggedValues() != null) {
tags = this.taggedValues(ctx.taggedValues(), importId);
}
this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
if (ctx.typeParametersWithContravarianceAndMultiplicityParameters() != null) {
this.typeParametersWithContravarianceAndMultiplicityParameters(ctx.typeParametersWithContravarianceAndMultiplicityParameters(), typeParameterNames, contravariants, multiplicityParameterNames);
}
if (ctx.projection() != null) {
return this.projectionParser(ctx, importId, addLines, stereotypes, tags);
} else {
if (ctx.EXTENDS() != null) {
for (TypeContext typeCtx : ctx.type()) {
superTypesGenericTypes.add(this.type(typeCtx, typeParameterNames, "", importId, addLines));
}
}
String className = ctx.qualifiedName().identifier().getText();
classInstance = ClassInstance.createPersistent(this.repository, className);
PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
classInstance._package(packageInstance);
packageInstance._childrenAdd(classInstance);
String fullName = this.getQualifiedNameString(ctx.qualifiedName());
ownerType = ImportStubInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()), fullName, importId);
LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
MutableList<Constraint> constraints = this.constraints(classInstance, ctx.constraints(), importId, lambdaContext, addLines);
this.propertyParser(ctx.classBody().properties(), properties, qualifiedProperties, typeParameterNames, multiplicityParameterNames, ownerType, importId, 0);
classInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
if (superTypesGenericTypes.isEmpty()) {
isDirectSubTypeofAny = true;
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
superTypesGenericTypes.add(genericTypeInstance);
}
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance classType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Class);
classifierGT._rawTypeCoreInstance(classType);
GenericTypeInstance classifierGTTA = GenericTypeInstance.createPersistent(this.repository);
classifierGTTA._rawTypeCoreInstance(classInstance);
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);
}
classInstance._typeParameters(typeParameters);
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);
}
classifierGTTA._typeArguments(typeArgs);
}
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);
}
classInstance._multiplicityParameters(this.processMultiplicityParametersInstance(multiplicityParameterNames));
classifierGTTA._multiplicityArguments(multParameters);
}
classifierGT._typeArguments(Lists.mutable.<GenericType>of(classifierGTTA));
classInstance._classifierGenericType(classifierGT);
if (properties.notEmpty()) {
classInstance._properties(properties);
}
if (qualifiedProperties.notEmpty()) {
classInstance._qualifiedProperties(qualifiedProperties);
}
classInstance._name(ctx.qualifiedName().identifier().getText());
if (stereotypes != null) {
classInstance._stereotypesCoreInstance(stereotypes);
}
if (tags != null) {
classInstance._taggedValues(tags);
}
if (constraints.notEmpty()) {
classInstance._constraints(constraints);
}
MutableList<Generalization> generalizations = Lists.mutable.empty();
for (GenericType superType : superTypesGenericTypes) {
GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, classInstance);
generalizations.add(generalizationInstance);
}
classInstance._generalizations(generalizations);
if (isDirectSubTypeofAny) {
MilestoningClassProcessor.addMilestoningProperty(classInstance, this.context, this.processorSupport, this.repository);
}
return classInstance;
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance 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.multiplicity.MultiplicityInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method buildMultiplicity.
private Multiplicity buildMultiplicity(MultiplicityArgumentContext ctx) {
if (ctx.identifier() == null) {
if ((ctx.fromMultiplicity() == null || "1".equals(ctx.fromMultiplicity().getText())) && "1".equals(ctx.toMultiplicity().getText())) {
if (this.pureOne == null) {
this.pureOne = (Multiplicity) this.processorSupport.package_getByUserPath(M3Paths.PureOne);
}
return this.pureOne;
} else if ((ctx.fromMultiplicity() == null || "0".equals(ctx.fromMultiplicity().getText())) && "*".equals(ctx.toMultiplicity().getText())) {
if (this.zeroMany == null) {
this.zeroMany = (Multiplicity) this.processorSupport.package_getByUserPath(M3Paths.ZeroMany);
}
return this.zeroMany;
} else if ((ctx.fromMultiplicity() == null || "0".equals(ctx.fromMultiplicity().getText())) && "0".equals(ctx.toMultiplicity().getText())) {
if (this.pureZero == null) {
this.pureZero = (Multiplicity) this.processorSupport.package_getByUserPath(M3Paths.PureZero);
}
return this.pureZero;
} else if (ctx.fromMultiplicity() != null && "0".equals(ctx.fromMultiplicity().getText()) && "1".equals(ctx.toMultiplicity().getText())) {
if (this.zeroOne == null) {
this.zeroOne = (Multiplicity) this.processorSupport.package_getByUserPath(M3Paths.ZeroOne);
}
return this.zeroOne;
} else if (ctx.fromMultiplicity() != null && "1".equals(ctx.fromMultiplicity().getText()) && "*".equals(ctx.toMultiplicity().getText())) {
if (this.oneMany == null) {
this.oneMany = (Multiplicity) this.processorSupport.package_getByUserPath(M3Paths.OneMany);
}
return this.oneMany;
} else {
String f = ctx.fromMultiplicity() == null ? "*".equals(ctx.toMultiplicity().getText()) ? "0" : ctx.toMultiplicity().getText() : ctx.fromMultiplicity().getText();
MultiplicityInstance instance = MultiplicityInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.toMultiplicity().getStart()));
MultiplicityValueInstance lower = MultiplicityValueInstance.createPersistent(this.repository);
lower._value(Long.parseLong(f));
MultiplicityValueInstance upper = MultiplicityValueInstance.createPersistent(this.repository);
if (!"*".equals(ctx.toMultiplicity().getText())) {
upper._value(Long.parseLong(ctx.toMultiplicity().getText()));
}
instance._lowerBound(lower);
instance._upperBound(upper);
return instance;
}
} else {
Token src = ctx.toMultiplicity() == null ? ctx.identifier().getStart() : ctx.toMultiplicity().getStart();
MultiplicityInstance instance = MultiplicityInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(src));
instance._multiplicityParameter(ctx.identifier().getText());
return instance;
}
}
Aggregations