Home /Modules/advantage/DiPurchaseOnion.sleela

DiPurchaseOnion.sleela

lib/advantage/learner-vs-professional/series/06-purchase-onion/DiPurchaseOnion.sleela · 134 line(s) · sleela

<< lib/advantage/learner-vs-professional/series/06-purchase-onion/DiPurchaseOnion.sleela
   SLeeLa courseware.
   Definition: Device Instructor lesson taking registry to onion: the student totals registered quotes by hand, the Purchase Master runs the Sequitur onion, and the instructor compares. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Lesson 6. Quotes are registered as nouns carried by suppliers, with integer >
< cents as values. The student adds them up by hand. The Purchase Master puts >
< the same figures through the advantage onion (dividend, additions, >
< subtraction). The instructor accepts only when both pick the same supplier >
< at the same total. On a tie the norm is supplier A. >
< A quote not in the field is never guessed at. >
class DiPurchaseOnion {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs. A worker-class
     lesson carries the baseline identity record. >>
  String metaVersion = "1.0.0";
  int metaId = 39106;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-11";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-11";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 5;
  < 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; }

  SLNounRegistry reg = new SLNounRegistry();

  void loadQuotes(boolean discountSeen) {
    reg = new SLNounRegistry();
    reg.declareField("purchase of one item");
    reg.register("supplier A", "purchase of one item");
    reg.registerValue("A quote", "supplier A", 12000);
    reg.registerValue("A fee", "supplier A", 450);
    if (discountSeen) { reg.registerValue("A discount", "supplier A", -800); }
    reg.register("supplier B", "purchase of one item");
    reg.registerValue("B quote", "supplier B", 11500);
    reg.registerValue("B fee", "supplier B", 900);
  }

  < The student: a plain sum of every registered value under the supplier. >
  int studentTotal(String supplier) { return reg.totalUnder(supplier); }

  < The Purchase Master: the same figures through the advantage onion. >
  int masterTotal(String label, int quote, int fee, int discount) {
    SLAdvantageDeveloperModel dev = new SLAdvantageDeveloperModel();
    dev.init(label, "purchase", quote);
    dev.setAdditions(fee);
    dev.setSubtraction(discount);
    dev.runPass();
    return dev.getDividend().getCurrent();
  }

  < Tie norm: supplier A is chosen when the two totals are equal. >
  String pick(int a, int b) {
    if (a <= b) { return "supplier A"; }
    return "supplier B";
  }

  void main() {
    print("== Lesson 6: purchase onion ==");

    print("-- master (onion) from the true quotes --");
    int mA = masterTotal("supplier A", 12000, 450, -800);
    int mB = masterTotal("supplier B", 11500, 900, 0);
    print("  supplier A total = " + mA + " cents");
    print("  supplier B total = " + mB + " cents");
    String mPick = pick(mA, mB);
    print("  master picks " + mPick);

    print("-- student, first pass: the discount entry was missed --");
    loadQuotes(false);
    int sA = studentTotal("supplier A");
    int sB = studentTotal("supplier B");
    print("  supplier A total = " + sA + " cents");
    print("  supplier B total = " + sB + " cents");
    String sPick = pick(sA, sB);
    print("  student picks " + sPick);
    if (sA == mA && sB == mB && sPick == mPick) { print("  verdict: AGREE, work accepted"); }
    else { print("  verdict: totals differ, supplier A is off by " + (sA - mA) + " cents -> re-observe supplier A"); }

    print("-- student, second pass: A discount observed and registered --");
    loadQuotes(true);
    sA = studentTotal("supplier A");
    sB = studentTotal("supplier B");
    print("  supplier A total = " + sA + " cents");
    print("  supplier B total = " + sB + " cents");
    sPick = pick(sA, sB);
    print("  student picks " + sPick);
    if (sA == mA && sB == mB && sPick == mPick) { print("  verdict: AGREE, work accepted"); }
    else { print("  verdict: totals differ"); }

    print("-- tie norm: equal totals go to supplier A --");
    int tieB = masterTotal("supplier B", 11500, 900, -750);
    print("  supplier A total = " + mA + " cents");
    print("  supplier B total = " + tieB + " cents");
    print("  master picks " + pick(mA, tieB));

    print("-- concerns: a quote outside the field --");
    print("  " + reg.chain("C quote"));
    print("  supplier C is not observed, so it is not compared");
  }
}