use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ElementWithStereotypes in project legend-pure by finos.
the class MilestoningFunctions method isGeneratedMilestoningProperty.
public static boolean isGeneratedMilestoningProperty(CoreInstance property, ProcessorSupport processorSupport, String stereotype, String milestoningPathSuffix) {
if (property instanceof ElementWithStereotypes) {
RichIterable<? extends CoreInstance> stereotypes = ((ElementWithStereotypes) property)._stereotypesCoreInstance();
if (stereotypes.notEmpty()) {
CoreInstance profile = processorSupport.package_getByUserPath(M3Paths.Milestoning);
CoreInstance milestoningStereotype = Profile.findStereotype(profile, stereotype);
return stereotypes.anySatisfy(st -> (st instanceof ImportStub) ? ((ImportStub) st)._idOrPath().endsWith(milestoningPathSuffix) : milestoningStereotype.equals(st));
}
}
return false;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ElementWithStereotypes in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method enumValue.
private CoreInstance enumValue(EnumValueContext ctx, CoreInstance enumeration, ImportGroup importId) {
ListIterable<CoreInstance> stereotypes = null;
ListIterable<TaggedValue> tags = null;
if (ctx.stereotypes() != null) {
stereotypes = this.stereotypes(ctx.stereotypes(), importId);
}
if (ctx.taggedValues() != null) {
tags = this.taggedValues(ctx.taggedValues(), importId);
}
CoreInstance enumValue = this.repository.newCoreInstance(ctx.identifier().getText(), enumeration, this.sourceInformation.getPureSourceInformation(ctx.identifier().getStart()), true);
enumValue.addKeyValue(Lists.immutable.of("Root", "children", "meta", "children", "pure", "children", "metamodel", "children", "type", "children", "Enum", "properties", "name"), this.repository.newStringCoreInstance_cached(ctx.identifier().getText()));
if (stereotypes != null) {
enumValue.setKeyValues(Lists.immutable.of("Root", "children", "meta", "children", "pure", "children", "metamodel", "children", "extension", "children", "ElementWithStereotypes", "properties", "stereotypes"), Lists.mutable.withAll(stereotypes));
}
if (tags != null) {
enumValue.setKeyValues(Lists.immutable.of("Root", "children", "meta", "children", "pure", "children", "metamodel", "children", "extension", "children", "ElementWithTaggedValues", "properties", "taggedValues"), Lists.mutable.<CoreInstance>withAll(tags));
}
return enumValue;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ElementWithStereotypes in project legend-pure by finos.
the class AccessLevelValidator method run.
@Override
public void run(CoreInstance instance, MatcherState state, Matcher matcher, ModelRepository repository, Context context) throws PureCompilationException {
ElementWithStereotypes elementWithStereotypes = ElementWithStereotypesCoreInstanceWrapper.toElementWithStereotypes(instance);
ProcessorSupport processorSupport = state.getProcessorSupport();
ListIterable<Stereotype> stereotypes = AccessLevel.getAccessLevelStereotypes(elementWithStereotypes, processorSupport);
switch(stereotypes.size()) {
case 0:
{
// Do nothing
break;
}
case 1:
{
this.validateExplicitAccessLevel(elementWithStereotypes, AccessLevel.getAccessLevel(elementWithStereotypes, context, processorSupport), processorSupport);
break;
}
default:
{
StringBuilder message = new StringBuilder();
if (Instance.instanceOf(instance, M3Paths.Function, processorSupport)) {
FunctionDescriptor.writeFunctionDescriptor(message, instance, processorSupport);
} else {
org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement.writeUserPathForPackageableElement(message, instance);
}
message.append(" has multiple access level stereotypes");
throw new PureCompilationException(instance.getSourceInformation(), message.toString());
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ElementWithStereotypes in project legend-pure by finos.
the class AccessLevelValidator method validateExplicitAccessLevel.
private void validateExplicitAccessLevel(ElementWithStereotypes instance, AccessLevel accessLevel, ProcessorSupport processorSupport) {
if (accessLevel == AccessLevel.EXTERNALIZABLE) {
// Validate that instance is a non-property concrete function definition
if (!(instance instanceof ConcreteFunctionDefinition) || instance instanceof AbstractProperty) {
throw new PureCompilationException(instance.getSourceInformation(), "Only functions may have an access level of " + accessLevel.getName());
}
// Validate that the function has a name and package
String functionName = ((ConcreteFunctionDefinition) instance)._functionName();
if (functionName == null) {
throw new PureCompilationException(instance.getSourceInformation(), "Functions with access level " + accessLevel.getName() + " must have a function name");
}
if (((ConcreteFunctionDefinition) instance)._package() == null) {
throw new PureCompilationException(instance.getSourceInformation(), "Functions with access level " + accessLevel.getName() + " must have a package");
}
// Validate parameter types and multiplicities
FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(instance);
int i = 1;
for (VariableExpression parameter : functionType._parameters()) {
GenericType genericType = parameter._genericType();
CoreInstance rawType = ImportStub.withImportStubByPass(genericType._rawTypeCoreInstance(), processorSupport);
if (!(rawType instanceof PrimitiveType || M3Paths.Map.equals(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement.getUserPathForPackageableElement(rawType)))) {
StringBuilder message = new StringBuilder("Functions with access level ");
message.append(accessLevel.getName());
message.append(" may only have primitive types or 'Maps' as parameter types; found ");
org.finos.legend.pure.m3.navigation.generictype.GenericType.print(message, genericType, processorSupport);
message.append(" for the type of parameter ");
String name = parameter._name();
if (name.isEmpty()) {
message.append(i);
} else {
message.append("'");
message.append(name);
message.append("'");
}
throw new PureCompilationException(instance.getSourceInformation(), message.toString());
}
Multiplicity multiplicity = parameter._multiplicity();
if (!org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.isToOne(multiplicity, false) && !org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.isZeroToMany(multiplicity)) {
StringBuilder message = new StringBuilder("Functions with access level ");
message.append(accessLevel.getName());
message.append(" may only have parameters with multiplicity 0..1, 1, or *; found ");
org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.print(message, multiplicity, false);
message.append(" for the multiplicity of parameter ");
String name = PrimitiveUtilities.getStringValue(parameter.getValueForMetaPropertyToOne(M3Properties.name));
if (name.isEmpty()) {
message.append(i);
} else {
message.append("'");
message.append(name);
message.append("'");
}
throw new PureCompilationException(instance.getSourceInformation(), message.toString());
}
i++;
}
// Validate return type and multiplicity
GenericType returnType = functionType._returnType();
CoreInstance returnRawType = ImportStub.withImportStubByPass(returnType._rawTypeCoreInstance(), processorSupport);
if ((returnRawType == null) || !(returnRawType instanceof PrimitiveType)) {
StringBuilder message = new StringBuilder("Functions with access level ").append(accessLevel.getName()).append(" may only have primitive types as return types; found ");
org.finos.legend.pure.m3.navigation.generictype.GenericType.print(message, returnType, processorSupport);
throw new PureCompilationException(instance.getSourceInformation(), message.toString());
}
// Validate no function name conflict
Stereotype stereotype = (Stereotype) accessLevel.getStereotype(processorSupport);
ListIterable<? extends CoreInstance> functionsWithSameName = stereotype.getValueInValueForMetaPropertyToManyByIndex(M3Properties.modelElements, IndexSpecifications.getPropertyValueNameIndexSpec(M3Properties.functionName), functionName);
if (functionsWithSameName.size() > 1) {
StringBuilder message = new StringBuilder("Externalizable function name conflict - multiple functions with the name '");
message.append(functionName).append("':");
for (CoreInstance func : functionsWithSameName.toSortedListBy(CoreInstance::getSourceInformation)) {
FunctionDescriptor.writeFunctionDescriptor(message.append("\n\t"), func, processorSupport);
func.getSourceInformation().appendMessage(message.append(" (")).append(')');
}
throw new PureCompilationException(instance.getSourceInformation(), message.toString());
}
} else {
// Check that instance is either a class or a non-property function
if (instance instanceof AbstractProperty || !(instance instanceof Function || instance instanceof Class)) {
throw new PureCompilationException(instance.getSourceInformation(), "Only classes and functions may have an access level");
}
}
// Check that instance has a package
if (((PackageableElement) instance)._package() == null) {
throw new PureCompilationException(instance.getSourceInformation(), "Element '" + instance + "' has an access level but no package");
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ElementWithStereotypes in project legend-pure by finos.
the class ElementWithStereotypesValidator method run.
@Override
public void run(CoreInstance instanceValue, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ElementWithStereotypes elementWithStereotypes = ElementWithStereotypesCoreInstanceWrapper.toElementWithStereotypes(instanceValue);
ImportStub.withImportStubByPasses(elementWithStereotypes._stereotypesCoreInstance().toList(), state.getProcessorSupport());
}
Aggregations