Search in sources :

Example 1 with M3AntlrParser

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser in project legend-pure by finos.

the class TestSimpleGrammar method testMappingAssociation.

@Test
public void testMappingAssociation() {
    Loader.parseM3("import other::*;\n" + "\n" + "Class other::Person\n" + "{\n" + "    name:String[1];\n" + "}\n" + "Class other::Firm\n" + "{\n" + "    legalName:String[1];\n" + "}\n" + "Association other::Firm_Person\n" + "{\n" + "    firm:Firm[1];\n" + "    employees:Person[1];\n" + "}\n" + "###Relational\n" + "Database mapping::db(\n" + "   Table employeeFirmDenormTable\n" + "   (\n" + "    id INT PRIMARY KEY,\n" + "    name VARCHAR(200),\n" + "    firmId INT,\n" + "    legalName VARCHAR(200)\n" + "   )\n" + "   Join firmJoin(employeeFirmDenormTable.firmId = {target}.firmId)\n" + ")\n" + "###Mapping\n" + "import other::*;\n" + "import mapping::*;\n" + "Mapping mappingPackage::subMapping1\n" + "(\n" + "    Person[per1]: Relational\n" + "    {\n" + "        name : [db]employeeFirmDenormTable.name\n" + "    }\n" + "    Firm[fir1]: Relational\n" + "    {\n" + "        legalName : [db]employeeFirmDenormTable.legalName\n" + "    }\n" + "\n" + "    Firm_Person: Relational\n" + "    {\n" + "        AssociationMapping\n" + "        (\n" + "           employees[fir1,per1] : [db]@firmJoin,\n" + "           firm[per1,fir1] : [db]@firmJoin\n" + "        )\n" + "    }\n" + ")\n", this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
    CoreInstance mapping = this.graphWalker.getMapping("mappingPackage::subMapping1");
    Assert.assertNotNull(mapping);
    ListIterable<? extends CoreInstance> associationMappings = this.graphWalker.getAssociationMappings(mapping);
    Assert.assertEquals(1, associationMappings.size());
    CoreInstance associationMapping = associationMappings.getFirst();
    Assert.assertNotNull(associationMapping);
    CoreInstance employeesMapping = this.graphWalker.getClassMappingImplementationPropertyMapping(associationMapping, "employees");
    Assert.assertNotNull(employeesMapping);
    Assert.assertEquals("fir1", employeesMapping.getValueForMetaPropertyToOne(M2MappingProperties.sourceSetImplementationId).getName());
    Assert.assertEquals("per1", employeesMapping.getValueForMetaPropertyToOne(M2MappingProperties.targetSetImplementationId).getName());
    CoreInstance firmPropertyMapping = this.graphWalker.getClassMappingImplementationPropertyMapping(associationMapping, "firm");
    Assert.assertNotNull(firmPropertyMapping);
    Assert.assertEquals("per1", firmPropertyMapping.getValueForMetaPropertyToOne(M2MappingProperties.sourceSetImplementationId).getName());
    Assert.assertEquals("fir1", firmPropertyMapping.getValueForMetaPropertyToOne(M2MappingProperties.targetSetImplementationId).getName());
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 2 with M3AntlrParser

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser in project legend-pure by finos.

the class TestSimpleGrammar method testMapping.

@Test
public void testMapping() {
    Loader.parseM3("import other::deep::*;\n" + "import other::*;\n" + "\n" + "Class other::Person\n" + "{\n" + "    name:String[1];\n" + "    firm:Firm[1];\n" + "}\n" + "Class other::deep::Firm\n" + "{\n" + "    legalName:String[1];\n" + "    employees:Person[1];\n" + "}\n" + "###Relational\n" + "Database mapping::pack::db(Table employeeTable\n" + "(\n" + "    id INT PRIMARY KEY,\n" + "    name VARCHAR(200),\n" + "    firmId INT\n" + ")\n" + "Table firmTable\n" + "(\n" + "    id INT PRIMARY KEY,\n" + "    name VARCHAR(200)\n" + ")\n" + "\n" + "Join Employee_Firm\n" + "(\n" + "    employeeTable.firmId = [mapping::pack::db]firmTable.id\n" + "))\n" + "###Mapping\n" + "import other::deep::*;\n" + "import mapping::pack::*;\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + "    other::Person: Relational\n" + "            {\n" + "                name : [db]employeeTable.name,\n" + "                firm : [db]@Employee_Firm\n" + "            }\n" + "    Firm : Relational\n" + "           {\n" + "                legalName: [db]firmTable.name,\n" + "                employees: [db]@Employee_Firm\n" + "           }\n" + ")\n", this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
    CoreInstance db = this.graphWalker.getDbInstance("mapping::pack::db");
    Assert.assertNotNull(db);
    ListIterable<? extends CoreInstance> schemas = this.graphWalker.getSchemas(db);
    Assert.assertEquals(1, schemas.size());
    CoreInstance defaultSchema = this.graphWalker.getDefaultSchema(db);
    Assert.assertNotNull(defaultSchema);
    Assert.assertNotNull(this.graphWalker.getTable(defaultSchema, "employeeTable"));
    Assert.assertNotNull(this.graphWalker.getTable(defaultSchema, "firmTable"));
    CoreInstance employeeTable = this.graphWalker.getTable(defaultSchema, "employeeTable");
    CoreInstance firmTable = this.graphWalker.getTable(defaultSchema, "firmTable");
    ListIterable<? extends CoreInstance> employeeTableColumns = this.graphWalker.getColumns(employeeTable);
    ListIterable<? extends CoreInstance> firmTableColumns = this.graphWalker.getColumns(firmTable);
    Assert.assertEquals(3, employeeTableColumns.size());
    Assert.assertEquals(2, firmTableColumns.size());
    Assert.assertNotNull(this.graphWalker.getColumn(employeeTable, "id"));
    Assert.assertNotNull(this.graphWalker.getColumn(employeeTable, "name"));
    Assert.assertNotNull(this.graphWalker.getColumn(firmTable, "id"));
    Assert.assertNotNull(this.graphWalker.getColumn(firmTable, "name"));
    Assert.assertEquals(200, this.graphWalker.getColumnSize(this.graphWalker.getColumn(firmTable, "name")));
    Assert.assertEquals("Varchar", this.graphWalker.getClassifier(this.graphWalker.getColumnType(this.graphWalker.getColumn(firmTable, "name"))));
    CoreInstance mapping = this.graphWalker.getMapping("mappingPackage::myMapping");
    Assert.assertNotNull(mapping);
    Assert.assertEquals(2, this.graphWalker.getClassMappings(mapping).size());
    CoreInstance personMapping = this.graphWalker.getClassMapping(mapping, "Person");
    Assert.assertNotNull(personMapping);
    CoreInstance firmMapping = this.graphWalker.getClassMapping(mapping, "Firm");
    Assert.assertNotNull(firmMapping);
    Assert.assertEquals("employeeTable", this.graphWalker.getName(this.graphWalker.getClassMappingImplementationMainTable(personMapping)));
    Assert.assertEquals("firmTable", this.graphWalker.getName(this.graphWalker.getClassMappingImplementationMainTable(firmMapping)));
    Assert.assertEquals(2, this.graphWalker.getClassMappingImplementationPropertyMappings(personMapping).size());
    Assert.assertEquals(2, this.graphWalker.getClassMappingImplementationPropertyMappings(firmMapping).size());
    CoreInstance namePropMapping = this.graphWalker.getClassMappingImplementationPropertyMapping(personMapping, "name");
    CoreInstance firmPropMapping = this.graphWalker.getClassMappingImplementationPropertyMapping(personMapping, "firm");
    Assert.assertNotNull(firmPropMapping);
    CoreInstance nameColumnAlias = this.graphWalker.getClassMappingImplementationPropertyMappingRelationalOperationElement(namePropMapping);
    Assert.assertEquals("employeeTable", this.graphWalker.getTableAliasColumnAliasName(nameColumnAlias));
    Assert.assertEquals("name", this.graphWalker.getTableAliasColumnColumnName(nameColumnAlias));
    CoreInstance firmJoinNode = this.graphWalker.getRelationalOperationElementJoinTreeNode(this.graphWalker.getClassMappingImplementationPropertyMappingRelationalOperationElement(firmPropMapping));
    Assert.assertEquals("Employee_Firm", this.graphWalker.getRelationalOperationElementJoinTreeNodeJoinName(firmJoinNode));
    CoreInstance legalNamePropMapping = this.graphWalker.getClassMappingImplementationPropertyMapping(firmMapping, "legalName");
    CoreInstance legalNameColumnAlias = this.graphWalker.getClassMappingImplementationPropertyMappingRelationalOperationElement(legalNamePropMapping);
    Assert.assertEquals("firmTable", this.graphWalker.getTableAliasColumnAliasName(legalNameColumnAlias));
    Assert.assertEquals("name", this.graphWalker.getTableAliasColumnColumnName(legalNameColumnAlias));
    CoreInstance employeesPropMapping = this.graphWalker.getClassMappingImplementationPropertyMappingRelationalOperationElement(firmPropMapping);
    CoreInstance employeesJoinNode = this.graphWalker.getRelationalOperationElementJoinTreeNode(employeesPropMapping);
    Assert.assertEquals("Employee_Firm", this.graphWalker.getRelationalOperationElementJoinTreeNodeJoinName(employeesJoinNode));
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 3 with M3AntlrParser

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser in project legend-pure by finos.

the class TestSimpleGrammar method testMappingIncludes.

@Test
public void testMappingIncludes() {
    String pureCode = "import other::*;\n" + "\n" + "Class other::Person\n" + "{\n" + "    name:String[1];\n" + "    firm:Firm[1];\n" + "}\n" + "Class other::Firm\n" + "{\n" + "    legalName:String[1];\n" + "    employees:Person[1];\n" + "}\n" + "###Relational\n" + "Database mapping::db(\n" + "   Table employeeFirmDenormTable\n" + "   (\n" + "    id INT PRIMARY KEY,\n" + "    name VARCHAR(200),\n" + "    firmId INT,\n" + "    legalName VARCHAR(200)\n" + "   )\n" + "   Join firmJoin(employeeFirmDenormTable.firmId = {target}.firmId)\n" + ")\n" + "###Mapping\n" + "import other::*;\n" + "import mapping::*;\n" + "import mappingPackage::*;\n" + "Mapping mappingPackage::subMapping1\n" + "(\n" + "    Person: Relational\n" + "    {\n" + "        name : [db]employeeFirmDenormTable.name\n" + "    }\n" + ")\n" + "Mapping mappingPackage::subMapping2\n" + "(\n" + "    Firm: Relational\n" + "    {\n" + "        legalName : [db]employeeFirmDenormTable.legalName\n" + "    }\n" + ")\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + "    include mappingPackage::subMapping1\n" + "    include subMapping2\n" + ")\n";
    Loader.parseM3(pureCode, this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
    CoreInstance mapping = this.graphWalker.getMapping("mappingPackage::myMapping");
    Assert.assertNotNull(mapping);
    ListIterable<? extends CoreInstance> includes = this.graphWalker.getMany(mapping, M3Properties.includes);
    Assert.assertEquals(2, includes.size());
    MutableList<String> includedMappingPaths = includes.collect(new Function<CoreInstance, String>() {

        @Override
        public String valueOf(CoreInstance include) {
            return PackageableElement.getUserPathForPackageableElement(graphWalker.getOne(include, M3Properties.included));
        }
    }, FastList.<String>newList(2));
    Verify.assertListsEqual(Lists.fixedSize.with("mappingPackage::subMapping1", "mappingPackage::subMapping2"), includedMappingPaths);
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 4 with M3AntlrParser

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser in project legend-pure by finos.

the class TestViewProcessing method testViewInSchemaPotentialIssue.

@Test
public void testViewInSchemaPotentialIssue() {
    Loader.parseM3("###Relational\n" + " Database db(\n" + "    Schema ep_Datastore(\n" + "       Table Team( TEAM VARCHAR(200) PRIMARY KEY  )\n" + "    )\n" + "    \n" + "    Schema viewSchema(\n" + "       View TeamDistinct(\n" + "             ~distinct\n" + "              TEAM: ep_Datastore.Team.TEAM PRIMARY KEY \n" + "        ) \n" + "    )\n" + ")", this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
    CoreInstance db = this.graphWalker.getDbInstance("db");
    CoreInstance viewSchema = this.graphWalker.getSchema(db, "viewSchema");
    CoreInstance teamDistinctView = this.graphWalker.getView(viewSchema, "TeamDistinct");
    CoreInstance teamCol = teamDistinctView.getValueForMetaPropertyToOne(M2RelationalProperties.columns);
    CoreInstance teamMappingCol = teamDistinctView.getValueForMetaPropertyToOne(M2RelationalProperties.columnMappings).getValueForMetaPropertyToOne(M2RelationalProperties.relationalOperationElement).getValueForMetaPropertyToOne(M2RelationalProperties.column);
    CoreInstance viewPkCol = teamDistinctView.getValueForMetaPropertyToOne(M2RelationalProperties.primaryKey);
    CoreInstance mainTable = teamDistinctView.getValueForMetaPropertyToOne(M2RelationalProperties.mainTableAlias).getValueForMetaPropertyToOne(M2RelationalProperties.relationalElement);
    Assert.assertEquals(mainTable, teamMappingCol.getValueForMetaPropertyToOne(M3Properties.owner));
    Assert.assertEquals(teamDistinctView, teamCol.getValueForMetaPropertyToOne(M3Properties.owner));
    Assert.assertEquals(teamDistinctView, viewPkCol.getValueForMetaPropertyToOne(M3Properties.owner));
}
Also used : MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 5 with M3AntlrParser

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser in project legend-pure by finos.

the class TestViewProcessing method testPrimaryKeyCannotBeSpecifiedInMapping.

@Test
public void testPrimaryKeyCannotBeSpecifiedInMapping() {
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("'PRIMARY KEY' cannot be specified in mapping");
    Loader.parseM3("Class person::Person\n" + "{\n" + "    name:String[1];\n" + "}\n" + "###Relational\n" + "Database db(\n" + "   Table employeeTable\n" + "   (\n" + "    name VARCHAR(200)\n" + "   )\n" + ")\n" + "###Mapping\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + "    person::Person: Relational\n" + "    {\n" + "       name : [db]employeeTable.name PRIMARY KEY\n" + "    }\n" + ")", this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
}
Also used : MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Aggregations

M3AntlrParser (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser)77 Test (org.junit.Test)68 ParserLibrary (org.finos.legend.pure.m3.serialization.grammar.ParserLibrary)57 RelationalParser (org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser)55 MappingParser (org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser)53 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)29 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)23 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)21 EnumerationMappingParser (org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser)11 M3ProcessorSupport (org.finos.legend.pure.m3.navigation.M3ProcessorSupport)3 AntlrContextToM3CoreInstance (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.AntlrContextToM3CoreInstance)3 Parser (org.finos.legend.pure.m3.serialization.grammar.Parser)2 ModelRepository (org.finos.legend.pure.m4.ModelRepository)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 RuleContext (org.antlr.v4.runtime.RuleContext)1 Token (org.antlr.v4.runtime.Token)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 RichIterable (org.eclipse.collections.api.RichIterable)1 Lists (org.eclipse.collections.api.factory.Lists)1 Sets (org.eclipse.collections.api.factory.Sets)1