Search in sources :

Example 1 with M4Parser

use of org.finos.legend.pure.m4.serialization.grammar.M4Parser in project legend-pure by finos.

the class TestCoreM4 method testCodeLineAnnotation.

@Test
public void testCodeLineAnnotation() {
    ModelRepository repository = new ModelRepository();
    new M4Parser().parse("^Class Class ?[a/b/c/file.txt:1,3,1,9,8,9]? \n" + "{\n" + "    Element.properties[name] : 'Class',\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property properties\n" + "                {\n" + "                    Property.properties[type] : Property\n" + "                }\n" + "        ]\n" + "}\n" + "^Class String" + "{" + "}" + "^Class Property" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property type\n" + "                {\n" + "                    Property.properties[type] : String\n" + "                }\n" + "        ]\n" + "}" + "^Class Element ?[otherElement:4,5,4,11,10,19]?\n" + "{\n" + "    Element.properties[name] : 'Element',\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property name ?[file:5,7,5,7,14,22]?\n" + "                {\n" + "                    Property.properties[type] : String\n" + "                }\n" + "        ]\n" + "}\n", repository, new VoidM4StateListener());
    Assert.assertEquals("Class_0(a/b/c/file.txt:1,3,1,9,8,9) instance Class_0(a/b/c/file.txt:1,3,1,9,8,9)\n" + "    name_8(file:5,7,5,7,14,22)(Property_3):\n" + "        Class_2 instance String_1\n" + "    properties_4(Property_3):\n" + "        properties_4 instance Property_3\n" + "            type_5(Property_3):\n" + "                Property_3 instance Class_0(a/b/c/file.txt:1,3,1,9,8,9)\n" + "                    properties_4(Property_3):\n" + "                        type_5 instance Property_3\n" + "                            type_5(Property_3):\n" + "                                String_1 instance Class_0(a/b/c/file.txt:1,3,1,9,8,9)", repository.getTopLevel("Class").printFull(""));
    Assert.assertEquals("Element_6(otherElement:4,5,4,11,10,19) instance Class_0(a/b/c/file.txt:1,3,1,9,8,9)\n" + "    name_8(file:5,7,5,7,14,22)(Property_3):\n" + "        Element_7 instance String_1\n" + "    properties_4(Property_3):\n" + "        name_8(file:5,7,5,7,14,22) instance Property_3\n" + "            type_5(Property_3):\n" + "                String_1 instance Class_0(a/b/c/file.txt:1,3,1,9,8,9)", repository.getTopLevel("Element").printFull(""));
}
Also used : M4Parser(org.finos.legend.pure.m4.serialization.grammar.M4Parser) VoidM4StateListener(org.finos.legend.pure.m4.statelistener.VoidM4StateListener) Test(org.junit.Test)

Example 2 with M4Parser

use of org.finos.legend.pure.m4.serialization.grammar.M4Parser in project legend-pure by finos.

the class TestCoreM4 method testReferentialIntegrity.

@Test
public void testReferentialIntegrity() {
    ModelRepository repository = new ModelRepository();
    try {
        new M4Parser().parse("^Class Class\n" + "{\n" + "    Element.properties[name] : 'Class'\n" + "}\n" + "^Class String" + "{" + "}\n" + "^Class Element\n" + "{\n" + "    Element.properties[name] : 'Element',\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property name\n" + "                {\n" + "                    Property.properties[type] : String\n" + "                }\n" + "        ]\n" + "}\n", repository, new VoidM4StateListener());
        Assert.assertEquals("Class_0 instance Class_0\n" + "    name_6(Property_5):\n" + "        Class_2 instance String_1", repository.getTopLevel("Class").printFull(""));
        try {
            repository.getTopLevel("Element").printFull("");
            Assert.fail();
        } catch (Exception e) {
            Assert.assertEquals("Error resolving path [Class, properties, properties]: 'properties' is unknown for the key 'properties' in 'Class'", e.getMessage());
        }
        repository.validate(new VoidM4StateListener());
        Assert.fail();
    } catch (Exception e) {
        PureException pe = PureException.findPureException(e);
        Assert.assertNotNull(pe);
        Assert.assertTrue(pe instanceof PureCompilationException);
        Assert.assertEquals("Property has not been defined!", pe.getInfo());
    }
}
Also used : PureException(org.finos.legend.pure.m4.exception.PureException) M4Parser(org.finos.legend.pure.m4.serialization.grammar.M4Parser) VoidM4StateListener(org.finos.legend.pure.m4.statelistener.VoidM4StateListener) PureException(org.finos.legend.pure.m4.exception.PureException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) Test(org.junit.Test)

