Search in sources :

Example 1 with SourceMutation

use of org.finos.legend.pure.m3.SourceMutation in project legend-pure by finos.

the class IncrementalCompiler_New method compile.

// ----------
// Compile
// ----------
@Override
SourceMutation compile(RichIterable<? extends Source> sources, Iterable<? extends CompilerEventHandler> compilerEventHandlers) throws PureCompilationException, PureParserException {
    MutableSet<CoreInstance> potentialToProcess = this.walkTheGraphForUnload(this.toUnload).withAll(this.toProcess).withAll(this.toUnbind);
    this.unload();
    this.toProcess = this.removeNodesFromRemovedSources(this.toProcess);
    IncrementalCompilerTransaction threadLocalTransaction = this.transactionManager.getThreadLocalTransaction();
    SourceMutation result;
    MutableListMultimap<String, Source> compiledSourcesByRepo = Multimaps.mutable.list.empty();
    if (sources.isEmpty()) {
        // We must compile even if the set of sources is empty, as post-processing or validation may be required for nodes from already compiled sources.
        boolean shouldCreateNewRepoTransaction = this.isTransactionalByDefault && (threadLocalTransaction == null);
        IncrementalCompilerTransaction repoTransaction = shouldCreateNewRepoTransaction ? this.newTransaction(true) : threadLocalTransaction;
        MutableSet<CoreInstance> repoTransactionInstances = Sets.mutable.empty();
        try {
            result = this.compileRepoSources(repoTransaction, "Pure", 1, 1, sources, this.toProcess.toImmutable(), this.toUnbind.toImmutable(), Lists.mutable.<SourceState>with(), repoTransactionInstances);
            if (shouldCreateNewRepoTransaction) {
                repoTransaction.commit();
            }
        } catch (Exception e) {
            if (shouldCreateNewRepoTransaction) {
                this.rollBack(repoTransaction, e, repoTransactionInstances);
            }
            throw e;
        }
    } else {
        result = new SourceMutation();
        Multimap<String, ? extends Source> sourcesByRepo = sources.groupBy(s -> PureCodeStorage.getSourceRepoName(s.getId()));
        Multimap<String, ? extends Source> sourcesByRepoNew = sources.groupBy(PureCodeStorage.GET_SOURCE_REPO);
        Multimap<String, SourceState> sourceStatesByRepo = this.oldSourceStates.groupBy(GET_SOURCE_STATE_REPO);
        Multimap<String, CoreInstance> potentialRepos = potentialToProcess.groupBy(GET_COREINSTANCE_REPO_NAME);
        MutableSet<String> allReposToCompile = Sets.mutable.withAll(sourcesByRepo.keysView()).withAll(potentialRepos.keysView());
        ListIterable<String> repoCompileOrder = allReposToCompile.toSortedList(new RepositoryComparator(this.codeStorage.getAllRepositories()));
        MutableList<String> newCompileOrder = Lists.mutable.empty();
        repoCompileOrder.forEach(repo -> {
            if ((repo != null) && repo.startsWith("model")) {
                if (!newCompileOrder.contains("model-all")) {
                    newCompileOrder.add("model-all");
                }
            } else {
                newCompileOrder.add(repo);
            }
        });
        int repoCount = newCompileOrder.size();
        boolean shouldCreateNewRepoTransactions = this.isTransactionalByDefault && (threadLocalTransaction == null);
        newCompileOrder.forEachWithIndex((repo, i) -> {
            IncrementalCompilerTransaction repoTransaction = shouldCreateNewRepoTransactions ? this.newTransaction(true) : threadLocalTransaction;
            RichIterable<? extends Source> repoSources = sourcesByRepoNew.get(repo);
            RichIterable<CoreInstance> toProcessThisRepo = this.toProcess.selectWith(CORE_INSTANCE_IS_FROM_REPO, repo).toImmutable();
            RichIterable<CoreInstance> toUnbindThisRepo = this.toUnbind.selectWith(CORE_INSTANCE_IS_FROM_REPO, repo).toImmutable();
            MutableSet<CoreInstance> repoTransactionInstances = Sets.mutable.empty();
            SourceMutation repoResult;
            try {
                repoResult = this.compileRepoSources(repoTransaction, repo, i + 1, repoCount, repoSources, toProcessThisRepo, toUnbindThisRepo, sourceStatesByRepo.get(repo), repoTransactionInstances);
                if (shouldCreateNewRepoTransactions) {
                    repoTransaction.commit();
                }
            } catch (Exception e) {
                if (shouldCreateNewRepoTransactions) {
                    this.rollBack(repoTransaction, e, repoTransactionInstances);
                }
                throw e;
            }
            result.merge(repoResult);
            if (repo == null) {
                compiledSourcesByRepo.putAll(repo, repoSources);
            } else if ("model-all".equals(repo)) {
                compiledSourcesByRepo.putAll(repoSources.groupBy(source -> PureCodeStorage.getSourceRepoName(source.getId())));
            } else {
                compiledSourcesByRepo.putAll(repo, repoSources);
            }
        });
    }
    this.runEventHandlers(compilerEventHandlers, this.processed, compiledSourcesByRepo);
    this.processed.clear();
    return result;
}
Also used : PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) PureParserException(org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException) PureException(org.finos.legend.pure.m4.exception.PureException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) SourceMutation(org.finos.legend.pure.m3.SourceMutation)

