Skip to content
Open
3 changes: 1 addition & 2 deletions api/src/main/java/org/fairdatapipeline/api/CodeRepo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.fairdatapipeline.api;

import java.net.URL;
import java.util.List;
import org.fairdatapipeline.dataregistry.restclient.APIURL;

Expand All @@ -15,7 +14,7 @@ class CodeRepo {

CodeRepo(
String latest_commit,
URL repo_url,
String repo_url,
String description,
List<APIURL> authors,
Coderun coderun) {
Expand Down
11 changes: 1 addition & 10 deletions api/src/main/java/org/fairdatapipeline/api/Coderun.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.*;
import java.time.LocalDateTime;
import java.util.*;
Expand Down Expand Up @@ -237,18 +235,11 @@ private void prepare_code_run() {
this.registryCode_run.setSubmission_script(this.script_object.getUrl());
String latest_commit = this.config.run_metadata().latest_commit().orElse("");
String remote_repo = this.config.run_metadata().remote_repo().orElse("");
URL remote_repo_url;
try {
remote_repo_url = new URL(remote_repo);
} catch (MalformedURLException e) {
throw (new ConfigException(
"Remote repo must be a valid URL; (" + remote_repo + " isn't)", e));
}

this.codeRepo =
new CodeRepo(
latest_commit,
remote_repo_url,
remote_repo,
"Analysis / processing script location",
this.authors,
this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import java.nio.file.Path;
import java.util.List;
import org.fairdatapipeline.distribution.Distribution;
import org.fairdatapipeline.distribution.ImmutableDistribution;
import org.fairdatapipeline.estimate.ImmutableEstimate;
import org.fairdatapipeline.file.CleanableFileChannel;
import org.fairdatapipeline.parameters.ImmutableBoolList;
import org.fairdatapipeline.parameters.ImmutableNumberList;
import org.fairdatapipeline.parameters.ImmutableStringList;
import org.fairdatapipeline.parameters.ReadComponent;
import org.fairdatapipeline.samples.ImmutableSamples;

/**
* This represents an object_component to read from (or raise issues with) An object_component
Expand Down Expand Up @@ -77,7 +83,11 @@ public Number readEstimate() {
} catch (IOException e) {
throw (new RuntimeException("readEstimate() -- IOException trying to read from file", e));
}
return data.getEstimate();
if (!(data instanceof ImmutableEstimate)) {
throw (new RuntimeException(
"readEstimate() -- this objComponent (" + this.component_name + ") is not an estimate"));
}
return ((ImmutableEstimate) data).getEstimate();
}

/**
Expand All @@ -93,7 +103,70 @@ public Distribution readDistribution() {
throw (new RuntimeException(
"readDistribution() -- IOException trying to read from file.", e));
}
return data.getDistribution();
if (!(data instanceof ImmutableDistribution)) {
throw (new RuntimeException(
"readDistribution() -- this objComponent ("
+ this.component_name
+ ") is not a distribution"));
}
return ((ImmutableDistribution) data).getDistribution();
}

/**
* read the Bools that were stored as this component in a TOML file.
*
* @return the Bools object
*/
public List<Boolean> readBools() {
ReadComponent data;
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
data = this.dp.coderun.parameterDataReader.read(fileChannel, this.component_name);
} catch (IOException e) {
throw (new RuntimeException("readBools() -- IOException trying to read from file.", e));
}
if (!(data instanceof ImmutableBoolList)) {
throw (new RuntimeException(
"readBools() -- this objComponent (" + this.component_name + ") is not a BoolList"));
}
return ((ImmutableBoolList) data).getBools();
}

/**
* read the Strings that were stored as this component in a TOML file.
*
* @return the Strings object
*/
public List<String> readStrings() {
ReadComponent data;
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
data = this.dp.coderun.parameterDataReader.read(fileChannel, this.component_name);
} catch (IOException e) {
throw (new RuntimeException("readStrings() -- IOException trying to read from file.", e));
}
if (!(data instanceof ImmutableStringList)) {
throw (new RuntimeException(
"readStrings() -- this objComponent (" + this.component_name + ") is not a StringList"));
}
return ((ImmutableStringList) data).getStrings();
}

/**
* read the Numbers that were stored as this component in a TOML file.
*
* @return the Numbers object
*/
public List<Number> readNumbers() {
ReadComponent data;
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
data = this.dp.coderun.parameterDataReader.read(fileChannel, this.component_name);
} catch (IOException e) {
throw (new RuntimeException("readStrings() -- IOException trying to read from file.", e));
}
if (!(data instanceof ImmutableNumberList)) {
throw (new RuntimeException(
"readNumbers() -- this objComponent (" + this.component_name + ") is not a NumberList"));
}
return ((ImmutableNumberList) data).getNumbers();
}

/**
Expand All @@ -108,7 +181,11 @@ public List<Number> readSamples() {
} catch (IOException e) {
throw (new RuntimeException("readSamples() -- IOException trying to read from file.", e));
}
return data.getSamples();
if (!(data instanceof ImmutableSamples)) {
throw (new RuntimeException(
"readSamples() -- this objComponent (" + this.component_name + ") is not a samples"));
}
return ((ImmutableSamples) data).getSamples();
}

void register_me_in_registry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.fairdatapipeline.distribution.Distribution;
import org.fairdatapipeline.estimate.ImmutableEstimate;
import org.fairdatapipeline.file.CleanableFileChannel;
import org.fairdatapipeline.parameters.BoolList;
import org.fairdatapipeline.parameters.NumberList;
import org.fairdatapipeline.parameters.StringList;
import org.fairdatapipeline.samples.Samples;

/**
Expand Down Expand Up @@ -64,6 +67,9 @@ public CleanableFileChannel writeFileChannel() throws IOException {
* @param estimateNumber the number to write.
*/
public void writeEstimate(Number estimateNumber) {
if (this.been_used) {
throw (new RuntimeException("obj component already written"));
}
var estimate =
ImmutableEstimate.builder().internalValue(estimateNumber).rng(this.dp.coderun.rng).build();

Expand All @@ -72,6 +78,7 @@ public void writeEstimate(Number estimateNumber) {
} catch (IOException e) {
throw (new RuntimeException("writeEstimate() -- IOException trying to write to file.", e));
}
this.been_used = true;
}

/**
Expand All @@ -80,12 +87,67 @@ public void writeEstimate(Number estimateNumber) {
* @param distribution the Distribution to write
*/
public void writeDistribution(Distribution distribution) {
if (this.been_used) {
throw (new RuntimeException("obj component already written"));
}
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, distribution);
} catch (IOException e) {
throw (new RuntimeException(
"writeDistribution() -- IOException trying to write to file.", e));
}
this.been_used = true;
}

