home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / CHARTP10.ZIP / CHART.CPP < prev    next >
C/C++ Source or Header  |  1992-04-05  |  3KB  |  96 lines

  1.  
  2. // Copyright 1992, David Perelman-Hall & Jamshid Afshar
  3.  
  4. #include "category.h"
  5. #include "agenda.h"
  6. #include "rules.h"
  7. #include "chart.h"
  8.  
  9.  
  10. Chart::Chart( const Category_Sequence& string )
  11.    : last_pos(string.length())
  12. {
  13.    active_edges_arr = new Edge_List[last_pos+1];
  14.    inactive_edges_arr = new Edge_List[last_pos+1];
  15. }
  16.  
  17. // destructor
  18. Chart::~Chart()
  19. {
  20.    delete [last_pos] active_edges_arr;
  21.    delete [last_pos] inactive_edges_arr;
  22. }
  23.  
  24.  
  25. // Add edge to chart. Try to combine edge before adding it to agenda.
  26. void Chart::add( const Edge& edge, Agenda& agenda, const RuleList& rules )
  27. {
  28.    cout << "Adding edge to chart: " << edge << " : " << edge.parse() << endl;
  29.  
  30.    // IF EDGE IS ACTIVE, TRY TO COMBINE IT WITH INACTIVE EDGES
  31.    if( edge.isActive() ){
  32.       active_edges_arr[edge.finish()].push( edge );
  33.       Edge_List temp = inactive_edges_arr[edge.finish()];
  34.       while( !temp.isEmpty() ) {
  35.          Edge e = temp.pop();
  36.          if( e.canCombineWith( edge ) )
  37.             agenda.add( combine( e, edge ), *this );
  38.       }
  39.    }
  40.    else{
  41.       // IF EDGE IS INACTIVE, TRY TO COMBINE IT WITH ACTIVE EDGES
  42.       inactive_edges_arr[edge.start()].push( edge );
  43.       Edge_List temp = active_edges_arr[edge.start()];
  44.       while( !temp.isEmpty() ) {
  45.          Edge e = temp.pop();
  46.          if( e.canCombineWith( edge ) )
  47.             agenda.add( combine( e, edge ), *this );
  48.       }
  49.       // ALSO, FOR INACTIVE EDGE, SEE IF ANY RULES APPLY TO CREATE NEW
  50.          EDGES. IF SO, ADD THESE TO AGENDA FOR PROCESSING.
  51.       for( int i=0; i<rules.num(); i++) {
  52.          if ( rules[i].appliesTo( edge ) )
  53.             agenda.add( rules[i].apply( edge ), *this );
  54.       }
  55.    }
  56. }
  57.  
  58. bool Chart::success( const Category& goal, const Category_Sequence& str ) const
  59. {
  60.    bool success = FALSE;
  61.    Edge_List temp = inactive_edges_arr[0];
  62.    while( !temp.isEmpty() ) {
  63.       Edge edge = temp.pop();
  64.       Category cat = edge.label();
  65.       if( edge.start() == 0 &&
  66.           edge.finish() == str.length() && 
  67.           cat == goal ) {
  68.          cout << "Successful edge: " << edge << " : " << edge.parse() << "\n";
  69.          success = TRUE;
  70.  
  71.        }
  72.    }
  73.    return success;
  74. }
  75.  
  76. ostream& operator << ( ostream& os, const Chart& chart )
  77. {
  78.  
  79.    // print out the active chart
  80.    os << "ACTIVE CHART:\n";
  81.    for(int i=0; i<chart.last_pos; i++)
  82.       os << chart.active_edges_arr[i];
  83.    
  84.    // line feed
  85.    os << endl;
  86.  
  87.    // print out the inactive chart
  88.    os << "INACTIVE CHART:\n";
  89.    for(i=0; i<chart.last_pos; i++)
  90.       os << chart.inactive_edges_arr[i];
  91.  
  92.    return os;
  93. }
  94.  
  95.  
  96.