From 74b087b38fbd4dea25e8a17e9028bc24c1d57baa Mon Sep 17 00:00:00 2001 From: dyrpsf Date: Mon, 11 May 2026 12:59:28 +0530 Subject: [PATCH 1/3] refactor: extract Antimony keywords into AntimonyConstants interface --- .../sbml/jsbml/util/AntimonyConstants.java | 27 +++++++++++++++ .../sbml/jsbml/util/AntimonySerializer.java | 34 +++++++++---------- 2 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 core/src/org/sbml/jsbml/util/AntimonyConstants.java diff --git a/core/src/org/sbml/jsbml/util/AntimonyConstants.java b/core/src/org/sbml/jsbml/util/AntimonyConstants.java new file mode 100644 index 000000000..58c6d57cc --- /dev/null +++ b/core/src/org/sbml/jsbml/util/AntimonyConstants.java @@ -0,0 +1,27 @@ +package org.sbml.jsbml.util; + +/** + * Interface defining constant keywords and operators used in the Antimony scripting language. + * Implementing this interface allows direct inheritance of all constants. + * + * @author Deepak Yadav + */ +public interface AntimonyConstants { + public static final String MODEL = "model "; + public static final String END = "end\n"; + public static final String COMPARTMENT = "compartment "; + public static final String SPECIES = "species "; + public static final String SUBSTANCE_ONLY = "substanceOnly species "; + public static final String IN = " in "; + public static final String IRREVERSIBLE = " => "; + public static final String REVERSIBLE = " -> "; + public static final String ASSIGNMENT = " := "; + public static final String RATE = "' = "; + public static final String ALGEBRAIC = "0 = "; + public static final String AT = "at "; + public static final String AFTER = " after "; + public static final String DELAY = ", delay = "; + public static final String PRIORITY = ", priority = "; + public static final String T0_FALSE = ", t0 = false"; + public static final String PERSISTENT_FALSE = ", persistent = false"; +} \ No newline at end of file diff --git a/core/src/org/sbml/jsbml/util/AntimonySerializer.java b/core/src/org/sbml/jsbml/util/AntimonySerializer.java index 7748965d6..8de82bc72 100644 --- a/core/src/org/sbml/jsbml/util/AntimonySerializer.java +++ b/core/src/org/sbml/jsbml/util/AntimonySerializer.java @@ -22,7 +22,7 @@ * * @author Deepak Yadav */ -public class AntimonySerializer { +public class AntimonySerializer implements AntimonyConstants { /** * Generic router for UI plugins (e.g., Eclipse, IntelliJ, VSCode). @@ -62,7 +62,7 @@ public static String toAntimony(Model model) { StringBuilder ant = new StringBuilder(); String modelName = model.isSetName() ? model.getName() : model.getId(); - ant.append("model ").append(modelName).append("()\n\n"); + ant.append(MODEL).append(modelName).append("()\n\n"); ant.append(" // Compartments\n"); for (Compartment c : model.getListOfCompartments()) { @@ -94,7 +94,7 @@ public static String toAntimony(Model model) { } ant.append("\n"); - ant.append("end\n"); + ant.append(END); return ant.toString(); } @@ -105,7 +105,7 @@ public static String toAntimony(Model model) { public static String toAntimony(Compartment c) { if (c == null) return ""; StringBuilder ant = new StringBuilder(); - ant.append("compartment ").append(c.getId()); + ant.append(COMPARTMENT).append(c.getId()); if (c.isSetSize()) { ant.append(" = ").append(c.getSize()); } @@ -126,9 +126,9 @@ public static String toAntimony(Species s) { // 1. Handle Substance Units if (hOSU) { - ant.append("substanceOnly species "); + ant.append(SUBSTANCE_ONLY); } else { - ant.append("species "); + ant.append(SPECIES); } // 2. Handle Boundary Condition @@ -140,7 +140,7 @@ public static String toAntimony(Species s) { // Compartment assignment if (s.isSetCompartment()) { - ant.append(" in ").append(s.getCompartment()); + ant.append(IN).append(s.getCompartment()); } // 3. Handle Initial Values based on Concentration vs Amount assumptions @@ -194,9 +194,9 @@ public static String toAntimony(Reaction r) { // 3. Reversibility (Antimony uses -> for reversible, => for irreversible) if (r.isSetReversible() && !r.getReversible()) { - ant.append(" => "); + ant.append(IRREVERSIBLE); } else { - ant.append(" -> "); + ant.append(REVERSIBLE); } // 4. Products @@ -236,11 +236,11 @@ public static String toAntimony(Rule r) { String math = ASTNode.formulaToString(r.getMath()); if (r instanceof AssignmentRule) { - return ((AssignmentRule) r).getVariable() + " := " + math + ";"; + return ((AssignmentRule) r).getVariable() + ASSIGNMENT + math + ";"; } else if (r instanceof RateRule) { - return ((RateRule) r).getVariable() + "' = " + math + ";"; + return ((RateRule) r).getVariable() + RATE + math + ";"; } else if (r instanceof AlgebraicRule) { - return "0 = " + math + ";"; + return ALGEBRAIC + math + ";"; } return "// Unsupported Rule type."; @@ -258,13 +258,13 @@ public static String toAntimony(Event e) { ant.append(e.getId()).append(": "); } - ant.append("at "); + ant.append(AT); boolean hasTrigger = e.isSetTrigger() && e.getTrigger().isSetMath(); boolean hasDelay = e.isSetDelay() && e.getDelay().isSetMath(); if (hasDelay && hasTrigger) { ant.append(ASTNode.formulaToString(e.getDelay().getMath())); - ant.append(" after "); + ant.append(AFTER); ant.append(ASTNode.formulaToString(e.getTrigger().getMath())); } else if (hasTrigger) { ant.append(ASTNode.formulaToString(e.getTrigger().getMath())); @@ -272,15 +272,15 @@ public static String toAntimony(Event e) { // Advanced Event Options if (e.isSetPriority() && e.getPriority().isSetMath()) { - ant.append(", priority = ").append(ASTNode.formulaToString(e.getPriority().getMath())); + ant.append(PRIORITY).append(ASTNode.formulaToString(e.getPriority().getMath())); } if (e.isSetTrigger()) { org.sbml.jsbml.Trigger t = e.getTrigger(); if (t.isSetInitialValue() && !t.getInitialValue()) { - ant.append(", t0 = false"); + ant.append(T0_FALSE); } if (t.isSetPersistent() && !t.getPersistent()) { - ant.append(", persistent = false"); + ant.append(PERSISTENT_FALSE); } } ant.append(": "); From e92ba77c16100b660578078070ddd502a9cee0de Mon Sep 17 00:00:00 2001 From: dyrpsf Date: Thu, 14 May 2026 22:20:25 +0530 Subject: [PATCH 2/3] test: add verification for AntimonyConstants interface --- .../jsbml/util/AntimonyConstantsTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java diff --git a/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java b/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java new file mode 100644 index 000000000..419c8eeec --- /dev/null +++ b/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java @@ -0,0 +1,25 @@ +package org.sbml.jsbml.util; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class AntimonyConstantsTest { + + @Test + public void testCoreConstants() { + // Verify that the core structural constants haven't been accidentally altered + assertEquals("model ", AntimonyConstants.MODEL); + assertEquals("end\n", AntimonyConstants.END); + assertEquals("species ", AntimonyConstants.SPECIES); + assertEquals("compartment ", AntimonyConstants.COMPARTMENT); + } + + @Test + public void testOperatorConstants() { + // Verify reaction and assignment operators + assertEquals(" => ", AntimonyConstants.IRREVERSIBLE); + assertEquals(" -> ", AntimonyConstants.REVERSIBLE); + assertEquals(" := ", AntimonyConstants.ASSIGNMENT); + assertEquals("' = ", AntimonyConstants.RATE); + } +} \ No newline at end of file From 17313ac1ada507e1c057d1611e6652bfe0568139 Mon Sep 17 00:00:00 2001 From: dyrpsf Date: Fri, 15 May 2026 10:50:32 +0530 Subject: [PATCH 3/3] docs: add author tag and javadoc to AntimonyConstantsTest --- core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java b/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java index 419c8eeec..6e0d491d5 100644 --- a/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java +++ b/core/test/org/sbml/jsbml/util/AntimonyConstantsTest.java @@ -3,6 +3,14 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +/** + * Tests for the {@link AntimonyConstants} interface. + * Ensures that core Antimony keywords and operators remain safely unchanged + * to prevent parser regressions. + * + * @author Deepak Yadav + */ + public class AntimonyConstantsTest { @Test