!7

concept AIConcept {
  S globalID = aGlobalID();
  S name;
  S comment;
}

concept C {
  S globalID = aGlobalID();
  S name;
  S comment;
  new Ref<C> type;
  new LinkedHashMap<C, C> slots;
  
  public S toString() {
    ret name;
  }
}

static C findCOpt(S name) {
  ret findConcept(C, +name);
}

static C findC(S name) {
  C c = findCOpt(name);
  assertNotNull(name, c);
  ret c;
}

static C intC(int i) {
  C c = findCOpt(str(i));
  if (c == null) {
    c = instance("Integer");
    cset(c, name := str(i));
  }
  ret c;
}

static C instance(S name) {
  ret cnew(C, type := findC(name));
}

static C getC(C parent, C slot) {
  ret parent == null ? null : parent.slots.get(slot);
}

static C getC(C parent, S slot) {
  ret parent == null ? null : parent.slots.get(findC(slot));
}

static void setC(C parent, C slot, C value) {
  parent.slots.put(slot, value);
}

static void setC(C parent, S slot, C value) {
  setC(parent, findC(slot), value);
}

p {
  conceptsAndBot();
  Concepts in = new Concepts(#1006463).load(); // load AIConcept's
  
  L<AIConcept> imported = list(in, AIConcept);
  print("Have AI concepts: " + struct(collect(imported, "name")));
  for (AIConcept c : imported) {
    C cc = findConcept(C, globalID := c.globalID);
    if (cc == null) {
      print("Importing " + c.name);
      uniq(C, globalID := c.globalID, name := c.name, comment := c.comment);
    }
  }
  
  C img = findConcept(C, type := findC("RGB Image"));
  if (img == null) {
    print("Making image.");
    img = instance("RGB Image");
  }
  C width = getC(img, "Width");
  print("Width: " + width);
  if (width == null)
    setC(img, "Width", intC(200));
  C height = getC(img, "Height");
  print("Height: " + height);
  if (height == null)
    setC(img, "Height", intC(100));
}