Skip to content
Browse files

major refactoring

  • Loading branch information...
1 parent ab7f21c commit 9fe169037da40894d7aded7d2ac4a62e48e44125 @phifty committed
View
6 lib/allegro_graph/federation.rb
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), "transport")
require File.join(File.dirname(__FILE__), "proxy", "statements")
require File.join(File.dirname(__FILE__), "proxy", "query")
-require File.join(File.dirname(__FILE__), "proxy", "geo")
+require File.join(File.dirname(__FILE__), "proxy", "geometric")
require File.join(File.dirname(__FILE__), "proxy", "mapping")
module AllegroGraph
@@ -15,7 +15,7 @@ class Federation
attr_reader :statements
attr_reader :query
- attr_reader :geo
+ attr_reader :geometric
attr_reader :mapping
def initialize(server, name, options = { })
@@ -26,7 +26,7 @@ def initialize(server, name, options = { })
@statements = Proxy::Statements.new self
@query = Proxy::Query.new self
- @geo = Proxy::Geo.new self
+ @geometric = Proxy::Geometric.new self
@mapping = Proxy::Mapping.new self
end
View
117 lib/allegro_graph/proxy/geo.rb
@@ -1,117 +0,0 @@
-
-module AllegroGraph
-
- module Proxy
-
- class Geo
-
- attr_reader :resource
-
- def initialize(resource)
- @resource = resource
- end
-
- def path
- "#{@resource.path}/geo"
- end
-
- def cartesian_type(strip_width, x_min, y_min, x_max, y_max)
- parameters = {
- :stripWidth => strip_width.to_s,
- :xmin => x_min.to_s,
- :ymin => y_min.to_s,
- :xmax => x_max.to_s,
- :ymax => y_max.to_s
- }
- type = @resource.request :post, self.path + "/types/cartesian", :parameters => parameters, :expected_status_code => 200
- type.sub! /^.*</, "<"
- type.sub! />.*$/, ">"
- type
- end
-
- def spherical_type(strip_width, unit, latitude_min, longitude_min, latitude_max, longitude_max)
- parameters = {
- :stripWidth => strip_width.to_s,
- :unit => unit.to_s,
- :latmin => latitude_min.to_s,
- :longmin => longitude_min.to_s,
- :latmax => latitude_max.to_s,
- :longmax => longitude_max.to_s
- }
- type = @resource.request :post, self.path + "/types/spherical", :parameters => parameters, :expected_status_code => 200
- type.sub! /^.*</, "<"
- type.sub! />.*$/, ">"
- type
- end
-
- def create_polygon(name, type, points)
- raise ArgumentError, "at least three points has to defined" unless points.is_a?(Array) && points.size >= 3
- parameters = {
- :resource => "\"#{name}\"",
- :point => points.map{ |point| "\"%+g%+g\"^^%s" % [ point[0], point[1], type ] }
- }
- @resource.request :put, self.path + "/polygon", :parameters => parameters, :expected_status_code => 204
- true
- end
-
- def inside_box(type, predicate, x_min, y_min, x_max, y_max)
- parameters = {
- :type => type,
- :predicate => predicate,
- :xmin => x_min.to_s,
- :ymin => y_min.to_s,
- :xmax => x_max.to_s,
- :ymax => y_max.to_s
- }
- @resource.request :get, self.path + "/box", :parameters => parameters, :expected_status_code => 200
- end
-
- def inside_circle(type, predicate, x, y, radius)
- parameters = {
- :type => type,
- :predicate => predicate,
- :x => x.to_s,
- :y => y.to_s,
- :radius => radius.to_s
- }
- @resource.request :get, self.path + "/circle", :parameters => parameters, :expected_status_code => 200
- end
-
- def inside_haversine(type, predicate, latitude, longitude, radius, unit = :km)
- parameters = {
- :type => type,
- :predicate => predicate,
- :lat => float_to_iso_6709(latitude, 2),
- :long => float_to_iso_6709(longitude, 3),
- :radius => radius.to_s,
- :unit => unit.to_s
- }
- @resource.request :get, self.path + "/haversine", :parameters => parameters, :expected_status_code => 200
- end
-
- def inside_polygon(type, predicate, name)
- parameters = {
- :type => type,
- :predicate => predicate,
- :polygon => "\"#{name}\""
- }
- @resource.request :get, self.path + "/polygon", :parameters => parameters, :expected_status_code => 200
- end
-
- private
-
- def float_to_iso_6709(value, digits)
- sign = "+"
- if value < 0
- sign = "-"
- value = -value
- end
- floor = value.to_i
- sign + (("%%0%dd" % digits) % floor) + (".%07d" % ((value - floor) * 10000000))
- end
-
- end
-
- end
-
-end
View
50 lib/allegro_graph/proxy/geometric.rb
@@ -0,0 +1,50 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "utility", "parameter_mapper"))
+
+module AllegroGraph
+
+ module Proxy
+
+ class Geometric
+
+ attr_reader :resource
+
+ def initialize(resource)
+ @resource = resource
+ end
+
+ def path
+ "#{@resource.path}/geo"
+ end
+
+ def cartesian_type(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :cartesian_type
+ type = @resource.request :post, self.path + "/types/cartesian", :parameters => parameters, :expected_status_code => 200
+ type.sub! /^.*</, "<"
+ type.sub! />.*$/, ">"
+ type
+ end
+
+ def spherical_type(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :spherical_type
+ type = @resource.request :post, self.path + "/types/spherical", :parameters => parameters, :expected_status_code => 200
+ type.sub! /^.*</, "<"
+ type.sub! />.*$/, ">"
+ type
+ end
+
+ def create_polygon(points, parameters = { })
+ type = parameters.delete :type
+ parameters = Utility::ParameterMapper.map parameters, :create_polygon
+
+ raise ArgumentError, "at least three points has to defined" unless points.is_a?(Array) && points.size >= 3
+ parameters[:point] = points.map{ |point| "\"%+g%+g\"^^%s" % [ point[0], point[1], type ] }
+
+ @resource.request :put, self.path + "/polygon", :parameters => parameters, :expected_status_code => 204
+ true
+ end
+
+ end
+
+ end
+
+end
View
41 lib/allegro_graph/proxy/statements.rb
@@ -1,3 +1,4 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "utility", "parameter_mapper"))
module AllegroGraph
@@ -12,16 +13,27 @@ def initialize(resource)
end
def path
- "#{@resource.path}/statements"
+ @resource.path
end
def create(subject, predicate, object, context = nil)
statement = [ subject, predicate, object ]
statement << context if context
- @resource.request :post, self.path, :body => [ statement ], :expected_status_code => 204
+ @resource.request :post, self.path + "/statements", :body => [ statement ], :expected_status_code => 204
true
end
+ def delete(options = { })
+ parameters = { }
+
+ { :subject => :subj, :predicate => :pred, :object => :obj, :context => :context }.each do |option_key, parameter_key|
+ value = options[option_key]
+ parameters.merge! parameter_key => value if value
+ end
+
+ @resource.request :delete, self.path + "/statements", :parameters => parameters, :expected_status_code => 200
+ end
+
def find(options = { })
parameters = { }
@@ -38,18 +50,27 @@ def find(options = { })
parameters = nil if parameters.empty?
- @resource.request :get, self.path, :parameters => parameters, :expected_status_code => 200
+ @resource.request :get, self.path + "/statements", :parameters => parameters, :expected_status_code => 200
end
- def delete(options = { })
- parameters = { }
+ def find_inside_box(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_box
+ @resource.request :get, self.path + "/geo/box", :parameters => parameters, :expected_status_code => 200
+ end
- { :subject => :subj, :predicate => :pred, :object => :obj, :context => :context }.each do |option_key, parameter_key|
- value = options[option_key]
- parameters.merge! parameter_key => value if value
- end
+ def find_inside_circle(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_circle
+ @resource.request :get, self.path + "/geo/circle", :parameters => parameters, :expected_status_code => 200
+ end
+
+ def find_inside_haversine(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_haversine
+ @resource.request :get, self.path + "/geo/haversine", :parameters => parameters, :expected_status_code => 200
+ end
- @resource.request :delete, self.path, :parameters => parameters, :expected_status_code => 200
+ def find_inside_polygon(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_polygon
+ @resource.request :get, self.path + "/geo/polygon", :parameters => parameters, :expected_status_code => 200
end
end
View
6 lib/allegro_graph/repository.rb
@@ -2,7 +2,7 @@
require File.join(File.dirname(__FILE__), "session")
require File.join(File.dirname(__FILE__), "proxy", "statements")
require File.join(File.dirname(__FILE__), "proxy", "query")
-require File.join(File.dirname(__FILE__), "proxy", "geo")
+require File.join(File.dirname(__FILE__), "proxy", "geometric")
require File.join(File.dirname(__FILE__), "proxy", "mapping")
module AllegroGraph
@@ -15,7 +15,7 @@ class Repository
attr_reader :statements
attr_reader :query
- attr_reader :geo
+ attr_reader :geometric
attr_reader :mapping
def initialize(server_or_catalog, name, options = { })
@@ -24,7 +24,7 @@ def initialize(server_or_catalog, name, options = { })
@name = name
@statements = Proxy::Statements.new self
@query = Proxy::Query.new self
- @geo = Proxy::Geo.new self
+ @geometric = Proxy::Geometric.new self
@mapping = Proxy::Mapping.new self
end
View
6 lib/allegro_graph/session.rb
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), "transport")
require File.join(File.dirname(__FILE__), "proxy", "statements")
require File.join(File.dirname(__FILE__), "proxy", "query")
-require File.join(File.dirname(__FILE__), "proxy", "geo")
+require File.join(File.dirname(__FILE__), "proxy", "geometric")
require File.join(File.dirname(__FILE__), "proxy", "mapping")
module AllegroGraph
@@ -14,7 +14,7 @@ class Session
attr_reader :statements
attr_reader :query
- attr_reader :geo
+ attr_reader :geometric
attr_reader :mapping
def initialize(options = { })
@@ -24,7 +24,7 @@ def initialize(options = { })
@statements = Proxy::Statements.new self
@query = Proxy::Query.new self
- @geo = Proxy::Geo.new self
+ @geometric = Proxy::Geometric.new self
@mapping = Proxy::Mapping.new self
end
View
77 lib/allegro_graph/utility/parameter_mapper.rb
@@ -0,0 +1,77 @@
+
+module AllegroGraph
+
+ module Utility
+
+ module ParameterMapper
+
+ MAP = {
+ :strip_width => { :to => :stripWidth, :for => [ :cartesian_type, :spherical_type ], :default => 1 },
+ :x_min => { :to => :xmin, :for => [ :cartesian_type, :find_inside_box ] },
+ :y_min => { :to => :ymin, :for => [ :cartesian_type, :find_inside_box ] },
+ :x_max => { :to => :xmax, :for => [ :cartesian_type, :find_inside_box ] },
+ :y_max => { :to => :ymax, :for => [ :cartesian_type, :find_inside_box ] },
+ :unit => { :to => :unit, :for => [ :spherical_type, :find_inside_haversine ], :default => :degree },
+ :latitude_min => { :to => :latmin, :for => [ :spherical_type ] },
+ :longitude_min => { :to => :longmin, :for => [ :spherical_type ] },
+ :latitude_max => { :to => :latmax, :for => [ :spherical_type ] },
+ :longitude_max => { :to => :longmax, :for => [ :spherical_type ] },
+ :name => { :to => :resource, :for => [ :create_polygon ], :modifier => [ :quote ] },
+ :type => { :to => :type, :for => [ :find_inside_box, :find_inside_circle, :find_inside_haversine, :find_inside_polygon ] },
+ :predicate => { :to => :predicate, :for => [ :find_inside_box, :find_inside_circle, :find_inside_haversine, :find_inside_polygon ] },
+ :x => { :to => :x, :for => [ :find_inside_circle ] },
+ :y => { :to => :y, :for => [ :find_inside_circle ] },
+ :radius => { :to => :radius, :for => [ :find_inside_circle, :find_inside_haversine ] },
+ :latitude => { :to => :lat, :for => [ :find_inside_haversine ], :modifier => :latitude_to_iso },
+ :longitude => { :to => :long, :for => [ :find_inside_haversine ], :modifier => :longitude_to_iso },
+ :polygon_name => { :to => :polygon, :for => [ :find_inside_polygon ], :modifier => [ :quote ] }
+ }.freeze unless defined?(MAP)
+
+ def self.map(parameters, method_name)
+ result = { }
+ MAP.each do |key, mapping|
+ value = parameters[key] || mapping[:default]
+ required = [ mapping[:for] ].flatten.include? method_name.to_sym
+ if required
+ if value
+ [ mapping[:modifier] ].flatten.compact.each do |modifier|
+ value = send modifier, value
+ end
+ result[ mapping[:to] ] = value.to_s
+ else
+ raise ArgumentError, "missing parameter :#{key}!"
+ end
+ end
+ end
+ result
+ end
+
+ def self.quote(value)
+ "\"#{value}\""
+ end
+
+ def self.latitude_to_iso(value)
+ float_to_iso value, 2
+ end
+
+ def self.longitude_to_iso(value)
+ float_to_iso value, 3
+ end
+
+ private
+
+ def self.float_to_iso(value, digits)
+ sign = "+"
+ if value < 0
+ sign = "-"
+ value = -value
+ end
+ floor = value.to_i
+ sign + (("%%0%dd" % digits) % floor) + (".%07d" % ((value - floor) * 10000000))
+ end
+
+ end
+
+ end
+
+end
View
79 spec/integration/basic_spec.rb
@@ -166,22 +166,31 @@
end
- describe "geo" do
+ describe "geo-spatial data" do
before :each do
@repository.create_if_missing!
- @geo = @repository.geo
+ @geometric = @repository.geometric
+ @statements = @repository.statements
end
describe "types" do
it "should provide a cartesian type" do
- result = @geo.cartesian_type 1.0, 2.0, 2.0, 20.0, 20.0
+ result = @geometric.cartesian_type :strip_width => 1.0,
+ :x_min => 2.0,
+ :y_min => 2.0,
+ :x_max => 20.0,
+ :y_max => 20.0
result.should == "<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"
end
it "should provide a spherical type" do
- result = @geo.spherical_type 1.0, :degree, 2.0, 2.0, 20.0, 20.0
+ result = @geometric.spherical_type :strip_width => 1.0,
+ :latitude_min => 2.0,
+ :longitude_min => 2.0,
+ :latitude_max => 20.0,
+ :longitude_max => 20.0
result.should == "<http://franz.com/ns/allegrograph/3.0/geospatial/spherical/degrees/2.0/20.0/2.0/20.0/1.0>"
end
@@ -190,12 +199,16 @@
describe "creating polygon" do
before :each do
- @type = @geo.cartesian_type 1.0, 2.0, 2.0, 20.0, 20.0
+ @type = @geometric.cartesian_type :strip_width => 1.0,
+ :x_min => 2.0,
+ :y_min => 2.0,
+ :x_max => 20.0,
+ :y_max => 20.0
@polygon = [ [ 2.0, 2.0 ], [ 11.0, 2.0 ], [ 11.0, 11.0 ], [ 2.0, 11.0 ] ]
end
it "should return true" do
- result = @geo.create_polygon "test_polygon", @type, @polygon
+ result = @geometric.create_polygon @polygon, :name => "test_polygon", :type => @type
result.should be_true
end
@@ -204,19 +217,32 @@
context "in a cartesian system" do
before :each do
- @type = @geo.cartesian_type 1.0, 2.0, 2.0, 20.0, 20.0
- @repository.statements.create "\"test_subject\"", "\"at\"", "\"+10+10\"^^#{@type}"
- @repository.statements.create "\"another_subject\"", "\"at\"", "\"+15+15\"^^#{@type}"
+ @type = @geometric.cartesian_type :strip_width => 1.0,
+ :x_min => 2.0,
+ :y_min => 2.0,
+ :x_max => 20.0,
+ :y_max => 20.0
+ @statements.create "\"test_subject\"", "\"at\"", "\"+10+10\"^^#{@type}"
+ @statements.create "\"another_subject\"", "\"at\"", "\"+15+15\"^^#{@type}"
end
it "should find objects inside a box" do
- result = @geo.inside_box @type, "\"at\"", 8.0, 8.0, 11.0, 11.0
+ result = @statements.find_inside_box :type => @type,
+ :predicate => "\"at\"",
+ :x_min => 8.0,
+ :y_min => 8.0,
+ :x_max => 11.0,
+ :y_max => 11.0
result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^#{@type}"])
result.should_not include([ "\"another_subject\"", "\"at\"", "\"+15.000000000465661+15.000000000465661\"^^#{@type}"])
end
it "should find objects inside a circle" do
- result = @geo.inside_circle @type, "\"at\"", 9.0, 9.0, 2.0
+ result = @statements.find_inside_circle :type => @type,
+ :predicate => "\"at\"",
+ :x => 9.0,
+ :y => 9.0,
+ :radius => 2.0
result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^#{@type}"])
result.should_not include([ "\"another_subject\"", "\"at\"", "\"+15.000000000465661+15.000000000465661\"^^#{@type}"])
end
@@ -224,14 +250,22 @@
context "with a defined polygon" do
before :each do
- @type = @geo.cartesian_type 1, -100, -100, 100, 100
- @repository.statements.create "\"test_subject\"", "\"at\"", "\"+1+1\"^^#{@type}"
- @geo.create_polygon "test_polygon", @type, [ [ 0, -100 ], [ 0, 100 ], [ 100, 100 ], [ 100, -100 ] ]
+ @type = @geometric.cartesian_type :strip_width => 1,
+ :x_min => -100,
+ :y_min => -100,
+ :x_max => 100,
+ :y_max => 100
+ @statements.create "\"test_subject\"", "\"at\"", "\"+1+1\"^^#{@type}"
+ @geometric.create_polygon [ [ 0, -100 ], [ 0, 100 ], [ 100, 100 ], [ 100, -100 ] ],
+ :name => "test_polygon",
+ :type => @type
end
it "should find objects inside that polygon" do
pending
- result = @geo.inside_polygon @type, "\"at\"", "test_polygon"
+ result = @statements.find_inside_polygon :type => @type,
+ :predicate => "\"at\"",
+ :polygon_name => "test_polygon"
result.should include([ "\"test_subject\"", "\"at\"", "\"+1+1\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"])
end
@@ -242,12 +276,21 @@
context "in a spherical system" do
before :each do
- @type = @geo.spherical_type 1.0, :degree, 2.0, 2.0, 20.0, 20.0
- @repository.statements.create "\"test_subject\"", "\"at\"", "\"+10.00+010.00\"^^#{@type}"
+ @type = @geometric.spherical_type :strip_width => 1.0,
+ :latitude_min => 2.0,
+ :longitude_min => 2.0,
+ :latitude_max => 20.0,
+ :longitude_max => 20.0
+ @statements.create "\"test_subject\"", "\"at\"", "\"+10.00+010.00\"^^#{@type}"
end
it "should find objects inside a haversine" do
- result = @geo.inside_haversine @type, "\"at\"", 9.0, 9.0, 200.0, :km
+ result = @statements.find_inside_haversine :type => @type,
+ :predicate => "\"at\"",
+ :latitude => 9.0,
+ :longitude => 9.0,
+ :radius => 200.0,
+ :unit => :km
result.should include([ "\"test_subject\"", "\"at\"", "\"+100000+0100000\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/spherical/degrees/2.0/20.0/2.0/20.0/1.0>"])
end
View
89 spec/lib/allegro_graph/proxy/geo_spec.rb
@@ -1,89 +0,0 @@
-require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
-require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "allegro_graph", "proxy", "geo"))
-
-describe AllegroGraph::Proxy::Geo do
-
- before :each do
- @server = AllegroGraph::Server.new :username => "test", :password => "test"
- @catalog = AllegroGraph::Catalog.new @server, "test_catalog"
- @repository = AllegroGraph::Repository.new @catalog, "test_repository"
- @geo = AllegroGraph::Proxy::Geo.new @repository
- @type = @geo.cartesian_type 1.0, 2.0, 2.0, 20.0, 20.0
- end
-
- describe "path" do
-
- it "should return the correct path" do
- @geo.path.should == "#{@repository.path}/geo"
- end
-
- end
-
- describe "cartesian_type" do
-
- it "should provide a cartesian type" do
- result = @geo.cartesian_type 1.0, 2.0, 2.0, 20.0, 20.0
- result.should == "<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"
- end
-
- end
-
- describe "spherical_type" do
-
- it "should provide a spherical type" do
- result = @geo.spherical_type 1.0, :degree, 2.0, 2.0, 20.0, 20.0
- result.should == "<http://franz.com/ns/allegrograph/3.0/geospatial/spherical/degrees/2.0/20.0/2.0/20.0/1.0>"
- end
-
- end
-
- describe "create_polygon" do
-
- before :each do
- @polygon = [ [ 2.0, 2.0 ], [ 10.0, 2.0 ], [ 10.0, 10.0 ], [ 2.0, 10.0 ] ]
- end
-
- it "should create a polygon" do
- result = @geo.create_polygon "test_polygon", @type, @polygon
- result.should be_true
- end
-
- end
-
- describe "inside_box" do
-
- it "should find objects inside a box" do
- result = @geo.inside_box @type, "\"at\"", 8.0, 8.0, 11.0, 11.0
- result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"])
- end
-
- end
-
- describe "inside_circle" do
-
- it "should find objects inside a circle" do
- result = @geo.inside_circle @type, "\"at\"", 9.0, 9.0, 2.0
- result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"])
- end
-
- end
-
- describe "inside_haversine" do
-
- it "should find objects inside a haversine" do
- result = @geo.inside_haversine @type, "\"at\"", 9.0, 9.0, 200.0, :km
- result.should include([ "\"test_subject\"", "\"at\"", "\"+100000+0100000\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/spherical/degrees/2.0/20.0/2.0/20.0/1.0>"])
- end
-
- end
-
- describe "inside_polygon" do
-
- it "should find objects inside a polygon" do
- result = @geo.inside_polygon @type, "\"at\"", "test_polygon"
- result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"])
- end
-
- end
-
-end
View
65 spec/lib/allegro_graph/proxy/geometric_spec.rb
@@ -0,0 +1,65 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "allegro_graph", "proxy", "geometric"))
+
+describe AllegroGraph::Proxy::Geometric do
+
+ before :each do
+ @server = AllegroGraph::Server.new :username => "test", :password => "test"
+ @catalog = AllegroGraph::Catalog.new @server, "test_catalog"
+ @repository = AllegroGraph::Repository.new @catalog, "test_repository"
+ @geometric = AllegroGraph::Proxy::Geometric.new @repository
+ @type = @geometric.cartesian_type :strip_width => 1.0,
+ :x_min => 2.0,
+ :y_min => 2.0,
+ :x_max => 20.0,
+ :y_max => 20.0
+ end
+
+ describe "path" do
+
+ it "should return the correct path" do
+ @geometric.path.should == "#{@repository.path}/geo"
+ end
+
+ end
+
+ describe "cartesian_type" do
+
+ it "should provide a cartesian type" do
+ result = @geometric.cartesian_type :strip_width => 1.0,
+ :x_min => 2.0,
+ :y_min => 2.0,
+ :x_max => 20.0,
+ :y_max => 20.0
+ result.should == "<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"
+ end
+
+ end
+
+ describe "spherical_type" do
+
+ it "should provide a spherical type" do
+ result = @geometric.spherical_type :strip_width => 1.0,
+ :latitude_min => 2.0,
+ :longitude_min => 2.0,
+ :latitude_max => 20.0,
+ :longitude_max => 20.0
+ result.should == "<http://franz.com/ns/allegrograph/3.0/geospatial/spherical/degrees/2.0/20.0/2.0/20.0/1.0>"
+ end
+
+ end
+
+ describe "create_polygon" do
+
+ before :each do
+ @polygon = [ [ 2.0, 2.0 ], [ 10.0, 2.0 ], [ 10.0, 10.0 ], [ 2.0, 10.0 ] ]
+ end
+
+ it "should create a polygon" do
+ result = @geometric.create_polygon @polygon, :name => "test_polygon", :type => @type
+ result.should be_true
+ end
+
+ end
+
+end
View
2 spec/lib/allegro_graph/proxy/mapping_spec.rb
@@ -1,7 +1,7 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "allegro_graph", "proxy", "mapping"))
-describe AllegroGraph::Proxy::Geo do
+describe AllegroGraph::Proxy::Geometric do
before :each do
@server = AllegroGraph::Server.new :username => "test", :password => "test"
View
67 spec/lib/allegro_graph/proxy/statements_spec.rb
@@ -8,12 +8,17 @@
@catalog = AllegroGraph::Catalog.new @server, "test_catalog"
@repository = AllegroGraph::Repository.new @catalog, "test_repository"
@statements = AllegroGraph::Proxy::Statements.new @repository
+ @type = @repository.geometric.cartesian_type :strip_width => 1.0,
+ :x_min => 2.0,
+ :y_min => 2.0,
+ :x_max => 20.0,
+ :y_max => 20.0
end
describe "path" do
it "should return the correct path" do
- @statements.path.should == "#{@repository.path}/statements"
+ @statements.path.should == @repository.path
end
end
@@ -27,6 +32,15 @@
end
+ describe "delete" do
+
+ it "should delete all statements" do
+ result = @statements.delete :subject => "test_subject"
+ result.should be_true
+ end
+
+ end
+
describe "find" do
it "should find all statements" do
@@ -46,11 +60,54 @@
end
- describe "delete" do
+ describe "find_inside_box" do
- it "should delete all statements" do
- result = @statements.delete :subject => "test_subject"
- result.should be_true
+ it "should find objects inside a box" do
+ result = @statements.find_inside_box :type => @type,
+ :predicate => "\"at\"",
+ :x_min => 8.0,
+ :y_min => 8.0,
+ :x_max => 11.0,
+ :y_max => 11.0
+ result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>" ])
+ end
+
+ end
+
+ describe "find_inside_circle" do
+
+ it "should find objects inside a circle" do
+ result = @statements.find_inside_circle :type => @type,
+ :predicate => "\"at\"",
+ :x => 9.0,
+ :y => 9.0,
+ :radius => 2.0
+ result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>" ])
+ end
+
+ end
+
+ describe "find_inside_haversine" do
+
+ it "should find objects inside a haversine" do
+ result = @statements.find_inside_haversine :type => @type,
+ :predicate => "\"at\"",
+ :latitude => 9.0,
+ :longitude => 9.0,
+ :radius => 200.0,
+ :unit => :km
+ result.should include([ "\"test_subject\"", "\"at\"", "\"+100000+0100000\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/spherical/degrees/2.0/20.0/2.0/20.0/1.0>"])
+ end
+
+ end
+
+ describe "find_inside_polygon" do
+
+ it "should find objects inside a polygon" do
+ result = @statements.find_inside_polygon :type => @type,
+ :predicate => "\"at\"",
+ :polygon_name => "test_polygon"
+ result.should include([ "\"test_subject\"", "\"at\"", "\"+10.000000000931323+10.000000000931323\"^^<http://franz.com/ns/allegrograph/3.0/geospatial/cartesian/2.0/20.0/2.0/20.0/1.0>"])
end
end
View
56 spec/lib/allegro_graph/utility/parameter_mapper_spec.rb
@@ -0,0 +1,56 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "allegro_graph", "utility", "parameter_mapper"))
+
+describe AllegroGraph::Utility::ParameterMapper do
+
+ before :each do
+ @parameters = {
+ :strip_width => 10,
+ :latitude_min => 2.0,
+ :longitude_min => 2.0,
+ :latitude_max => 20.0,
+ :longitude_max => 20.0,
+ :x_min => 2,
+ :y_min => 2,
+ :x_max => 20,
+ :y_max => 20,
+ :name => "test"
+ }
+ end
+
+ describe "map" do
+
+ def do_map(method_name = :spherical_type)
+ AllegroGraph::Utility::ParameterMapper.map @parameters, method_name
+ end
+
+ it "should return the mapped parameters" do
+ do_map.should == {
+ :stripWidth => "10",
+ :unit => "degree",
+ :latmin => "2.0",
+ :longmin => "2.0",
+ :latmax => "20.0",
+ :longmax => "20.0"
+ }
+ end
+
+ it "should map only the right parameters" do
+ do_map(:cartesian_type).should == {
+ :stripWidth => "10",
+ :xmin => "2",
+ :ymin => "2",
+ :xmax => "20",
+ :ymax => "20"
+ }
+ end
+
+ it "should modify the parameter values" do
+ do_map(:create_polygon).should == {
+ :resource => "\"test\""
+ }
+ end
+
+ end
+
+end
View
19 test.rb
@@ -0,0 +1,19 @@
+require 'lib/agraph'
+
+server = AllegroGraph::Server.new :username => "test", :password => "test"
+
+catalog = AllegroGraph::Catalog.new server, "tests"
+repository = AllegroGraph::Repository.new catalog, "foo"
+
+# repository = AllegroGraph::Repository.new server, "test_repository"
+
+type = repository.geo.cartesian_type 1, -100, -100, 100, 100
+
+repository.statements.create "\"phil\"", "\"at\"", "\"+1+1\"^^#{type}"
+#repository.statements.create "\"bernd\"", "\"at\"", "\"+5+80\"^^#{type}"
+#repository.statements.create "\"manni\"", "\"at\"", "\"+30+10\"^^#{type}"
+#repository.statements.create "\"anna\"", "\"at\"", "\"+35+50\"^^#{type}"
+
+repository.geo.create_polygon "right", type, [ [ 0, -100 ], [ 0, 100 ], [ 100, 100 ], [ 100, -100 ] ]
+
+puts repository.geo.inside_polygon(type, "\"at\"", "right").inspect

0 comments on commit 9fe1690

Please sign in to comment.
Something went wrong with that request. Please try again.