Skip to content

Commit

Permalink
Implement API for getting function descriptor from function protocol (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-kim1 authored Apr 12, 2024
1 parent 9fd99f6 commit 0aeb2d7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,27 @@ public static String printFullPath(String fullPath, DEPRECATED_PureGrammarCompos
return fullPath;
}

public static String getFunctionDescriptor(Function function)
{
StringBuilder builder = new StringBuilder();
String packageName = function._package;
String functionName = getFunctionNameWithNoPackage(function);
String functionSignature = LazyIterate.collect(function.parameters, HelperValueSpecificationGrammarComposer::getFunctionDescriptorParameterSignature).select(Objects::nonNull).makeString(",");
String returnTypeSignature = getClassSignature(function.returnType);
String returnMultiplicitySignature = HelperDomainGrammarComposer.renderMultiplicity(function.returnMultiplicity);
builder.append(packageName)
.append("::")
.append(functionName)
.append("(")
.append(functionSignature)
.append("):")
.append(returnTypeSignature)
.append("[")
.append(returnMultiplicitySignature)
.append("]");
return builder.toString();
}

public static String getFunctionName(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function fn)
{
int signatureIndex = fn.name.indexOf(getFunctionSignature(fn));
Expand All @@ -272,6 +293,11 @@ private static String getParameterSignature(Variable p)
return p._class != null ? getClassSignature(p._class) + "_" + getMultiplicitySignature(p.multiplicity) : null;
}

private static String getFunctionDescriptorParameterSignature(Variable p)
{
return p._class != null ? getClassSignature(p._class) + "[" + HelperDomainGrammarComposer.renderMultiplicity(p.multiplicity) + "]" : null;
}

private static String getClassSignature(String _class)
{
if (_class == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.utility.ListIterate;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser;
import org.finos.legend.engine.language.pure.grammar.to.HelperValueSpecificationGrammarComposer;
import org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer;
import org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposerContext;
import org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.identity.Identity;
import org.finos.legend.engine.shared.core.identity.factory.*;
Expand All @@ -39,6 +42,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TestGrammarParser
{
Expand Down Expand Up @@ -181,6 +185,56 @@ public void testFunction()
);
}

@Test
public void testGetFunctionDescriptor()
{
PureModelContextData pmcd = TestGrammarParserTestSuite.test("###Pure \n" +
"function model::test(name: String[1], isTrue: Boolean[*]): String[*]\n" +
"{ \n" +
" 'test'; \n" +
"} \n");
String testFunctionName = "model::test_String_1__Boolean_MANY__String_MANY_";
List<PackageableElement> testFunction = pmcd.getElements().stream().filter(el -> testFunctionName.equals(el.getPath())).collect(Collectors.toList());
Assert.assertEquals(1, testFunction.size());
Assert.assertEquals("model::test(String[1],Boolean[*]):String[*]",
HelperValueSpecificationGrammarComposer.getFunctionDescriptor((Function) testFunction.get(0)));
}

@Test
public void testGetFunctionDescriptorEmptyParameter()
{
PureModelContextData pmcd = TestGrammarParserTestSuite.test("###Pure \n" +
"function model::test(): Any[1]\n" +
"{ \n" +
" 'test'; \n" +
"} \n");
String testFunctionName = "model::test__Any_1_";
List<PackageableElement> testFunction = pmcd.getElements().stream().filter(el -> testFunctionName.equals(el.getPath())).collect(Collectors.toList());
Assert.assertEquals(1, testFunction.size());
Assert.assertEquals("model::test():Any[1]",
HelperValueSpecificationGrammarComposer.getFunctionDescriptor((Function) testFunction.get(0)));
}

@Test
public void testGetFunctionDescriptorClassInParametersAndReturnValue()
{
PureModelContextData pmcd = TestGrammarParserTestSuite.test("###Pure \n" +
"Class model::TestClass\n" +
"{\n" +
" name: String[1];\n" +
"}\n" +
"\n" +
"function model::test(myClass: model::TestClass[1]): model::TestClass[1]\n" +
"{ \n" +
" $myClass; \n" +
"} \n");
String testFunctionName = "model::test_TestClass_1__TestClass_1_";
List<PackageableElement> testFunction = pmcd.getElements().stream().filter(el -> testFunctionName.equals(el.getPath())).collect(Collectors.toList());
Assert.assertEquals(1, testFunction.size());
Assert.assertEquals("model::test(TestClass[1]):TestClass[1]",
HelperValueSpecificationGrammarComposer.getFunctionDescriptor((Function) testFunction.get(0)));
}

public static void testFromJson(Class<?> _class, String path, String code)
{
PureModelContextData modelData = null;
Expand Down

0 comments on commit 0aeb2d7

Please sign in to comment.