Example 2 with SourceMutation

use of org.finos.legend.pure.m3.SourceMutation in project legend-pure by finos.

the class PureSession method saveFiles.

public JSONObject saveFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {
    JSONObject mainObject = null;
    try {
        mainObject = (JSONObject) new JSONParser().parse(new InputStreamReader(request.getInputStream()));
        JSONArray openFiles = (JSONArray) mainObject.get("openFiles");
        if ((null != openFiles) && !openFiles.isEmpty()) {
            PureRuntime pureRuntime = this.getPureRuntime();
            MutableList<JSONObject> diagrams = Lists.mutable.empty();
            for (Object rawOpenFile : openFiles) {
                JSONObject openFile = (JSONObject) rawOpenFile;
                if (null == openFile.get("diagram")) {
                    String path = (String) openFile.get("path");
                    String code = (String) openFile.get("code");
                    if (null == pureRuntime.getSourceById(path)) {
                        pureRuntime.loadSourceIfLoadable(path);
                    }
                    pureRuntime.modify(path, code);
                } else {
                    diagrams.add(openFile);
                }
            }
            if (diagrams.notEmpty()) {
                JSONArray modifiedFiles = new JSONArray();
                for (JSONObject d : diagrams) {
                    SourceMutation mutation = pureRuntime.compile();
                    Iterate.addAllIterable(mutation.getModifiedFiles(), modifiedFiles);
                    CoreInstance diagram = pureRuntime.getProcessorSupport().package_getByUserPath(d.get("diagram").toString());
                    if (null != diagram) {
                        SourceInformation sourceInformation = diagram.getSourceInformation();
                        Source source = pureRuntime.getSourceById(sourceInformation.getSourceId());
                        String content = source.getContent();
                        String[] lines = LINE_SPLITTER.split(content);
                        StringBuilder buffer = new StringBuilder(content.length());
                        for (int i = 0; i < sourceInformation.getStartLine() - 1; i++) {
                            buffer.append(lines[i]);
                        }
                        buffer.append(lines[sourceInformation.getStartLine() - 1].substring(0, diagram.getSourceInformation().getStartColumn() - 1));
                        buffer.append(d.get("code"));
                        buffer.append(lines[sourceInformation.getEndLine() - 1].substring(sourceInformation.getEndColumn()));
                        for (int i = sourceInformation.getEndLine(); i < lines.length; i++) {
                            buffer.append(lines[i]);
                        }
                        pureRuntime.modify(sourceInformation.getSourceId(), buffer.toString());
                        modifiedFiles.add(diagram.getSourceInformation().getSourceId());
                    }
                }
                mainObject.put("modifiedFiles", modifiedFiles);
            }
        }
    } catch (Exception e) {
        try (OutputStream outputStream = response.getOutputStream()) {
            outputStream.write(exceptionToJson(this, e, null).getBytes());
        }
    }
    return mainObject;
}
Also used : PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) JSONArray(org.json.simple.JSONArray) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) Source(org.finos.legend.pure.m3.serialization.runtime.Source) IOException(java.io.IOException) JSONObject(org.json.simple.JSONObject) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) SourceMutation(org.finos.legend.pure.m3.SourceMutation)