Example 3 with M4Parser

use of org.finos.legend.pure.m4.serialization.grammar.M4Parser in project legend-pure by finos.

the class TestCoreM4 method testNameSpacing.

@Test
public void testNameSpacing() throws Exception {
    ModelRepository repository = new ModelRepository();
    String body = "^Class Class\n" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property properties\n" + "                {\n" + "                    Property.properties[type] : Property\n" + "                }\n" + "        ]\n" + "}\n" + "\n" + "^Class Property\n" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property type\n" + "                {\n" + "                    Property.properties[type] : Class\n" + "                }\n" + "        ]\n" + "}\n" + "\n" + "^Class Package\n" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property children\n" + "            {\n" + "                Property.properties[type] : Class\n" + "            }\n" + "        ]\n" + "}\n" + "\n" + "^Package Root\n" + "{\n" + "    Package.properties[children] :\n" + "        [\n" + "            ^Package subPackage\n" + "            {\n" + "                Package.properties[children] :\n" + "                                [\n" + "                                    ^Class ClassTest\n" + "                                    {\n" + "\n" + "                                    }\n" + "                                ]\n" + "            }\n" + "        ]\n" + "}\n" + "\n" + "^Root.children[subPackage].children[ClassTest] T\n" + "{\n" + "\n" + "}\n" + "\n" + "^Class NewInstance @Root.children[subPackage].children\n" + "{\n" + "\n" + "}";
    new M4Parser().parse(body, repository, new VoidM4StateListener());
    repository.validate(new VoidM4StateListener());
    Assert.assertEquals("T_9 instance ClassTest_8", repository.getTopLevel("T").printFull(""));
    Assert.assertEquals("Root_6 instance Package_4\n" + "    children_5(Property_1):\n" + "        subPackage_7 instance Package_4\n" + "            children_5(Property_1):\n" + "                ClassTest_8 instance Class_0\n" + "                NewInstance_10 instance Class_0", repository.getTopLevel("Root").printFull(""));
}
Also used : M4Parser(org.finos.legend.pure.m4.serialization.grammar.M4Parser) VoidM4StateListener(org.finos.legend.pure.m4.statelistener.VoidM4StateListener) Test(org.junit.Test)

Example 4 with M4Parser

use of org.finos.legend.pure.m4.serialization.grammar.M4Parser in project legend-pure by finos.

the class TestModelRepositoryTransaction method testTopLevelCommit.

