예제 #1
0
 public BoundFrame primePattern(String name, ISet<DMIElem> xV) {
   NamedPattern p = find(name);
   if (p != null) {
     int fCnt = p.itsInitialFrame.itsPatternDefinition.itsFormalArgumentCount;
     int aCnt = xV.size();
     if (aCnt > fCnt) lang.errMsg("too many parameters to pattern: " + name);
     BoundFrame ans = new BoundFrame(p.itsInitialFrame, true);
     for (int i = 0; i < aCnt; i++) {
       DMIElem m = xV.get(i);
       if (m != null) ans.bindInputVariable(i, m);
     }
     return ans;
   }
   return null;
 }
예제 #2
0
  private ISet<DMIElem> traverse(
      ISet<DMIElem> inNodes,
      BoundFork inPatterns,
      IList<InferredRelationship> inferSet,
      ISet<DMIElem> suggested) {
    BoundFork currPatterns = inPatterns;

    ISet<DMIElem> currNodes = new XSetList<DMIElem>(XSetList.AsSet);
    ISet<DMIElem> currLinks = new XSetList<DMIElem>(XSetList.AsSet);

    for (DMIElem src : inNodes) if (src != null) setVisited(src);

    for (DMIElem src : inNodes) if (src != null) stepFromConcept(src, currNodes, currLinks);

    if (suggested != null) {
      for (DMIElem src : suggested) classifyConcept(src, currNodes, currLinks);
    }

    ISet<DMIElem> ansSet = new XSetList<DMIElem>(XSetList.AsSet);
    ISet<BoundFrame> nonRejectedCandidates = new XSetList<BoundFrame>(XSetList.AsSet);

    int iterCnt = 0;

    for (; ; ) {
      ++iterCnt;

      if ((Tracing.gTraceSet & Tracing.TraceFrame) != 0)
        Tracing.gTraceFile.println("---- Iteration " + iterCnt + " ----");

      // pattern match exhausted?  if so, there's nothing left to match.
      if (currPatterns.isEmpty()) break;

      IList<BoundFrame> lbf = currPatterns.getFrames();
      int cnt = lbf.size();

      if ((Tracing.gTraceSet & Tracing.TraceFrame) != 0) {
        Tracing.gTraceFile.println("frames for iteration:");
        for (int i = 0; i < cnt; i++) {
          BoundFrame fr = lbf.get(i);
          fr.traceOut();
        }
        Tracing.gTraceFile.println("==========");
      }

      // Pass 1 search new frames for successes or success roots
      for (int i = 0; i < cnt; i++) {
        BoundFrame fr = lbf.get(i);
        if (fr.succcessPathComplete()) {
          if (fr.couldBeRejected()) {
            if (fr.markAsSuccessRoot()) {

              if ((Tracing.gTraceSet & Tracing.TraceWave) != 0) {
                Tracing.gTraceFile.println("$$$ Adding frame root: ");
                fr.traceOut();
              }

              boolean b = nonRejectedCandidates.add(fr);

              if (!b && (Tracing.gTraceSet & Tracing.TraceWave) != 0)
                Tracing.gTraceFile.println(" ****** was already on root list");
            }
          } else if (fr.completed(ansSet, inferSet, null)) {

            if ((Tracing.gTraceSet & Tracing.TraceFrame) != 0) {
              Tracing.gTraceFile.print("$$$Taking result frame: ");
              int st = Tracing.gTraceSet;
              Tracing.gTraceSet |= Tracing.TraceFrameX;
              fr.traceOut();
              Tracing.gTraceFile.println();
              Tracing.gTraceSet = st;
            }

            if (fr.whenCompletedisFinished()) lbf.clear(i);
          }
        }
      }

      // Pass 2 search for rejections
      for (int i = 0; i < cnt; i++) {
        BoundFrame fr = lbf.get(i);
        if (fr != null) {
          if (fr.rejected()) {
            lbf.clear(i);
            BoundFrame successRoot = fr.getSuccessRoot();

            if (successRoot != null) nonRejectedCandidates.remove(successRoot);

            if ((Tracing.gTraceSet & Tracing.TraceWave) != 0) {
              Tracing.gTraceFile.println("$$$ Rejecting frame: ");
              fr.traceOut();
              if (successRoot != null) {
                Tracing.gTraceFile.println(" Which takes out root frame: ");
                successRoot.traceOut();
              }
            }
          }
        }
      }
      lbf.trim();

      if ((Tracing.gTraceSet & Tracing.TraceFrame) != 0) {
        Tracing.gTraceFile.println("frames after checking success:");
        for (int i = 0; i < lbf.size(); i++) {
          BoundFrame fr = lbf.get(i);
          fr.traceOut();
        }
        Tracing.gTraceFile.println("==========");
      }

      cnt = currNodes.size();
      for (int i = 0; i < cnt; i++) {
        DMIElem m = currNodes.get(i);
        stepFromNode(m, currLinks);
      }

      // input set exhausted?  if so, there's nothing left to traverse.
      if (currLinks.isEmpty()) break;

      BoundFork nextPatterns = new BoundFork();
      ISet<DMIElem> nextNodes = new XSetList<DMIElem>(XSetList.AsSet);
      ISet<DMIElem> nextLinks = new XSetList<DMIElem>(XSetList.AsSet);
      ISet<BoundFork[]> updatedSubpatterns = new XSetList<BoundFork[]>(XSetList.AsSet);

      cnt = currLinks.size();
      for (int i = 0; i < cnt; i++) {
        DMIElem m = currLinks.get(i);
        if ((Tracing.gTraceSet & Tracing.TraceFrame) != 0)
          Tracing.gTraceFile.println("visiting: " + m.itsIndex);

        // all patterns have to see all statements,
        //  even newly generated patterns from current generation
        BoundFork morePatterns = new BoundFork();
        accept(m, nextPatterns, morePatterns, nextNodes, nextLinks, updatedSubpatterns);
        nextPatterns.mergeIntoFork(morePatterns);

        // existing patterns have to see all statements,
        // and if they cannot advance, they'll be retired this generation
        accept(m, currPatterns, nextPatterns, nextNodes, nextLinks, updatedSubpatterns);
      }

      // $21$: carry frames over whose subforks are modified
      lbf = currPatterns.getFrames();
      cnt = lbf.size();
      for (int i = 0; i < cnt; i++) {
        BoundFrame fr = lbf.get(i);
        if (fr.usesSubForkIn(updatedSubpatterns)) {
          if ((Tracing.gTraceSet & Tracing.TraceFrame) != 0)
            Tracing.gTraceFile.println("carrying over frame #" + fr.itsIndex);
          nextPatterns.addFrame(fr);
        }
      }

      currNodes = nextNodes; // switch over to new set of nodes
      currLinks = nextLinks; // switch over to new set of links
      currPatterns = nextPatterns; // retire all patterns, having seen a full step
    }

    // process nonRejectedCandidates list
    for (int i = 0; i < nonRejectedCandidates.size(); i++) {
      BoundFrame fr = nonRejectedCandidates.get(i);
      boolean y = fr.completed(ansSet, inferSet, null);
      if (y && (Tracing.gTraceSet & Tracing.TraceFrame) != 0)
        Tracing.gTraceFile.println("took: " + fr.itsIndex);
    }

    return ansSet;
  }