Basic Operation of Writers

Having launched, a Writer is triggered through its trigger pin. When triggered, a Writer reads its input and then writes it to its internal buffer databuf. When databuf is full, the data in databuf is processed. Thus, the length of the data that is to be processed by the Writer is determined by the length of their internal buffer databuf.

Let us construct a Writer.

julia> using Causal # hide

julia> w = Writer(input=Inport(), buflen=5)
Writer(path:/tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2, nin:1)

The file of w is closed and the trigger pin of w is not writable. That is, it is not possible to trigger w from its trigger pin.

julia> w.file
JLDFile /tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2 (read/write)

julia> w.trigger
Inpin(eltype:Float64, isbound:false)

To trigger w, we need to open and launch it,

julia> oport, trg, hnd = Outport(), Outpin(), Inpin{Bool}()
(Outport(numpins:1, eltype:Outpin{Float64}), Outpin(eltype:Float64, isbound:false), Inpin(eltype:Bool, isbound:false))

julia> connect!(oport, w.input)
1-element Vector{Link{Float64}}:
 Link(state:open, eltype:Float64, isreadable:false, iswritable:false)

julia> connect!(trg, w.trigger)
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)

julia> connect!(w.handshake, hnd)
Link(state:open, eltype:Bool, isreadable:false, iswritable:false)

julia> open(w)
Writer(path:/tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2, nin:1)

julia> t = launch(w)
Task (runnable) @0x00007fee1a5cf9d0

Now, the internal file of w is opened in read/write mode and its trigger pin is writable.

julia> w.file
JLDFile /tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2 (read/write)
  (no datasets)

julia> w.trigger.link
Link(state:open, eltype:Float64, isreadable:false, iswritable:true)

Let us now trigger w.

julia> put!(trg, 1.)

The input of w is now readable and handshake pin is not readable since w have not signaled that its triggering is succeeded yet. To do that, we need to put a value to the input of w

julia> put!(oport, [10.])
1-element Vector{Float64}:

Now, w signalled that its step is succeeded. It read the data from its input and written it into is databuf.

julia> hnd.link
Link(state:open, eltype:Bool, isreadable:true, iswritable:false)

julia> take!(hnd)

julia> w.databuf
5-element Buffer{Cyclic, Float64, 1}:

Since the databuf is not full nothing is written to the file of w.

julia> w.file
JLDFile /tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2 (read/write)
  (no datasets)

Let us continue triggering w until the databuf of w is full.

julia> for t in 2. : 5.
           put!(trg, t)
           put!(oport, [t * 10])

Now check that the content of the file of w.

julia> w.file
JLDFile /tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2 (read/write)
 โ””โ”€๐Ÿ”ข [5.0, 4.0, 3.0, 2.0, 1.0]

Note that the content of databuf is written to the file of w. The operation of w can be terminated.

julia> put!(trg, NaN)

When terminated, the file of w is closed.

julia> w.file
JLDFile /tmp/f3ef21ca-fd80-439a-a711-ad93dab930be.jld2 (read/write)

In this example, w does not have a plugin so nothing has been derived or computed from the data in databuf. The data in databuf is just written to file of w. To further data processing, see Plugins

Full API

Causal.Writer โ€” Type
mutable struct Writer{A, FL, var"4004", var"4005", var"4006", Symbol, var"4007", var"4014", Int, var"4015", var"4016", var"4017", var"4018"} <: AbstractSink

Constructs a Writer whose input bus is input. buflen is the length of the internal buffer of Writer. If not nothing, plugin is used to processes the incomming data. path determines the path of the file of Writer.


  • action::Any

    Writer action to write data to file

  • path::String

    File path of the writer

  • file::Any

    File in which data is recorded

  • trigger::Any

  • handshake::Any

  • callbacks::Any

  • name::Any

  • id::Any

  • input::Any

  • buflen::Any

  • plugin::Any

  • timebuf::Any

  • databuf::Any

  • sinkcallback::Any


The type of file of Writer is JLD2.


When initialized, the file of Writer is closed. See open(writer::Writer) and close(writer::Writer).

Causal.write! โ€” Method
write!(writer, td, xd)

Writes xd corresponding to xd to the file of writer.


julia> w = Writer(Inport(1))
Writer(path:/tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2, nin:1)

julia> open(w)
Writer(path:/tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2, nin:1)

julia> write!(w, 0., 10.)

julia> write!(w, 1., 20.)

julia> w.file
JLDFile /tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2 (read/write)
 โ”œโ”€๐Ÿ”ข 0.0
 โ””โ”€๐Ÿ”ข 1.0

julia> w.file[string(0.)]
Base.read โ€” Method
read(writer; flatten)

Read the contents of the file of writer and returns the sorted content of the file. If flatten is true, the content is also flattened.

Causal.fread โ€” Function
fread(path; flatten)

Reads the content of jld2 file and returns the sorted file content.

Base.Filesystem.mv โ€” Method
mv(writer, dst; force)

Moves the file of writer to dst. If force is true, the if dst is not a valid path, it is forced to be constructed.


julia> mkdir(joinpath(tempdir(), "testdir1"))

julia> mkdir(joinpath(tempdir(), "testdir2"))

julia> w = Writer(Inport(), path="/tmp/testdir1/myfile.jld2")
Writer(path:/tmp/testdir1/myfile.jld2, nin:1)

julia> mv(w, "/tmp/testdir2")
Writer(path:/tmp/testdir2/myfile.jld2, nin:1)
Base.Filesystem.cp โ€” Method
cp(writer, dst; force, follow_symlinks)

Copies the file of writer to dst. If force is true, the if dst is not a valid path, it is forced to be constructed. If follow_symlinks is true, symbolinks are followed.


julia> mkdir(joinpath(tempdir(), "testdir1"))

julia> mkdir(joinpath(tempdir(), "testdir2"))

julia> w = Writer(Inport(), path="/tmp/testdir1")
Writer(path:/tmp/testdir1.jld2, nin:1)

julia> cp(w, "/tmp/testdir2")
Writer(path:/tmp/testdir2/1e72bad1-9800-4ca0-bccd-702afe75e555, nin:1)
Base.open โ€” Method
open(writer, mode)

Opens writer by opening the its file in read/write mode. When writer is not openned, it is not possible to write data in writer. See also close(writer::Writer)

Base.close โ€” Method

Closes writer by closing its file. When writer is closed, it is not possible to write data in writer. See also open(writer::Writer)