cycr 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/cycr.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cycr"
3
- s.version = "0.0.3"
3
+ s.version = "0.0.4"
4
4
  s.date = "2010-10-19"
5
5
  s.summary = "Ruby client for the (Open)Cyc server"
6
6
  s.email = "apohllo@o2.pl"
7
- s.homepage = "http://www.opencyc.org"
7
+ s.homepage = "http://github.com/apohllo/cycr"
8
8
  s.require_path = "lib"
9
9
  s.description = "Ruby wrapper for (Open)Cyc server and ontology"
10
10
  s.has_rdoc = false
@@ -0,0 +1,33 @@
1
+ module Cyc
2
+ # Author:: Aleksander Pohl (mailto:apohllo@o2.pl)
3
+ # License:: MIT License
4
+ #
5
+ # This class is used to capture calls to the Cyc client, to allow
6
+ # nested calls, like
7
+ #
8
+ # cyc.with_any_mt do
9
+ # comment :Collection
10
+ # end
11
+ class Builder
12
+ def initialize
13
+ reset
14
+ end
15
+
16
+ def reset
17
+ @query = ""
18
+ end
19
+
20
+ def to_cyc
21
+ @query
22
+ end
23
+
24
+ def method_missing(name,*args,&block)
25
+ @query << "(" << name.to_s.gsub("_","-") << " "
26
+ @query << args.map{|a| a.to_cyc}.join(" ")
27
+ if block
28
+ self.instance_exec(&block)
29
+ end
30
+ @query << ")"
31
+ end
32
+ end
33
+ end
data/lib/cycr/client.rb CHANGED
@@ -18,6 +18,7 @@ module Cyc
18
18
  @pid = Process.pid
19
19
  @parser = Parser.new
20
20
  @mts_cache = {}
21
+ @builder = Builder.new
21
22
 
22
23
  # read domains mapings
23
24
  # talk(File.read(File.join(
@@ -75,27 +76,9 @@ module Cyc
75
76
  @conn = nil
76
77
  end
77
78
 
78
- NART_QUERY =<<-END
79
- (clet ((result ())
80
- (call-value :call))
81
- (pif (listp call-value)
82
- (cdolist (el call-value)
83
- (pif (nart-p el)
84
- (cpush (nart-id el) result)
85
- (cpush el result)))
86
- (pif (nart-p call-value)
87
- (cpush (nart-id call-value) result)
88
- (cpush call-value result)))
89
- result)
90
- END
91
-
92
-
93
79
  # Sends message +msg+ directly to the Cyc server and receives
94
80
  # the answer.
95
81
  def talk(msg, options={})
96
- #conn.puts(msg.respond_to?(:to_cyc) ? msg.to_cyc : msg)
97
- msg = NART_QUERY.sub(/:call/,msg) if options[:nart]
98
-
99
82
  send_message(msg)
100
83
  receive_answer(options)
101
84
  end
@@ -125,12 +108,12 @@ module Cyc
125
108
  end
126
109
  end
127
110
  # XXX ignore some potential asynchronous answers
128
- answer = answer.split("\n")[-1]
111
+ # XXX check if everything works ok
112
+ #answer = answer.split("\n")[-1]
129
113
  answer = answer.sub(/(\d\d\d) (.*)/,"\\2")
130
114
  if($1.to_i == 200)
131
115
  begin
132
116
  result = @parser.parse(answer,options[:stack])
133
- result = options[:nart] ? substitute_narts(result) : result
134
117
  rescue Parser::ContinueParsing => ex
135
118
  result = ex.stack
136
119
  current_result = result
@@ -148,49 +131,21 @@ module Cyc
148
131
  puts $2.sub(/^"/,"").sub(/"$/,"") + "\n" +
149
132
  @last_message
150
133
  else
151
- puts "unknown error!"
134
+ puts "Unknown error! #{answer}"
152
135
  end
153
136
  nil
154
137
  end
155
138
  end
156
139
 
157
140
 
