Warehouse {Peanut} | R Documentation |
A warehouse is an object which stores a collection of
Pnode
s or Pnet
s. When requested, it will
supply the given object. If the object already exists, it is
returned. If it does not yet exist, it is built using meta-data in
the warehouse's manifest.
WarehouseSupply(warehouse, name) ## S4 method for signature 'ANY' WarehouseSupply(warehouse, name) WarehouseFetch(warehouse, name) WarehouseMake(warehouse, name) WarehouseFree(warehouse, name) ClearWarehouse(warehouse) is.PnetWarehouse(obj) is.PnodeWarehouse(obj)
warehouse |
A warehouse object from which the object is to be created. |
name |
A character vector giving the name of the object. Note that for net warehouses, the key is usually has length one, but for node warehouses, this usuall has the form (model,node). |
obj |
An object whose type is to be determined. |
The warehouse is a combination of a cache and a factory. The idea is
that when a Pnet or Pnode object is needed, it is requested from the
corresponding warehouse. If the object exists, it is returned. If
the object does not exist, then the information in the manifest (see
WarehouseManifest()
is used to create a new object. The key
function is WarehouseSupply(warehouse,name)
; this function
looks for an object corresponding to name in warehouse
.
If it exists, it is returned, if not a new one is created.
The generic functions
WarehouseFetch(warehouse,name)
and
WarehouseMake(warehouse,name)
implement the supply
protocol. WarehouseFetch(warehouse,name)
searches
for an object corresponding to name in the warehouse
and
returns it if it exists or returns NULL
if it does not. The
generic function WarehouseMake(warehouse,name)
creates the object using the data in the manifest.
The WarehouseFree
and WarehouseClear
functions complete
the Warehouse prototocl. These respectively remove the named object
from the cache, and clear the cache. Note that these may our may not
make sense with the implementation. (In the current
PNetica-package implementation, the cache is
maintained by the underlying RNetica-package objects,
and hence it doesn't make sense to free an object without deleting
it.)
Each warehouse has a manifest which supplies the necessary data to
build a praticular object. The generic function
WarehouseManifest()
accesses the manifest, which
generally takes the form of a data.frame
object.
The functions BuildNetManifest()
and
BuildNodeManifest()
build manifests for network and node
objects respectively. The generic function
WarehouseData(warehouse,name)
returns the
rows of the manifest which correspond to a paraticular name
.
The Peanut package is concerned with two kinds of warehouses: Pnet
warehouses and Pnode warehouses. Pnet warehouses contain
Pnets, and the key is the name of the network. Each Pnet corresponds
to a single line in the manifest, and the name is a character
scalar. A Pnet warehouse should return true when the generic function
is.PnetWarehouse()
is called.
Pnode warehouses contain Pnodes, and the name is a
character vector of length 2, with structure (netname,
nodename). This is because nodes with the same name will
frequently exist in two different networks. Currently the manifest
for a node contains one line for each possible state of the node.
A Pnode warehouse should return true when the generic function
is.PnodeWarehouse()
is called.
The warehouse object is an abstract class, and implementing classes
need to provide methods for the generic functions
WarehouseFetch()
, WarehouseMake()
,
WarehouseFree()
, WarehouseData()
,
WarehouseManifest()
, and ClearWarehouse()
as well as one
of the generic functions is.PnetWarehouse
or
is.PnodeWarehouse
.
There are two reference implementations in
BNWarehouse
and
NNWarehouse
(network and node warehouses
respectively). Both of these take advantage of the fact that the
session and network objects in RNetica-package
have built in environments which cache the networks and nodes
respectively. The Warehouse-class
object is a
generic implementation that also may be of some use to potential
implementors.
The return type of most functions will depend on the type of the warehouse. In most cases, the functions return an object of the type of the warehouse.
Pnet Warehouses |
These return |
Pnode Warehouses |
These return |
The returns from the functions WarehouseFree()
and
ClearWarehouse()
are arbitrary depending on the implementation.
The cache part of the warehouse, almost certainly needs to be
implemented using the reference class system of Chambers (2016). In
particular, an environment
object provides the kind
of persistent storage and object persistance and uniqueness necessary
(this breaks the usual functional programming paradigm of R).
Russell G. Almond
Almond, R. G. (presented 2017, August). Tabular views of Bayesian networks. In John-Mark Agosta and Tomas Singlair (Chair), Bayeisan Modeling Application Workshop 2017. Symposium conducted at the meeting of Association for Uncertainty in Artificial Intelligence, Sydney, Australia. (International) Retrieved from http://bmaw2017.azurewebsites.net/
Chambers, J. M. (2016) Extending R. CRC Press.
These functions support the manifest process.
WarehouseManifest()
, WarehouseData()
These functions construct manifests:
BuildNetManifest()
, BuildNodeManifest()
These functions use the warehouse to build networks:
Omega2Pnet
Qmat2Pnet
## Not run: ## Requires PNetica package library(PNetica) sess <- NeticaSession() startSession(sess) ### This tests the manifest and factory protocols. nodeman1 <- read.csv(paste(library(help="Peanut")$path, "auxdata", "Mini-PP-Nodes.csv", sep=.Platform$file.sep), row.names=1,stringsAsFactors=FALSE) netman1 <- read.csv(paste(library(help="Peanut")$path, "auxdata", "Mini-PP-Nets.csv", sep=.Platform$file.sep), row.names=1, stringsAsFactors=FALSE) ### Test Net building Nethouse <- BNWarehouse(manifest=netman1,session=sess,key="Name") stopifnot(is.PnetWarehouse(Nethouse)) setwd(paste(library(help="PNetica")$path, "testnets",sep=.Platform$file.sep)) CM <- WarehouseSupply(Nethouse,"miniPP_CM") stopifnot(is.null(WarehouseFetch(Nethouse,"PPcompEM"))) EM1 <- WarehouseMake(Nethouse,"PPcompEM") EMs <- lapply(c("PPcompEM","PPconjEM", "PPtwostepEM", "PPdurAttEM"), function(nm) WarehouseSupply(Nethouse,nm)) ### Test Node Building with already loaded nets Nodehouse <- NNWarehouse(manifest=nodeman1, key=c("Model","NodeName"), session=sess) stopifnot(is.PnodeWarehouse(Nodehouse)) phyd <- WarehouseData(Nodehouse,c("miniPP_CM","Physics")) p3 <- MakePnode.NeticaNode(CM,"Physics",phyd) phys <- WarehouseSupply(Nodehouse,c("miniPP_CM","Physics")) stopifnot(p3==phys) for (n in 1:nrow(nodeman1)) { name <- as.character(nodeman1[n,c("Model","NodeName")]) if (is.null(WarehouseFetch(Nodehouse,name))) { cat("Building Node ",paste(name,collapse="::"),"\n") WarehouseSupply(Nodehouse,name) } } stopSession(sess) ## End(Not run)