use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping in project legend-engine by finos.
the class RelationalCompilerExtension method getExtraAssociationMappingProcessors.
@Override
public List<Function3<AssociationMapping, Mapping, CompileContext, AssociationImplementation>> getExtraAssociationMappingProcessors() {
return Collections.singletonList((associationMapping, parentMapping, context) -> {
if (associationMapping instanceof RelationalAssociationMapping) {
RelationalAssociationMapping relationalAssociationImplementation = (RelationalAssociationMapping) associationMapping;
RelationalAssociationImplementation base = new Root_meta_relational_mapping_RelationalAssociationImplementation_Impl("");
final org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Association pureAssociation = context.resolveAssociation(relationalAssociationImplementation.association, associationMapping.sourceInformation);
MutableList<EmbeddedRelationalInstanceSetImplementation> embeddedRelationalPropertyMappings = Lists.mutable.empty();
MutableList<Store> stores = ListIterate.collect(relationalAssociationImplementation.stores, context::resolveStore);
// NOTE: we set the association before processing the property mappings, so we can resolve the correct property in the association
// in the case where the user does not provide the class name of where the association property comes from
base._association(pureAssociation);
RichIterable<org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping> rpm = ListIterate.collect(relationalAssociationImplementation.propertyMappings, propertyMapping -> HelperRelationalBuilder.processAbstractRelationalPropertyMapping(propertyMapping, context, base, null, embeddedRelationalPropertyMappings, HelperMappingBuilder.getAllEnumerationMappings(parentMapping), Maps.mutable.empty()));
base._stores(stores)._propertyMappings(rpm)._parent(parentMapping);
parentMapping._classMappingsAddAll(embeddedRelationalPropertyMappings);
return base;
}
return null;
});
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping in project legend-engine by finos.
the class RelationalGrammarComposerExtension method getExtraAssociationMappingComposers.
@Override
public List<Function2<AssociationMapping, PureGrammarComposerContext, String>> getExtraAssociationMappingComposers() {
return Lists.mutable.with((associationMapping, context) -> {
if (associationMapping instanceof RelationalAssociationMapping) {
RelationalGrammarComposerContext ctx = RelationalGrammarComposerContext.Builder.newInstance(context).build();
RelationalAssociationMapping relationalAssociationMapping = (RelationalAssociationMapping) associationMapping;
StringBuilder associationMappingBuilder = new StringBuilder();
associationMappingBuilder.append(relationalAssociationMapping.association).append(": ").append("Relational\n");
associationMappingBuilder.append(PureGrammarComposerUtility.getTabString()).append("{\n");
associationMappingBuilder.append(getTabString(2)).append("AssociationMapping").append("\n");
associationMappingBuilder.append(PureGrammarComposerUtility.getTabString(2)).append("(").append("\n");
if (!relationalAssociationMapping.propertyMappings.isEmpty()) {
RelationalGrammarComposerContext indentedContext = RelationalGrammarComposerContext.Builder.newInstance(ctx).withIndentation(6).build();
associationMappingBuilder.append(LazyIterate.collect(relationalAssociationMapping.propertyMappings, propertyMapping -> HelperRelationalGrammarComposer.renderAbstractRelationalPropertyMapping(propertyMapping, indentedContext, true)).makeString(",\n"));
associationMappingBuilder.append("\n");
}
associationMappingBuilder.append(PureGrammarComposerUtility.getTabString(2)).append(")\n").append(getTabString()).append("}");
return associationMappingBuilder.toString();
}
return null;
});
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping in project legend-engine by finos.
the class MappingParseTreeWalker method visitMappingElement.
private Mapping visitMappingElement(MappingParserGrammar.MappingElementContext ctx, Mapping mapping) {
String parserName = ctx.parserName().getText();
// Construct the mapping element string (with spacing) to be dispatched to another parser
// NOTE: we want to preserve the spacing so we can correctly produce source information in the dispatched parser
StringBuilder mappingElementStringBuilder = new StringBuilder();
for (MappingParserGrammar.MappingElementBodyContentContext fragment : ctx.mappingElementBody().mappingElementBodyContent()) {
mappingElementStringBuilder.append(fragment.getText());
}
String mappingElementCode = mappingElementStringBuilder.length() > 0 ? mappingElementStringBuilder.substring(0, mappingElementStringBuilder.length() - 1) : mappingElementStringBuilder.toString();
// prepare island grammar walker source information
int startLine = ctx.mappingElementBody().BRACE_OPEN().getSymbol().getLine();
int lineOffset = walkerSourceInformation.getLineOffset() + startLine - 1;
// only add current walker source information column offset if this is the first line
int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + ctx.mappingElementBody().BRACE_OPEN().getSymbol().getCharPositionInLine() + ctx.mappingElementBody().BRACE_OPEN().getText().length();
ParseTreeWalkerSourceInformation mappingElementWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(mapping.getPath(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
MappingElementSourceCode mappingElementSourceCode = new MappingElementSourceCode(mappingElementCode, parserName, mappingElementWalkerSourceInformation, ctx, this.walkerSourceInformation);
MappingElementParser extraParser = this.extensions.getExtraMappingElementParser(mappingElementSourceCode.name);
if (extraParser == null) {
throw new EngineException("No parser for " + mappingElementSourceCode.name, this.walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
}
Object mappingElement = extraParser.parse(mappingElementSourceCode, this.parserContext);
if (mappingElement instanceof ClassMapping) {
mapping.classMappings.add((ClassMapping) mappingElement);
} else if (mappingElement instanceof EnumerationMapping) {
mapping.enumerationMappings.add((EnumerationMapping) mappingElement);
} else if (mappingElement instanceof AssociationMapping) {
mapping.associationMappings.add((AssociationMapping) mappingElement);
} else {
throw new EngineException("Invalid parser result for " + mappingElementSourceCode.name + ": " + mappingElement, this.walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
}
return mapping;
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping in project legend-engine by finos.
the class HelperMappingBuilder method processAssociationImplementation.
public static AssociationImplementation processAssociationImplementation(AssociationMapping associationMapping, CompileContext context, Mapping parentMapping) {
if (associationMapping instanceof XStoreAssociationMapping) {
XStoreAssociationMapping xStoreAssociationMapping = (XStoreAssociationMapping) associationMapping;
XStoreAssociationImplementation base = new Root_meta_pure_mapping_xStore_XStoreAssociationImplementation_Impl("");
final org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Association pureAssociation = context.resolveAssociation(xStoreAssociationMapping.association);
MutableList<Store> stores = ListIterate.collect(xStoreAssociationMapping.stores, context::resolveStore);
base._association(pureAssociation)._stores(stores)._parent(parentMapping)._propertyMappings(ListIterate.collect(xStoreAssociationMapping.propertyMappings, propertyMapping -> propertyMapping.accept(new PropertyMappingBuilder(context, parentMapping, base, HelperMappingBuilder.getAllClassMappings(parentMapping)))));
return base;
}
return context.getCompilerExtensions().getExtraAssociationMappingProcessors().stream().map(processor -> processor.value(associationMapping, parentMapping, context)).filter(Objects::nonNull).findFirst().orElseThrow(() -> new UnsupportedOperationException("Unsupported association mapping type '" + associationMapping.getClass() + "'"));
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping in project legend-engine by finos.
the class CorePureGrammarParser method parseXStoreAssociationMapping.
private static AssociationMapping parseXStoreAssociationMapping(MappingElementSourceCode mappingElementSourceCode, PureGrammarParserContext parserContext) {
MappingParserGrammar.MappingElementContext ctx = mappingElementSourceCode.mappingElementParserRuleContext;
SourceCodeParserInfo parserInfo = getXStoreAssociationMappingParserInfo(mappingElementSourceCode);
XStoreAssociationMappingParseTreeWalker walker = new XStoreAssociationMappingParseTreeWalker(parserInfo.walkerSourceInformation, parserInfo.input, parserContext);
XStoreAssociationMapping xStoreAssociationMapping = new XStoreAssociationMapping();
xStoreAssociationMapping.id = ctx.mappingElementId() != null ? ctx.mappingElementId().getText() : null;
xStoreAssociationMapping.association = PureGrammarParserUtility.fromQualifiedName(ctx.qualifiedName().packagePath() == null ? Collections.emptyList() : ctx.qualifiedName().packagePath().identifier(), ctx.qualifiedName().identifier());
xStoreAssociationMapping.sourceInformation = parserInfo.sourceInformation;
walker.visitXStoreAssociationMapping((XStoreAssociationMappingParserGrammar.XStoreAssociationMappingContext) parserInfo.rootContext, xStoreAssociationMapping);
return xStoreAssociationMapping;
}
Aggregations