Example 3 with SourceMutation

use of org.finos.legend.pure.m3.SourceMutation in project legend-pure by finos.

the class TestSourceMutation method testAssociationViewWithNonExistentAssociation.

@Test
public void testAssociationViewWithNonExistentAssociation() {
    compileTestSource("testModel.pure", "Class test::pure::TestClass1 {}\n" + "Class test::pure::TestClass2 {}\n");
    SourceMutation m = compileTestSource("testDiagram.pure", "###Diagram\n" + "Diagram test::pure::TestDiagram(width=10.0, height=10.0)\n" + "{\n" + "    TypeView TestClass1(type=test::pure::TestClass1, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(874.0, 199.46875), width=353.0, height=57.1875)\n" + "    TypeView TestClass2(type=test::pure::TestClass2, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(75.0, 97.1875), width=113.0, height=57.1875)\n" + "    AssociationView TestAssociation(association=test::pure::TestAssociation, stereotypesVisible=true, nameVisible=false,\n" + "                                    color=#000000, lineWidth=1.0,\n" + "                                    lineStyle=SIMPLE, points=[(132.5, 77.0), (155.2, 77.0)],\n" + "                                    label='TestAssociation',\n" + "                                    source=TestClass1,\n" + "                                    target=TestClass2,\n" + "                                    sourcePropertyPosition=(132.5, 76.2),\n" + "                                    sourceMultiplicityPosition=(132.5, 80.0),\n" + "                                    targetPropertyPosition=(155.2, 76.2),\n" + "                                    targetMultiplicityPosition=(155.2, 80.0))\n" + "}\n");
    Verify.assertSetsEqual(Sets.mutable.with("testDiagram.pure"), m.getModifiedFiles().toSet());
    Verify.assertSize(1, m.getLineRangesToRemoveByFile().get("testDiagram.pure"));
    Assert.assertEquals(12, m.getLineRangesToRemoveByFile().get("testDiagram.pure").get(0).getOne());
    Assert.assertEquals(21, m.getLineRangesToRemoveByFile().get("testDiagram.pure").get(0).getTwo());
    CoreInstance testDiagram = this.runtime.getCoreInstance("test::pure::TestDiagram");
    Assert.assertNotNull(testDiagram);
    Verify.assertSize(2, Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.typeViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.associationViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.propertyViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.generalizationViews, this.processorSupport));
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) SourceMutation(org.finos.legend.pure.m3.SourceMutation) Test(org.junit.Test)

Example 4 with SourceMutation

use of org.finos.legend.pure.m3.SourceMutation in project legend-pure by finos.

the class TestSourceMutation method testAssociationViewWithNonExistentTypeViewId.

