Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping Relations to Classes #912

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class M3ToJavaGenerator
StubDef.build("Association", "ImportStub"),
StubDef.build("ClassProjection", "ImportStub"),
StubDef.build("Function", "ImportStub"),
StubDef.build("FunctionDefinition", "ImportStub"),
StubDef.build("Property", "PropertyStub", Sets.immutable.with("Class", "Association")),
StubDef.build("Enum", "EnumStub", Sets.immutable.with("Enumeration"))
).groupByUniqueKey(StubDef::getClassName);
Expand Down Expand Up @@ -1840,7 +1841,9 @@ private String createInterface(CoreInstance instance, String javaPackage, Mutabl
{
CoreInstance generalGenericType = generalization.getValueForMetaPropertyToOne("general");
CoreInstance type = getTypeFromGenericType(generalGenericType);
return getInterfaceName(Objects.requireNonNull(type)) + getTypeArgs(type, generalGenericType, false, true);
String fullyQualifiedInterfaceName = getFullyQualifiedM3InterfaceForCompiledModel(type);
String finalInterfaceName = imports.shouldFullyQualify(fullyQualifiedInterfaceName) ? fullyQualifiedInterfaceName : getInterfaceName(type);
return finalInterfaceName + getTypeArgs(type, generalGenericType, false, true);
}, Sets.mutable.empty()).makeString(", ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ else if (ctx.columnBuilders() != null)
ListIterate.forEach(ctx.columnBuilders().oneColSpec(), oneColSpec ->
{
M3Parser.ColumnNameContext colNameCtx = oneColSpec.columnName();
String colName = colNameCtx.STRING() != null ? this.removeQuotes(colNameCtx.STRING()) : colNameCtx.identifier().getText();
String colName = removeQuotes(colNameCtx.getText());
columnNames.add(this.repository.newStringCoreInstance(colName));
String returnType = null;
if (oneColSpec.anyLambda() != null)
Expand Down Expand Up @@ -2019,7 +2019,7 @@ public GenericType type(TypeContext ctx, MutableList<String> typeParametersNames
c ->
{
M3Parser.ColumnNameContext colNameCtx = c.mayColumnName().columnName();
String colName = colNameCtx != null ? colNameCtx.STRING() != null ? this.removeQuotes(colNameCtx.STRING()) : colNameCtx.identifier().getText() : "";
String colName = colNameCtx != null ? removeQuotes(colNameCtx.getText()) : "";
return _Column.getColumnInstance(
c.mayColumnName().QUESTION() != null ? "" : colName,
c.mayColumnName().QUESTION() != null,
Expand Down Expand Up @@ -3270,7 +3270,7 @@ private ListIterable<TaggedValue> taggedValues(TaggedValuesContext ctx, ImportGr
private TaggedValue taggedValue(ImportGroup importId, TaggedValueContext ctx)
{
ImportStubInstance importStubInstance = ImportStubInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().getStart(), ctx.identifier().getStart(), ctx.identifier().getStop()), this.getQualifiedNameString(ctx.qualifiedName()) + "%" + ctx.identifier().getText(), importId);
return TaggedValueInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.STRING().get(0).getSymbol(), ctx.STRING().get(ctx.STRING().size() - 1).getSymbol()), importStubInstance, Lists.mutable.withAll(ctx.STRING()).collect(this::removeQuotes).makeString());
return TaggedValueInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.STRING().get(0).getSymbol(), ctx.STRING().get(ctx.STRING().size() - 1).getSymbol()), importStubInstance, Lists.mutable.withAll(ctx.STRING()).collect(AntlrContextToM3CoreInstance::removeQuotes).makeString());
}

private DefaultValue defaultValue(DefaultValueContext ctx, ImportStub isOwner, ImportGroup importId, String propertyName)
Expand Down Expand Up @@ -3335,9 +3335,9 @@ else if (ctx.defaultValueExpressionsArray() != null)
return result;
}

private String removeQuotes(TerminalNode stringNode)
public static String removeQuotes(TerminalNode stringNode)
{
return stringNode.getText().substring(1, stringNode.getText().length() - 1);
return removeQuotes(stringNode.getText());
}

private String packageToString(PackagePathContext ctx)
Expand Down Expand Up @@ -3962,7 +3962,7 @@ private InstanceValue doWrap(PropertyNameContext content)
return this.doWrap(Lists.mutable.with(stringInstance), content.getStart());
}

