Skip to content

Commit

Permalink
Support for pure model context collection
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Nov 7, 2023
1 parent f67d5af commit af485a3
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-shared-core</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-protocol</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-protocol-pure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import java.util.concurrent.ExecutionException;
Expand All @@ -31,8 +32,11 @@
import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContext;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextCollection;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextPointer;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextText;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextVisitor;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.deployment.DeploymentMode;
Expand Down Expand Up @@ -110,22 +114,52 @@ public String getLambdaReturnType(Lambda lambda, PureModelContext context, Strin
// Remove clientVersion
public PureModelContextData loadData(PureModelContext context, String clientVersion, MutableList<CommonProfile> pm)
{
try (Scope scope = tracer.buildSpan("Load Model").startActive(true))
Span span = GlobalTracer.get().buildSpan("Load Model").start();
try (Scope ignored = GlobalTracer.get().activateSpan(span))
{
scope.span().setTag("context", context.getClass().getSimpleName());
if (context instanceof PureModelContextData)
{
return (PureModelContextData) context;
}
else if (context instanceof PureModelContextText)
{
return PureGrammarParser.newInstance().parseModel(((PureModelContextText) context).code);
}
else
span.setTag("context", context.getClass().getSimpleName());

PureModelContextVisitor<PureModelContextData> visitor = new PureModelContextVisitor<PureModelContextData>()
{
ModelLoader loader = this.modelLoaderForContext(context);
return loader.load(pm, context, clientVersion, scope.span());
}
@Override
public PureModelContextData visit(PureModelContextData data)
{
return data;
}

@Override
public PureModelContextData visit(PureModelContextText text)
{
return PureGrammarParser.newInstance().parseModel(text.code);
}

@Override
public PureModelContextData visit(PureModelContextPointer pointer)
{
return this.visit((PureModelContext) pointer);
}

@Override
public PureModelContextData visit(PureModelContextCollection collection)
{
PureModelContextData.Builder builder = PureModelContextData.newBuilder();
collection.contexts.stream().map(x -> x.accept(this)).forEach(builder::addPureModelContextData);
return builder.distinct().sorted().build();
}

@Override
public PureModelContextData visit(PureModelContext catchAll)
{
ModelLoader loader = modelLoaderForContext(catchAll);
return loader.load(pm, catchAll, clientVersion, span);
}
};

return context.accept(visitor);
}
finally
{
span.finish();
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2023 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.finos.legend.engine.language.pure.modelManager;

import io.opentracing.Span;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.MutableList;
import org.finos.legend.engine.protocol.Protocol;
import org.finos.legend.engine.protocol.pure.v1.model.context.PackageableElementPointer;
import org.finos.legend.engine.protocol.pure.v1.model.context.PackageableElementType;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContext;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextCollection;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextPointer;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextText;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureSDLC;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class;
import org.finos.legend.engine.shared.core.deployment.DeploymentMode;
import org.junit.Assert;
import org.junit.Test;
import org.pac4j.core.profile.CommonProfile;

public class TestModelManager
{
@Test
public void testPureModelCaching()
{
MockModelLoader mockModelLoader = new MockModelLoader();
ModelManager manager = new ModelManager(DeploymentMode.TEST, mockModelLoader);
Protocol protocol = new Protocol("pure", "v1_17_0");
PackageableElementPointer element0 = new PackageableElementPointer(PackageableElementType.MAPPING, "meta::relational::tests::milestoning::milestoningmap");
PackageableElementPointer element1 = new PackageableElementPointer(PackageableElementType.STORE, "meta::relational::tests::dbInc");
PackageableElementPointer element2 = new PackageableElementPointer(PackageableElementType.STORE, "meta::relational::tests::db");
List<PackageableElementPointer> list = new ArrayList<>();
list.add(element0);
list.add(element1);
list.add(element2);
PureSDLC pureSDLC = new PureSDLC();
pureSDLC.packageableElementPointers = list;
PureModelContextPointer pointer = new PureModelContextPointer();
pointer.serializer = protocol;
pointer.sdlcInfo = pureSDLC;
manager.loadModel(pointer, null, null, null);
Assert.assertEquals(1, mockModelLoader.loadCount);
manager.loadModel(pointer, null, null, null);
Assert.assertEquals("Should not invoke loader as this is cached", 1, mockModelLoader.loadCount);
}

@Test
public void testCompositeResolving()
{
MockModelLoader mockModelLoader = new MockModelLoader();
ModelManager manager = new ModelManager(DeploymentMode.TEST, mockModelLoader);

Class class1 = new Class();
class1.name = "Class1";
class1._package = "my::package";

Class class2 = new Class();
class2.name = "Class2";
class2._package = "my::package";

Class class3 = new Class();
class3.name = "Class3";
class3._package = "my::package";

PureModelContextData data1 = PureModelContextData.newPureModelContextData(null, null, Lists.mutable.with(class1));
PureModelContextData data2 = PureModelContextData.newPureModelContextData(null, null, Lists.mutable.with(class1));
PureModelContextData data3 = PureModelContextData.newPureModelContextData(null, null, Lists.mutable.with(class2, class3));

PureModelContextText text = new PureModelContextText();
text.code = "Class my::package::Class4{}";

PureModelContextCollection subComposite = new PureModelContextCollection();
subComposite.contexts = Lists.mutable.with(
data1,
data2,
data3
);

PureModelContextCollection composite = new PureModelContextCollection();
composite.contexts = Lists.mutable.with(
subComposite,
text
);

PureModelContextData contextData = manager.loadData(composite, null, null);
Assert.assertEquals(5, contextData.getElements().size());
Assert.assertEquals("__internal__::SectionIndex", contextData.getElements().get(0).getPath());
Assert.assertEquals("my::package::Class1", contextData.getElements().get(1).getPath());
Assert.assertEquals("my::package::Class2", contextData.getElements().get(2).getPath());
Assert.assertEquals("my::package::Class3", contextData.getElements().get(3).getPath());
Assert.assertEquals("my::package::Class4", contextData.getElements().get(4).getPath());
}

private static class MockModelLoader implements ModelLoader
{
private int loadCount = 0;

@Override
public boolean supports(PureModelContext context)
{
return true;
}

@Override
public PureModelContextData load(MutableList<CommonProfile> profiles, PureModelContext context, String clientVersion, Span parentSpan)
{
loadCount++;
PureModelContextPointer pointer = (PureModelContextPointer) context;
return PureModelContextData.newPureModelContextData(null, pointer, Lists.fixedSize.empty());
}

@Override
public void setModelManager(ModelManager modelManager)
{

}

@Override
public boolean shouldCache(PureModelContext context)
{
return this.supports(context);
}

@Override
public PureModelContext cacheKey(PureModelContext context, MutableList<CommonProfile> pm)
{
return context;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
})
public abstract class PureModelContext
{
public <T> T accept(PureModelContextVisitor<T> visitor)
{
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package org.finos.legend.engine.protocol.pure.v1.model.context;

import java.util.List;
import org.eclipse.collections.api.factory.Lists;

public class PureModelContextCollection extends PureModelContext
{
public List<PureModelContext> contexts = Lists.fixedSize.empty();

@Override
public <T> T accept(PureModelContextVisitor<T> visitor)
{
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,9 @@ public PureModelContextData build()
return new PureModelContextData(this.serializer, this.origin, this.elements.toList());
}
}

public <T> T accept(PureModelContextVisitor<T> visitor)
{
return visitor.visit(this);
}
}
Loading

0 comments on commit af485a3

Please sign in to comment.