amigo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ === 0.1.0 / 2010-05-14
2
+
3
+ * Initial version
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Simon Harris
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ = Amigo - In-memory triple store
2
+
3
+ GitHub: http://github.com/harukizaemon/amigo
4
+ Gemcutter: http://gemcutter/gems/amigo
5
+ email: haruki.zaemon@gmail.com
6
+ IRC: #haruki_zaemon on freenode
7
+
8
+ == Introduction
9
+
10
+ == Installation
11
+
12
+ Amigo is distributed as a gem via gemcutter (http://gemcutter.org/gems/amigo) or as source via GitHub (http://github.com/harukizaemon/amigo).
13
+
14
+ Installation via the gem is easy:
15
+
16
+ > gem install amigo
17
+
18
+ Once installed, all that remains is to make the classes available in your code:
19
+
20
+ require 'amigo'
@@ -0,0 +1,5 @@
1
+ Dir[File.expand_path("tasks/**/*.rb")].each do |task_file|
2
+ require task_file
3
+ end
4
+
5
+ task :default => [ :spec ]
@@ -0,0 +1,4 @@
1
+ require 'amigo/triple'
2
+ require 'amigo/store'
3
+ require 'amigo/select'
4
+ require 'amigo/version'
@@ -0,0 +1 @@
1
+ require 'amigo/core_ext/object'
@@ -0,0 +1,21 @@
1
+ module Amigo
2
+
3
+ module CoreExt
4
+
5
+ module Object
6
+
7
+ def to_ntriple
8
+ inspect
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+
17
+ class Object
18
+
19
+ include Amigo::CoreExt::Object
20
+
21
+ end
@@ -0,0 +1,31 @@
1
+ require 'hamster/immutable'
2
+ require 'hamster/set'
3
+ require 'hamster/hash'
4
+
5
+ require 'amigo/solution'
6
+ require 'amigo/join_result'
7
+
8
+ module Amigo
9
+
10
+ class InitialResult
11
+
12
+ include Hamster::Immutable
13
+
14
+ SOLUTIONS = Hamster.set(Solution.new(Hamster.set, Hamster.hash))
15
+
16
+ def initialize(variables, triples)
17
+ @variables = variables
18
+ @triples = triples
19
+ end
20
+
21
+ def add(where)
22
+ JoinResult.new(@variables, @triples, where.select(@triples))
23
+ end
24
+
25
+ def solutions
26
+ SOLUTIONS
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,27 @@
1
+ require 'hamster/immutable'
2
+
3
+ require 'amigo/solution'
4
+
5
+ module Amigo
6
+
7
+ class JoinResult
8
+
9
+ include Hamster::Immutable
10
+
11
+ def initialize(variables, triples, rows)
12
+ @variables = variables
13
+ @triples = triples
14
+ @rows = rows
15
+ end
16
+
17
+ def add(where)
18
+ transform { @rows = where.join(@triples, @rows) }
19
+ end
20
+
21
+ def solutions
22
+ @rows.map { |row| Solution.new(@variables, row) }
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,55 @@
1
+ require 'hamster/immutable'
2
+ require 'hamster/list'
3
+
4
+ require 'amigo/variables'
5
+ require 'amigo/where'
6
+ require 'amigo/initial_result'
7
+ require 'amigo/join_result'
8
+ require 'amigo/solution'
9
+
10
+ module Amigo
11
+
12
+ class Select
13
+
14
+ include Hamster::Immutable
15
+
16
+ def initialize(first_variable, *other_variables)
17
+ @variables = Variables.new(first_variable, *other_variables)
18
+ @where_clauses = Hamster.list
19
+ end
20
+
21
+ def where(subject, predicate, object)
22
+ transform {
23
+ @where_clauses = @where_clauses.cons(Where.new(subject, predicate, object))
24
+ }
25
+ end
26
+
27
+ def from(store)
28
+ store.execute(self)
29
+ end
30
+
31
+ def execute(triples)
32
+ @where_clauses.
33
+ reduce(InitialResult.new(@variables, triples), &:add).
34
+ solutions
35
+ end
36
+
37
+ def to_sparql
38
+ "SELECT #{@variables.to_sparql} WHERE {#{@where_clauses.reverse.map(&:to_sparql).join(" ")}}"
39
+ end
40
+
41
+ def eql?(other)
42
+ return true if other.equal?(self)
43
+ other.class.equal?(self.class) &&
44
+ @variables.eql?(other.instance_variable_get(:@variables)) &&
45
+ @where_clauses.eql?(other.instance_variable_get(:@where_clauses))
46
+ end
47
+ alias_method :==, :eql?
48
+
49
+ def hash
50
+ @variables.hash ^ @where_clauses.hash
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,44 @@
1
+ require 'hamster/immutable'
2
+
3
+ module Amigo
4
+
5
+ class Solution
6
+
7
+ include Hamster::Immutable
8
+
9
+ def initialize(variables, row)
10
+ @variables = variables
11
+ @row = row
12
+ end
13
+
14
+ def include?(name)
15
+ @variables.include?(name)
16
+ end
17
+
18
+ def [](name)
19
+ return @row[name] if include?(name)
20
+ raise NameError, "#{name} not found"
21
+ end
22
+
23
+ def eql?(other)
24
+ return true if other.equal?(self)
25
+ other.class.equal?(self.class) &&
26
+ @variables.eql?(other.instance_variable_get(:@variables)) &&
27
+ @row.eql?(other.instance_variable_get(:@row))
28
+ end
29
+ alias_method :==, :eql?
30
+
31
+ def hash
32
+ @variables.hash ^ @row.hash
33
+ end
34
+
35
+ private
36
+
37
+ def method_missing(name, *args, &block)
38
+ return self[name] if args.empty? && !block_given?
39
+ super
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,44 @@
1
+ require 'hamster/immutable'
2
+ require 'hamster/set'
3
+ require 'amigo/triple'
4
+
5
+ module Amigo
6
+
7
+ def self.store
8
+ EmptyStore
9
+ end
10
+
11
+ class Store
12
+
13
+ include Hamster::Immutable
14
+
15
+ def initialize
16
+ @triples = Hamster.set
17
+ end
18
+
19
+ def add(subject, predicate, object)
20
+ triple = Triple.new(subject, predicate, object)
21
+ return self if @triples.include?(triple)
22
+ transform { @triples = @triples.add(triple) }
23
+ end
24
+
25
+ def execute(query)
26
+ query.execute(@triples)
27
+ end
28
+
29
+ def eql?(other)
30
+ return true if other.equal?(self)
31
+ other.class.equal?(self.class) &&
32
+ @triples.eql?(other.instance_variable_get(:@triples))
33
+ end
34
+ alias_method :==,:eql?
35
+
36
+ def hash
37
+ @triples.hash
38
+ end
39
+
40
+ end
41
+
42
+ EmptyStore = Store.new
43
+
44
+ end
@@ -0,0 +1,44 @@
1
+ require 'hamster/immutable'
2
+
3
+ module Amigo
4
+
5
+ class Term
6
+
7
+ include Hamster::Immutable
8
+
9
+ def initialize(component, value)
10
+ @component = component
11
+ @value = value
12
+ end
13
+
14
+ def select?(statement)
15
+ @value === statement.send(@component)
16
+ end
17
+
18
+ def bind(statement, row)
19
+ row
20
+ end
21
+
22
+ def join?(statement, row)
23
+ true
24
+ end
25
+
26
+ def to_sparql
27
+ @value.inspect
28
+ end
29
+
30
+ def eql?(other)
31
+ return true if other.equal?(self)
32
+ other.class.equal?(self.class) &&
33
+ @component.eql?(other.instance_variable_get(:@component)) &&
34
+ @value.eql?(other.instance_variable_get(:@value))
35
+ end
36
+ alias_method :==, :eql?
37
+
38
+ def hash
39
+ @component.hash ^ @value.hash
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,45 @@
1
+ require 'hamster/immutable'
2
+ require 'amigo/core_ext'
3
+
4
+ module Amigo
5
+
6
+ class Triple
7
+
8
+ include Hamster::Immutable
9
+
10
+ attr_reader :subject, :predicate, :object
11
+
12
+ def initialize(subject, predicate, object)
13
+ @subject = subject
14
+ @predicate = predicate
15
+ @object = object
16
+ end
17
+
18
+ def to_ntriple
19
+ "#{[@subject, @predicate, @object].map(&:to_ntriple).join(" ")} ."
20
+ end
21
+
22
+ def inspect
23
+ to_ntriple
24
+ end
25
+
26
+ def eql?(other)
27
+ return true if other.equal?(self)
28
+ return other.class.equal?(self.class) &&
29
+ other.subject == self.subject &&
30
+ other.predicate == self.predicate &&
31
+ other.object == self.object
32
+ end
33
+ alias_method :==, :eql?
34
+
35
+ def hash
36
+ subject.hash ^ predicate.hash ^ object.hash
37
+ end
38
+
39
+ def <=>(other)
40
+ [subject, predicate, object] <=> [other.subject, other.predicate, other.object]
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,38 @@
1
+ require 'hamster/immutable'
2
+
3
+ module Amigo
4
+
5
+ class URI
6
+
7
+ include Hamster::Immutable
8
+
9
+ class << self
10
+ alias_method :parse, :new
11
+ end
12
+
13
+ def initialize(uri)
14
+ @uri = uri
15
+ end
16
+
17
+ def to_sparql
18
+ to_ntriple
19
+ end
20
+
21
+ def to_ntriple
22
+ "<#{@uri}>"
23
+ end
24
+
25
+ def eql?(other)
26
+ return true if other.equal?(self)
27
+ other.class.equal?(self.class) &&
28
+ @uri.eql?(other.instance_variable_get(:@uri))
29
+ end
30
+ alias_method :==, :eql?
31
+
32
+ def hash
33
+ @uri.hash
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,45 @@
1
+ require 'hamster/immutable'
2
+
3
+ module Amigo
4
+
5
+ class Variable
6
+
7
+ include Hamster::Immutable
8
+
9
+ def initialize(component, name)
10
+ @component = component
11
+ @name = name
12
+ end
13
+
14
+ def select?(statement)
15
+ true
16
+ end
17
+
18
+ def bind(statement, row)
19
+ row.put(@name, statement.send(@component))
20
+ end
21
+
22
+ def join?(statement, row)
23
+ return true unless row.has_key?(@name)
24
+ statement.send(@component) == row[@name]
25
+ end
26
+
27
+ def to_sparql
28
+ "?#{@name}"
29
+ end
30
+
31
+ def eql?(other)
32
+ return true if other.equal?(self)
33
+ other.class.equal?(self.class) &&
34
+ @component.eql?(other.instance_variable_get(:@component)) &&
35
+ @name.eql?(other.instance_variable_get(:@name))
36
+ end
37
+ alias_method :==, :eql?
38
+
39
+ def hash
40
+ @component.hash ^ @name.hash
41
+ end
42
+
43
+ end
44
+
45
+ end