Class: Cyc::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/cyc/console.rb

Constant Summary collapse

API_QUIT =
"(api-quit)"
CompletionProc =
proc {|input|
  case input
  when /\A#/
    @@constants.grep(/^#{Regexp.quote(input)}/)
  when /\A[a-zA-Z-]*\Z/
    @@functions.grep(/#{Regexp.quote(input)}/)
  end
}
@@constants =
[]
@@functions =
CycFunctions.new.to_a

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Console

Initializes connection with Cyc runing on host :host: on port :port:



101
102
103
104
105
106
107
108
# File 'lib/cyc/console.rb', line 101

def initialize(host, port)
  History.new({})
  @host = host
  @port = port
  @cyc = Cyc::Client.new(:host => @host,:port => @port)
  @line = ""
  @count = 0
end

Instance Method Details

#add_autocompletion(str) ⇒ Object



110
111
112
# File 'lib/cyc/console.rb', line 110

def add_autocompletion(str)
  @@constants |= str.split(/[() ]/).select{|e| e =~ /\#\$/}
end

#main_loopObject

The main loop of the console



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cyc/console.rb', line 115

def main_loop
  loop do
    begin
      line = Readline::readline(("cyc@" + "#{@host}:#{@port}" +
          (@count > 0 ? ":#{"("*@count}" : "") + "> "))
      case line
      when nil
        puts
        break
      when API_QUIT,"exit"
        @cyc.raw_talk(API_QUIT) rescue nil
        break
      else
        @line += "\n" unless @line.empty?
        @line += line
        letters = @line.split("")
        Readline::HISTORY.push(@line) if line
        message = @cyc.raw_talk(@line)
        @line = ""
        @count = 0
        puts message.gsub(/(\#\$[\w-]+)/,"\\1".hl(:blue))
        add_autocompletion(message)
      end
    rescue ::Cyc::UnbalancedClosingParenthesis => exception
      puts exception.to_s.sub(/<error>([^<]*)<\/error>/,"\\1".hl(:red))
      @line = ""
    rescue ::Cyc::UnbalancedOpeningParenthesis => exception
      @count = exception.count
    rescue Exception => exception
      puts "Error: " + exception.to_s
      @count = 0
      @line = ""
    end
  end
end