R/graphs.R

Defines functions to_igraph .check_graph relation_graph

Documented in relation_graph to_igraph

# Optional igraph adapter; the package core has no graph dependency.
relation_graph <- function(nodes=NULL, kinds=NULL, context=NULL) {
  ctx <- .context(context)
  .require(validate_context(ctx)$status!="INCONSISTENT_CONTEXT","Cannot construct an applicable graph in an inconsistent context")
  if(is.null(kinds)) kinds <- "IMPLIES_SAME_OBJECT"
  .require((is.character(kinds) || is.list(kinds)) && all(vapply(as.list(kinds),function(k) .scalar_string(k) && k %in% .kinds,logical(1))),"Expected a list of relation kinds")
  kinds <- unlist(kinds,use.names=FALSE); data <- .catalogue()
  rows <- c(data$definitions,data$statements); ids <- vapply(rows,`[[`,character(1),"id")
  if(!is.null(nodes)) .require((is.character(nodes) || is.list(nodes)) && all(vapply(as.list(nodes),function(n) .scalar_string(n) && n %in% ids,logical(1))),"Expected known node IDs")
  edges <- list()
  for(k in kinds) for(e in .relation_edges(k,ctx)) if(e$kind %in% kinds) edges[[e$id]] <- e
  chosen <- if(!is.null(nodes)) unique(unlist(nodes,use.names=FALSE)) else unique(unlist(lapply(edges,function(e) c(e$premise,e$conclusion)),use.names=FALSE))
  chosen <- sort(as.character(chosen),method="radix")
  edges <- Filter(function(e) e$premise %in% chosen && e$conclusion %in% chosen,edges)
  if(length(edges)) edges <- edges[order(names(edges),method="radix")]
  vertices <- lapply(chosen,function(n) {
    x <- rows[[match(n,ids)]]
    list(id=n,label=if(is.null(x$name)) gsub("_"," ",n,fixed=TRUE) else x$name,
         kind=if(is.null(x$kind)) "statement" else x$kind,topic=x$topic)
  })
  list(type="relation_graph",directed=TRUE,multigraph=TRUE,context=ctx,kinds=.array(sort(unique(kinds),method="radix")),
    nodes=vertices,edges=unname(edges),catalogue_sha256=catalogue_manifest()$sha256,
    limitations=list("Only direct reviewed catalogue relations are drawn.",
      "A graph path is not a proof unless its edge types and hypotheses compose.",
      "Layout coordinates do not measure cardinal size or consistency strength."))
}
.check_graph <- function(graph) {
  .require(is.list(graph) && identical(graph$type,"relation_graph"),"Expected a relation graph")
  expected <- relation_graph(lapply(graph$nodes,`[[`,"id"),graph$kinds,graph$context)
  .require(identical(serialize(graph),serialize(expected)),"Graph does not match its declared catalogue, context and filters")
  graph
}
to_igraph <- function(graph=NULL, nodes=NULL, kinds=NULL, context=NULL) {
  if(is.null(graph)) graph <- relation_graph(nodes,kinds,context)
  else {
    .require(is.null(nodes) && is.null(kinds) && is.null(context),"Supply a graph or graph filters, not both")
    .check_graph(graph)
  }
  .require(requireNamespace("igraph",quietly=TRUE),"Install the optional igraph dependency","MISSING_DEPENDENCY")
  out <- igraph::make_empty_graph(n=0L,directed=TRUE)
  if(length(graph$nodes)) {
    out <- igraph::add_vertices(out,length(graph$nodes),name=vapply(graph$nodes,`[[`,character(1),"id"))
    for(key in c("id","label","kind","topic")) out <- igraph::set_vertex_attr(out,key,value=vapply(graph$nodes,`[[`,character(1),key))
  }
  if(length(graph$edges)) {
    endpoints <- unlist(lapply(graph$edges,function(e) c(e$premise,e$conclusion)),use.names=FALSE)
    out <- igraph::add_edges(out,endpoints)
    for(key in c("id","kind","premise","conclusion","source_model","target_model","bindings","review_status")) {
      values <- lapply(graph$edges,`[[`,key)
      if(all(vapply(values,.scalar_string,logical(1)))) values <- unlist(values,use.names=FALSE)
      out <- igraph::set_edge_attr(out,key,value=values)
    }
    # List-valued attributes preserve complete evidence and prerequisite records.
    for(key in c("requires","evidence")) out <- igraph::set_edge_attr(out,key,value=lapply(graph$edges,`[[`,key))
  }
  for(key in setdiff(names(graph),c("nodes","edges","directed"))) out <- igraph::set_graph_attr(out,key,graph[[key]])
  out
}

Try the largecardinals package in your browser

Any scripts or data that you put into this service are public.

largecardinals documentation built on Nov. 25, 2026, 5:09 p.m.