@Test
public void testTopLevelCommit() throws Exception {
    ModelRepository repository = new ModelRepository();
    new M4Parser().parse("^Class Class\n" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property properties\n" + "                {\n" + "                    Property.properties[type] : Property\n" + "                }\n" + "        ]\n" + "}\n" + "\n" + "^Class Property\n" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property type\n" + "                {\n" + "                    Property.properties[type] : Class\n" + "                }\n" + "        ]\n" + "}", repository, new VoidM4StateListener());
    repository.validate(new VoidM4StateListener());
    Assert.assertEquals(2, repository.getTopLevels().size());
    ModelRepositoryTransaction transaction = repository.newTransaction(true);
    try (ThreadLocalTransactionContext ignore = transaction.openInCurrentThread()) {
        new M4Parser().parse("^Class Other\n" + "{\n" + "    Class.properties[properties] :\n" + "        [\n" + "            ^Property type\n" + "                {\n" + "                    Property.properties[type] : Class\n" + "                }\n" + "        ]\n" + "}", repository, new VoidM4StateListener());
        Assert.assertEquals(3, repository.getTopLevels().size());
    }
    Assert.assertEquals(2, repository.getTopLevels().size());
    transaction.commit();
    Assert.assertEquals(3, repository.getTopLevels().size());
}
Also used : ModelRepositoryTransaction(org.finos.legend.pure.m4.transaction.ModelRepositoryTransaction) M4Parser(org.finos.legend.pure.m4.serialization.grammar.M4Parser) VoidM4StateListener(org.finos.legend.pure.m4.statelistener.VoidM4StateListener) ThreadLocalTransactionContext(org.finos.legend.pure.m4.transaction.framework.ThreadLocalTransactionContext) Test(org.junit.Test)

Example 5 with M4Parser

use of org.finos.legend.pure.m4.serialization.grammar.M4Parser in project legend-pure by finos.

the class TestSerialization method testSerial.

