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 << "Main options" << endl;
cerr << " -si <file> Load a support file." << endl;
cerr << " -so <file> Save the support into a file." << endl;
cerr << " -gi <file> Load objects from a file (graph, rule, constraint)." << endl;
cerr << " -go <file> Save an object into a file." << endl;
cerr << " -fi <file> Load a file (support, graphs, rules, constraints, rdf)." << endl;
cerr << " -fo <file> Write everything into a file (support + objects)." << endl;
cerr << " -ff Show file formats and extensions." << endl;
cerr << "Specific options" << 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 << " -fg <id> Filter objects (graphs, rules, constrains). Objects not named <id>" << endl;
cerr << " are ignored in write operations." << endl;
cerr << "CoGXML options" << endl;
cerr << " -ci Ignore DTD errors when loading a file. Use this to load graphs from" << endl;
cerr << " CoGui since CoGui uses extensions of Cogxml." << endl;
cerr << " -cl Use labels instead of ids." << endl;
cerr << "RDF options" << endl;
cerr << " -rb <uri> Set a base URI for relative URis when loading RDF files." << endl;
cerr << " -rp Force the use of prefixes while writing N3 files." << endl;
cerr << "Linear form options" << endl;
cerr << " -ll <lang> Set the language for output." << endl;
cerr << "Fig output options" << endl;
cerr << " -gl Show label on edges." << endl;
cerr << " -gm Show * marker on concepts." << endl;
cerr << 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;
}
static void printFormats(cogitant::Environment const & env)
{
cout << "File formats" << endl;
for (cogitant::IOHandler::Format f = 1; f <= env.ioHandler()->size(); f++)
{
if (env.ioHandler()->operationInput(f) != NULL)
cout << "IN ";
else
cout << " ";
if (env.ioHandler()->operationOutput(f) != NULL)
cout << "OUT ";
else
cout << " ";
cout << env.ioHandler()->name(f) << " (";
vector<string> const & exts(env.ioHandler()->extensions(f));
for (vector<string>::const_iterator ie=exts.begin(); ie!=exts.end(); ie++)
{
if (ie != exts.begin()) cout << ",";
cout << (*ie);
}
cout << ")" << 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
// recognises text formats such as BCGCT, CGIF, CoGXML, RDF 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 == "-ff")
printFormats(env);
else if (arg == "-at")
else if (arg == "-ai")
else if (arg == "-fp")
else if (arg == "-fg")
{
i++;
vector<cogitant::iSet>::iterator j=igraphs.begin();
while (j != igraphs.end())
if (env.objects(*j)->name() == argv[i])
j++;
else
igraphs.erase(j);
cout << igraphs.size() << " selected object(s)" << endl;
}
else if (arg == "-ci")
dynamic_cast<cogitant::OperationCoGXMLInput *>(env.ioHandler()->operationInput(cogitant::IOHandler::COGXML))->setParamIgnoreUnknownElements(true);
else if (arg == "-cl")
else if (arg == "-rp")
{
cogitant::OperationRdfsOutput* outn3(dynamic_cast<cogitant::OperationRdfsOutput*>(env.ioHandler()->operationOutput(cogitant::IOHandler::RDFN3)));
dynamic_cast<cogitant::RdfsOutputN3 *>(outn3->getOut())->setAutoPrefixes(true);
}
else if (arg == "-rb")
{
i++;
env.ioHandler()->baseIds(argv[i]);
}
else if (arg == "-ll")
{
i++;
}
else if ((arg == "-gl") || (arg == "-gm"))
{
for (vector<cogitantdisplay::Operation *>::const_reverse_iterator i=disphandler.layoutHandler()->operations().rbegin(); i!=disphandler.layoutHandler()->operations().rend(); ++i)
{
if (ogl != NULL)
{
if (arg == "-gl")
const_cast<cogitantdisplay::OperationGraphLayout *>(ogl)->setParamRelationLabelLink(true);
else if (arg == "-gm")
const_cast<cogitantdisplay::OperationGraphLayout *>(ogl)->setParamConceptCompleteLabel(true);
}
}
}
else
{
cerr << "Unknown command: " << argv[i] << endl;
printHelp();
return 1;
}
}
cout << "Done." << endl;
}
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;
}