158
- def method_missing(name,*args)
159
- #"_missing_method_#{name}"
160
- method_name = name.to_s.gsub("_","-").sub(/-nart$/,"")
161
- options = {}
162
- def method_name.to_cyc(quote=false)
163
- self
164
- end
165
- options[:nart] = true if name.to_s =~ /_nart$/
166
- talk(([method_name] + args).to_cyc,options)
167
- end
168
-
169
- DENOTATION_QUERY =<<-END
170
- (nart-denotation-mapper ":word")
171
- END
172
-
173
- def denotation_mapper(name)
174
- send_message(DENOTATION_QUERY.sub(/:word/,name))
175
- receive_answer(:nart => true)
176
- end
177
-
178
-
179
- # Finds collection with given name. The name has to
180
- # be the name of the exact name of the constant.
181
- def find_collection(name)
182
- term = self.find_constant(name)
183
- Collection.new(term, self) unless term.nil?
141
+ def method_missing(name,*args,&block)
142
+ @builder.reset
143
+ @builder.send(name,*args,&block)
144
+ talk(@builder.to_cyc)
184
145
  end
185
146
 
186
147
  protected
187
148
 
188
- def substitute_narts(terms)
189
- unless terms.nil?
190
- terms.collect{|t| t.is_a?(String) ? Cyc::Nart.new(t,self) : t}
191
- end
192
- end
193
-
194
149
  def relevant_mts(term)
195
150
  @mts_cache[term] ||=
196
151
  (mts = self.term_mts(term)
@@ -201,8 +156,5 @@ protected
201
156
  []
202
157
  end)
203
158
  end
204
-
205
- def with_reconnect
206
- end
207
159
  end
208
160
  end
@@ -1,25 +1,23 @@
1
1
  class String
2
- def to_cyc(quote = false)
3
- #self =~ /_missing_method_(.*)/ ? "#{$1.gsub("_","-")}" : "\"#{self}\""
2
+ def to_cyc
4
3
  "\"#{self}\""
5
4
  end
6
5
  end
7
6
 
8
7
  class Symbol
9
- def to_cyc(quote = false)
10
- (quote ? "'" : "") + "#\$#{self}"
8
+ def to_cyc
9
+ "#\$#{self}"
11
10
  end
12
11
  end
13
12
 
14
13
  class Array
15
- def to_cyc(quote = false)
16
- (quote ? "'" : "") +
17
- "("+map{|e| e.to_cyc(quote)}.join(" ")+")"
14
+ def to_cyc
15
+ "'("+map{|e| e.to_cyc}.join(" ")+")"
18
16
  end
19
17
  end
20
18
 
21
19
  class Fixnum
22
- def to_cyc(quote = false)
20
+ def to_cyc
23
21
  to_s
24
22
  end
25
23
  end