private static String removeQuotes(String name)
public static String removeQuotes(String name)
{
name = name.trim();
return name.startsWith("'") ? name.substring(1, name.length() - 1) : name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@

package org.finos.legend.pure.m3.tests.elements.relation;

import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiledPlatform;
import org.finos.legend.pure.m4.exception.PureCompilationException;
import org.finos.legend.pure.m3.tests.RuntimeTestScriptBuilder;
import org.finos.legend.pure.m3.tests.RuntimeVerifier;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class TestRelationTypeInference extends AbstractPureTestWithCoreCompiledPlatform
Expand Down Expand Up @@ -753,4 +760,57 @@ private void deleteInferenceTest()
runtime.delete(inferenceTestFileName);
runtime.compile();
}

@Test
@Ignore
// Test fails due to bug in type inference, potentially due to incorrect unbind. The type (RelationType) for the
// only expression in the function f() is correctly inferred after first compile. However, the type parameters
// remain unresolved post unbind and re-compile.
public void testRelationTypeInferenceIntegrityWithSelect()
{
String functionSource = "import meta::pure::metamodel::relation::*;" +
"function f(t:Relation<(value:Integer,str:String)>[1]):Relation<Any>[1]\n" +
"{\n" +
" $t->select(~[value, str])\n" +
"}";
String nativeFunctionSource = "import meta::pure::metamodel::relation::*;" +
"native function select<T,X>(x:Relation<X>[1], rel:ColSpecArray<T⊆X>[1]):Relation<T>[1];";

ImmutableMap<String, String> sources = Maps.immutable.of("1.pure", functionSource, "2.pure", nativeFunctionSource);

new RuntimeTestScriptBuilder().createInMemorySources(sources).compile().run(runtime, functionExecution);

RuntimeVerifier.deleteCompileAndReloadMultipleTimesIsStable(
runtime,
functionExecution,
Lists.fixedSize.of(Tuples.pair("2.pure", nativeFunctionSource)),
this.getAdditionalVerifiers()
);
}

