1
2## ----------------------------------------------------------------------
3##
4##   IGraph R package
5##   Copyright (C) 2014  Gabor Csardi <csardi.gabor@gmail.com>
6##   334 Harvard street, Cambridge, MA 02139 USA
7##
8##   This program is free software; you can redistribute it and/or modify
9##   it under the terms of the GNU General Public License as published by
10##   the Free Software Foundation; either version 2 of the License, or
11##   (at your option) any later version.
12##
13##   This program is distributed in the hope that it will be useful,
14##   but WITHOUT ANY WARRANTY; without even the implied warranty of
15##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16##   GNU General Public License for more details.
17##
18##   You should have received a copy of the GNU General Public License
19##   along with this program; if not, write to the Free Software
20##   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA
21##   02110-1301 USA
22##
23## ----------------------------------------------------------------------
24
25#' Igraph data structure versions
26#'
27#' Igraph's internal data representation changes sometimes between
28#' versions. This means that it is not possible to use igraph objects
29#' that were created (and possibly saved to a file) with an older
30#' igraph version.
31#'
32#' \code{graph_version} queries the current data format,
33#' or the data format of a possibly older igraph graph.
34#'
35#' \code{\link{upgrade_graph}} can convert an older data format
36#' to the current one.
37#'
38#' @param graph The input graph. If it is missing, then
39#'   the version number of the current data format is returned.
40#' @return A character scalar.
41#'
42#' @seealso upgrade_graph to convert the data format of a graph.
43#' @export
44
45graph_version <- function(graph) {
46  if (missing(graph)) {
47    "0.8.0"
48
49  } else {
50    stopifnot(is_igraph(graph))
51    .Call(C_R_igraph_graph_version, graph)
52  }
53}
54
55#' Igraph data structure versions
56#'
57#' Igraph's internal data representation changes sometimes between
58#' versions. This means that it is not possible to use igraph objects
59#' that were created (and possibly saved to a file) with an older
60#' igraph version.
61#'
62#' \code{\link{graph_version}} queries the current data format,
63#' or the data format of a possibly older igraph graph.
64#'
65#' \code{upgrade_graph} can convert an older data format
66#' to the current one.
67#'
68#' @param graph The input graph.
69#' @return The graph in the current format.
70#'
71#' @seealso graph_version to check the current data format version
72#' or the version of a graph.
73#' @export
74
75upgrade_graph <- function(graph) {
76
77  stopifnot(is_igraph(graph))
78
79  g_ver <- graph_version(graph)
80  p_ver <- graph_version()
81
82  if (g_ver < p_ver) {
83
84    if ((g_ver == "0.4.0" && p_ver == "0.8.0")) {
85      .Call(C_R_igraph_add_env, graph)
86
87    } else if (g_ver == "0.7.999" && p_ver == "0.8.0") {
88      .Call(C_R_igraph_add_version_to_env, graph)
89
90    } else {
91      stop("Don't know how to upgrade graph from ", g_ver, " to ", p_ver)
92    }
93
94  } else if (g_ver > p_ver) {
95    stop("Don't know how to downgrade graph from ", g_ver, " to ", p_ver)
96
97  } else {
98    graph
99  }
100}
101
102## Check that the version is the latest
103
104check_version <- function(graph) {
105  if (graph_version() != graph_version(graph)) {
106    stop("This graph was created by an old(er) igraph version.\n",
107         "  Call upgrade_graph() on it to use with the current igraph version")
108  }
109}
110
111warn_version <- function(graph) {
112  if (graph_version() != graph_version(graph)) {
113    message("This graph was created by an old(er) igraph version.\n",
114            "  Call upgrade_graph() on it to use with the current igraph version\n",
115            "  For now we convert it on the fly...")
116    TRUE
117  } else {
118    FALSE
119  }
120}
121