Search in sources :

Example 1 with Mapping

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping 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 Mapping

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping 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 Mapping

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping 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 Mapping

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping 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)

Example 5 with Mapping

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping in project legend-pure by finos.

the class TestPureRuntimeClassMapping method testMilestoningMappingUnbindStability.

@Test
public void testMilestoningMappingUnbindStability() {
    String modelTrade = "Class my::Trade{\n" + "   id:Integer[1];\n" + "   product:my::Product[1];\n" + "}";
    String modelProduct = "Class my::Product{\n" + "   id:Integer[1];\n" + "}";
    String modelProductTemporal = "Class <<temporal.businesstemporal>> my::Product{\n" + "   id:Integer[1];\n" + "}";
    String storeAndMapping = "###Mapping\n" + "import meta::relational::tests::*;\n" + "import my::*;\n" + "\n" + "Mapping myMapping\n" + "(\n" + "   Trade : Relational {id : [myDB] tradeTable.ID, product : [myDB] @trade_product} \n" + "   Product : Relational { id : [myDB] productTable.ID}\n" + ")\n" + "###Relational\n" + "Database myDB\n" + "(\n" + "   Table tradeTable(ID INT PRIMARY KEY, PRODID INT)\n" + "   Table productTable(ID INT PRIMARY KEY)\n" + "   \n" + "   Join trade_product(tradeTable.PRODID = productTable.ID)\n" + ")";
    String f = "function f():Any[0..1]{let m = myMapping}";
    RuntimeVerifier.verifyOperationIsStable(new RuntimeTestScriptBuilder().createInMemorySource("modelTrade.pure", modelTrade).createInMemorySource("modelProductTemporal.pure", modelProductTemporal).createInMemorySource("storeAndMapping.pure", storeAndMapping).createInMemorySource("f.pure", f).compile(), new RuntimeTestScriptBuilder().deleteSource("modelProductTemporal.pure").createInMemorySource("modelProduct.pure", modelProduct).compile().deleteSource("modelProduct.pure").createInMemorySource("modelProductTemporal.pure", modelProductTemporal).compile(), this.runtime, this.functionExecution, this.getAdditionalVerifiers());
}
Also used : RuntimeTestScriptBuilder(org.finos.legend.pure.m3.RuntimeTestScriptBuilder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)101 M3AntlrParser (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser)51 ParserLibrary (org.finos.legend.pure.m3.serialization.grammar.ParserLibrary)46 MappingParser (org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser)45 RelationalParser (org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser)45 Mapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping)43 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)43 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)43 RootRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)31 PropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping)28 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)28 MutableList (org.eclipse.collections.api.list.MutableList)25 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)24 EmbeddedRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.EmbeddedRelationalInstanceSetImplementation)22 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)20 PureInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)20 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)20 InstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation)19 RichIterable (org.eclipse.collections.api.RichIterable)18 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)18