Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

164
LINES

< > BotCompany Repo | #1030723 // AGI Pattern Spike 1 v1 [OK]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1059K of libraries. Click here for Pure Java version (20836L/113K).

!7

extend BEAObject {
  bool canReactWith(BEAObject o) {
    ret isInstanceOfAny(o, (Cl) reactableWithClasses());
  }
  
  Cl<Class<? extends BEAObject>> reactableWithClasses() {
    ret singleArgumentMethodTypesSubclassing(this, "reactWith", BEAObject);
  }
}

beaConcept BInput {
  S text;
  
  toString { ret "[\*id*/] Input " + quote(text); }
}

beaConcept BOutput {
  S text;
  
  toString { ret "[\*id*/] Output " + quote(text); }
}

beaConcept BPattern {
  S text;
  
  toString { ret "[\*id*/] " + shortDynName(this) + " " + quote(text); }
  
  void reactWith(BInput input) {}
  
  @Override Map<S, FieldMigration> _fieldMigrations() {
    ret litmap(pattern := new FieldMigration("text"));
  }
}

beaConcept BStarPattern > BPattern {
  void reactWith(BInput input) {
    LS mapping = matchesToStringList(flexMatchIC_first(text, input.text));
    if (mapping == null) ret;
    uniqPeer BEAObject(this,
      type := "match",
      +input,
      pattern := this,
      +mapping);
  }
}

beaConcept BAngleVarPattern > BPattern {
  void reactWith(BInput input) {
    SS mapping = flexMatchAngleBracketVarsIC_first(text, input.text);
    if (mapping == null) ret;
    uniqPeer BEAObject(this,
      type := "match",
      +input,
      pattern := this,
      +mapping);
  }
}

beaConcept BMatch {
  new Ref<BPattern> pattern;
  SS mapping;
}

beaConcept BMatchToScenario {
  void reactWith(BMatch match) {
    uniqPeer(BEAObject, this,
      /*fromReaction_1 := this,
      fromReaction_2 := match,*/
      fromReaction := ll(this, match),
      type := "scenario",
      text := mapToLines(match.mapping,
          (a, b) -> b + " is a " + a));
  }
}

beaConcept BScenarioToOutput {
  bool canReactWith(BEAObject o) {
    ret o.typeIs("scenario");
  }
  
  void reactWith(BEAObject scenario) {
    uniqPeer(BOutput, this,
      fromReaction := ll(this, scenario),
      type := "Output",
      text := getString text(scenario));
  }
}

cmodule2 > DynCRUD_v2<BEAObject> {
  transient Map<S, Class<? extends BEAObject>> beaClasses = calcBEAClasses();
  
  start {
    print(+beaClasses);
    crud.humanizeFieldNames = false;
    crud.showBackRefs = true;
    crud.specialFieldsForItem = a -> {
      MapSO map = litorderedmap(crud.hID, str(a.id));
      S j = crud.javaClassDescForItem(a);
      mapPut(map, "Class/Type", joinUniqueNemptiesCI("/", j, a.type()));
      ret map;
    };

    thread {
      uniq BMatchToScenario();
      uniq BScenarioToOutput();
      uniq BInput(text := "UBports community delivers 'second-largest release of Ubuntu Touch ever'");
      //uniq BPattern(text := "* delivers *");
      uniq BAngleVarPattern(text := "<company> delivers <product>");
    }
  }
  
  afterVisualize {
    tablePopupMenuFirst(table(), (menu, row) -> {
      BEAObject o = getItem(row);
      print("Item: " + o);
      if (o == null) ret;
      
      Class<? extends BEAObject> targetClass = defaultCustomClass(o);
      print(+targetClass);
      if (targetClass != null && targetClass != _getClass(o)) pcall-short {
        migrateConceptToClass(targetClass, o);
        addMenuItem(menu, "Convert to " + shortName(targetClass), r {
          replaceConceptAndUpdateRefs(o, migrateConceptToClass(targetClass, o));
        });
      }
      
      pcall {
        L<BEAObject> partners = filter(list(BEAObject),
          partner -> o.canReactWith(partner));
        if (nempty(partners))
          addScrollingSubMenu(menu, "React with", menu2 -> {
            for (BEAObject p : partners)
              addMenuItem(menu2, str(p), rThread {
                call(o, 'reactWith, p);
              });
          });
      }
    });
  }
  
  // e.g. "Input" => BInput
  Map<S, Class<? extends BEAObject>> calcBEAClasses() {
    ret ciMapToKeys(c -> dropPrefix("B", shortClassName(c)),
      listMinusItem(BEAObject, myNonAbstractClassesImplementing(BEAObject)));
  }
  
  Class<? extends BEAObject> defaultCustomClass(BEAObject o) {
    ret beaClasses.get(o.type());
  }
  
  <A extends BEAObject> A migrateConceptToClass(Class<A> c, BEAObject in) {
    A out = unlisted(c);
    for (S field : conceptFields(in)) try {
      continue if eq(field, "type");
      cset(out, field, cget(in, field));
      // TODO: check Ref types
    } catch e {
      fail("Can't convert field " + field, e);
    }
    ret out;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030723
Snippet name: AGI Pattern Spike 1 v1 [OK]
Eternal ID of this version: #1030723/67
Text MD5: e9e2a01317213c29a2f20427b8be8a6d
Transpilation MD5: 7e086129337e4e8821d5fe628d26d218
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-03-19 14:38:17
Source code size: 4492 bytes / 164 lines
Pitched / IR pitched: No / No
Views / Downloads: 266 / 822
Version history: 66 change(s)
Referenced in: [show references]