Search in sources :

Example 1 with AssociationImplementation

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;
}
Also used : Association(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Association) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with AssociationImplementation

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.");
                }
            }
        }
    }
}
Also used : PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 3 with AssociationImplementation

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);
}
Also used : ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 4 with AssociationImplementation

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;
}
Also used : MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.MappingParser) PureException(org.finos.legend.pure.m4.exception.PureException) PureParserException(org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.MappingParser) Parser(org.finos.legend.pure.m3.serialization.grammar.Parser)

Example 5 with AssociationImplementation

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);
    }
}
Also used : Association(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Association) PropertyMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

AssociationImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.AssociationImplementation)4 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)4 EnumerationMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping)3 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)3 PureInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)2 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)2 Association (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Association)2 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 RichIterable (org.eclipse.collections.api.RichIterable)1 Procedure2 (org.eclipse.collections.api.block.procedure.Procedure2)1 MutableList (org.eclipse.collections.api.list.MutableList)1 ImmutableSet (org.eclipse.collections.api.set.ImmutableSet)1 MutableSet (org.eclipse.collections.api.set.MutableSet)1 SetIterable (org.eclipse.collections.api.set.SetIterable)1 Lists (org.eclipse.collections.impl.factory.Lists)1 Sets (org.eclipse.collections.impl.factory.Sets)1