/**
* write a BoolList, as this named component in the data product.
*
* @param bools the Booleans to write
*/
public void writeBools(BoolList bools) {
if (this.been_used) {
throw (new RuntimeException("obj component already written"));
}
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, bools);
} catch (IOException e) {
throw (new RuntimeException("writeBools() -- IOException trying to write to file.", e));
}
this.been_used = true;
}

/**
* write a StringList, as this named component in the data product.
*
* @param strings the Strings to write
*/
public void writeStrings(StringList strings) {
if (this.been_used) {
throw (new RuntimeException("obj component already written"));
}
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, strings);
} catch (IOException e) {
throw (new RuntimeException("writeStrings() -- IOException trying to write to file.", e));
}
this.been_used = true;
}

/**
* write NumberList, as this named component in the data product.
*
* @param numbers the Numbers to write
*/
public void writeNumbers(NumberList numbers) {
if (this.been_used) {
throw (new RuntimeException("obj component already written"));
}
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, numbers);
} catch (IOException e) {
throw (new RuntimeException("writeStrings() -- IOException trying to write to file.", e));
}
this.been_used = true;
}

/**
Expand All @@ -94,11 +156,15 @@ public void writeDistribution(Distribution distribution) {
* @param samples a Samples object containing the samples
*/
public void writeSamples(Samples samples) {
if (this.been_used) {
throw (new RuntimeException("obj component already written"));
}
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, samples);
} catch (IOException e) {
throw (new RuntimeException("writeSamples() -- IOException trying to write to file.", e));
}
this.been_used = true;
}

