TKCellCode *cellCode = mContainer->getCellCode(cellInfo->cellCodeClassOrType);
if (cellCode)
{
- newCell = mContainer->createCellWithCellCodeClass(cellInfo->location, cellInfo->cellName, cellCode, cellInfo->customScript);
+ newCell = mContainer->addCell(cellInfo->location, cellInfo->cellName, cellCode, cellInfo->customScript);
}
else
{
}
else
{
- newCell = mContainer->createCellWithoutCellCodeClass(cellInfo->location, cellInfo->cellName, cellInfo->cellCodeClassOrType, cellInfo->customScript);
+ newCell = mContainer->addCell(cellInfo->location, cellInfo->cellName, cellInfo->cellCodeClassOrType, cellInfo->customScript);
}
if (newCell == NULL)
DEBUG_TRACE("\n===== Define cell code class ===== \nClass:%s\nAPI:%s\n%s\n", fqnName.c_str(), type.c_str(), cellScript.c_str());
- mContainer->createCellCode(fqnName, type, cellScript);
+ mContainer->addCellCode(fqnName, type, cellScript);
}
void DNContainerBuilder::defineConnection(const char *path, std::string origine, std::string destination, std::string name)
#include "TKCell.h"
#include "TKCellCode.h"
+#include "TKAxon.h"
+#include "TKReceptor.h"
+#include "TKAxonTerminal.h"
#include "TKContainer.h"
#include "TKLog.h"
#include "DNUtils.h"
#include "dnqscellcode.h"
#include "DNGlobal.h"
-const std::string DNQSContainer::CELLTYPE_JSBASIC = "B";
-const std::string DNQSContainer::CELLTYPE_OUT = "O";
-const std::string DNQSContainer::CELLTYPE_IN = "I";
-const std::string DNQSContainer::CELLTYPE_BASICSTORAGE = "BS";
-
static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() >= 1)
//static
TKContainer* TKContainer::createContainer()
{
- return new DNQSContainer;
+ TKContainer *container = new DNQSContainer();
+ container->init();
+ return container;
}
DNQSContainer::DNQSContainer()
mQSCellContainer = mQSEngine->newObject();
mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
-
- mEmptyCellClass = createCellCode("_DNEMPTY",CELLTYPE_JSBASIC,"");
}
DNQSContainer::~DNQSContainer()
if (mQSEngine)
delete mQSEngine;
}
-TKCell* DNQSContainer::createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript)
-{
- TKCell *cell = NULL;
- bool noScript = false;
- if (type == CELLTYPE_JSBASIC || type.length() == 0)
- {
- cell = new DNQSBasicCell(this, theLocation, theName, false);
- }
- else if (type == CELLTYPE_IN)
- {
- cell = new DNQSInputCell(this, theLocation, theName, true);
- noScript = true;
- }
- else if (type == CELLTYPE_OUT)
- {
- cell = new DNQSOutputCell(this, theLocation, theName, true);
- noScript = true;
- }
- else if (type == CELLTYPE_BASICSTORAGE)
- {
- cell = new DNQSBasicStorageCell(this, theLocation, theName, false);
- }
- else
- {
- std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
- message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type.");
- dnNotifyError("Initialization failed", message);
- }
+void DNQSContainer::setValue(std::string key, float value)
+{
+ mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
+}
- if (cell)
+float DNQSContainer::getValue(std::string key)
+{
+ QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
+ if (v.isNumber())
{
- std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
- mCells.insert(TKCellMap::value_type(fqnName, cell));
- if (cell->isInterface())
- {
- mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
- }
- if (!noScript)
- {
- cell->setCellCode(mEmptyCellClass,(const void*)customScript.c_str());
- }
- else
- {
- if (customScript.length()>0)
- {
- std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
- message.append("'\nThe cellcode is type '").append(type).append("'. This type doesn't support custom script.");
- dnNotifyError("Initialization failed",message);
- }
- }
+ return v.toNumber();
}
else
{
- std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
- std::string message = "Failed to create a cell. ";
- message += fqnName;
- message += "\n";
- message += "Out of memory?";
- dnNotifyError("Initialization failed", message);
+ return 0;
}
-
- return cell;
}
-TKCell* DNQSContainer::createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript)
+TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterface)
{
TKCell *cell = NULL;
- std::string type = cellCode->getCellAPIName();
-
- if (type == CELLTYPE_JSBASIC)
- {
- cell = new DNQSBasicCell(this, theLocation, theName, false);
- }
- else if (type == CELLTYPE_BASICSTORAGE)
- {
- cell = new DNQSBasicStorageCell(this, theLocation, theName, false);
- }
- else if (type == CELLTYPE_OUT)
+ if (type == CELLTYPE_JSBASIC || type.length() == 0)
{
- std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
- message.append("'\nThe cellcode type '").append(type).append("' doesn't support to have a CellCode class.");
- dnNotifyError("Initialization failed", message);
+ cell = new DNQSBasicCell(this, location, name, canInterface);
+ cell->init();
}
else if (type == CELLTYPE_IN)
{
- std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
- message.append("'\nThe cellcode requires type '").append(type).append("' doesn't support to have a CellCode class.");
- dnNotifyError("Initialization failed", message);
- }
- else
- {
- std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
- message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type.");
- dnNotifyError("Initialization failed",message);
+ cell = new DNQSInputCell(this, location, name, canInterface);
+ cell->init();
}
-
-
- if (cell)
+ else if (type == CELLTYPE_OUT)
{
- std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
- mCells.insert(TKCellMap::value_type(fqnName, cell));
- cell->setCellCode(cellCode,(const void*)customScript.c_str());
-
- if (cell->isInterface())
- {
- mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
- }
+ cell = new DNQSOutputCell(this, location, name, canInterface);
+ cell->init();
}
- else
+ else if (type == CELLTYPE_BASICSTORAGE)
{
- std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
- std::string message = "Failed to create a cell. ";
- message += fqnName;
- message += "\n";
- message += "Out of memory?";
- dnNotifyError("Initialization failed", message);
+ cell = new DNQSBasicStorageCell(this, location, name, canInterface);
+ cell->init();
}
return cell;
}
-TKCellCode* DNQSContainer::createCellCode(std::string theName, std::string theAPIType, std::string code)
+TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
{
- DNQSCellCode *cellCode = new DNQSCellCode(theName, theAPIType, this, code);
+ return new TKAxon(theOwner);
+}
- if (cellCode)
- {
- mCellCodes.insert( std::map<std::string, TKCellCode*>::value_type(theName, cellCode));
- }
- return cellCode;
+TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
+{
+ return new TKReceptor(theOwner);
}
-void DNQSContainer::setValue(std::string key, float value)
+TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
{
- mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
+ return new TKAxonTerminal(theOwner);
}
-float DNQSContainer::getValue(std::string key)
+TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
{
- QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
- if (v.isNumber())
- {
- return v.toNumber();
- }
- else
- {
- return 0;
- }
+ return new DNQSCellCode(name, cellapi, this, code);
}
+
DNQSContainer();
virtual ~DNQSContainer();
- virtual TKCell* createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript);
- virtual TKCell* createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript);
- virtual TKCellCode* createCellCode(std::string theName, std::string theAPIType, std::string code);
-
virtual void setValue(std::string key, float value);
virtual float getValue(std::string key);
inline QScriptValue getScriptGlobalObject() { return mQSGlobalObject; }
inline QScriptValue getQSCellContainer() { return mQSCellContainer; }
- static const std::string CELLTYPE_JSBASIC;
- static const std::string CELLTYPE_OUT;
- static const std::string CELLTYPE_IN;
- static const std::string CELLTYPE_BASICSTORAGE;
+ virtual TKCell* cellFactory(std::string location, std::string name, std::string type, bool canInterface = true);
+ virtual TKAxon* axonFactory(TKCell *theOwner);
+ virtual TKReceptor* receptorFactory(TKCell *theOwner);
+ virtual TKAxonTerminal* axonTerminalFactory(TKAxon *theOwner);
+ virtual TKCellCode* cellCodeFactory(std::string name, std::string cellapi, std::string code);
private:
QScriptEngine *mQSEngine;
QScriptValue mQSGlobalObject;
QScriptValue mQSCellContainer;
- TKCellCode *mEmptyCellClass;
};
#endif // DNQSCONTAINER_H
// Created by tkawata on 12/11/2011.
//
#include "TKAxon.h"
+#include "TKCell.h"
+#include "TKContainer.h"
#include "TKAxonTerminal.h"
#include "TKLock.h"
#include "DNUtils.h"
TKAxonTerminal* TKAxon::addTerminal()
{
- TKAxonTerminal *terminal = new TKAxonTerminal(this);
+ TKAxonTerminal *terminal = mOwner->getContainer()->axonTerminalFactory(this);
if (terminal)
{
TKCell::TKCell(TKContainer *container, std::string location, std::string name, bool canIntarface)
: mName(name), mLocation(location), mContainer(container), mAxon(NULL), mCellCodeInstance(NULL), mCanInterface(canIntarface)
{
- mAxon = new TKAxon(this);
}
TKCell::~TKCell()
mCellCodeInstance = NULL;
}
- for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
- delete it->second;
- }
+ for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
+ delete it->second;
+ }
mReceptors.clear();
if (mAxon)
}
}
+void TKCell::init()
+{
+ if (!mAxon)
+ mAxon = mContainer->axonFactory(this);
+}
+
float TKCell::getAxonValue()
{
if (!mAxon)
bool TKCell::connectTo(std::string connectionName, TKCell *targetCell)
{
- TKReceptor *receptor = targetCell->createReceptor(connectionName);
+ TKReceptor *receptor = targetCell->createReceptor(connectionName);
if (receptor)
{
TKAxonTerminal *terminal = mAxon->addTerminal();
receptor->setTarget(terminal);
-
+
return true;
}
else
}
else
{
- receptor = new TKReceptor(this);
+ receptor = mContainer->receptorFactory(this);
if (receptor)
{
mReceptors.insert( TKReceptorMap::value_type( name, receptor ) );
}
}
-
+
return receptor;
}
class TKCell
{
public:
- TKCell(TKContainer *container, std::string location, std::string name, bool canInterface);
- virtual ~TKCell();
-
+ TKCell(TKContainer *container, std::string location, std::string name, bool canInterface);
+ virtual ~TKCell();
+
+ void init();
+
std::string getName() { return mName; }
float getAxonValue();
void setAxonValue(float value);
-
+
const TKReceptorMap* getReceptors() { return &mReceptors; }
-
+
virtual bool setCellCode(TKCellCode *code, const void *data);
-
- bool connectTo(std::string connectionName, TKCell *targetReceptor);
- inline TKContainer* getContainer() { return mContainer; }
+
+ bool connectTo(std::string connectionName, TKCell *targetReceptor);
+ inline TKContainer* getContainer() { return mContainer; }
bool isInterface() { return mCanInterface; }
-
- virtual bool doTick(float time) = 0;
+
+ virtual bool doTick(float time) = 0;
virtual bool doInit() = 0;
virtual bool doDestroy() = 0;
protected:
virtual TKReceptor* createReceptor(std::string name);
-
+
std::string mName;
std::string mLocation;
- TKContainer *mContainer;
- TKAxon *mAxon;
- TKReceptorMap mReceptors;
+ TKContainer *mContainer;
+ TKAxon *mAxon;
+ TKReceptorMap mReceptors;
TKCellCodeInstance *mCellCodeInstance;
bool mCanInterface;
};
#endif
-
+
#include "TKContainer.h"
#include "TKCell.h"
+#include "TKCellCode.h"
#include "DNStorage.h"
#include "DNUtils.h"
+#include <string>
+
+const std::string TKContainer::CELLTYPE_JSBASIC = "B";
+const std::string TKContainer::CELLTYPE_OUT = "O";
+const std::string TKContainer::CELLTYPE_IN = "I";
+const std::string TKContainer::CELLTYPE_BASICSTORAGE = "BS";
+
+TKContainer::TKContainer() : mStorage(NULL), mEmptyCellClass(NULL)
+{
+}
+
+void TKContainer::init()
+{
+ mEmptyCellClass = addCellCode("_DNEMPTY",CELLTYPE_JSBASIC,"");
+}
+
TKContainer::~TKContainer()
{
doDestroy();
return true;
}
+TKCell* TKContainer::addCell(std::string theLocation, std::string theName, std::string type, std::string customScript)
+{
+ TKCell *cell = NULL;
+ bool noScript = false;
+
+ if (type == CELLTYPE_JSBASIC || type.length() == 0)
+ {
+ cell = cellFactory(theLocation, theName, type, false);
+ }
+ else if (type == CELLTYPE_IN)
+ {
+ cell = cellFactory(theLocation, theName, type, true);
+ noScript = true;
+ }
+ else if (type == CELLTYPE_OUT)
+ {
+ cell = cellFactory(theLocation, theName, type, true);
+ noScript = true;
+ }
+ else if (type == CELLTYPE_BASICSTORAGE)
+ {
+ cell = cellFactory(theLocation, theName, type, false);
+ }
+ else
+ {
+ std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
+ message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type.");
+ dnNotifyError("Initialization failed", message);
+ }
+
+ if (cell)
+ {
+ std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
+ mCells.insert(TKCellMap::value_type(fqnName, cell));
+ if (cell->isInterface())
+ {
+ mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
+ }
+ if (!noScript)
+ {
+ cell->setCellCode(mEmptyCellClass,(const void*)customScript.c_str());
+ }
+ else
+ {
+ if (customScript.length()>0)
+ {
+ std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
+ message.append("'\nThe cellcode is type '").append(type).append("'. This type doesn't support custom script.");
+ dnNotifyError("Initialization failed",message);
+ }
+ }
+ }
+ else
+ {
+ std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
+ std::string message = "Failed to create a cell. ";
+ message += fqnName;
+ message += "\n";
+ message += "Out of memory?";
+ dnNotifyError("Initialization failed", message);
+ }
+
+ return cell;
+}
+
+TKCell* TKContainer::addCell(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript)
+{
+ TKCell *cell = NULL;
+
+ std::string type = cellCode->getCellAPIName();
+
+ if (type == CELLTYPE_JSBASIC)
+ {
+ cell = cellFactory(theLocation, theName, type, false);
+ }
+ else if (type == CELLTYPE_BASICSTORAGE)
+ {
+ cell = cellFactory(theLocation, theName, type, false);
+ }
+ else if (type == CELLTYPE_OUT)
+ {
+ std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
+ message.append("'\nThe cellcode type '").append(type).append("' doesn't support to have a CellCode class.");
+ dnNotifyError("Initialization failed", message);
+ }
+ else if (type == CELLTYPE_IN)
+ {
+ std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
+ message.append("'\nThe cellcode requires type '").append(type).append("' doesn't support to have a CellCode class.");
+ dnNotifyError("Initialization failed", message);
+ }
+ else
+ {
+ std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName);
+ message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type.");
+ dnNotifyError("Initialization failed",message);
+ }
+
+
+ if (cell)
+ {
+ std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
+ mCells.insert(TKCellMap::value_type(fqnName, cell));
+ cell->setCellCode(cellCode,(const void*)customScript.c_str());
+
+ if (cell->isInterface())
+ {
+ mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell));
+ }
+ }
+ else
+ {
+ std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
+ std::string message = "Failed to create a cell. ";
+ message += fqnName;
+ message += "\n";
+ message += "Out of memory?";
+ dnNotifyError("Initialization failed", message);
+ }
+
+ return cell;
+}
+
+TKCellCode* TKContainer::addCellCode(std::string theName, std::string theAPIType, std::string code)
+{
+ TKCellCode *cellCode = cellCodeFactory(theName, theAPIType, code);
+
+ if (cellCode)
+ {
+ mCellCodes.insert( std::map<std::string, TKCellCode*>::value_type(theName, cellCode));
+ }
+ return cellCode;
+}
+
bool TKContainer::setDataStore(std::string storagePath)
{
mStorage = new DNStorage(storagePath.c_str());
#include <map>
class TKCell;
+class TKReceptor;
+class TKAxon;
+class TKAxonTerminal;
class TKCellCode;
class DNStorage;
class TKContainer
{
public:
+ static const std::string CELLTYPE_JSBASIC;
+ static const std::string CELLTYPE_OUT;
+ static const std::string CELLTYPE_IN;
+ static const std::string CELLTYPE_BASICSTORAGE;
+
static TKContainer* createContainer();
+ virtual void init();
virtual ~TKContainer();
- const TKCellMap* getCells() { return &mCells; }
+ const TKCellMap* getCells() { return &mCells; }
const TKCellCodeMap* getCellCodes() { return &mCellCodes; }
void setContentPath(std::string contentPath) { mContentPath = contentPath; }
bool doTick(float time);
bool doDestroy();
- virtual TKCell* createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript) = 0;
- virtual TKCell* createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript) = 0;
- virtual TKCellCode* createCellCode(std::string theName, std::string theAPIType, std::string code) = 0;
-
+ TKCell* addCell(std::string theLocation, std::string theName, std::string type, std::string customScript);
+ TKCell* addCell(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript);
+ TKCellCode* addCellCode(std::string theName, std::string theAPIType, std::string code);
+
virtual void setValue(std::string key, float value) = 0;
inline virtual float getValue(std::string key) = 0;
+ virtual TKCell* cellFactory(std::string location, std::string name, std::string type, bool canInterface = true) = 0;
+ virtual TKAxon* axonFactory(TKCell *theOwner) = 0;
+ virtual TKReceptor* receptorFactory(TKCell *theOwner) = 0;
+ virtual TKAxonTerminal* axonTerminalFactory(TKAxon *theOwner) = 0;
+ virtual TKCellCode* cellCodeFactory(std::string name, std::string cellapi, std::string code) = 0;
+
protected:
- TKContainer() : mStorage(NULL){}
+ TKContainer();
TKCellMap mCells;
TKCellMap mInterfaceCells;
TKCellCodeMap mCellCodes;
std::string mContentPath;
DNStorage *mStorage;
+ TKCellCode *mEmptyCellClass;
private:
TKLock mLock;