Skip to content

Commit

Permalink
[File buffering] adds an operator to manually flush buffered write op…
Browse files Browse the repository at this point in the history
…erations
  • Loading branch information
lesquoyb committed May 30, 2024
1 parent d839396 commit 1ce9c80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
1 change: 0 additions & 1 deletion gama.core/src/gama/core/runtime/GAMA.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public static boolean flushWriteSimulation(SimulationAgent owner) {
public static boolean flushWriteStep(SimulationAgent owner) {
return writeController.flushCycleOwner(owner);
}
//TODO: other flushes + their operators

/**
* Gets the controllers.
Expand Down
62 changes: 32 additions & 30 deletions gama.core/src/gama/gaml/operators/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
Expand All @@ -37,9 +35,11 @@
import gama.annotations.precompiler.ITypeProvider;
import gama.core.common.interfaces.IKeyword;
import gama.core.common.util.FileUtils;
import gama.core.kernel.simulation.SimulationAgent;
import gama.core.metamodel.agent.IAgent;
import gama.core.metamodel.shape.GamaShape;
import gama.core.metamodel.shape.IShape;
import gama.core.runtime.GAMA;
import gama.core.runtime.IScope;
import gama.core.runtime.exceptions.GamaRuntimeException;
import gama.core.util.IContainer;
Expand All @@ -59,16 +59,7 @@
public class Files {


// @operator (
// value = IKeyword.FILE,
// can_be_const = true,
// category = IOperatorCategory.FILE,
// concept = { IConcept.FILE })
// @doc (
// value = "Creates a file in read/write mode, setting its contents to the container passed in parameter",
// comment = "The type of container to pass will depend on the type of file (see the management of files in the
// documentation). Can be used to copy files since files are considered as containers. For example: save
// file('image_copy.png', file('image.png')); will copy image.png to image_copy.png")

/**
* From.
*
Expand All @@ -89,24 +80,6 @@ public static IGamaFile from(final IScope scope, final String s, final IContaine
return (IGamaFile) Types.FILE.cast(scope, s, container, key, content, false);
}

//
// @operator (
// value = IKeyword.FILE,
// can_be_const = true,
// category = IOperatorCategory.FILE,
// concept = { IConcept.FILE })
// @doc (
// value = "opens a file in read only mode, creates a GAML file object, and tries to determine and store the file
// content in the contents attribute.",
// comment = "The file should have a supported extension, see file type definition for supported file extensions.",
// usages = @usage ("If the specified string does not refer to an existing file, an exception is risen when the
// variable is used."),
// examples = { @example (
// value = "let fileT type: file value: file(\"../includes/Stupid_Cell.Data\"); "),
// @example (
// value = " // fileT represents the file \"../includes/Stupid_Cell.Data\""),
// @example (
// value = " // fileT.contents here contains a matrix storing all the data of the text file") },
/**
* From.
*
Expand Down Expand Up @@ -753,4 +726,33 @@ public static IGamaFile newFolder(final IScope scope, final String folder) throw

}

/**
* Flushes all the pending write operations in the current simulation
* @param scope
* @return true if everything went well, false if there was a problem while flushing
* @throws GamaRuntimeException
*/
@operator (
value = { "flush_all_files" },
category = IOperatorCategory.FILE,
concept = { IConcept.FILE },
type = IType.BOOL
)
@doc (
value = "Flushes all the pending write operations in the current simulation. This operator is only useful "
+ "in simulation that save files using a buffering strategy.",
comment = "",
usages = {
@usage ("If the specified string does not refer to an existing repository, the repository is created."),
@usage ("If the string refers to an existing file, an exception is risen.") },
examples = {
@example ("file dirNewT <- new_folder(\"incl/\"); // dirNewT represents the repository \"../incl/\""),
@example (" // eventually creates the directory ../incl") },
see = { "save"})
public static boolean flushAllFiles(final IScope scope, final SimulationAgent simulation) throws GamaRuntimeException {
boolean success = GAMA.flushWriteStep(simulation);
success &= GAMA.flushWriteSimulation(simulation);
return success;
}

}

0 comments on commit 1ce9c80

Please sign in to comment.