Search in sources :

Example 1 with SectionIndex

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-sdlc by finos.

the class PureDomainDeserializer method deserialize.

@Override
public Entity deserialize(String content) throws IOException {
    PureModelContextData pureModelContextData;
    try {
        pureModelContextData = this.pureParser.parseModel(content);
    } catch (EngineException e) {
        throw new RuntimeException(EngineException.buildPrettyErrorMessage(e.getMessage(), e.getSourceInformation(), e.getErrorType()), e);
    }
    List<PackageableElement> elements = pureModelContextData.getElements();
    if ((elements.size() != 2) || Iterate.noneSatisfy(elements, e -> e instanceof SectionIndex)) {
        throw new RuntimeException("Unexpected parsing result (element count: " + elements.size() + ", SectionIndex present: " + Iterate.anySatisfy(elements, e -> e instanceof SectionIndex) + ")");
    }
    PackageableElement element = elements.get((elements.get(0) instanceof SectionIndex) ? 1 : 0);
    String classifierPath = getClassifierPath(element);
    String intermediateJson = this.jsonMapper.writeValueAsString(element);
    Map<String, Object> entityContent = this.jsonMapper.readValue(intermediateJson, this.jsonMapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class));
    return Entity.newEntity(element.getPath(), classifierPath, entityContent);
}
Also used : Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) PureGrammarParser(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Association(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Association) Enumeration(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Enumeration) IOException(java.io.IOException) Reader(java.io.Reader) Iterate(org.eclipse.collections.impl.utility.Iterate) EntityTextSerializer(org.finos.legend.sdlc.serialization.EntityTextSerializer) Profile(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Profile) List(java.util.List) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Map(java.util.Map) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Writer(java.io.Writer) PureProtocolObjectMapperFactory(org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Map(java.util.Map) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)

Example 2 with SectionIndex

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-engine by finos.

the class TestSectionRoundtrip method testGrammarRoundtripWithoutSectionIndex.

@Test
public void testGrammarRoundtripWithoutSectionIndex() {
    ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
    // NOTE: stress test to account for flakiness
    for (int i = 0; i < 500; i++) {
        StringBuilder code = new StringBuilder();
        for (int t = 200; t > 0; t--) {
            code.append("Class model::class").append(t).append("\n").append("{\n").append("  prop: String[1];\n").append("}\n");
            if (t != 1) {
                code.append("\n");
            }
        }
        PureModelContextData modelData = null;
        try {
            modelData = PureGrammarParser.newInstance().parseModel(code.toString());
            String json = objectMapper.writeValueAsString(modelData);
            modelData = objectMapper.readValue(json, PureModelContextData.class);
            List<PackageableElement> elements = ListIterate.select(modelData.getElements(), element -> !(element instanceof SectionIndex));
            modelData = PureModelContextData.newPureModelContextData(modelData.getSerializer(), modelData.getOrigin(), elements);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
        Assert.assertEquals(code.toString(), grammarTransformer.renderPureModelContextData(modelData));
    }
}
Also used : PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) Test(org.junit.Test)

Example 3 with SectionIndex

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-sdlc by finos.

the class PureEntitySerializer method deserializeToElement.

private PackageableElement deserializeToElement(String content) {
    PureModelContextData pureModelContextData = this.pureParser.parseModel(content);
    List<PackageableElement> elements = pureModelContextData.getElements();
    switch(elements.size()) {
        case 0:
            {
                throw new RuntimeException("No element found");
            }
        case 1:
            {
                if (elements.get(0) instanceof SectionIndex) {
                    throw new RuntimeException("No element found");
                }
                return elements.get(0);
            }
        case 2:
            {
                if (elements.get(0) instanceof SectionIndex) {
                    validateSectionIndex((SectionIndex) elements.get(0));
                    if (elements.get(1) instanceof SectionIndex) {
                        throw new RuntimeException("No element found");
                    }
                    return elements.get(1);
                }
                if (elements.get(1) instanceof SectionIndex) {
                    validateSectionIndex((SectionIndex) elements.get(1));
                    return elements.get(0);
                }
                throw new RuntimeException("Expected one element, found 2");
            }
        default:
            {
                int sectionIndexCount = Iterate.count(elements, e -> e instanceof SectionIndex);
                String message = (sectionIndexCount > 1) ? ("Expected at most one SectionIndex, found " + sectionIndexCount) : ("Expected one element, found " + (elements.size() - sectionIndexCount));
                throw new RuntimeException(message);
            }
    }
}
Also used : Section(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.Section) OutputStream(java.io.OutputStream) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Logger(org.slf4j.Logger) PureGrammarParser(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser) LoggerFactory(org.slf4j.LoggerFactory) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) IOException(java.io.IOException) Reader(java.io.Reader) Iterate(org.eclipse.collections.impl.utility.Iterate) ListIterate(org.eclipse.collections.impl.utility.ListIterate) RenderStyle(org.finos.legend.engine.shared.core.api.grammar.RenderStyle) StandardCharsets(java.nio.charset.StandardCharsets) EntityTextSerializer(org.finos.legend.sdlc.serialization.EntityTextSerializer) List(java.util.List) PureGrammarComposerContext(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposerContext) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) ImportAwareCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.ImportAwareCodeSection) Writer(java.io.Writer) Optional(java.util.Optional) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) Collections(java.util.Collections) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)

