cycr 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/changelog.txt CHANGED
@@ -1,3 +1,5 @@
1
+ 0.2.1
2
+ - Introduce query cache
1
3
  0.2.0
2
4
  - Rake task for changelog
3
5
  - Support for Ruby 1.8 dropped
data/cycr.gemspec CHANGED
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
19
19
  s.extra_rdoc_files = ["README.rdoc"]
20
20
  s.add_development_dependency("rspec", ["~> 2.8.0"])
21
21
  s.add_development_dependency("em-synchrony", ["~> 1.0.0"])
22
+ s.add_dependency("ref", ["~> 1.0.0"])
22
23
  end
@@ -45,6 +45,20 @@ shared_examples Cyc::Client do
45
45
  @client.talk('(gather-predicate-extent-index #$minimizeExtent)').size.should > 100
46
46
  end
47
47
 
48
+ it "should allow to use cached results" do
49
+ @client.cache_enabled = true
50
+ start_time = Time.now
51
+ @client.specs(:Animal).size.should > 100
52
+ end_time = Time.now
53
+ duration1 = end_time - start_time
54
+ start_time = Time.now
55
+ 100.times{ @client.specs(:Animal).size.should > 100}
56
+ end_time = Time.now
57
+ duration2 = end_time - start_time
58
+ duration2.should < duration1
59
+ @client.cache_enabled = false
60
+ end
61
+
48
62
  end
49
63
 
50
64
  describe Cyc::Client do
data/lib/cyc/cache.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'ref'
2
+
3
+ module Cyc
4
+ class Cache
5
+ def initialize
6
+ @soft_references = Ref::SoftValueMap.new
7
+ @hard_references = {}
8
+ end
9
+
10
+ # Get a value from cache.
11
+ def [](key)
12
+ if @hard_references.has_key?(key)
13
+ @hard_references[key]
14
+ else
15
+ @soft_references[key]
16
+ end
17
+ end
18
+
19
+ # Put a value for a given key to the cache.
20
+ def []=(key,value)
21
+ case value
22
+ when TrueClass,FalseClass,NilClass,Fixnum,Symbol
23
+ @soft_references.delete(key)
24
+ @hard_references[key] = value
25
+ else
26
+ @hard_references.delete(key)
27
+ @soft_references[key] = value
28
+ end
29
+ end
30
+
31
+ # Clear the cache.
32
+ def clear
33
+ @soft_references.clear
34
+ @hard_references.clear
35
+ end
36
+ end
37
+ end
data/lib/cyc/client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'uri'
2
2
  require 'cyc/connection'
3
3
  require 'cyc/exception'
4
+ require 'cyc/cache'
4
5
 
5
6
  module Cyc #:nodoc:
6
7
  # Author:: Aleksander Pohl (mailto:apohllo@o2.pl)
@@ -19,6 +20,14 @@ module Cyc #:nodoc:
19
20
  # If set to true, all communication with the server is logged
20
21
  # to standard output
21
22
  attr_accessor :debug
23
+ #
24
+ # If set to true, results of the queries are cached. This is
25
+ # turned off by default, since there is a functional-languages
26
+ # assumption, that the result for the same query is always the
27
+ # same, but this might not be true in case of Cyc (however
28
+ # highly probable). The cache is used only in the +talk+ call
29
+ # (and calls based on it -- i.e. direct Cyc calls, e.g. cyc.genls :Dog).
30
+ attr_accessor :cache_enabled
22
31
 
23
32
  # The +host+ the client connects to.
24
33
  attr_reader :host
@@ -46,6 +55,7 @@ module Cyc #:nodoc:
46
55
  # - +:host+ = +localhost+ server address
47
56
  # - +:port+ = +3601+ server port
48
57
  # - +:debug+ = +false+ initial debug flag
58
+ # - +:cache+ = +false+ initial cache enabled flag
49
59
  # - +:timeout+ = +0.2+ connection timeout in seconds
