/*
 * Copyright (c) 2005-2006 Tom N Harris.
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     A copy of the GNU General Public License can be found at
 *     http://www.gnu.org/
 *
 * Written by Tom N Harris <telliamed@whoopdedo.org>
 *
 */

#ifndef RDF_GRAPH_H
#define RDF_GRAPH_H

#include <string>
#include <vector>
#include <ext/hash_map>
#include "string_hash_fun.h"

#include "raptor.h"


namespace SimpleRDF
{

struct RdfTriple
{
	const std::string pred;
	const std::string subj;
	const std::string obj;
	const std::string lang;
	const std::string type;

	RdfTriple(char const * __s, char const * __p, char const * __o);
	RdfTriple(char const * __s, char const * __p, char const * __o, char const * __l);
	RdfTriple(char const * __s, char const * __p, char const * __o, char const * __l, char const * __t);
};

class RdfGraph
{
public:
	typedef std::vector<RdfTriple*> TripleList;

private:
	typedef __gnu_cxx::hash_map<std::string,TripleList> PredList;

	TripleList m_triples;
	PredList m_predicates;
	PredList m_types;
	PredList m_sequences;

	static unsigned long LibraryCounter;

public:
	std::string ErrorMsg;
	std::string ErrorLoc;

	RdfGraph();
	~RdfGraph();

	TripleList FindByType(char const * __t) const;
	TripleList FindByPredicate(char const * __p) const;
	TripleList FindBySubject(char const * __s) const;
	TripleList FindBySubjectPredicate(char const * __s, char const * __p) const;

	RdfTriple* FindOneBySubjectPredicate(char const * __s, char const * __p) const;
	RdfTriple* FindOneBySubjectPredicateLang(char const * __s, char const * __p, char const * __l) const;

	TripleList GetSequence(char const * __s) const;

	TripleList const& GetAllStatements() const
		{ return m_triples; };

	bool LoadFromFile(char const * __f);
	bool LoadFromUri(char const * __u);

private:
	RdfTriple* AddStatement(raptor_statement const * stmt, 
				char const * __s, char const * __p, char const * __o,
				char const * __l, char const * __t);
	void EraseAllStatements(void);

	raptor_parser* m_parser;

	static void statement_callback(void * __d, const raptor_statement * __s);
	static void error_callback(void * __d, raptor_locator * __l, const char * __s);
	static void warning_callback(void * __d, raptor_locator * __l, const char * __s);
};

};

#endif /* RDF_GRAPH_H */