Example 4 with SectionIndex

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-engine by finos.

the class TestRelationalMappingGrammarParser method testLocalMappingPropertyParsing.

@Test
public void testLocalMappingPropertyParsing() throws Exception {
    String val = "###Mapping\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + "    Person: Relational\n" + "    {\n" + "        firstName : [db]personTable.firstName,\n" + "        +localProp : String[1] : [db]personTable.firstName\n" + "    }\n" + ")\n";
    PureModelContextData data = PureGrammarParser.newInstance().parseModel(val);
    String expected = "{\"_type\":\"data\",\"serializer\":null,\"origin\":null,\"elements\":[{\"_type\":\"mapping\",\"name\":\"myMapping\",\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":2,\"startColumn\":1,\"endLine\":9,\"endColumn\":1},\"classMappings\":[{\"_type\":\"relational\",\"id\":null,\"mappingClass\":null,\"extendsClassMappingId\":null,\"root\":false,\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":4,\"startColumn\":5,\"endLine\":8,\"endColumn\":5},\"classSourceInformation\":{\"sourceId\":\"\",\"startLine\":4,\"startColumn\":5,\"endLine\":4,\"endColumn\":10},\"primaryKey\":[],\"propertyMappings\":[{\"_type\":\"relationalPropertyMapping\",\"property\":{\"property\":\"firstName\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":9,\"endLine\":6,\"endColumn\":17},\"class\":\"Person\"},\"source\":null,\"target\":null,\"localMappingProperty\":null,\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":19,\"endLine\":6,\"endColumn\":45},\"enumMappingId\":null,\"relationalOperation\":{\"_type\":\"column\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":21,\"endLine\":6,\"endColumn\":45},\"table\":{\"_type\":\"Table\",\"table\":\"personTable\",\"schema\":\"default\",\"database\":\"db\",\"mainTableDb\":\"db\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":25,\"endLine\":6,\"endColumn\":35}},\"tableAlias\":\"personTable\",\"column\":\"firstName\"},\"bindingTransformer\":null},{\"_type\":\"relationalPropertyMapping\",\"property\":{\"property\":\"localProp\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":10,\"endLine\":7,\"endColumn\":18},\"class\":null},\"source\":null,\"target\":null,\"localMappingProperty\":{\"type\":\"String\",\"multiplicity\":{\"lowerBound\":1,\"upperBound\":1},\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":20,\"endLine\":7,\"endColumn\":30}},\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":32,\"endLine\":7,\"endColumn\":58},\"enumMappingId\":null,\"relationalOperation\":{\"_type\":\"column\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":34,\"endLine\":7,\"endColumn\":58},\"table\":{\"_type\":\"Table\",\"table\":\"personTable\",\"schema\":\"default\",\"database\":\"db\",\"mainTableDb\":\"db\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":38,\"endLine\":7,\"endColumn\":48}},\"tableAlias\":\"personTable\",\"column\":\"firstName\"},\"bindingTransformer\":null}],\"mainTable\":null,\"distinct\":false,\"groupBy\":[],\"filter\":null,\"class\":\"Person\"}],\"includedMappings\":[],\"associationMappings\":[],\"enumerationMappings\":[],\"tests\":[],\"package\":\"mappingPackage\"},{\"_type\":\"sectionIndex\",\"name\":\"SectionIndex\",\"sourceInformation\":null,\"sections\":[{\"_type\":\"importAware\",\"parserName\":\"Pure\",\"elements\":[],\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":1,\"startColumn\":1,\"endLine\":1,\"endColumn\":8},\"imports\":[]},{\"_type\":\"importAware\",\"parserName\":\"Mapping\",\"elements\":[\"mappingPackage::myMapping\"],\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":2,\"startColumn\":8,\"endLine\":11,\"endColumn\":2},\"imports\":[]}],\"package\":\"__internal__\"}]}";
    Assert.assertEquals(expected, PureProtocolObjectMapperFactory.getNewObjectMapper().writeValueAsString(data));
}
Also used : PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Test(org.junit.Test)

