Home /Modules/philosophers/SLPhilosopherChain.sleela

SLPhilosopherChain.sleela

lib/philosophers/SLPhilosopherChain.sleela · 169 line(s) · sleela

<< lib/philosophers/SLPhilosopherChain.sleela
   SLeeLa Standard Library Definition.
   Definition: A Constructor's chain of growths, written with "::" between one
   growth and the next -- Constructor() :: Patch :: Constructor :: Aim :: Guide.
   >>
#sleela 2.5
material;
<< Family: philosophers. A chain is the leading Constructor() call followed by a
   bounded run of Guides, each separated from the next by a double colon "::".
   The chain owns the Guides in order and enforces the seating register's
   admitted capacity (base 21, band [-2,+6], so [19,27]). The "::" is a conform
   token: it joins the identities on either side under one Cause, and renders in
   the editor between two colored Guides. This class builds a chain by seating
   named Guides in order and can render the canonical chain text with "::" so an
   editor or catalog sees exactly what was declared. Char-level string parsing
   is deliberately avoided (it is a bridged capability); a chain is assembled
   from names, and renders itself with "+", so check and run stay pure. >>
class SLPhilosopherChain {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11567;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-24";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-24";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 6;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  < the growth separator: one growth :: the next >
  static final String GROWTH = " :: ";
  < the base of the chain is always a Constructor() call >
  static final String ROOT = "Constructor()";

  < the seating register governing how many Guides this chain may hold >
  SLPhilosopherSeating seating;
  < the Guides of this chain, in order (index 0 is the first after the root) >
  SLPhilosopherGuide[] guides;
  < how many Guides have been seated >
  int count;
  < 1 if a seat request was refused because the chain was at capacity >
  int refusedFlag;

  < Prepare an empty chain at the default (band 0 -> 21) capacity. >
  void create() {
    seating = new SLPhilosopherSeating();
    seating.create();
    < 27 = SLPhilosopherSeating.MAX_CAPACITY (BASE 21 + ADDITIONAL_MAX 6) >
    guides = new SLPhilosopherGuide[27];
    count = 0;
    refusedFlag = 0;
  }

  < Admit a signed band of additional seatings before building the chain. >
  < Returns the band actually admitted (clamped into [-2,+6]). >
  int admit(int band) {
    return seating.admit(band);
  }

  int capacity() { return seating.getCapacity(); }
  int size() { return count; }
  boolean refused() { return refusedFlag == 1; }

  < Seat one named Guide onto the given seating ordinal. An approving Guide >
  < carries favor (Gold); a growing Guide carries Green. Returns the Guide's >
  < index, or -1 when the chain is already at its admitted capacity. >
  int seat(String guideName, int seatOrdinal, boolean carriesFavor) {
    if (count >= seating.getCapacity()) {
      refusedFlag = 1;
      return -1;
    }
    SLPhilosopherGuide g = new SLPhilosopherGuide();
    g.create();
    g.sit(guideName, seatOrdinal, carriesFavor);
    guides[count] = g;
    int idx = count;
    count = count + 1;
    return idx;
  }

  SLPhilosopherGuide at(int i) {
    if (i < 0 || i >= count) { return null; }
    return guides[i];
  }

  < Broadcast one context/impart to every seated Guide; each matching Guide >
  < counts the match. Returns how many Guides matched this impart. >
  int broadcast(String impart) {
    int hits = 0;
    int i = 0;
    while (i < count) {
      SLPhilosopherGuide g = guides[i];
      if (g.listen(impart)) { hits = hits + 1; }
      i = i + 1;
    }
    return hits;
  }

  < Dispose every Guide after the object lives free. Idempotent per Guide. >
  void disposeAll() {
    int i = 0;
    while (i < count) {
      SLPhilosopherGuide g = guides[i];
      g.dispose();
      i = i + 1;
    }
  }

  < Render the canonical chain text, e.g. >
  < "Constructor() :: Patch :: Constructor :: Aim :: Guide". Built with "+" so >
  < no bridged char op is needed; this is the text an editor colors. >
  String render() {
    String s = ROOT;
    int i = 0;
    while (i < count) {
      SLPhilosopherGuide g = guides[i];
      s = s + GROWTH + g.getName();
      i = i + 1;
    }
    return s;
  }

  < A compact color manifest for the editor: the color impart of each Guide in >
  < order, joined with "::", e.g. "Gold/Yellow :: Green :: Gold/Yellow". >
  String colorManifest() {
    String s = "";
    int i = 0;
    while (i < count) {
      SLPhilosopherGuide g = guides[i];
      if (i == 0) { s = g.colorName(); }
      else { s = s + GROWTH + g.colorName(); }
      i = i + 1;
    }
    if (count == 0) { return "none"; }
    return s;
  }
}