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

104
LINES

< > BotCompany Repo | #1029845 // ConceptsRefChecker - detect + fix broken references

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (16250L/105K).

1  
sclass ConceptsRefChecker {
2  
  Concepts cc;
3  
  new L<Err> errors;
4  
  bool veryVerbose;
5  
  
6  
  sclass Err {}
7  
  srecord ErrNoBackRef(Concept.Ref ref, Concept dest) > Err {}
8  
  srecord ErrSuperfluousBackRef(Concept.Ref ref, Concept dest) > Err {}
9  
  
10  
  srecord ErrDuplicateRef(Concept c) > Err {}
11  
  srecord ErrNullRef(Concept c) > Err {}
12  
  
13  
  srecord ErrMissingRef(Concept c, Concept.Ref ref) > Err {}
14  
  srecord ErrSuperfluousRef(Concept c, Concept.Ref ref) > Err {}
15  
  
16  
  // a reference from a concept that is not in the concepts list
17  
  srecord ErrDanglingSource(Concept.Ref ref, Concept dest) > Err {}
18  
  
19  
  // a reference to a concept that is not in the concepts list
20  
  srecord ErrDanglingDestination(Concept.Ref ref, Concept dest) > Err {}
21  
  
22  
  *() {}
23  
  *(Concepts *cc) {}
24  
  
25  
  L<Err> run() {
26  
    errors.clear();
27  
    if (cc == null) ret errors;
28  
    Cl<Concept> concepts = cc.allConcepts();
29  
    if (veryVerbose)
30  
      print("Checking " + nConcepts(concepts));
31  
    for (Concept c : concepts) try {
32  
      O badElement = firstElementNotSubclassing(c.backRefs, Concept.Ref.class);
33  
      if (badElement != null) continue with print("Bad element in backRefs of " + c + ": " + badElement);
34  
      
35  
      for (Concept.Ref ref : cloneList(c.backRefs)) {
36  
        if (ref! != c)
37  
          errors.add(new ErrSuperfluousBackRef(ref, c));
38  
        if (ref.concept()._concepts != cc)
39  
          errors.add(new ErrDanglingSource(ref, c));
40  
      }
41  
          
42  
      Cl<Concept.Ref> refs = c._refs();
43  
      
44  
      badElement = firstElementNotSubclassing(refs, Concept.Ref.class);
45  
      if (badElement != null) continue with print("Bad element in backRefs of " + c + ": " + badElement);
46  
      
47  
      if (!allUnique(refs))
48  
        errors.add(new ErrDuplicateRef(c));
49  
      if (contains(refs, null))
50  
        errors.add(new ErrNullRef(c));
51  
        
52  
      refs = nonNulls(refs);
53  
      Cl<Concept.Ref> actualRefs = scanConceptForRefs(c);
54  
      for (Concept.Ref ref : listMinusSet(refs, actualRefs))
55  
        errors.add(new ErrSuperfluousRef(c, ref));
56  
      for (Concept.Ref ref : listMinusSet(actualRefs, refs))
57  
        errors.add(new ErrMissingRef(c, ref));
58  
        
59  
      for (Concept.Ref ref : refs)
60  
        if (ref.has()) {
61  
          if (!contains(ref->backRefs, ref))
62  
            errors.add(new ErrNoBackRef(ref, c));
63  
          if (ref->_concepts != cc)
64  
            errors.add(new ErrDanglingDestination(ref, c));
65  
        }
66  
    } catch print e {
67  
      print("Error processing concept " + c);
68  
    }
69  
    ret errors;
70  
  }
71  
  
72  
  L<Err> errors() { ret errors; }
73  
  
74  
  S fixAll() {
75  
    new LS out;
76  
    // start from end because list removes should be faster like this
77  
    for (Err err : reversed(cloneAndClear(errors))) pcall {
78  
      out.add("Processing " + err);
79  
      if (err cast ErrNoBackRef) {
80  
        err.ref.index();
81  
        out.add("  reference added");
82  
      } else if (err cast ErrSuperfluousRef) {
83  
        err.ref.unindexAndDrop();
84  
        out.add("  reference dropped");
85  
      } else if (err cast ErrDanglingSource) {
86  
        err.ref->_removeBackRef(err.ref);
87  
        out.add("  reference removed from destination");
88  
      } else if (err cast ErrDanglingDestination) {
89  
        err.ref->_removeBackRef(err.ref);
90  
        out.add("  reference removed from destination");
91  
      } else if (err cast ErrMissingRef) {
92  
        err.ref.registerRef();
93  
        out.add("  reference registered");
94  
      } else if (err cast ErrSuperfluousBackRef) {
95  
        err.dest._removeBackRef(err.ref);
96  
        out.add("  backreference dropped");
97  
      } else
98  
        out.add("  TODO - fix not implemented");
99  
    }
100  
    ret lines(out);
101  
  }
102  
  
103  
  S runAndFixAll() { run(); ret fixAll(); }
104  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1029845
Snippet name: ConceptsRefChecker - detect + fix broken references
Eternal ID of this version: #1029845/29
Text MD5: 3d3c9c60c6febc0bdc33459ea2837e64
Transpilation MD5: 12f401791ec7643c24e390b11eee0691
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-27 06:49:30
Source code size: 3735 bytes / 104 lines
Pitched / IR pitched: No / No
Views / Downloads: 319 / 672
Version history: 28 change(s)
Referenced in: [show references]