cyc-console 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt ADDED
@@ -0,0 +1,133 @@
1
+ = cyc-console
2
+
3
+ * http://github.com/apohllo/cyc-colors
4
+
5
+ == DESCRIPTION:
6
+
7
+ Console for the Cyc ontology written in Ruby.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * support for history
12
+ * highlighting of unmatched parenthesis
13
+ * colorful prompt
14
+ * readline based editing (arrows, delete, etc. works!)
15
+ * autocompletion of symbols (present in server answers)
16
+ and functions (listed in ~/.cyc_functions)
17
+
18
+ * problems with multiline commands
19
+ * messeges send to stdout are not visible
20
+ * errors are not printed - only nil is returned
21
+ * text protocol is used (lenghty output is cut, it might be slower
22
+ than the binary protocol, but is not as important when working
23
+ in interactive mode)
24
+
25
+ == SYNOPSIS:
26
+
27
+ Cyc-console is console for the Cyc ontology written in Ruby.
28
+ It is inspired by the Ruby console - irb. The original Cyc console
29
+ has many limitations. There is no support for readline, which
30
+ means that arrow keys doesn't work as expected. There is also
31
+ no auto-completion, no history, etc. This console aims at overcomming
32
+ these limitations.
33
+
34
+ == INSTALL:
35
+
36
+ You need RubyGems v. 1.3.3
37
+
38
+ $ gem -v
39
+ 1.2.0 #=> not OK
40
+ $ gem update --system
41
+ ...
42
+ $ gem -v
43
+ 1.3.3 #=> OK
44
+
45
+ You need the gemcutter repository to be added to your sources list:
46
+
47
+ $ gem sources -a http://gemcutter.org
48
+
49
+ Then you can install the Cyc console with:
50
+
51
+ $ sudo gem install cyc-console
52
+
53
+ If it doesn't work as expected:
54
+
55
+ ERROR: could not find gem cyc-console locally or in a repository
56
+
57
+ try installing the gemcutter gem, and issuing
58
+ the gem tumble command:
59
+
60
+ $ sudo gem install gemcutter
61
+ $ sudo gem tumble
62
+
63
+ And then try to install the gem once again:
64
+
65
+ $ sudo gem install cyc-console
66
+
67
+ == BASIC USAGE:
68
+
69
+ The gem comes with +cyc+ command, so you can simply type
70
+ (provided the Cyc server is running on localhost on the default
71
+ port and you have rubygems bin directory in your path):
72
+
73
+ $ cyc
74
+ cyc@localhost:3601>
75
+
76
+ By defualt the console connects with Cyc on the localhost on the default port 3601.
77
+ You can change it, by providing the name of the host, and the port as parameters:
78
+
79
+ $ cyc www.example.com 4567
80
+ cyc@www.example.com:4567>
81
+
82
+ The console works like regular Cyc console (however, see problems above):
83
+
84
+ cyc@localhost:3601>(genl? #$Cat #$Animal)
85
+ T
86
+ cyc@localhost:3601>
87
+
88
+ If you run the console once again, you will have access to previous commands.
89
+ The history is kept ~/.cyc_history and remembers 1000 commands by default.
90
+ You can change these, by providing CYC_HISTORY_FILE and CYC_HISTORY_SIZE
91
+ environment varibles respectively.
92
+
93
+ One helpful feature is to brows the history by providing the beginning of the command.
94
+ If you type:
95
+
96
+ cyc@localhost:3601>(genl?<PG-UP>
97
+
98
+ you'll get the latest command which has the same form:
99
+
100
+ cyc@localhost:3601>(genl? #$Cat #$Animal)
101
+
102
+ This really speeds-up working with the console.
103
+
104
+
105
+ == LICENSE:
106
+
107
+ (The MIT License)
108
+
109
+ Copyright (c) 2009 Aleksander Pohl
110
+
111
+ Permission is hereby granted, free of charge, to any person obtaining
112
+ a copy of this software and associated documentation files (the
113
+ 'Software'), to deal in the Software without restriction, including
114
+ without limitation the rights to use, copy, modify, merge, publish,
115
+ distribute, sublicense, and/or sell copies of the Software, and to
116
+ permit persons to whom the Software is furnished to do so, subject to
117
+ the following conditions:
118
+
119
+ The above copyright notice and this permission notice shall be
120
+ included in all copies or substantial portions of the Software.
121
+
122
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
123
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
124
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
125
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
126
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
127
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
128
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
129
+
130
+ == FEEDBACK
131
+
132
+ * mailto:apohllo@o2.pl
133
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ task :default => [:test]
2
+
3
+ $gem_name = "cyc-console"
4
+
5
+ desc "Test the library"
6
+ task :test do
7
+ ruby Dir.glob("test/*")
8
+ end
9
+
10
+ desc "Run specs"
11
+ task :spec do
12
+ sh "spec spec/* --format specdoc --color"
13
+ end
14
+
15
+ desc "Run features"
16
+ task :features do
17
+ sh "features features/* "
18
+ end
19
+
20
+ desc "Build the gem"
21
+ task :build do
22
+ sh "gem build #$gem_name.gemspec"
23
+ end
24
+
25
+ desc "Install the library at local machnie"
26
+ task :install => :build do
27
+ sh "sudo gem install #$gem_name -l"
28
+ end
29
+
30
+ desc "Uninstall the library from local machnie"
31
+ task :uninstall do
32
+ sh "sudo gem uninstall #$gem_name"
33
+ end
34
+
35
+ desc "Reinstall the library in the local machine"
36
+ task :reinstall => [:uninstall, :install] do
37
+ end
38
+
39
+ desc "Clean"
40
+ task :clean do
41
+ sh "rm #$gem_name*.gem"
42
+ end
data/bin/cyc ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ require 'readline'
4
+ require 'cyc/console'
5
+
6
+ if Readline.respond_to?("basic_word_break_characters=")
7
+ Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
8
+ end
9
+ Readline.completion_append_character = nil
10
+ Readline.completion_proc = Cyc::Console::CompletionProc
11
+
12
+ host = ARGV[0] || "localhost"
13
+ port = ARGV[1] || 3601
14
+ console = Cyc::Console.new(host,port)
15
+ console.main_loop
16
+
data/changelog.txt ADDED
@@ -0,0 +1,11 @@
1
+ 0.0.4
2
+ - autocompletion for symbols
3
+ - autocompletion for functions defined in ~/.cyc_functions
4
+ 0.0.3
5
+ - fix: added missing library and changelog.txt to gemspec
6
+ 0.0.2
7
+ - versin bump for github
8
+ 0.0.1
9
+ - history support
10
+ - highlighting of unmatched parenthesis
11
+ - colorful prompt
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cyc-console"
3
+ s.version = "0.0.4"
4
+ s.date = "2009-10-08"
5
+ s.summary = "Ruby console for the Cyc ontology"
6
+ s.email = "apohllo@o2.pl"
7
+ s.homepage = "http://apohllo.pl"
8
+ s.description = "Ruby console for the Cyc ontology with " +
9
+ "support for readline, colorful output, history, etc."
10
+ s.authors = ['Aleksander Pohl']
11
+ # the lib path is added to the LOAD_PATH
12
+ s.require_path = "lib"
13
+ # include Rakefile, gemspec, README and all the source files
14
+ s.files = [
15
+ 'lib/cyc/console.rb',
16
+ "Rakefile",
17
+ "cyc-console.gemspec",
18
+ "README.txt",
19
+ "changelog.txt"
20
+ ]
21
+ # include README while generating rdoc
22
+ s.rdoc_options = ["--main", "README.txt"]
23
+ s.has_rdoc = true
24
+ s.extra_rdoc_files = ["README.txt"]
25
+ s.executables = ['cyc']
26
+ # s.add_dependency("apohllo-colors", [">= 0.0.4"])
27
+ end
28
+
@@ -0,0 +1,187 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'readline'
4
+ require 'colors'
5
+ require 'net/telnet'
6
+
7
+ module Cyc
8
+ class ParenthesisNotMatched < RuntimeError; end
9
+
10
+ class Configurable
11
+
12
+ attr_reader :configuration
13
+ attr_writer :verbose
14
+
15
+ def initialize
16
+ load_configuration
17
+ end
18
+
19
+ protected
20
+ def say(*args)
21
+ puts *args if @verbose
22
+ end
23
+
24
+ def cfg(key)
25
+ @opt["#{key}".intern]
26
+ end
27
+
28
+ def load_configuration
29
+ # expand functions file and make sure it exists
30
+ real_path = File.expand_path(cfg('path'))
31
+ unless File.exist?(real_path)
32
+ say "Configuration file #{real_path} doesn't exist."
33
+ @configuration = []
34
+ return
35
+ end
36
+
37
+ # read lines from file and add them to configuration list
38
+ @configuration = File.readlines(real_path).map { |line| line.chomp }
39
+
40
+ say 'Read %d lines from configuration file %s' % [@configuration.size, cfg('path')]
41
+ @configuration
42
+ end
43
+
44
+ end
45
+ #
46
+ # Stolen (with permission from the author :) from Wirble history for IRB
47
+ # http://pablotron.org/software/wirble/
48
+ #
49
+ class History < Configurable
50
+ DEFAULTS = {
51
+ :path => ENV['CYC_HISTORY_FILE'] || "~/.cyc_history",
52
+ :size => (ENV['CYC_HISTORY_SIZE'] || 1000).to_i,
53
+ :perms => File::WRONLY | File::CREAT | File::TRUNC,
54
+ }
55
+
56
+ def save_history
57
+ path, max_size, perms = %w{path size perms}.map { |v| cfg(v) }
58
+
59
+ # read lines from history, and truncate the list (if necessary)
60
+ lines = Readline::HISTORY.to_a.uniq
61
+ lines = lines[-max_size, -1] if lines.size > max_size
62
+
63
+ # write the history file
64
+ real_path = File.expand_path(path)
65
+ File.open(real_path, perms) { |fh| fh.puts lines }
66
+ say 'Saved %d lines to history file %s.' % [lines.size, path]
67
+ end
68
+
69
+ public
70
+
71
+ def initialize(opt = nil)
72
+ @opt = DEFAULTS.merge(opt || {})
73
+ super()
74
+ return unless defined? Readline::HISTORY
75
+ Readline::HISTORY.push(*self.configuration)
76
+ Kernel.at_exit { save_history }
77
+ end
78
+ end
79
+
80
+ class CycFunctions < Configurable
81
+ DEFAULTS = {
82
+ :path => ENV['CYC_FUNCTIONS_FILE'] || "~/.cyc_functions",
83
+ :perms => File::WRONLY | File::CREAT | File::TRUNC
84
+ }
85
+
86
+ def initialize(opt = nil)
87
+ @opt = DEFAULTS.merge(opt || {})
88
+ super()
89
+ end
90
+
91
+ def to_a
92
+ self.configuration
93
+ end
94
+ end
95
+
96
+ class Console
97
+ @@constants = []
98
+ @@functions = CycFunctions.new.to_a
99
+
100
+ API_QUIT = "(api-quit)"
101
+
102
+ # Initializes connection with Cyc runing on host :host: on port :port:
103
+ def initialize(host, port)
104
+ History.new({})
105
+ @host = host
106
+ @port = port
107
+ @conn = Net::Telnet.new("Port" => @port, "Telnetmode" => false,
108
+ "Host" => @host, "Timeout" => 600)
109
+ @line = ""
110
+ @count = 0
111
+ end
112
+
113
+ # Scans the :str: to find out if the parenthesis are matched
114
+ # raises ParenthesisNotMatched exception if there is not matched closing
115
+ # parenthesis. The message of the exception contains the string with the
116
+ # unmatched parenthesis highlighted.
117
+ def match_par(str)
118
+ position = 0
119
+ str.scan(/./) do |char|
120
+ position += 1
121
+ next if char !~ /\(|\)/
122
+ @count += (char == "(" ? 1 : -1)
123
+ if @count < 0
124
+ @count = 0
125
+ raise ParenthesisNotMatched.
126
+ new((position > 1 ? str[0..position-2] : "") +
127
+ ")".hl(:red) + str[position..-1])
128
+ end
129
+ end
130
+ @count == 0
131
+ end
132
+
133
+ def add_autocompletion(str)
134
+ @@constants |= str.split(/[() ]/).select{|e| e =~ /\#\$/}
135
+ end
136
+
137
+ # The main loop of the console
138
+ def main_loop
139
+ loop do
140
+ begin
141
+ line = Readline::readline(("cyc@" + "#{@host}:#{@port}" +
142
+ (@count > 0 ? ":#{"("*@count}" : "") + "> ").hl(:blue))
143
+ case line
144
+ when API_QUIT
145
+ @conn.puts(line)
146
+ break
147
+ when "exit"
148
+ @conn.puts(API_QUIT)
149
+ break
150
+ else
151
+ @line += "\n" unless @line.empty?
152
+ @line += line
153
+ letters = @line.split("")
154
+ Readline::HISTORY.push(@line) if line
155
+ if match_par(line)
156
+ @conn.puts(@line)
157
+ @line = ""
158
+ answer = @conn.waitfor(/\d\d\d/)
159
+ message = answer.sub(/(\d\d\d) (.*)\n/,"\\2") if answer
160
+ if($1.to_i == 200)
161
+ puts message
162
+ add_autocompletion(message)
163
+ else
164
+ puts "Error: " + answer.to_s
165
+ end
166
+ end
167
+ end
168
+ rescue ParenthesisNotMatched => exception
169
+ puts exception
170
+ @line = ""
171
+ # rescue Exception => exception
172
+ # puts exception
173
+ end
174
+ end
175
+ end
176
+
177
+ CompletionProc = proc {|input|
178
+ case input
179
+ when /\A#/
180
+ @@constants.grep(/^#{Regexp.quote(input)}/)
181
+ when /\A[a-zA-Z-]*\Z/
182
+ @@functions.grep(/#{Regexp.quote(input)}/)
183
+ end
184
+ }
185
+ end
186
+ end
187
+
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cyc-console
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Aleksander Pohl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-08 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby console for the Cyc ontology with support for readline, colorful output, history, etc.
17
+ email: apohllo@o2.pl
18
+ executables:
19
+ - cyc
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - lib/cyc/console.rb
26
+ - Rakefile
27
+ - cyc-console.gemspec
28
+ - README.txt
29
+ - changelog.txt
30
+ has_rdoc: true
31
+ homepage: http://apohllo.pl
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --main
37
+ - README.txt
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Ruby console for the Cyc ontology
59
+ test_files: []
60
+