COdb

The OjectDatabase is a Graph-Database.

It aims to build a natural image of the relevant links between CThings (=nodes) amongst each other as well as things and atoms (=data fields).

CThings are objects or nodes, like cars in cars or trees or people.

CThing’s may be linked amongst each other for specified CReasons CReason’s are unidirectional linking CThing’s CProperty is a primitive property of a CThing CAtom’s are attributes of CThings containing descriptive data

  • A thing may be a tree
  • Attributes of a tree may be, position, health status, animals living in it
  • A tree may relate to other trees by taking the light, building a forrest

Sample code

#include <iostream>

#include "odb.h"

int main()
   {
   auto oOdb = odb::COdb();
   oOdb.print_json( std::cout );
   std::cout << '\n';
   }
class odb::COdb

The database.

A Object Database itself is an Identifiable object. Enabeling multiple database instances in one application.

Author
Manfred Morgner
Since
0.1.17

Inherits from Identifiable< COdb >

Public Functions

COdb()

Creates a COdb, a database.

We only default construct the database, no copy construction no copy of the database at all. It is not known, what copying the database means.

COdb(COdb const &src)

deleted: copy constructor

Copying a databse is not supported yet. We will support it as soon as we find out what it means.

COdb(COdb const &&src)

deleted: move constructor

Moving a databse is not supported yet.

COdb &operator=(COdb const &src)

deleted: Assignment operator

Here we are in the same situation as with copy construction

virtual ~COdb()

Some cleanup.

If we wish to convience valgrind we are doing it right, we have to free all links to other objects before leaving the show.

void clear()

Frees all objects.

Frees all known objects at last so far that freeing the object collections releases all memory, bound to them. So valgrind will find nothing left on the table.

PThing MakeThing(std::string const &crsName = "")

Creates a PThing.

Creates a shared_ptr with a new CThing named as given in the call. If no name is given, the name will be the class default

Parameters
  • crsName: The name for the CThing

PThing LoadThing(size_t nId, std::string const &crsName = "")

Creates a PThing with predefined ID.

Creates a shared_ptr with a new CThing named as given in the call. If no name is given, the name will be the class default

Parameters
  • nId: The predefined ID if loading given sets into odb
  • crsName: The name for the CThing

PProperty MakeProperty(std::string const &crsName = "")

Creates a PProperty.

Creates a shared_ptr with a new CProperty named as given in the call. If no name is given, the name will be the class default

Parameters

PProperty LoadProperty(size_t nId, std::string const &crsName = "")

Creates a PProperty with predefined ID.

Creates a shared_ptr with a new CProperty named as given in the call. If no name is given, the name will be the class default

Parameters
  • nId: The predefined ID if loading given sets into odb
  • crsName: The name for the CProperty

PAtom const MakeAtom(int data, std::string const &crsName = "", std::string const &crsPrefix = "", std::string const &crsSuffix = "", std::string const &crsFormat = "")

Creates a PAtom.

Creates a shared_ptr with a new CAtom named as given in the call. If no name is given, the name will be the class default

The data type of the data element in CAtom follows the input data type. It can be if any primitive type or most of the simple containers, like string or vector.

CAtom further on manages the life time of the data element. It’s a unique_ptr

Parameters
  • data: The data for the CAtom
  • crsName: The name for the CAtom
  • crsPrefix: The prefix for the CAtom in a GUI
  • crsSuffix: The suffix for the CAtom in a GUI
  • crsFormat: The format for the CAtom in a GUI

PAtom const LoadAtom(size_t nId, int data, std::string const &crsName = "", std::string const &crsPrefix = "", std::string const &crsSuffix = "", std::string const &crsFormat = "")

Creates a PAtom with predefined ID.

Creates a shared_ptr with a new CAtom named as given in the call. If no name is given, the name will be the class default

The data type of the data element in CAtom follows the input data type. It can be if any primitive type or most of the simple containers, like string or vector.

CAtom further on manages the life time of the data element. It’s a unique_ptr

Parameters
  • nId: The predefined ID if loading given sets into odb
  • data: The data for the CAtom
  • crsName: The name for the CAtom
  • crsPrefix: The prefix for the CAtom in a GUI
  • crsSuffix: The suffix for the CAtom in a GUI
  • crsFormat: The format for the CAtom in a GUI