@Test
public void testAssociationViewWithNonExistentTypeViewId() {
    compileTestSource("testModel.pure", "Class test::pure::TestClass1 {}\n" + "Class test::pure::TestClass2 {}\n" + "Association test::pure::TestAssociation\n" + "{\n" + "    prop1:test::pure::TestClass1[0..1];\n" + "    prop2:test::pure::TestClass2[1..*];\n" + "}\n");
    SourceMutation m = compileTestSource("testDiagram.pure", "###Diagram\n" + "Diagram test::pure::TestDiagram(width=10.0, height=10.0)\n" + "{\n" + "    TypeView TestClass1(type=test::pure::TestClass1, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(874.0, 199.46875), width=353.0, height=57.1875)\n" + "    TypeView TestClass2(type=test::pure::TestClass2, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(75.0, 97.1875), width=113.0, height=57.1875)\n" + "    AssociationView TestAssociation(association=test::pure::TestAssociation, stereotypesVisible=true, nameVisible=false,\n" + "                                    color=#000000, lineWidth=1.0,\n" + "                                    lineStyle=SIMPLE, points=[(132.5, 77.0), (155.2, 77.0)],\n" + "                                    label='TestAssociation',\n" + "                                    source=TestClass1,\n" + "                                    target=TestClass3,\n" + "                                    sourcePropertyPosition=(132.5, 76.2),\n" + "                                    sourceMultiplicityPosition=(132.5, 80.0),\n" + "                                    targetPropertyPosition=(155.2, 76.2),\n" + "                                    targetMultiplicityPosition=(155.2, 80.0))\n" + "}\n");
    // TODO consider whether this is the correct behavior
    Verify.assertSetsEqual(Sets.mutable.with("testDiagram.pure"), m.getModifiedFiles().toSet());
    Verify.assertSize(1, m.getLineRangesToRemoveByFile().get("testDiagram.pure"));
    Assert.assertEquals(12, m.getLineRangesToRemoveByFile().get("testDiagram.pure").get(0).getOne());
    Assert.assertEquals(21, m.getLineRangesToRemoveByFile().get("testDiagram.pure").get(0).getTwo());
    CoreInstance testDiagram = this.runtime.getCoreInstance("test::pure::TestDiagram");
    Assert.assertNotNull(testDiagram);
    Verify.assertSize(2, Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.typeViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.associationViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.propertyViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.generalizationViews, this.processorSupport));
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) SourceMutation(org.finos.legend.pure.m3.SourceMutation) Test(org.junit.Test)

Example 5 with SourceMutation

use of org.finos.legend.pure.m3.SourceMutation in project legend-pure by finos.

the class TestSourceMutation method testAssociationViewWithSourceViewWithNonExistentTypeInTheSameFile.