@Test
public void testSerial() {
    ModelRepository repository = new ModelRepository(370);
    M4StateListener listener = new VoidM4StateListener();
    new M4Parser().parse("^package.children[deep].children[Class] PrimitiveType\n" + "{\n" + "}\n" + "\n" + "^PrimitiveType String\n" + "{\n" + "}\n" + "\n" + "^Package package\n" + "{\n" + "    Package.properties[children] :\n" + "        [\n" + "            ^Package deep\n" + "            {\n" + "                  Package.properties[children] :\n" + "                      [\n" + "                          package.children[deep].children[Class]\n" + "                      ]\n" + "            }\n" + "        ]\n" + "}\n" + "^package.children[deep].children[Class] Class ?[a/b/c/file.txt:1,3,1,9,45,89]? @package.children[deep].children\n" + "{\n" + "    Element.properties[name] : 'Class',\n" + "    package.children[deep].children[Class].properties[properties] :\n" + "        [\n" + "            ^Property properties\n" + "                {\n" + "                    Property.properties[type] : Property\n" + "                }\n" + "        ]\n" + "}\n" + "\n" + "^package.children[deep].children[Class] Element ?[e/f/file2.txt:5,13,5,13,12,16]?\n" + "{\n" + "    Element.properties[name] : 'Element\\u2022',\n" + "    package.children[deep].children[Class].properties[properties] :\n" + "        [\n" + "            ^Property name\n" + "                {\n" + "                    Property.properties[type] : String\n" + "                }\n" + "        ]\n" + "}\n" + "\n" + "^package.children[deep].children[Class] Package ?[t/y/file4.txt:1,2,1,2,12,13]?\n" + "{\n" + "    package.children[deep].children[Class].properties[properties] :\n" + "        [\n" + "            ^Property children\n" + "            {\n" + "                Property.properties[type] : package.children[deep].children[Class]\n" + "            }\n" + "        ]\n" + "}\n" + "\n" + "^package.children[deep].children[Class] Property\n" + "{\n" + "    Element.properties[name] : 'Property',\n" + "    package.children[deep].children[Class].properties[properties] :\n" + "        [\n" + "            ^Property type\n" + "                {\n" + "                    Property.properties[type] : package.children[deep].children[Class]\n" + "                }\n" + "        ]\n" + "}\n", repository, new VoidM4StateListener());
    repository.validate(listener);
    byte[] res = repository.serialize();
    ModelRepository newRepository = new ModelRepository();
    BinaryRepositorySerializer.build(res, newRepository, VoidMessageCallBack.instance());
    newRepository.validate(listener);
    Assert.assertEquals(6, newRepository.getTopLevels().size());
    Assert.assertEquals("PrimitiveType instance Class(a/b/c/file.txt:1,3,1,9,45,89)", newRepository.getTopLevel("PrimitiveType").print("", 10));
    Assert.assertEquals("String instance PrimitiveType", newRepository.getTopLevel("String").print("", 10));
    Assert.assertEquals("package instance Package(t/y/file4.txt:1,2,1,2,12,13)\n" + "    children(Property):\n" + "        deep instance Package(t/y/file4.txt:1,2,1,2,12,13)\n" + "            children(Property):\n" + "                Class(a/b/c/file.txt:1,3,1,9,45,89) instance Class(a/b/c/file.txt:1,3,1,9,45,89)\n" + "                    name(Property):\n" + "                        Class instance String\n" + "                    properties(Property):\n" + "                        properties instance Property\n" + "                            type(Property):\n" + "                                Property instance Class(a/b/c/file.txt:1,3,1,9,45,89)", newRepository.getTopLevel("package").print("", 10));
    Assert.assertEquals("Package(t/y/file4.txt:1,2,1,2,12,13) instance Class(a/b/c/file.txt:1,3,1,9,45,89)\n" + "    properties(Property):\n" + "        children instance Property\n" + "            type(Property):\n" + "                Class(a/b/c/file.txt:1,3,1,9,45,89) instance Class(a/b/c/file.txt:1,3,1,9,45,89)\n" + "                    name(Property):\n" + "                        Class instance String\n" + "                    properties(Property):\n" + "                        properties instance Property\n" + "                            type(Property):\n" + "                                Property instance Class(a/b/c/file.txt:1,3,1,9,45,89)", newRepository.getTopLevel("Package").print("", 10));
    Assert.assertEquals("Property instance Class(a/b/c/file.txt:1,3,1,9,45,89)\n" + "    name(Property):\n" + "        Property instance String\n" + "    properties(Property):\n" + "        type instance Property\n" + "            type(Property):\n" + "                Class(a/b/c/file.txt:1,3,1,9,45,89) instance Class(a/b/c/file.txt:1,3,1,9,45,89)\n" + "                    name(Property):\n" + "                        Class instance String\n" + "                    properties(Property):\n" + "                        properties instance Property\n" + "                            type(Property):\n" + "                                Property instance Class(a/b/c/file.txt:1,3,1,9,45,89)", newRepository.getTopLevel("Property").print("", 10));
    Assert.assertEquals("Element(e/f/file2.txt:5,13,5,13,12,16) instance Class(a/b/c/file.txt:1,3,1,9,45,89)\n" + "    name(Property):\n" + "        Element\u2022 instance String\n" + "    properties(Property):\n" + "        name instance Property\n" + "            type(Property):\n" + "                String instance PrimitiveType", newRepository.getTopLevel("Element").print("", 10));
}
Also used : ModelRepository(org.finos.legend.pure.m4.ModelRepository) M4StateListener(org.finos.legend.pure.m4.statelistener.M4StateListener) VoidM4StateListener(org.finos.legend.pure.m4.statelistener.VoidM4StateListener) M4Parser(org.finos.legend.pure.m4.serialization.grammar.M4Parser) VoidM4StateListener(org.finos.legend.pure.m4.statelistener.VoidM4StateListener) Test(org.junit.Test)

Aggregations

M4Parser (org.finos.legend.pure.m4.serialization.grammar.M4Parser)11 VoidM4StateListener (org.finos.legend.pure.m4.statelistener.VoidM4StateListener)10 Test (org.junit.Test)8 ModelRepository (org.finos.legend.pure.m4.ModelRepository)4 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)2 M4StateListener (org.finos.legend.pure.m4.statelistener.M4StateListener)2 ModelRepositoryTransaction (org.finos.legend.pure.m4.transaction.ModelRepositoryTransaction)2 ThreadLocalTransactionContext (org.finos.legend.pure.m4.transaction.framework.ThreadLocalTransactionContext)2 IOException (java.io.IOException)1 Parser (org.finos.legend.pure.m3.serialization.grammar.Parser)1 M3AntlrParser (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser)1 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)1 PureException (org.finos.legend.pure.m4.exception.PureException)1