PReason MakeReason(std::string const &crsName = "")

Creates a PReason.

Creates a shared_ptr with a new CReason named as given in the call. If no name is given, the name will be the class default

Parameters
  • crsName: The name for the CReason

PReason LoadReason(size_t nId, std::string const &crsName = "")

Creates a PReason with predefined ID.

Creates a shared_ptr with a new CReason named as given in the call. If no name is given, the name will be the class default

Parameters
  • nId: The predefined ID if loading given sets into odb
  • crsName: The name for the CReason

PStrand MakeStrand(std::string const &crsName = "")

Creates a PStrand.

Creates a shared_ptr with a new CStrand named as given in the call. If no name is given, the name will be the class default

Parameters
  • crsName: The name for the CStrand

void print() const

Print out the database (Informative format)

void print(const CAtoms &crContainer) const

Print out container of CAtom objects.

Parameters
  • crContainer: The forward iterable container, containing PAtom’s

template <typename T>
void print(const CT<T> &crContainer) const

Print out container of CThing’s.

Parameters
  • crContainer: The forward iterable container, containing all CThing instances

auto Escape(std::string const &crsInput)

Replaces 2 with ” and \ with .

Parameters
  • crsInput: The string to be escaped

void print_json(CThings const &crContainer, std::ostream &ros)

Dump all CThings in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CThing instances
  • ros: The output destination

void print_json_stream(CThings const &crContainer, std::ostream &ros)

Dump all CThings in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CThing instances
  • ros: The output destination

void print_json(CProperties const &crContainer, std::ostream &ros)

Dump all CProperty’s in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CProperty instances
  • ros: The output destination

void print_json_stream(CProperties const &crContainer, std::ostream &ros)

Dump all CProperty’s in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CProperty instances
  • ros: The output destination

void print_json(CAtoms const &crContainer, std::ostream &ros)

Dump all CAtoms in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CAtom instances
  • ros: The output destination

void print_json_stream(CAtoms const &crContainer, std::ostream &ros)

Dump all CAtoms in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CAtom instances
  • ros: The output destination

void print_json(CReasons const &crContainer, std::ostream &ros)

Dump all CReasons in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CReason instances
  • ros: The output destination

void print_json_stream(CReasons const &crContainer, std::ostream &ros)

Dump all CReasons in Sub-JSON format.

Parameters
  • crContainer: The forward iterable container, containing all CReason instances
  • ros: The output destination

void print_json(std::ostream &ros)

Dump the hole database in JSON format.

see also: JSON validator+converter: https://jsonformatter.org/

Sample: Link Atoms to Things and Thing to Thing and dump it as JSON
#include <iostream>

#include "odb.h"
#include "atom.h"
#include "thing.h"

int main()
{
auto oOdb    = odb::COdb();
auto pThing1 = oOdb.MakeThing("Ulrich");
auto pThing2 = oOdb.MakeThing("Fred");
auto pAtom1  = oOdb.MakeAtom("Leader", "Role");
auto pAtom2  = oOdb.MakeAtom("Member", "Role");
auto pReason = oOdb.MakeReason("pays");
pThing1->Append(pAtom1);
pThing2->Append(pAtom2);
pThing1->Link(pThing2, pReason);
oOdb.print_json(std::cout);
}

Output

{
"Object Database Dump":
    {
    "Sizes": [ {"P": 0},{"A": 2},{"R": 1},{"T": 2} ],
    "Properties":
        [
        ],
    "Atoms":
        [
            { "id": 0, "data": "Leader" },
            { "id": 1, "data": "Member" }
        ],
    "Reasons":
        [
            { "id": 0, "name": "pays" }
        ],
    "Things":
        [
            { "id": 0, "name": "Ulrich",
                "properties": [  ],
                "atoms": [ {"id": 0} ],
                "links": [ {"thing-id": 1, "reason-id": 0} ] },
            { "id": 1, "name": "Fred",
                "properties": [  ],
                "atoms": [ {"id": 1} ],
                "links": [  ] }
        ]
    }
 }
Parameters
  • ros: The output destination

void print_json_stream(std::ostream &ros)

Dump the hole database in JSON format.

Parameters
  • ros: The output destination

void SaveDB(std::string const &crsFilename)

