Skip to content

Commit

Permalink
Replace rulestring by integer input
Browse files Browse the repository at this point in the history
  • Loading branch information
Sedictious committed May 2, 2019
1 parent fc42c68 commit bc89e0e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 56 deletions.
5 changes: 4 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"author": "Eleftheria Chatziargyriou",
"description": "This implements life-like cellular automata.",
"supportedGraphs": ["squareGrid"],
"pluginAttributesScope": [ {"rules": "string"} ],
"pluginAttributesScope": [
{"birth": "int[-1,max]"},
{"survival": "int[-1,max]"}
],
"nodeAttributesScope": [ {"live": "bool"} ]
}
82 changes: 29 additions & 53 deletions plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,41 @@

namespace evoplex {

QBitArray LifeLike::parseCmd(const QString &cmd)
QBitArray LifeLike::parseCmd(const QString &cmd1, const QString &cmd2)
{
if (cmd.isEmpty())
{
qWarning() << "The command cannot be empty";
return QBitArray();
}

QString _cmd1 = cmd1;
QString _cmd2 = cmd2;
QBitArray rules(18);
QStringList _cmds;
QString _cmd1, _cmd2;

bool isInt1 = false;
bool isInt2 = false;

if (cmd.indexOf("/") == -1)
{
qWarning() << "Commands must be of the form B{number of neighbors for cell birth} / S{number of neighbors for cell survival";
return QBitArray();
}

_cmds = cmd.split("/");

if (!_cmds.at(0).startsWith("B") || !_cmds.at(1).startsWith("S"))
{
qWarning() << "Commands must be of the form B{number of neighbors for cell birth} / S{number of neighbors for cell survival";
return QBitArray();
}

_cmd1 = _cmds.at(0);
_cmd1.remove(0, 1);
_cmd2 = _cmds.at(1);
_cmd2.remove(0, 1);

int intRule1 = _cmd1.toInt(&isInt1);
int intRule2 = _cmd2.toInt(&isInt2);

// check if rules are integers or empty
if (!(isInt1 || _cmd1.isEmpty()) || !(isInt2 || _cmd2.isEmpty()))
{
qWarning() << "Unable to parse command. Make sure you give a valid integer.";
return QBitArray();
}
int intRule1 = _cmd1.toInt();
int intRule2 = _cmd2.toInt();

// convert rule to a bitstream
for (const auto& c : _cmd1)

// Check if either of the commands should be empty (input of -1)
// before iterating.
if (intRule1 != -1)
{
if (rules[c.digitValue() + 0x0A]){
qWarning() << "Rulestring should contain only unique integers.";
return QBitArray();
for (const auto& c : _cmd1)
{
if (rules[c.digitValue() + 0x0A]){
qWarning() << "Rulestring should contain only unique integers.";
return QBitArray();
}
rules.setBit(c.digitValue() + 0x0A);
}
rules.setBit(c.digitValue() + 0x0A);
}
for (const auto& c: _cmd2)

if (intRule2 != -1)
{
if (rules[c.digitValue()]){
qWarning() << "Rulestring should contain only unique integers.";
return QBitArray();
for (const auto& c: _cmd2)
{
if (rules[c.digitValue()]){
qWarning() << "Rulestring should contain only unique integers.";
return QBitArray();
}
rules.setBit(c.digitValue());
}
rules.setBit(c.digitValue());
}
return rules;
}
Expand All @@ -80,15 +55,16 @@ bool LifeLike::init()
m_liveAttrId = node(0).attrs().indexOf("live");

// parses the ruleset
if (attrExists("rules")){
m_ruleset = attr("rules").toQString();
if (attrExists("birth") && attrExists("survival")){
m_birth = attr("birth").toQString();
m_survival = attr("survival").toQString();
}
else{
qWarning() << "missing attributes.";
return false;
}

m_rulesetLst = parseCmd(m_ruleset);
m_rulesetLst = parseCmd(m_birth, m_survival);

if (m_rulesetLst.isNull())
{
Expand Down
5 changes: 3 additions & 2 deletions plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class LifeLike: public AbstractModel
public:
bool init() override;
bool algorithmStep() override;
QBitArray parseCmd(const QString &cmd);
QBitArray parseCmd(const QString &cmd1, const QString &cmd2);

private:
int m_liveAttrId; // the id of the 'live' node's attribute
QString m_ruleset; // the model's ruleset (B/S format)
QString m_birth; // neighbours needed for cell birth
QString m_survival; //neighbours needed for cell survival
QBitArray m_rulesetLst; // the bitstream representing the input ruleset
};
} // evoplex
Expand Down

0 comments on commit bc89e0e

Please sign in to comment.