@Test
public void testAssociationViewWithSourceViewWithNonExistentTypeInTheSameFile() {
    SourceMutation m1 = compileTestSource("testFile.pure", "Class test::pure::TestClass1 {}\n" + "Class test::pure::TestClass2 {}\n" + "Association test::pure::TestAssociation\n" + "{\n" + "  toTC1_1 : test::pure::TestClass1[*];\n" + "  toTC2_1 : test::pure::TestClass2[*];\n" + "}\n" + "" + "###Diagram\n" + "Diagram test::pure::TestDiagram(width=10.0, height=10.0)\n" + "{\n" + "    TypeView TestClass1(type=test::pure::TestClass1, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(874.0, 199.46875), width=353.0, height=57.1875)\n" + "    TypeView TestClass2(type=test::pure::TestClass2, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(75.0, 97.1875), width=113.0, height=57.1875)\n" + "    AssociationView TestAssociation(association=test::pure::TestAssociation, stereotypesVisible=true, nameVisible=false,\n" + "                                    color=#000000, lineWidth=1.0,\n" + "                                    lineStyle=SIMPLE, points=[(132.5, 77.0), (155.2, 77.0)],\n" + "                                    label='TestAssociation',\n" + "                                    source=TestClass1,\n" + "                                    target=TestClass2,\n" + "                                    sourcePropertyPosition=(132.5, 76.2),\n" + "                                    sourceMultiplicityPosition=(132.5, 80.0),\n" + "                                    targetPropertyPosition=(155.2, 76.2),\n" + "                                    targetMultiplicityPosition=(155.2, 80.0))\n" + "}\n");
    Verify.assertEmpty(m1.getLineRangesToRemoveByFile());
    Verify.assertEmpty(m1.getMarkedForDeletion());
    Verify.assertEmpty(m1.getModifiedFiles());
    this.runtime.modify("testFile.pure", "Class test::pure::TestClass1 {}\n" + "Class test::pure::TestClass3 {}\n" + "Association test::pure::TestAssociation\n" + "{\n" + "  toTC1_1 : test::pure::TestClass1[*];\n" + "  toTC2_1 : test::pure::TestClass3[*];\n" + "}\n" + "\n" + "###Diagram\n" + "Diagram test::pure::TestDiagram(width=10.0, height=10.0)\n" + "{\n" + "    TypeView TestClass1(type=test::pure::TestClass1, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(874.0, 199.46875), width=353.0, height=57.1875)\n" + "    TypeView TestClass2(type=test::pure::TestClass2, stereotypesVisible=true, attributesVisible=true,\n" + "                        attributeStereotypesVisible=true, attributeTypesVisible=true,\n" + "                        color=#FFFFCC, lineWidth=1.0,\n" + "                        position=(75.0, 97.1875), width=113.0, height=57.1875)\n" + "    AssociationView TestAssociation(association=test::pure::TestAssociation, stereotypesVisible=true, nameVisible=false,\n" + "                                    color=#000000, lineWidth=1.0,\n" + "                                    lineStyle=SIMPLE, points=[(132.5, 77.0), (155.2, 77.0)],\n" + "                                    label='TestAssociation',\n" + "                                    source=TestClass1,\n" + "                                    target=TestClass2,\n" + "                                    sourcePropertyPosition=(132.5, 76.2),\n" + "                                    sourceMultiplicityPosition=(132.5, 80.0),\n" + "                                    targetPropertyPosition=(155.2, 76.2),\n" + "                                    targetMultiplicityPosition=(155.2, 80.0))\n" + "}\n");
    SourceMutation m2 = this.runtime.compile();
    Verify.assertSetsEqual(Sets.mutable.with("testFile.pure"), m2.getModifiedFiles().toSet());
    Verify.assertSize(2, m2.getLineRangesToRemoveByFile().get("testFile.pure"));
    Assert.assertEquals(16, m2.getLineRangesToRemoveByFile().get("testFile.pure").get(0).getOne());
    Assert.assertEquals(19, m2.getLineRangesToRemoveByFile().get("testFile.pure").get(0).getTwo());
    Assert.assertEquals(20, m2.getLineRangesToRemoveByFile().get("testFile.pure").get(1).getOne());
    Assert.assertEquals(29, m2.getLineRangesToRemoveByFile().get("testFile.pure").get(1).getTwo());
    CoreInstance testDiagram = this.runtime.getCoreInstance("test::pure::TestDiagram");
    Assert.assertNotNull(testDiagram);
    Verify.assertSize(1, Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.typeViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.associationViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.propertyViews, this.processorSupport));
    Verify.assertEmpty(Instance.getValueForMetaPropertyToManyResolved(testDiagram, M3Properties.generalizationViews, this.processorSupport));
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) SourceMutation(org.finos.legend.pure.m3.SourceMutation) Test(org.junit.Test)

Aggregations

SourceMutation (org.finos.legend.pure.m3.SourceMutation)15 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)15 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)5 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)5 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)5 Test (org.junit.Test)5 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 RichIterable (org.eclipse.collections.api.RichIterable)4 Procedure (org.eclipse.collections.api.block.procedure.Procedure)4 Sets (org.eclipse.collections.api.factory.Sets)4 MutableList (org.eclipse.collections.api.list.MutableList)4 Multimap (org.eclipse.collections.api.multimap.Multimap)4 ListMultimap (org.eclipse.collections.api.multimap.list.ListMultimap)4 MutableSet (org.eclipse.collections.api.set.MutableSet)4 ValidationType (org.finos.legend.pure.m3.compiler.validation.ValidationType)4 CoreInstanceFactoryRegistry (org.finos.legend.pure.m3.coreinstance.CoreInstanceFactoryRegistry)4 PureCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage)4 CodeStorage (org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.CodeStorage)4 Parser (org.finos.legend.pure.m3.serialization.grammar.Parser)4 InlineDSL (org.finos.legend.pure.m3.serialization.grammar.m3parser.inlinedsl.InlineDSL)4