File Inputs and Outputs
ArupCompute supports calculations taking file inputs and outputs. They require some specific metadata in order to work which is detailed on this page.
Inputs
To set an input as a file input you must use the FileInput attribute instead of the standard Input attribute. You must also set your input type to be a ComputeFile object.
The FileInput attribute takes the same inputs as the Input attribute, as well as requiring an array of file types that the input should expect. The file types must include the ".", eg. ".txt". ArupCompute will only allow files of the specified type(s) to be passed into your calculation.
Below is an example of a calculation that takes in a text file and then reads the lines and the name of the file into an ArupComputeResult object:
using Arup.Compute.DotNetSdk;
using Arup.Compute.DotNetSdk.Attributes;
using Arup.Compute.DotNetSdk.FileIO;
[Calculation(
"Single file input",
"A calc that takes in a single text file and outputs the lines in that file",
Arup.Compute.DotNetSdk.Enums.LevelOfReview.Complete
)]
[Output("Lines", "The lines in the file", "", "")]
[Output("Name", "The file name", "", "")]
public static ArupComputeResult SingleFileInput(
[FileInput("file input", "The file to input", "", new string[] { ".txt" }, null)]
ComputeFile file
)
{
ArupComputeResult result = new()
{
ArupComputeResultItems = new()
{
new ArupComputeResultItem()
{
Description = "Lines",
Symbol = "Lines",
Value = file.GetLines().ToList(),
},
new ArupComputeResultItem()
{
Description = "File name",
Symbol = "Name",
Value = file.GetFileName(),
},
},
};
return result;
}
The file input type only has to be a class that implements the IBaseComputeFile interface. For most libraries we recommend using the ComputeFile class which contains most of the methods that you will need for basic file processing.
More information on the IBaseComputeFile interface and how to write your own class using it can be found on the next page.
Outputs
ArupCompute allows you to return just a single file, as well as including file(s) in an ArupComputeResult object if you want to return files as well as regular outputs.
Single file outputs
If you only want to return a single file then you must indicate this by setting a FileOutput attribute on the class and then returning a ComputeFile object.
The FileOutput attribute works in the same way as the existing Output attribute. It takes in the same values, as well as a string representing the file output type. Unlike with file inputs, you can only set one file type as your output file type. This output type will also be checked against the file that is outputted before your file is returned to the user so it must be accurate.
Below is an example of a calculation that takes a comma-separated string then splits the string by the commas. It creates a text file with that array, using each item as a new line:
using Arup.Compute.DotNetSdk;
using Arup.Compute.DotNetSdk.Attributes;
using Arup.Compute.DotNetSdk.FileIO;
[Calculation(
"Single file output",
"A calc that takes in a comma-separated string and returns a text file with the lines",
Arup.Compute.DotNetSdk.Enums.LevelOfReview.Complete
)]
[FileOutput("out", "The output file", "", ".txt")]
public static ComputeFile SingleFileOutput(
[Input("Lines", "The comma-separated lines to add to the file", "", "", null)]
string linesString
)
{
using MemoryStream ms = new();
using TextWriter tw = new StreamWriter(ms);
foreach (string line in linesString.Split(","))
{
tw.WriteLine(line);
}
tw.Flush();
ms.Position = 0;
ComputeFile outputFile = new(ms, "out.txt");
return outputFile;
}
As with the outputs, you don't have to use the ComputeFile. You just need to make sure that the object you return implements the IBaseComputeFile interface.
Multi file outputs
To return multiple files you must use the ArupComputeResult object.
To add a file to your ArupComputeResult object you must use the ArupComputeResultFileItems field. This field takes a list of ArupComputeResultFileItem objects. This object is described below:
ArupComputeResultFileItem Properties
| Property name | Description |
|---|---|
| FileName | The file name to return to the user. Do not include the file type. |
| FileData | The file data in an IBaseComputeFile format. ComputeFile will also work. |
| FileType | The file type of the file, the starting "." must be included e.g. .xlsx, .txt. |
| Description | Some additional text describing the result. |
| FileLink | The link to download the file. This property should be left empty when writing a calculation |
Example
Below is an example calculation that takes in two strings, runs the SingleFileOutput method from above on each of the inputs, then returns both generated files as well as a count of the total words in both files.
using Arup.Compute.DotNetSdk;
using Arup.Compute.DotNetSdk.Attributes;
using Arup.Compute.DotNetSdk.FileIO;
[Calculation(
"Multiple file output",
"A calculation that outputs multiple files",
Arup.Compute.DotNetSdk.Enums.LevelOfReview.Complete
)]
[FileOutput("File output 1", "The first output file", "", ".txt")]
[FileOutput("File output 2", "The second output file", "", ".txt")]
[Output("Word count", "The total number of words in both files", "", "")]
public static ArupComputeResult MultipleFileOutput(
[Input("Lines 1", "The comma-separated lines to add to the first file", "", "", null)]
string linesString1,
[Input("Lines 2", "The comma-separated lines to add to the second file", "", "", null)]
string linesString2
)
{
ComputeFile file1 = SingleFileOutput(linesString1); // the method from the above calculation
ComputeFile file2 = SingleFileOutput(linesString2); // the method from the above calculation
ArupComputeResult arupComputeResult = new()
{
ArupComputeResultItems = new()
{
new ArupComputeResultItem()
{
Description = "The total number of words in both files",
Symbol = "Word count",
Value = linesString1.Split(" ").Length + linesString2.Split(" ").Length,
},
},
ArupComputeResultFileItems = new()
{
new ArupComputeResultFileItem()
{
FileName = "File output 1",
FileData = file1,
FileType = ".txt",
Description = "The first output file",
},
new ArupComputeResultFileItem()
{
FileName = "File output 2",
FileData = file2,
FileType = ".txt",
Description = "The second output file",
},
},
};
return arupComputeResult;
}