50
60
  # - +:url+ (String): +cyc://host:port+ overrides +:host+, +:port+
51
61
  # - +:driver+ (Class) = Cyc::Connection::Socket client connection driver class
@@ -75,6 +85,8 @@ module Cyc #:nodoc:
75
85
  @timeout = (options[:timeout] || 0.2).to_f
76
86
  @driver = options[:driver] || Connection.driver
77
87
  @debug = !!options[:debug]
88
+ @cache_enabled = !!options[:cache]
89
+ @cache = Cache.new
78
90
  @thread_safe = !!options[:thread_safe]
79
91
 
80
92
  if @thread_safe
@@ -137,8 +149,15 @@ module Cyc #:nodoc:
137
149
 
138
150
  # Sends the +messsage+ to the Cyc server and returns a parsed answer.
139
151
  def talk(message, options={})
152
+ if @cache_enabled && @cache[message]
153
+ return @cache[message]
154
+ end
140
155
  send_message(message)
141
- receive_answer(options)
156
+ result = receive_answer(options)
157
+ if @cache_enabled
158
+ @cache[message] = result
159
+ end
160
+ result
142
161
  end
143
162
 
144
163
  # Sends the +message+ to the Cyc server and
@@ -27,6 +27,12 @@ class Fixnum
27
27
  end
28
28
  end
29
29
 
30
+ class Proc
31
+ def to_cyc(raw=false)
32
+ self.call.to_s
33
+ end
34
+ end
35
+
30
36
  module Cyc
31
37
  class LiteralString < String
32
38
  def to_cyc(raw=false)
data/lib/cyc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cyc
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/cycr.rb CHANGED
@@ -9,3 +9,4 @@ require 'cyc/sexpr.rex'
9
9
  require 'cyc/symbol'
10
10
  require 'cyc/variable'
11
11
  require 'cyc/version'
12
+ require 'cyc/cache'
@@ -0,0 +1,10 @@
1
+ $:.unshift "lib"
2
+ require 'cycr'
3
+
4
+ describe Cyc do
5
+ describe "Ruby types conversions" do
6
+ it "should convert proc to literal string" do
7
+ lambda{ "#'null"}.to_cyc.should == "#'null"
8
+ end
9
+ end
10
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cycr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Aleksander Pohl
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-03-16 00:00:00 Z
14
+ date: 2012-06-02 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec
@@ -35,6 +35,17 @@ dependencies:
35
35
  version: 1.0.0
36
36
  type: :development
37
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: ref
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.0
47
+ type: :runtime
48
+ version_requirements: *id003
38
49
  description: Ruby wrapper for (Open)Cyc server and ontology
39
50
  email: apohllo@o2.pl
40
51
  executables: []
@@ -53,6 +64,7 @@ files:
53
64
  - integration/client.rb
54
65
  - lib/cyc/assertion.rb
55
66
  - lib/cyc/builder.rb
67
+ - lib/cyc/cache.rb
56
68
  - lib/cyc/client.rb
57
69
  - lib/cyc/collection.rb
58
70
  - lib/cyc/connection.rb
@@ -69,6 +81,7 @@ files:
69
81
  - lib/cyc/variable.rb
70
82
  - lib/cyc/version.rb
71
83
  - lib/cycr.rb
84
+ - spec/conversion.rb
72
85
  - spec/parser.rb
73
86
  homepage: http://github.com/apohllo/cycr
74
87
  licenses: []
@@ -94,11 +107,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
107
  requirements: []
95
108
 
96
109
  rubyforge_project:
97
- rubygems_version: 1.8.5
110
+ rubygems_version: 1.8.24
98
111
  signing_key:
99
112
  specification_version: 3
100
113
  summary: Ruby client for the (Open)Cyc server
101
114
  test_files:
102
115
  - spec/parser.rb
116
+ - spec/conversion.rb
103
117
  - integration/client.rb
104
118
  - integration/assertion.rb
119
+ has_rdoc: true