ISymbolicCAModelInformation info =
(ISymbolicCAModelInformation) data.getAsDataStructure();
if (info instanceof SymbolicCAModelInformation
&& info.getModelSource() != null) {
return new CARulesAntlrDocument(info.getModelSource());
}
// if not try to create model source from model information
if (info.getModelComment() != null) {
builder.append("/**\n");
builder.append(info.getModelComment() + "\n");
builder.append("\n*/\n");
}
builder.append("@caversion 1;\n");
if (info.isWolfram()) {
builder.append("wolframrule " + info.getWolframRule() + ";\n");
} else {
builder.append("dimensions " + info.getDimensions() + ";\n");
if (info.getNeighborhood().getComment() != null) {
builder.append("/**\n");
builder.append(info.getNeighborhood().getComment());
builder.append("\n*/\n");
}
if (info.getNeighborhood() instanceof MooreNeighborhood) {
builder.append("neighborhood moore;\n");
} else if (info.getNeighborhood() instanceof NeumannNeighborhood) {
builder.append("neighborhood neumann;\n");
} else {
builder.append("neighborhood free {\n");
INeighborhood n = info.getNeighborhood();
for (int i = 0; i < n.getCellCount(); i++) {
int[] cell = n.getCell(i);
builder.append(" [");
for (int d = 0; d < n.getDimensions(); d++) {
if (d > 0) {
builder.append(", ");
}
builder.append(cell[d]);
}
builder.append("]\n");
}
builder.append(" };\n");
}
String st = null;
for (String s : info.getStates()) {
if (st != null) {
st += ", ";
} else {
st = "";
}
st += s;
}
builder.append("state " + st + ";\n\n");
for (CARule rule : info.getRules()) {
String c = conditionToString(rule.getCurrentCondition(), info);
String ruleStr = "";
if (c.length() > 0) {
ruleStr += "{";
ruleStr += c;
ruleStr += "}";
}
if (Double.compare(rule.getProbability(), 1.0) != 0) {
NumberFormat nFormat = NumberFormat.getNumberInstance();
nFormat.setGroupingUsed(false);
nFormat.setMaximumFractionDigits(Integer.MAX_VALUE);
ruleStr +=
"[" + nFormat.format(rule.getProbability()).replace(',', '.')
+ "]";
}
ruleStr += " : ";
ruleStr += conditionToString(rule.getPreCondition(), info);
ruleStr +=
" -> " + info.getStates().get(rule.getDestinationState()) + ";\n";
if (rule.getComment() != null) {
builder.append("/**\n");
builder.append(rule.getComment());
builder.append("\n*/\n");
}
builder.append("rule " + ruleStr + "\n");
}
}
return new CARulesAntlrDocument(builder.toString());
}