Quick links: Tutorial - Examples - Files - Symbols.
Classes: Hierarchy - Index - List - Members.
Namespaces: Index - base - cs - display.

converter.cpp

Conversion de fichiers dans les différents format gérés par Cogitant. Le code source de ce programme se trouve dans "samples/converter". Il n'est utilisable que dans le cas où Cogitant a été compilé avec les fonctions de dessin.

See Also
Graph drawing.
// ==============
// CoGITaNT 5
// ==============
/* Example showing how to use Cogitant 5: File Conversion
*
* This program allows conversion of files using the different
* file formats recognised by Cogitant. It can be used to convert
* supports but also graphs and rules.
* Caution: Certain files are only accessible in "write" mode
* (FIG, Linear) and cannot represent all objects manipulated by
* Cogitant (FIG and Linear allow representation of graphs only,
* CGIF allows representation of graphs and supports but not rules).
* Cogitant is using the file extension to know the format of a file
* (in read and write access). Therefore there is no need to indicate
* the format explicitly. But if you use an extension unknown to
* Cogitant, the file will not be read or written (unknown format).
* The application is used by giving commands executed in the order
* in which they are entered. Therefore, the order is important.
* Caution: A support must be loaded before loading a graph. If this
* is not the case, the graph will not load because it will try to use
* types not defined for the current support (if no support is loaded,
* the current support is empty).
* Example:
* converter -si bucolic.bcs -gi fact.bcg -go fact.cgif -go tmp.fig
* This command starts by loading a support (SI : Support - Input)
* called bucolic.bcs (and therefore in the BCGCT format), then a graph
* is loaded (GI : Graph - Input). It is a BCGCT graph (extension BCG).
* Then the graph is saved in CGIF format under the name fact.cgif
* (GO : Graph - Output) and saved again under the FIG format with the
* name tmp.fig.
*/
#include <iostream>
// Basic functions of the library.
// This file gives access to the drawing functions
using namespace std;
// Displays the user manual
static void printHelp()
{
cerr << "Usage: converter <options>" << endl;
cerr << " -si <fic> Load a support file" << endl;
cerr << " -so <fic> Save the support into a file" << endl;
cerr << " -gi <fic> Load objects from a file (graph, rule, constraint)" << endl;
cerr << " -go <fic> Save an object into a file" << endl;
cerr << " -fi <fic> Load a file (support, graphs, rules, constraints, rdf)" << endl;
cerr << " -fo <fic> Write everything into a file (support + objects)" << endl;
cerr << " -vo <ver> Version of the output format" << endl;
cerr << " CGIF: 1: simplified 2001, 2: 2001, 3: core, 4: extended (default: 4)" << endl;
cerr << " BCGCT: 1, 2: Cogitant v-4, 3: Cogitant v-5 (default: 3)" << endl;
cerr << " -at Allow unknown types when loading a graph" << endl;
cerr << " -ai Allow unknown individuals when loading a graph" << endl;
cerr << " -fp Filter properties. Additional properties (size, colors, etc.) are" << endl;
cerr << " filtered while reading objects." << endl;
cerr << " -ie Ignore DTD errors when loading a file. Use this to load graphs from" << endl;
cerr << " CoGui since CoGui uses extensions of Cogxml." << endl;
cerr << " -ol Use labels instead of ids while writing Cogxml files." << endl;
cerr << " -ib <uri> Set a base URI for relative URis when loading RDF files." << endl;
cerr << "Warning: -so, -go and -fo overwrite file with the given name" << endl;
cerr << "Example: converter -si bucolic.bcs -gi fact.bcg -go fact.cgif -go tmp.fig" << endl;
}
int main(int argc, char* argv[])
{
if (argc == 1)
{
printHelp();
return 0;
}
try
{
// Initialisations. An environment will be used for input
// and output. In order to save in graphic formats (FIG),
// a Display Manager is created which ensure export in graphic
// format (without the Display Manager, the environment only
// recognise text formats such as BCGCT, CGIF, CoGXML and Linear).
disphandler.addFormats(env);
vector<cogitant::iSet> igraphs;
unsigned int versionout = 0;
// Looping through arguments entered via the command line.
// They are analysed one after the other and immediately interpreted.
for (int i=1; i<argc; i++)
{
string arg(argv[i]);
if (arg == "-si")
{
// if i is the index of the "-si" argument, then
// the name of the file is found at the next value
// of i.
i++;
cout << "Loading support " << argv[i] << endl;
// The environment is made empty (support and graph) in case
// the "-si" is to be used several times.
env.clear();
// The file format is recognised automatically
// by Cogitant thanks to the file extension.
env.readSupport(string(argv[i]));
}
else if (arg == "-so")
{
i++;
cout << "Writing support " << argv[i] << endl;
env.writeSupport(string(argv[i]), versionout);
}
else if (arg == "-gi")
{
i++;
cout << "Loading objects " << argv[i] << " ..." << endl;
// The readGraphs method returns the identifier (in the
// environment) of the first graph which is read.
// But the file can contain several graphs. Therefore we
// use an optional parameter of readGraphs() which is
// a vector of iSet and which contains the iSet values of the
// read objects.
igraphs.clear();
env.readGraphs(string(argv[i]), &igraphs);
cout << "... " << igraphs.size() << " object(s)" << endl;
}
else if (arg == "-go")
{
i++;
if (!igraphs.empty())
{
cout << "Writing objects " << argv[i] << endl;
env.writeGraphs(string(argv[i]), igraphs, versionout);
}
}
else if (arg == "-fi")
{
i++;
cout << "Loading file " << argv[i] << endl;
env.read(string(argv[i]), &igraphs);
}
else if (arg == "-fo")
{
i++;
cout << "Writing file " << argv[i] << endl;
env.ioHandler()->write(string(argv[i]), cogitant::OperationOutput::SO_ALL, true, cogitant::IOHandler::AUTO, versionout, &igraphs);
}
else if (arg == "-vo")
{
i++;
versionout = cogitant::strToInt(argv[i]);
}
else if (arg == "-at")
else if (arg == "-ai")
else if (arg == "-fp")
else if (arg == "-ie")
dynamic_cast<cogitant::OperationCoGXMLInput *>(env.ioHandler()->operationInput(cogitant::IOHandler::COGXML))->setParamIgnoreUnknownElements(true);
else if (arg == "-ol")
else if (arg == "-ib")
{
i++;
env.ioHandler()->baseIds(argv[i]);
}
else
{
cerr << "Unknown command: " << argv[i] << endl;
printHelp();
return 1;
}
}
}
catch (cogitant::Exception & e)
{
// If an exception is found (i.e.: cannot read file, cannot write file,
// unknown format, syntax error in a file, etc), an error message
// is displayed and the program ends.
cerr << e.toString() << endl;
return 1;
}
return 0;
}