Saves an odb json file.

Parameters
  • crsFilename:

bool LoadDB(std::string const &crsFilename)

Loads an odb json file.

Parameters
  • crsFilename:

PThing FindOrLoadThingById(size_t const nId, std::string const &crsName = "")

Has to return a thing with specified ID, if it does not exists, it is to make.

Parameters
  • nId: The id of the thing
  • crsName: The name of the thing if it has to be created

OThing FindThingByProperty(std::string const &crsProperty)

Finds PThing with a named Property only if it’s unique.

Parameters

CThings FindThingsByProperty(std::string const &crsProperty)

Finds PThing with a named Property only if it’s one.

Parameters

CThings FindThingsByProperty(std::regex const &crsRegex)

Finds PThing with a named Property only if it’s one.

Parameters

PThing FindOrMakeThingByProperty(std::string const &crsThing, std::string const &crsProperty)

Finds or creates a PThing with a named Property, which also may be created and assigned.

Parameters
  • crsThing: The name for the CThing
  • crsProperty: The name for the CProperty

PProperty FindOrMakeProperty(std::string const &crsProperty)

Has to return a property, if it does not exists, it is to make.

Parameters
  • crsProperty: The name of the Property

PReason FindOrMakeReason(std::string const &crsReason)

Has to return a Reason, if it does not exists, it is to make.

Parameters
  • crsReason: The name of the Reason

bool AppendProperty2Thing(size_t nProperty, size_t nThing)

todo: optimize / Appends a Property to a Thing by given index value

bool AppendProperty2Thing(std::string const &crsProperty, bool bForce, std::string const &crsThing)

todo: optimize / Appends a Property to a Thing by given names

bool AppendAtom2Thing(size_t nThing, size_t nAtom)

todo: optimize / Appends an Atom to a Thing by given index value

bool LinkThing2Thing(size_t nThingFrom, size_t nThingTo, size_t nReason)

todo: optimize / Links a Thing to a Thing for a Reason by given index value

template <typename T>
std::optional<PT<T>> Find(CT<T> const &croContainer, size_t nId)

Finds the T with ID nId.

Template Parameters
  • T: the type of the filtered objects
Parameters
  • croContainer: The container to be filtered
  • nId: The ID of the T

template <typename T>
CT<T> Find(CT<T> const &croContainer, std::string const &crsName)

Finds all Ts with the given NAME.

Template Parameters
  • T: the type of the filtered objects
Parameters
  • croContainer: The container to be filtered
  • crsName: The NAME of the Reasons

template <typename T>
CT<T> Find(CT<T> const &croContainer, std::regex const &crsRegex)

Finds all Ts with the given NAME as RegEx.

Template Parameters
  • T: the type of the filtered objects
Parameters
  • croContainer: The container to be filtered
  • crsRegex: The NAME of the Ts in Regex

auto FindThing(size_t nId)

API Adapter.

auto FindThings(std::string const &crsName)

API Adapter.

auto FindThings(std::regex const &crsRegex)

API Adapter.

auto FindProperty(size_t nId)

API Adapter.

auto FindProperties(std::string const &crsName)

API Adapter.

auto FindProperties(std::regex const &crsRegex)

API Adapter.

auto FindReason(size_t nId)

API Adapter.

auto FindReasons(std::string const &crsName)

API Adapter.

auto FindReasons(std::regex const &crsRegex)

API Adapter.

auto FindAtoms(size_t nId)

API Adapter.

auto FindAtoms(std::string const &crsName)

API Adapter.

auto FindAtoms(std::regex const &crsRegex)

API Adapter.

CThings const &Things() const

Access function to call then container of CThing’s.

CProperties const &Properties() const

Access function to call then container of CProperties.

CAtoms const &Atoms() const

Access function to call then container of CAtom’s.

CReasons const &Reasons() const

Access function to call then container of CReason’s.

CStrands const &Strands() const

Access function to call then container of CStrand’s.

Protected Attributes

CThings m_oThings

A container instance of CThing’s.

CProperties m_oProperties

A container instance of CProperties.

CAtoms m_oAtoms

A container instance of CAtom’s.

CReasons m_oReasons

A container instance of CReason’s.

CStrands m_oStrands

A container instance of CStrand’s.