void register_me_in_code_run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
Expand All @@ -14,6 +13,7 @@
/** This is used to store a file or a remote repo to the registry as a RegistryStorage_location. */
class Storage_location {
private static final Logger logger = LoggerFactory.getLogger(Storage_location.class);

RegistryStorage_location registryStorage_location;

/**
Expand All @@ -25,8 +25,8 @@ class Storage_location {
* we will use the existing one instead of storing this one.
* @param coderun link back to the Coderun that created us.
*/
Storage_location(URL remote_repo, String latest_commit, Coderun coderun) {
String[] split_repo = Storage_root.url_to_root(remote_repo);
Storage_location(String remote_repo, String latest_commit, Coderun coderun) {
String[] split_repo = Storage_root.gitrepo_to_root(remote_repo);
Storage_root storage_root = new Storage_root(URI.create(split_repo[0]), coderun.restClient);
create_storagelocation(latest_commit, storage_root, coderun, split_repo[1], null);
}
Expand Down
34 changes: 26 additions & 8 deletions api/src/main/java/org/fairdatapipeline/api/Storage_root.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.fairdatapipeline.api;

import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.fairdatapipeline.dataregistry.content.RegistryStorage_root;
import org.fairdatapipeline.dataregistry.restclient.APIURL;
import org.fairdatapipeline.dataregistry.restclient.RestClient;

/** Retrieve or create the RegistryStorage_root with a given 'root'. */
class Storage_root {
private static Pattern git_repo_url =
Pattern.compile("(\\w+://)(.+@)*([\\w\\d\\.]+)(:[\\d]+){0,1}/*(.*)");
private static Pattern git_repo_file = Pattern.compile("file://(.*)");
private static Pattern git_repo_ssh = Pattern.compile("(.+@)([\\w\\d\\.]+):(.*)");
RegistryStorage_root registryStorage_root;

/**
Expand Down Expand Up @@ -47,15 +53,27 @@ Path getPath() {
}

/**
* split the repository URL into a storage root (proto://authority/ part) and path (/xxx/xxx) part
* split the repository location into a storage root HTTPS: proto://authority/path/to/stuff
* becomes proto://authority AND /path/to/stuff SSH: [email protected]:bboskamp/BTv.git becomes
* [email protected] AND /bboskamp/BTv.git
*
* @param url the URL to split up into scheme/authority and path.
* @param repo_location the repository location string to split up into scheme/authority and path.
* @return string array of length 2.
*/
static String[] url_to_root(URL url) {
String path = url.getPath().substring(1);
String scheme_and_authority_part =
url.toString().substring(0, url.toString().length() - path.length());
return new String[] {scheme_and_authority_part, path};
static String[] gitrepo_to_root(String repo_location) {
Matcher m1 = git_repo_url.matcher(repo_location);
if (m1.find())
return new String[] {
Objects.toString(m1.group(1), "")
+ Objects.toString(m1.group(2), "")
+ Objects.toString(m1.group(3), "")
+ Objects.toString(m1.group(4), ""),
m1.group(5)
};
m1 = git_repo_file.matcher(repo_location);
if (m1.find()) return new String[] {"file://", m1.group(1)};
m1 = git_repo_ssh.matcher(repo_location);
if (m1.find()) return new String[] {m1.group(1) + m1.group(2), m1.group(3)};
return new String[] {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.apache.commons.math3.random.EmpiricalDistribution;
import org.fairdatapipeline.parameters.Component;
import org.fairdatapipeline.parameters.RngComponent;
import org.immutables.value.Value.Auxiliary;
import org.immutables.value.Value.Check;
import org.immutables.value.Value.Immutable;
Expand All @@ -33,7 +33,7 @@
TODO support other distributions:
https://github.com/ScottishCovidResponse/SCRCIssueTracking/issues/671
*/
public interface Distribution extends Component {
public interface Distribution extends RngComponent {
enum DistributionType {
gamma(),
exponential(),
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/org/fairdatapipeline/estimate/Estimate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.List;
import org.fairdatapipeline.distribution.Distribution;
import org.fairdatapipeline.parameters.Component;
import org.fairdatapipeline.parameters.RngComponent;
import org.immutables.value.Value.Immutable;

/** a component to store a plain simple single Number */
@Immutable
@JsonDeserialize
@JsonSerialize
public interface Estimate extends Component {
public interface Estimate extends RngComponent {
/** @return Number - the value that is stored in this component */
@JsonProperty("value")
Number internalValue();
Expand Down
Loading