Example 5 with SectionIndex

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-engine by finos.

the class PackageableElementFirstPassBuilder method visit.

@Override
public PackageableElement visit(SectionIndex sectionIndex) {
    // NOTE: we stub out since this element doesn't have an equivalent packageable element form in PURE metamodel
    org.finos.legend.pure.m3.coreinstance.Package pack = this.context.pureModel.getOrCreatePackage(sectionIndex._package);
    PackageableElement stub = new Root_meta_pure_metamodel_PackageableElement_Impl("")._package(pack)._name(sectionIndex.name);
    pack._childrenAdd(stub);
    // NOTE: we don't really need to add section index to the PURE graph
    ListIterate.forEach(sectionIndex.sections, section -> ListIterate.forEach(section.elements, elementPath -> this.context.pureModel.sectionsIndex.putIfAbsent(elementPath, section)));
    return stub;
}
Also used : Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PackageableElementVisitor(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElementVisitor) EngineErrorType(org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Mapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.Mapping) ListIterate(org.eclipse.collections.impl.utility.ListIterate) DataElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.data.DataElement) FastList(org.eclipse.collections.impl.list.mutable.FastList) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) UserDefinedFunctionHandler(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.UserDefinedFunctionHandler) org.finos.legend.pure.generated(org.finos.legend.pure.generated) List(java.util.List) PackageableConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.PackageableConnection) org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain) PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Lists(org.eclipse.collections.impl.factory.Lists) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) ListIterable(org.eclipse.collections.api.list.ListIterable) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) TypeAndMultiplicity(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity) PackageableRuntime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.PackageableRuntime) PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement)

Aggregations

PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)5 SectionIndex (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex)5 PackageableElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)4 IOException (java.io.IOException)3 List (java.util.List)3 ListIterate (org.eclipse.collections.impl.utility.ListIterate)3 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Reader (java.io.Reader)2 Writer (java.io.Writer)2 Iterate (org.eclipse.collections.impl.utility.Iterate)2 PureGrammarParser (org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser)2 PureGrammarComposer (org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer)2 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)2 ImportAwareCodeSection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.ImportAwareCodeSection)2 Section (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.Section)2 Logger (org.slf4j.Logger)2 OutputStream (java.io.OutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collections (java.util.Collections)1