Reads a theory object authored as YAML (or JSON, chosen by file extension) into a named list.
Arguments
- path
Path to a
.yaml,.ymlor.jsonfile. The file is read as JSON when its extension is.json, in any letter case, and as YAML otherwise. A name that begins with its only dot, such as.json, has no extension. A leading~is expanded aspath.expand()expands it:~alone or before a separator is the home directory, and~useris that user's home directory on Unix, an unknown user being left as written, and is left as written on Windows. The Python package follows the same rule.
Details
The file is read as the Python package's theoryforge.read() reads
it. The text is UTF-8, and a byte-order mark is ignored. A file that is not
valid UTF-8 stops with (<path>) the file is not valid UTF-8, and one
that holds a NUL byte with (<path>) the file holds a NUL byte, which
theoryforge cannot read. A YAML or JSON
sequence is always a list, even with one element, so tf_validate() refuses
maturity: [draft].
Unquoted y, Y, n and N stay strings, as do
dates, while yes, no, true, false, on and
off are logicals. Integers are decimal, octal or hexadecimal only, so
1:30 and 1_000 are strings, and so is a number written with a
comma (1,000). An integer too large for an R integer is read as a
double. A tab is read as YAML allows it: as white space after a value, before
a comment and in a flow collection, and as text inside an unquoted value. A
merge key (<<) lets the mapping's own keys win. A key repeated in any
mapping, in YAML or JSON, stops with (<path>) Duplicate map key:
'<key>'. A YAML document that holds more than 1000000 values once its
aliases (*a) are expanded stops with (<path>) the document holds
more than 1000000 values once its aliases are expanded, before anything
walks it, and a document nested more than 64 levels deep, the document being
the first, stops with (<path>) the document is nested more than 64
levels deep. An alias whose anchor is undefined, or that lies inside the
node its anchor names, stops with (<path>) the alias '<name>' names no
node that ends before it, and an anchor given to two nodes with
(<path>) the anchor '<name>' names two nodes, where yaml would read a
placeholder or the first of the two nodes. A value tagged !expr, which
the yaml package evaluates as R code when the option yaml.eval.expr is
TRUE, stops with (<path>) a value is tagged !expr, which
theoryforge does not read, whatever the option says, and no code is run. A
comment in a JSON file stops with the message of the JSON parser, since JSON
has no comments. A string or key that holds U+0000 or a lone surrogate, which
R would cut short or misread, stops with (<path>) a string holds
U+0000, which theoryforge cannot read, or the same message naming a
lone surrogate. A file with more than one of these faults is refused for
the first in this order. A JSON text is measured for depth before it is
parsed, a file that is not valid UTF-8 or holds a NUL byte is refused next,
or first in YAML, and a syntax error anywhere in a JSON text comes before a
repeated key. In
YAML, a syntax error, a repeated key and an alias that names no node come in
the order the parser meets them, a repeated key where its mapping ends, and
a value tagged !expr once the whole text has been parsed, after any
alias that names no node. The depth and the count of values come next, then
U+0000 or a lone surrogate and last an anchor given twice. A document that
is not a mapping, such as a sequence or an empty YAML file, stops with
Theory data must be a mapping.
Examples
# Round-trip a theory through a temporary file.
theory <- tf_theory("demo-1", "A demonstration theory")
path <- tempfile(fileext = ".yaml")
tf_write(theory, path)
tf_read(path)
#> $schema_version
#> [1] "1.1"
#>
#> $id
#> [1] "demo-1"
#>
#> $title
#> [1] "A demonstration theory"
#>
#> $maturity
#> [1] "building"
#>
#> $theory_form
#> [1] "network"
#>
#> $provenance
#> $provenance[[1]]
#> $provenance[[1]]$step
#> [1] "1"
#>
#> $provenance[[1]]$action
#> [1] "tf_theory"
#>
#> $provenance[[1]]$detail
#> [1] "demo-1"
#>
#>
#>