Skip to content

Commit

Permalink
Different metadata repository for each session
Browse files Browse the repository at this point in the history
  • Loading branch information
vpinna80 committed May 13, 2024
1 parent bb94226 commit 35f3305
Show file tree
Hide file tree
Showing 79 changed files with 628 additions and 577 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
</profiles>

<properties>
<revision>1.1.5-SNAPSHOT</revision>
<revision>1.2.0-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@
*/
public interface ConfigurationManager
{
/**
* Same as {@link ConfigurationManagerFactory#getInstance()}.
*
* @return a default instance of this interface.
*/
public static ConfigurationManager getDefault()
{
return ConfigurationManagerFactory.getInstance();
}

/**
* @return The {@link MetadataRepository} instance
*/
Expand Down Expand Up @@ -82,7 +72,7 @@ public default VTLSession createSession(Reader reader) throws IOException
/**
* @return The {@link List} of {@link Environment} instances
*/
public List<? extends Environment> getEnvironments();
public List<Environment> getEnvironments();

/**
* Saves the current configuration to the provided Writer as a list of Java properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import java.util.Optional;

import it.bancaditalia.oss.vtl.model.data.DataSet;
import it.bancaditalia.oss.vtl.model.data.VTLValue;
import it.bancaditalia.oss.vtl.model.data.VTLValueMetadata;
import it.bancaditalia.oss.vtl.session.MetadataRepository;

/**
* A provider of VTL data objects.
Expand All @@ -37,15 +39,19 @@ public interface Environment
* @param alias The name of requested object.
* @return true if this environment provides the specified object.
*/
public boolean contains(String alias);
public default boolean contains(String alias)
{
return getValueMetadata(alias).isPresent();
}

/**
* Returns an {@link Optional} reference to a VTL object with the specified name in this environment.
*
* @param alias The name of requested object.
* @param repo TODO
* @return An Optional with a reference to the requested object o {@link Optional#empty()} if the object is not found in this environment.
*/
public Optional<VTLValue> getValue(String alias);
public Optional<VTLValue> getValue(MetadataRepository repo, String alias);

/**
* Persistently store the given value in this environment for later use
Expand All @@ -67,18 +73,6 @@ public default boolean store(VTLValue value, String alias)
*/
public default Optional<VTLValueMetadata> getValueMetadata(String alias)
{
return getValue(alias).map(VTLValue::getMetadata);
}

/**
* Implementing classes may override this method if they need to use a particular initialization procedure.
*
* @param configuration Parameters that may be passed to the implementing class.
*
* @return {@code this} instance, initialized.
*/
public default Environment init(Object... configuration)
{
return this;
return getValue(null, alias).map(DataSet.class::cast).map(DataSet::getMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,6 @@ public default <R extends Component, S extends ValueDomainSubset<S, D>, D extend
*/
public DataSetMetadata joinForOperators(DataSetMetadata other);

/**
* Creates a new structure by renaming a component of this {@link DataSetMetadata}.
*
* If a component with the new name already exists, the behaviour is undefined.
*
* @param component the component to rename
* @param newName the new name for the component
* @return the new structure.
*/
public DataSetMetadata rename(DataStructureComponent<?, ?, ?> component, String newName);

/**
* Creates a new structure by pivoting the specified measure over an identifier that is defined on a string enumerated domain (codelist).
*
* @param <S> the domain subset type of the measure
* @param <D> the domain type of the measure
* @param identifier the identifier
* @param measure the measure
* @return the new structure.
*/
public <S extends ValueDomainSubset<S, D>, D extends ValueDomain> DataSetMetadata pivot(DataStructureComponent<Identifier, ?, ?> identifier, DataStructureComponent<Measure, S, D> measure);

/**
* Extracts a singleton component of a given role if it exists in the current structure, or throws an exception otherwise.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import it.bancaditalia.oss.vtl.model.data.Component.Measure;
import it.bancaditalia.oss.vtl.model.domain.ValueDomain;
import it.bancaditalia.oss.vtl.model.domain.ValueDomainSubset;
import it.bancaditalia.oss.vtl.session.MetadataRepository;

/**
* The immutable representation of a component of a dataset.
Expand Down Expand Up @@ -79,7 +80,7 @@ public default boolean is(Class<? extends Component> role)
}

/**
* Narrows the role of this {@link DataStructureComponent} to the specified role if possible.
* Convencience method that narrows the role of this {@link DataStructureComponent} to the specified role if possible.
*
* @param role the role to narrow to
* @return this component with the narrowed role.
Expand All @@ -94,9 +95,17 @@ public default <R2 extends Component> DataStructureComponent<R2, S, D> asRole(Cl
else
throw new ClassCastException("In component " + this + ", cannot cast " + getRole().getSimpleName() + " to " + role.getSimpleName());
}

public default DataStructureComponent<R, S, D> getRenamed(String newName)

/**
* Convenience method that obtains a renamed component in the given {@link MetadataRepository} the same role and domain of this {@link DataStructureComponent}.
*
* @param repo The repository
* @param newName the new name to assign to the new component
* @return the renamed component
*/
@SuppressWarnings("unchecked")
public default DataStructureComponent<R, S, D> getRenamed(MetadataRepository repo, String newName)
{
return getVariable().getRenamed(newName).as(getRole());
return ((Variable<S, D>) repo.createTempVariable(newName, getVariable().getDomain())).as(getRole());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,4 @@ public static String normalizeAlias(String alias)
{
return getDomain().cast(value);
}

public Variable<S, D> getRenamed(String newName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@
*/
package it.bancaditalia.oss.vtl.session;

import java.util.Collection;

import it.bancaditalia.oss.vtl.exceptions.VTLCastException;
import it.bancaditalia.oss.vtl.exceptions.VTLUnboundAliasException;
import it.bancaditalia.oss.vtl.model.data.DataSetMetadata;
import it.bancaditalia.oss.vtl.model.data.Variable;
import it.bancaditalia.oss.vtl.model.domain.ValueDomain;
import it.bancaditalia.oss.vtl.model.domain.ValueDomainSubset;
import it.bancaditalia.oss.vtl.model.rules.DataPointRuleSet;
import it.bancaditalia.oss.vtl.model.rules.HierarchicalRuleSet;
import it.bancaditalia.oss.vtl.model.transform.TransformationScheme;

/**
* A repository to contain and query all the defined domains.
Expand All @@ -38,11 +34,6 @@
*/
public interface MetadataRepository
{
/**
* @return a collection of all {@link ValueDomainSubset}s defined in this {@link MetadataRepository}.
*/
public Collection<ValueDomainSubset<?, ?>> getValueDomains();

/**
* Checks if a {@link ValueDomainSubset} with the specified name exists.
*
Expand Down Expand Up @@ -100,22 +91,14 @@ public interface MetadataRepository
* @return a {@link Variable} instance.
* @throws VTLUnboundAliasException if the alias is not defined.
*/
public <S extends ValueDomainSubset<S, D>, D extends ValueDomain> Variable<S, D> getVariable(String alias, ValueDomainSubset<S, D> domain);
public Variable<?, ?> getVariable(String alias);

public <S extends ValueDomainSubset<S, D>, D extends ValueDomain> Variable<S, D> getDefaultVariable(ValueDomainSubset<S, D> domain);

public TransformationScheme getTransformationScheme(String alias);

/**
* Initialize this {@link MetadataRepository}.
*
* This method should be always called once per instance, before attempting any other operation.
* Creates a temporary variable with provided alias and domain. An error is raised if a persistent variable with a different domain is already defined in metadata.
*
* @param params optional initialization parameters
* @return this instance
* @param alias the alias of the variable
* @param alias the domain of the variable
* @return a {@link Variable} instance.
*/
public default MetadataRepository init(Object... params)
{
return this;
}
public Variable<?, ?> createTempVariable(String alias, ValueDomainSubset<?, ?> domain);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Void call() throws Exception
ConfigurationManagerFactory.loadConfiguration(reader);
}

ConfigurationManager manager = ConfigurationManager.getDefault();
ConfigurationManager manager = ConfigurationManagerFactory.getInstance();

VTLSession session;
try (Reader reader = file != null ? Files.newBufferedReader(file.toPath(), UTF_8) : new BufferedReader(new InputStreamReader(System.in, UTF_8)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import it.bancaditalia.oss.vtl.config.ConfigurationManager;
import it.bancaditalia.oss.vtl.config.ConfigurationManagerFactory;
import it.bancaditalia.oss.vtl.impl.meta.InMemoryMetadataRepository;
import it.bancaditalia.oss.vtl.impl.session.VTLSessionImpl;
import it.bancaditalia.oss.vtl.model.data.DataPoint;
Expand Down Expand Up @@ -96,7 +96,7 @@ public void test(String testName, String testCode)
{
try
{
((InMemoryMetadataRepository) ConfigurationManager.getDefault().getMetadataRepository()).clearVariables();
((InMemoryMetadataRepository) ConfigurationManagerFactory.getInstance().getMetadataRepository()).clearVariables();

System.out.println("------------------------------------------------------------------------------------------------");
System.out.println(" " + testName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import it.bancaditalia.oss.vtl.environment.Workspace;
import it.bancaditalia.oss.vtl.impl.environment.WorkspaceImpl;
import it.bancaditalia.oss.vtl.model.data.VTLValue;
import it.bancaditalia.oss.vtl.session.MetadataRepository;

public class JupyterWorkspace implements Workspace
{
Expand Down Expand Up @@ -60,7 +61,7 @@ public synchronized boolean contains(String alias)
}

@Override
public Optional<VTLValue> getValue(String alias)
public Optional<VTLValue> getValue(MetadataRepository repo, String alias)
{
return Optional.ofNullable(values.get(alias));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public VTLValue resolve(String alias)
if (rule.isPresent())
return acquireResult(rule.get(), alias);
else
return acquireValue(alias, Environment::getValue)
return acquireValue(alias, (env, a) -> env.getValue(getRepository(), a))
.orElseThrow(() -> new VTLUnboundAliasException(alias));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.toList;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -102,17 +101,19 @@ private void exec() throws IOException, InvalidKeyException, NoSuchAlgorithmExce
ConfigurationManagerFactory.loadConfiguration(new FileReader(System.getProperty("user.home") + "/.vtlStudio.properties"));
SESSION_IMPLEMENTATION.setValue(VTLJupyterSession.class.getName());

String workspaceClass = ConfigurationManagerFactory.getInstance().getEnvironments().stream()
.filter(Workspace.class::isInstance)
.map(Object::getClass)
.map(Class::getName)
.findAny().orElseThrow();

ENVIRONMENT_IMPLEMENTATION.setValues(
Stream.concat(ENVIRONMENT_IMPLEMENTATION.getValues().stream(), Stream.of(JupyterWorkspace.class.getName()))
.filter(not(workspaceClass::equals))
.collect(toList())
.toArray(new String[0]));
Stream.concat(ENVIRONMENT_IMPLEMENTATION.getValues().stream()
.filter(not(cls -> {
try
{
return Workspace.class.isAssignableFrom(Class.forName(cls, true, Thread.currentThread().getContextClassLoader()));
}
catch (ClassNotFoundException e)
{
throw new ExceptionInInitializerError(e);
}
})),
Stream.of(JupyterWorkspace.class.getName())).toArray(String[]::new));

new VTLJupyterKernel(connInfo);
}
Expand Down
Loading

0 comments on commit 35f3305

Please sign in to comment.