use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.AssociationImplementation in project legend-pure by finos.
the class MappingValidator method validateId.
public static SetImplementation validateId(AssociationImplementation associationMapping, PropertyMapping propertyMapping, MapIterable<String, ? extends SetImplementation> classMappingIndex, String setImplementationId, String sourceOrTarget, ProcessorSupport processorSupport) {
SetImplementation setImplementation = classMappingIndex.get(setImplementationId);
Property property = (Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport);
if ("target".equals(sourceOrTarget)) {
// Target, so need to find the property on the other end
Association association = (Association) ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport);
property = association._properties().select(Predicates.notEqual(property)).getFirst();
}
if (setImplementation == null) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Unable to find " + sourceOrTarget + " class mapping (id:" + setImplementationId + ") for property \'" + propertyMapping._propertyCoreInstance().getName() + "\' in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\'. Make sure that you have specified a valid Class mapping id as the source id and target id, using the syntax \'property[sourceId, targetId]: ...\'.");
}
Class _class = (Class) ImportStub.withImportStubByPass(setImplementation._classCoreInstance(), processorSupport);
String propertyName = property.getName();
if (processorSupport.class_findPropertyUsingGeneralization(_class, propertyName) == null) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Association mapping property \'" + propertyName + "\' in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\' is not a property of source class \'" + _class.getName() + "\'. Make sure that you have specified a valid source id.");
}
return setImplementation;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.AssociationImplementation in project legend-pure by finos.
the class MappingValidator method validateAssociationMappings.
private static void validateAssociationMappings(Mapping mapping, final ProcessorSupport processorSupport) {
RichIterable<? extends AssociationImplementation> associationMappings = mapping._associationMappings();
if (!associationMappings.isEmpty()) {
MapIterable<String, ? extends SetImplementation> classMappingIndex = org.finos.legend.pure.m2.dsl.mapping.Mapping.getClassMappingsByIdIncludeEmbedded(mapping, processorSupport);
// Add association mappings to Class mappings
for (AssociationImplementation associationMapping : associationMappings) {
for (PropertyMapping propertyMapping : associationMapping._propertyMappings()) {
Property property = (Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport);
final String propertyName = property.getName();
SetImplementation sourceClassMapping = validateId(associationMapping, propertyMapping, classMappingIndex, propertyMapping._sourceSetImplementationId(), "source", processorSupport);
String targetId = propertyMapping._targetSetImplementationId();
SetImplementation targetClassMapping = validateId(associationMapping, propertyMapping, classMappingIndex, propertyMapping._targetSetImplementationId(), "target", processorSupport);
if (targetClassMapping instanceof EmbeddedSetImplementation) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Invalid target class mapping for property \'" + propertyName + "\' in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\'. Target \'" + targetId + "\' is an embedded class mapping, embedded mappings are only allowed to be the source in an Association Mapping.");
}
ListIterable<? extends PropertyMapping> sourcePropertyMappings = ((InstanceSetImplementation) sourceClassMapping)._propertyMappings().toList();
PropertyMapping alreadyMapped = sourcePropertyMappings.detect(new Predicate<PropertyMapping>() {
@Override
public boolean accept(PropertyMapping propertyMapping) {
return propertyName.equals(ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport).getName());
}
});
if (alreadyMapped != null) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Property \'" + propertyName + "\' is mapped twice, once in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\' and once in Class mapping \'" + ((Class) ImportStub.withImportStubByPass(targetClassMapping._classCoreInstance(), processorSupport))._name() + "\'. Only one mapping is allowed.");
}
}
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.AssociationImplementation in project legend-pure by finos.
the class AssociationImplementationUnbind method run.
@Override
public void run(AssociationImplementation associationMapping, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ProcessorSupport processorSupport = state.getProcessorSupport();
CoreInstance association = associationMapping._associationCoreInstance();
Shared.cleanUpReferenceUsage(association, associationMapping, processorSupport);
Shared.cleanImportStub(association, processorSupport);
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.AssociationImplementation in project legend-pure by finos.
the class MappingGraphBuilder method visitClassMapping.
@Override
public String visitClassMapping(MappingParser.ClassMappingContext ctx) {
StringBuilder builder = new StringBuilder();
String parserName = ctx.parserName().getText();
if (ctx.superClassMappingId() != null && !("Relational").equals(parserName)) {
throw new PureParserException(this.sourceInformation.getPureSourceInformation(ctx.parserName().getStart()), "Mapping Inheritance feature is applicable only for Class Mappings, not applicable for " + ("Pure".equals(parserName) ? "Model to Model Pure" : parserName) + " Mappings.");
}
Parser parser;
try {
parser = this.parserLibrary.getParser(parserName);
} catch (RuntimeException e) {
throw new PureParserException(this.sourceInformation.getPureSourceInformation(ctx.mappingInstance().CURLY_BRACKET_OPEN().getSymbol()), e.getMessage(), e);
}
String setSourceInfo = this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().packagePath() != null ? ctx.qualifiedName().packagePath().getStart() : ctx.qualifiedName().identifier().getStart(), ctx.qualifiedName().identifier().getStart(), ctx.mappingInstance().mappingInstanceElement().get(ctx.mappingInstance().mappingInstanceElement().size() - 1).getStart()).toM4String();
String classMappingName = ctx.classMappingName() == null ? "default" : ctx.classMappingName().getText();
String classSourceInfo = this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()).toM4String();
StringBuilder mappingInstanceText = new StringBuilder();
for (MappingParser.MappingInstanceElementContext element : ctx.mappingInstance().mappingInstanceElement()) {
mappingInstanceText.append(element.getText());
}
String parseContent = mappingInstanceText.length() > 0 ? mappingInstanceText.substring(0, mappingInstanceText.length() - 1) : mappingInstanceText.toString();
if (parserName.equals("EnumerationMapping")) {
String linesResult = parser.parseMapping(parseContent, ctx.classMappingId() == null ? null : ctx.classMappingId().getText(), null, setSourceInfo, ctx.STAR() != null, ctx.qualifiedName().getText(), this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()).toM4String(), ((MappingParser.MappingContext) ctx.getParent()).qualifiedName().getText(), this.sourceInformation.getSourceName(), this.sourceInformation.getOffsetLine() + ctx.mappingInstance().CURLY_BRACKET_OPEN().getSymbol().getLine() - 1, this.importId, this.repository, this.context);
builder.append("^meta::pure::mapping::EnumerationMapping<Any>").append(classSourceInfo).append("(").append("name = '").append(classMappingName).append("',").append("parent = ").append(((MappingParser.MappingContext) ctx.getParent()).qualifiedName().getText()).append(",").append("enumeration = ").append("^meta::pure::metamodel::import::ImportStub ").append(this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()).toM4String()).append(" (importGroup=system::imports::").append(this.importId).append(", idOrPath='").append(ctx.qualifiedName().getText()).append("')").append(",").append("enumValueMappings = [").append(linesResult).append("]").append(")");
this.results.put("enumerationMappings", builder.toString());
} else {
try {
builder.append(parser.parseMapping(parseContent, ctx.classMappingId() == null ? null : ctx.classMappingId().getText(), ctx.superClassMappingId() == null ? null : ctx.superClassMappingId().getText(), setSourceInfo, ctx.STAR() != null, ctx.qualifiedName().getText(), classSourceInfo, ((MappingParser.MappingContext) ctx.getParent()).qualifiedName().getText(), this.sourceInformation.getSourceName(), this.sourceInformation.getOffsetLine() + ctx.mappingInstance().CURLY_BRACKET_OPEN().getSymbol().getLine() - 1, this.importId, this.repository, this.context));
} catch (PureException e) {
if (e.getSourceInformation() != null) {
throw e;
}
throw new PureParserException(this.sourceInformation.getPureSourceInformation(ctx.mappingInstance().CURLY_BRACKET_OPEN().getSymbol()), e.getInfo(), e);
} catch (RuntimeException e) {
PureException pe = PureException.findPureException(e);
if ((pe != null) && (pe.getSourceInformation() != null)) {
throw e;
}
throw new PureParserException(this.sourceInformation.getPureSourceInformation(ctx.mappingInstance().CURLY_BRACKET_OPEN().getSymbol()), e.getMessage(), e);
}
String mapping = builder.toString();
if (mapping.contains("AssociationImplementation")) {
this.results.put("associationMappings", mapping);
} else {
this.results.put("classMappings", mapping);
}
}
return null;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.AssociationImplementation in project legend-pure by finos.
the class AssociationImplementationProcessor method process.
@Override
public void process(AssociationImplementation associationMapping, ProcessorState state, Matcher matcher, ModelRepository repository, Context context, ProcessorSupport processorSupport) {
Association association = (Association) ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport);
if (association == null) {
throw new PureCompilationException(associationMapping.getSourceInformation(), "Association mapping missing association");
}
if (associationMapping._id() == null) {
String id = PackageableElement.getUserPathForPackageableElement(association, "_");
associationMapping._id(id);
}
for (PropertyMapping propertyMapping : associationMapping._propertyMappings()) {
PropertyMappingProcessor.processPropertyMapping(propertyMapping, repository, processorSupport, association, associationMapping);
}
}
Aggregations