Changes

Jump to navigation Jump to search
5,260 bytes added ,  02:01, 3 September 2023
}}
== [[Help:Reference|Syntax]] (autogenerated) ==
<syntax>definedataset</syntax>
== [[Help:Reference|Syntax]] ==
<table cellspacing="4" cellpadding="2" class="cmd">
</texcode>
== Storing and Accessing Datasets from Lua ==
 
The macro interface for datasets does not allow saving arbitrarily
structured data to the <code>.tuc</code> file. This limitation can be
overcome by using the governing Lua functions directly. They reside in
the <em>job.datasets</em> namespace.
(Cf. {{src|core-dat.lua}}, later than 2012-09-18.)
 
=== Saving Data ===
 
The function <code>job.datasets.setdata</code> handles the storing.
It takes one argument of type ''table''.
This table has three required fields: ''name'', ''tag'' and ''data''.
The ''name'' field is equivalent to the first argument of
{{cmd|definedataset}} (see above): the dataset answers to this
identifier.
The ''tag'' field is the id of the entry that will be saved,
corresponding to the second argument of {{cmd|setdataset}}.
The ''data'' field is supposed to containt the actual content that
needs saving. If it is a table, it will be serialized to the
<code>.tuc</code> file under the namespace
<code>utilitydata.job.datasets.collected</code>.
 
Take the following snippet as an example. A fairly elaborated nested
structure is passed to <code>datasets.setdata()</code>.
<texcode>
\starttext
 
\startluacode
local setdata = job.datasets.setdata
setdata {
name = "dataset_id",
tag = "this",
data = {
"does",
work = {
"splendidly",
"for",
nested = { "tables", "!", {{}} }
}
}
}
\stopluacode
 
\stoptext \endinput
</texcode>
 
This will cause the following section to be added to the
<code>.tuc</code> file (note that the “tag” is represented by a simple
hash entry):
<pre>
utilitydata.job.datasets.collected={
["dataset_id"]={
["this"]={
"does",
["work"]={
"splendidly",
"for",
["nested"]={
"tables",
"!",
{
{},
},
},
},
},
},
}
</pre>
 
=== Retrieving Stored Data ===
 
The complement to <code>setdata</code> is <code>getdata</code>.
Of its four possible arguments, the two are essential. They correspond
to the ''name'' and ''tag'' fields of a stored dataset.
'''NB''': It is not possible to retrieve the entire dataset all at once
by passing <code>getdata</code> only one argument.
 
The following example demonstrates how to restore the subentry ''this''
of the dataset ''dataset_id'' as defined in the previous section:
<texcode>
\startluacode
local getdata = job.datasets.getdata
table.print(getdata ("dataset_id", "this"), "this")
\stopluacode
</texcode>
 
== Processing Datasets ==
 
You can process a dataset with {{cmd|processdataset}} and use setups to generate the desired output:
 
<context source="yes" text="Result:">
% Create the dataset:
\definedataset[autores]
 
% Fill the dataset:
\setdataset[autores][
nombre={Ana Valeria Ruiz Gómez},
bio={Investigadora de la Universidad Ficticia del Valle, con experiencia en análisis, diseño de sistemas futuristas, centrando sus estudios en comunidades virtuales, sistemas autónomos y robots.},
email={avruizg@unificticia.edu.xy},
]
 
\setdataset[autores][
nombre={Carlos Julio Estévez Pérez},
bio={Ingeniero de Sistemas de la Universidad Imaginaria de la Montaña, con experiencia en comunidades virtuales, diseño de software y trabajo con sistemas de inteligencia artificial.},
email={cjestevezp@uniimaginaria.edu.xy},
]
 
\setdataset[autores][
nombre={Luz María Torres Herrera},
bio={Matemática y especialista en datos de la Universidad Inexistente del Río, con maestría en Análisis Numérico del Instituto de Tecnologías Noexistentes. Experiencia en modelado matemático, análisis de datos y desarrollo de algoritmos para ciencia de datos. Cofundadora de la Organización SinNombre trabajando en proyectos de tecnología para el bienestar y desarrollo humano.},
email={lmtorresh@uninexistente.edu.xy},
]
 
% Now, create setups for presentation:
 
% A setup to display an author bio as a section:
\startsetups[userdata:bioautor]
\startsection[title={\datasetentry{nombre}}]
\datasetentry{bio}\par\page[no]
Correo: \datasetentry{email}
\stopsection
\stopsetups
 
% A setup to display a vertical list of author names, separated with paragraph breaks:
\startsetups[userdata:lvautores]
\datasetentry{nombre}\par
\stopsetups
 
% A setup using \processaction to display an horizontal list of author names,
% each followed by a comma and a space, except the penultimate name,
% which will be followed by { \ampersand }, and the last one, followed by a period.
% We determine position in the dataset using \datasetindex
% and compare it with the dataset size (\datasetsize{autores}).
\startsetups[userdata:lhautores]
\dontleavehmode
\datasetentry{nombre}
\processaction[\datasetindex][
\datasetsize{autores}=>{.},
\number\numexpr\datasetsize{autores}-1\relax=>{ \ampersand },
unknown=>{, }
]
\stopsetups
 
\startdocument
\startchapter[title={Authors}]
{\bf Vertical list}
 
\processdataset[autores][userdata:lvautores]
 
\blank[2*big]
 
{\bf Horizontal list}
 
\processdataset[autores][userdata:lhautores]
\stopchapter
\startchapter[title={Author bios}]
\processdataset[autores][userdata:bioautor]
\stopchapter
\stopdocument
</context>
== See also ==
18

edits

Navigation menu