data/lib/cycr/sexpr.rex CHANGED
@@ -8,7 +8,7 @@ macro
8
8
  INPUT_CHARACTER [^\r\n\"\(\):& ]
9
9
  WHITE_SPACE [\ \t\f\r\n] | \r\n
10
10
  SYMBOL :[^<>\r\n\"\(\):&\ ]+
11
- CYC_SYMBOL \#\$[a-zA-Z0-9-]+
11
+ CYC_SYMBOL \#\$[a-zA-Z0-9:_-]+
12
12
  ATOM [^\r\n\"\(\):&\ ]+
13
13
  OPEN_PAR \(
14
14
  CLOSE_PAR \)
@@ -37,13 +37,13 @@ rule
37
37
  {QUOTE} { state = :STRING; @str = ""; [:in_string] }
38
38
  # whitespace
39
39
  {WHITE_SPACE} # ignore
40
- :STRING {QUOTE} { state = nil; [:string,@str] }
41
40
  :STRING [^\n\r\"\\]+ { @str << text; [:in_string]}
42
- :STRING \t { @str << '\t'; [:in_string] }
43
- :STRING \n { @str << '\n'; [:in_string] }
44
- :STRING \r { @str << '\r'; [:in_string] }
45
- :STRING \\" { @str << '\"'; [:in_string] }
46
- :STRING \\ { @str << '\\'; [:in_string] }
41
+ :STRING \t { @str << "\t"; [:in_string] }
42
+ :STRING \n { @str << "\n"; [:in_string] }
43
+ :STRING \r { @str << "\n"; [:in_string] }
44
+ :STRING \\" { @str << '"'; [:in_string] }
45
+ :STRING \\ { @str << "\\"; [:in_string] }
46
+ :STRING {QUOTE} { state = nil; [:string,@str] }
47
47
  # error fallback
48
48
  .|\n { raise "Illegal character <#{text}>" }
49
49
  inner
@@ -76,7 +76,7 @@ class SExpressionLexer
76
76
  when (text = ss.scan(/:[^<>\r\n\"\(\):&\ ]+/))
77
77
  @rex_tokens.push action { [:symbol,text] }
78
78
 
79
- when (text = ss.scan(/\#\$[a-zA-Z0-9-]+/))
79
+ when (text = ss.scan(/\#\$[a-zA-Z0-9:_-]+/))
80
80
  @rex_tokens.push action { [:cyc_symbol,text] }
81
81
 
82
82
  when (text = ss.scan(/[^\r\n\"\(\):&\ ]+/))
@@ -98,26 +98,26 @@ class SExpressionLexer
98
98
 
99
99
  when :STRING
100
100
  case
101
- when (text = ss.scan(/\"/))
102
- @rex_tokens.push action { state = nil; [:string,@str] }
103
-
104
101
  when (text = ss.scan(/[^\n\r\"\\]+/))
105
102
  @rex_tokens.push action { @str << text; [:in_string]}
106
103
 
107
104
  when (text = ss.scan(/\t/))
108
- @rex_tokens.push action { @str << '\t'; [:in_string] }
105
+ @rex_tokens.push action { @str << "\t"; [:in_string] }
109
106
 
110
107
  when (text = ss.scan(/\n/))
111
- @rex_tokens.push action { @str << '\n'; [:in_string] }
108
+ @rex_tokens.push action { @str << "\n"; [:in_string] }
112
109
 
113
110
  when (text = ss.scan(/\r/))
114
- @rex_tokens.push action { @str << '\r'; [:in_string] }
111
+ @rex_tokens.push action { @str << "\n"; [:in_string] }
115
112
 
116
113
  when (text = ss.scan(/\\"/))
117
- @rex_tokens.push action { @str << '\"'; [:in_string] }
114
+ @rex_tokens.push action { @str << '"'; [:in_string] }
118
115
 
119
116
  when (text = ss.scan(/\\/))
120
- @rex_tokens.push action { @str << '\\'; [:in_string] }
117
+ @rex_tokens.push action { @str << "\\"; [:in_string] }
118
+
119
+ when (text = ss.scan(/\"/))
120
+ @rex_tokens.push action { state = nil; [:string,@str] }
121
121
 
122
122
  else
123
123
  text = ss.string[ss.pos .. -1]
data/spec/client.rb CHANGED
@@ -18,13 +18,6 @@ describe Cyc::Client do
18
18
  @client.constant_count.should_not == nil
19
19
  end
20
20
 
21
- it "should allow to find 'Cat' collection" do
22
- cat = @client.find_collection("Cat")
23
- cat.should_not == nil
24
- cat.should be_instance_of(Cyc::Collection)
25
- cat.symbol.should == :Cat
26
- end
27
-
28
21
  it "should allow multiple processes to use the client" do
29
22
  parent_pid = Process.pid
30
23
  if fork
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cycr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aleksander Pohl
@@ -50,7 +50,7 @@ files:
50
50
  - lib/cycr/parser.rb
51
51
  - lib/cycr/domains.lisp
52
52
  - lib/cycr/sexpr.flex
53
- - lib/cycr/nart.rb
53
+ - lib/cycr/builder.rb
54
54
  - lib/cycr/constants.rb
55
55
  - lib/cycr/collection.rb
56
56
  - lib/cycr/sexpr.rex.rb
@@ -62,7 +62,7 @@ files:
62
62
  - spec/client.rb
63
63
  - spec/assertion.rb
64
64
  has_rdoc: true
65
- homepage: http://www.opencyc.org
65
+ homepage: http://github.com/apohllo/cycr
66
66
  licenses: []
67
67
 
68
68
  post_install_message:
data/lib/cycr/nart.rb DELETED
@@ -1,21 +0,0 @@
1
- module Cyc
2
- class Nart
3
- attr_accessor :id, :value
4
- def initialize(id, cyc)
5
- @id = id.to_i
6
- @value = cyc.find_nart_by_id @id
7
- end
8
-
9
- def to_cyc(quote=false)
10
- "(find-nart-by-id #{@id})"
11
- end
12
-
13
- def to_s
14
- "NART[#{@id}]: #{@value.inspect} "
15
- end
16
-
17
- def self.find_by_name(name,cyc)
18
- self.new(name.match(/^NART\[([^\]]+)\]/)[1],cyc)
19
- end
20
- end
21
- end