@Test
@Ignore
// Test fails due to bug in type inference, potentially due to incorrect unbind. The column type for the renamed
// column in the RelationType returned by the function is set to null post unbind and re-compile.
public void testRelationTypeInferenceIntegrityWithRename()
{
String functionSource = "import meta::pure::metamodel::relation::*;" +
"function f(t:Relation<(value:Integer,str:String)>[1]):Relation<Any>[1]\n" +
"{\n" +
" $t->rename(~value, ~newValue)\n" +
"}";
String nativeFunctionSource = "import meta::pure::metamodel::relation::*;" +
"native function rename<T,Z,K,V>(r:Relation<T>[1], old:ColSpec<Z=(?:K)⊆T>[1], new:ColSpec<V=(?:K)>[1]):Relation<T-Z+V>[1];";

ImmutableMap<String, String> sources = Maps.immutable.of("1.pure", functionSource, "2.pure", nativeFunctionSource);

new RuntimeTestScriptBuilder().createInMemorySources(sources).compile().run(runtime, functionExecution);

RuntimeVerifier.deleteCompileAndReloadMultipleTimesIsStable(
runtime,
functionExecution,
Lists.fixedSize.of(Tuples.pair("2.pure", nativeFunctionSource)),
this.getAdditionalVerifiers()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<phase>initialize</phase>
<configuration>
<target>
<copy todir="target/antlr">
<fileset dir="../../../legend-pure-core/legend-pure-m3-core/src/main/antlr4/org/finos/legend/pure/m3/serialization/grammar/m3parser/antlr/core" />
</copy>
<copy todir="target/antlr">
<fileset dir="../../../legend-pure-core/legend-pure-m4/src/main/antlr4/org/finos/legend/pure/m4/serialization/grammar/core" />
</copy>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
lexer grammar RelationMappingLexer;

import M3CoreLexer;

RELATION_FUNCTION : '~func' ;

WHITESPACE: Whitespace -> skip ;
COMMENT: Comment -> skip ;
LINE_COMMENT: LineComment -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parser grammar RelationMappingParser;

import M3CoreParser;

options
{
tokenVocab = RelationMappingLexer;
}

mapping: RELATION_FUNCTION qualifiedName
(singlePropertyMapping (COMMA singlePropertyMapping)*)?
EOF
;

singlePropertyMapping: ((PLUS qualifiedName COLON qualifiedName multiplicity) | qualifiedName) COLON columnName
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright 2024 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.RelationMappingLexer;
import org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.antlr.RelationMappingGraphBuilder;
import org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.processor.RelationFunctionInstanceSetImplementationProcessor;
import org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.unloader.RelationFunctionInstanceSetImplementationUnbind;
import org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.validator.RelationFunctionInstanceSetImplementationValidator;
import org.finos.legend.pure.m3.compiler.Context;
import org.finos.legend.pure.m3.coreinstance.CoreInstanceFactoryRegistry;
import org.finos.legend.pure.m3.navigation.M3ProcessorSupport;
import org.finos.legend.pure.m3.serialization.grammar.Parser;
import org.finos.legend.pure.m3.serialization.grammar.ParserLibrary;
import org.finos.legend.pure.m3.serialization.runtime.SourceState;
import org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer;
import org.finos.legend.pure.m3.serialization.runtime.navigation.NavigationHandler;
import org.finos.legend.pure.m3.statelistener.M3M4StateListener;
import org.finos.legend.pure.m3.tools.matcher.MatchRunner;
import org.finos.legend.pure.m4.ModelRepository;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrDescriptiveErrorListener;
import org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation;
import org.finos.legend.pure.m4.serialization.grammar.antlr.PureAntlrErrorStrategy;
import org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException;

import static org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.ParsingUtils.isAntlrRecognitionExceptionUsingFastParser;

public class RelationMappingParser implements Parser
{
@Override
public String getName()
{
return "Relation";
}

@Override
public void parse(String string, String sourceName, boolean addLines, int offset, ModelRepository repository, MutableList<CoreInstance> coreInstancesResult, M3M4StateListener listener, Context context, int count, SourceState oldState) throws PureParserException
{
throw new RuntimeException("Not implemented");
}

@Override
public String parseMapping(String content, String id, String extendsId, String setSourceInfo, boolean root, String classPath, String classSourceInfo, String mappingPath, String sourceName, int offset, String importId, ModelRepository repository, Context context) throws PureParserException
{
return parseMapping(true, content, id, extendsId, setSourceInfo, root, classPath, classSourceInfo, mappingPath, sourceName, offset, importId, repository, context);
}

public String parseMapping(boolean useFastParser, String content, String id, String extendsId, String setSourceInfo, boolean root, String classPath, String classSourceInfo, String mappingPath, String sourceName, int offset, String importId, ModelRepository repository, Context context)
{
AntlrSourceInformation sourceInformation = new AntlrSourceInformation(offset, 0, sourceName, true);
org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.RelationMappingParser parser = initAntlrParser(useFastParser, content, sourceInformation);

try
{
RelationMappingGraphBuilder visitor = new RelationMappingGraphBuilder(importId, repository, new M3ProcessorSupport(context, repository), sourceInformation);
org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.RelationMappingParser.MappingContext c = parser.mapping();
return visitor.visitMapping(c, id, extendsId, setSourceInfo, root, classPath, classSourceInfo, mappingPath);
}
catch (Exception e)
{
if (isAntlrRecognitionExceptionUsingFastParser(useFastParser, e))
{
return this.parseMapping(false, content, id, extendsId, setSourceInfo, root, classPath, classSourceInfo, mappingPath, sourceName, offset, importId, repository, context);
}
else
{
throw e;
}
}
}

@Override
public Parser newInstance(ParserLibrary library)
{
return new RelationMappingParser();
}

@Override
public RichIterable<MatchRunner> getProcessors()
{
return FastList.newListWith(new RelationFunctionInstanceSetImplementationProcessor());
}

@Override
public RichIterable<MatchRunner> getUnLoadWalkers()
{
return Lists.immutable.empty();
}

@Override
public RichIterable<MatchRunner> getUnLoadUnbinders()
{

return FastList.newListWith(new RelationFunctionInstanceSetImplementationUnbind());
}

@Override
public RichIterable<MatchRunner> getValidators()
{
return FastList.newListWith(new RelationFunctionInstanceSetImplementationValidator());
}

@Override
public RichIterable<NavigationHandler> getNavigationHandlers()
{
return Lists.immutable.empty();
}

@Override
public RichIterable<ExternalReferenceSerializer> getExternalReferenceSerializers()
{
return Lists.immutable.empty();
}

@Override
public SetIterable<String> getRequiredParsers()
{
return Sets.immutable.with("Pure", "Mapping");
}

@Override
public ListIterable<String> getRequiredFiles()
{
return Lists.immutable.with("/platform_dsl_mapping/grammar/mapping.pure");
}

@Override
public RichIterable<CoreInstanceFactoryRegistry> getCoreInstanceFactoriesRegistry()
{
return Lists.immutable.empty();
}

private static org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.RelationMappingParser initAntlrParser(boolean fastParser, String code, AntlrSourceInformation sourceInformation)
{
AntlrDescriptiveErrorListener pureErrorListener = new AntlrDescriptiveErrorListener(sourceInformation);

RelationMappingLexer lexer = new RelationMappingLexer(new ANTLRInputStream(code));
lexer.removeErrorListeners();
lexer.addErrorListener(pureErrorListener);

org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.RelationMappingParser parser = new org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.RelationMappingParser(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(pureErrorListener);
parser.setErrorHandler(new PureAntlrErrorStrategy(sourceInformation));
parser.getInterpreter().setPredictionMode(fastParser ? PredictionMode.SLL : PredictionMode.LL);
return parser;
}
}
Loading
Loading