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

64
LINES

< > BotCompany Repo | #1033864 // Meta mix-in

JavaX fragment (include)

1  
// Meta - a "minimal" approach to adding meta-level to Java objects
2  
// (implementing the interface IMeta)
3  
4  
// We allocate one extra field for each Java object to make it
5  
// reasoning-compatible (reasoning-compatible = extensible with
6  
// fields of any name at runtime).
7  
//
8  
// We couldn't go for 0 extra fields (meta values must be linked
9  
// directly from the object) and there are no half fields in
10  
// Java... so there you go.
11  
//
12  
// Also, if you don't use any meta data, you are probably not
13  
// reasoning about anything. The point of reasoning in JavaX is
14  
// to attach information to objects directly used in the program.
15  
16  
// Possible information contained in the meta field:
17  
//   Origin, destination, security level, sender, cost center,
18  
//   purpose, list of reifications, ...
19  
20  
// So here it is. THE FIELD YOU HAVE BEEN WAITING FOR!
21  
22  
// [We also have IMeta to retrofit foreign classes (rare but
23  
// probably useful).]
24  
25  
//////////////////////
26  
// The "meta" field //
27  
//////////////////////
28  
29  
// Generic meta value of any kind, but the typical case is it's a
30  
// Map with extra field values for the object etc.
31  
// "meta" is volatile to avoid synchronization; but you can also synchronize on
32  
// _tempMetaMutex() which is usually the object itself. Collections
33  
// and maps are exempt from using the collections's monitor as the meta
34  
// mutex because their monitor tends to be held for long operations
35  
// (e.g. cloneList). For those we use a substantially more complex
36  
// algorithm using a weakMap. Probably overkill. I may reconsider.
37  
38  
volatile O meta;
39  
40  
// The meta field is not transient, thus by default it will be
41  
// persisted like anything else unless you customize your object
42  
// to suppress or modulate this.
43  
44  
// ...and the interface methods
45  
46  
public void _setMeta(O meta) { this.meta = meta; }
47  
public O _getMeta() { ret meta; }
48  
49  
// MOST functions are implemented in IMeta (default implementations)
50  
51  
// Scaffolding convenience functions
52  
53  
bool scaffoldingEnabled aka scaffolding aka scaffolded() { ret main scaffoldingEnabled(this); }
54  
bool scaffoldingEnabled(O o) { ret main scaffoldingEnabled(o); }
55  
56  
// Implementing setMetaToString
57  
58  
S toString_base() { ret super.toString(); }
59  
toString {
60  
  O o = metaGet toString(this);
61  
  if (o cast S) ret o;
62  
  if (o cast IF1) ret str(o.get(this));
63  
  ret toString_base();
64  
}

Author comment

Began life as a copy of #1031838

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, ekrmjmnbrukm, elmgxqgtpvxh, gjtlkbvenryc, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, wnsclhtenguj

No comments. add comment

Snippet ID: #1033864
Snippet name: Meta mix-in
Eternal ID of this version: #1033864/7
Text MD5: b872ccb8917ed705c894db17febfce0d
Author: stefan
Category: javax / reasoning
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-07 04:31:22
Source code size: 2368 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 121 / 8114
Version history: 6 change(s)
Referenced in: [show references]