case_model 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ module Model
2
+ class Number
3
+ attr_reader :query
4
+
5
+ include Answer
6
+
7
+ def initialize(node, repository)
8
+ @query = NodeQuery.new(node, repository)
9
+ end
10
+
11
+ def value
12
+ query.value(LV.value)
13
+ end
14
+
15
+ def value=(value)
16
+ query.set_value(LV.value, value)
17
+ end
18
+
19
+ def min_value
20
+ question.query.value(LV.minValue)
21
+ end
22
+
23
+ def max_value
24
+ question.query.value(LV.maxValue)
25
+ end
26
+ end
27
+ end
@@ -3,50 +3,71 @@ require 'rdf/vocab'
3
3
 
4
4
  require_relative '../services/repository_proxy'
5
5
 
6
- class Ontology
7
- attr_reader :repository
6
+ module Model
7
+ class Ontology
8
+ attr_reader :repository
8
9
 
9
- include RepositoryProxy
10
+ include RepositoryProxy
10
11
 
11
- def self.from_n3(n3_input)
12
- Ontology.new(RDF::Repository.new, n3_input)
13
- end
12
+ def self.from_n3(n3_input)
13
+ Ontology.new(RDF::Repository.new, n3_input)
14
+ end
14
15
 
15
- def initialize(repository, n3_input = nil)
16
- @repository = repository
17
- load_and_process_n3 n3_input if n3_input
18
- # initialize_state
19
- end
16
+ def initialize(repository, n3_input = nil)
17
+ @repository = repository
18
+ load_and_process_n3 n3_input if n3_input
19
+ # initialize_state
20
+ end
20
21
 
21
- def load_and_process_n3(n3_input)
22
- (@n3_inputs ||= []) << n3_input
23
- refresh
24
- end
22
+ def load_and_process_n3(n3_input)
23
+ (@n3_inputs ||= []) << n3_input
24
+ refresh
25
+ end
25
26
 
26
- def applications
27
- query([nil, RDF.type, LV.Application]).map do |statement|
28
- Application.new(statement.subject, self)
27
+ def applications
28
+ query([nil, RDF.type, LV.Application]).
29
+ map(&:subject).uniq.map do |subject|
30
+ Application.new(subject, self)
31
+ end
29
32
  end
30
- end
31
33
 
32
- def devices
33
- query([nil, RDF.type, LV.Device]).map do |statement|
34
- Device.new(statement.subject, self)
34
+ def installations
35
+ query([nil, RDF.type, LV.Installation]).map do |statement|
36
+ Installation.new(statement.subject, self)
37
+ end
35
38
  end
36
- end
37
39
 
38
- def device_with_description(description)
39
- devices.find { |d| d.description == description }
40
- end
40
+ def devices
41
+ query([nil, RDF.type, LV.Device]).map do |statement|
42
+ Device.new(statement.subject, self)
43
+ end
44
+ end
41
45
 
42
- def refresh
43
- @n3_inputs.each { |input| Reasoner.new(self).load_and_process_n3(input) }
44
- end
46
+ def scenarios
47
+ query([nil, RDF.type, LV.Scenario]).map do |statement|
48
+ Scenario.new(statement.subject, self)
49
+ end
50
+ end
45
51
 
46
- private
52
+ def properties
53
+ query([nil, RDF.type, LV.Property]).map do |statement|
54
+ Property.new(statement.subject, self)
55
+ end
56
+ end
57
+
58
+ def device_with_description(description)
59
+ devices.find { |d| d.description == description }
60
+ end
47
61
 
48
- ## load an empty State formula in case there are some StateChange statements
49
- def initialize_state
50
- load_and_process_n3 '{ <http://example.com/x> <http://example.com/y> <http://example.com/z>.} a <http://purl.org/restdesc/states#State>.'
62
+ def refresh
63
+ @n3_inputs.each { |input| Reasoner.new(self).load_and_process_n3(input) }
64
+ end
65
+
66
+ private
67
+
68
+ ## load an empty State formula in case there are some StateChange statements
69
+ def initialize_state
70
+ load_and_process_n3 '{ <http://example.com/x> <http://example.com/y> <http://example.com/z>.} a <http://purl.org/restdesc/states#State>.'
71
+ end
51
72
  end
52
73
  end
@@ -0,0 +1,46 @@
1
+ module Model
2
+ class Property
3
+ attr_reader :query
4
+
5
+ def initialize(node, repository)
6
+ @query = NodeQuery.new(node, repository)
7
+ end
8
+
9
+ def uri
10
+ if query.value_exists?(LV.type)
11
+ query.value(LV.type).to_s
12
+ else
13
+ query.node_uri
14
+ end
15
+ end
16
+
17
+ def description
18
+ query.value(LV.description)
19
+ end
20
+
21
+ def unit
22
+ val = query.value(LV.unit)
23
+ val.to_s if val
24
+ end
25
+
26
+ def value
27
+ val = query.value(LV.value)
28
+ val.to_s if val
29
+ end
30
+
31
+ def maximum
32
+ val = query.value(LV.maximum)
33
+ val.to_s if val
34
+ end
35
+
36
+ def minimum
37
+ val = query.value(LV.minimum)
38
+ val.to_s if val
39
+ end
40
+
41
+ def device
42
+ device_node = query.value(LV.device)
43
+ Device.new device_node, query.repository if device_node
44
+ end
45
+ end
46
+ end
@@ -1,51 +1,56 @@
1
- class Question
2
- attr_reader :query
1
+ module Model
2
+ class Question
3
+ attr_reader :query
3
4
 
4
- def initialize(node, repository)
5
- @query = NodeQuery.new(node, repository)
6
- end
5
+ def initialize(node, repository)
6
+ @query = NodeQuery.new(node, repository)
7
+ end
7
8
 
8
- def text
9
- query.value(QV.text).to_s
10
- end
9
+ def uri
10
+ query.node_uri
11
+ end
11
12
 
12
- def reply_type
13
- QuestionReplyType.to_sym(query)
14
- end
13
+ def text
14
+ query.value(QV.text).to_s
15
+ end
15
16
 
16
- def has_answer?
17
- query.value_exists?(QV.has_answer)
18
- end
17
+ def reply_type
18
+ QuestionReplyType.to_sym(query)
19
+ end
19
20
 
20
- # returns an existing one or nil if there is no
21
- def answer
22
- node = query.value(QV.has_answer)
23
- return nil if node.nil?
21
+ def has_answer?
22
+ query.value_exists?(QV.has_answer)
23
+ end
24
24
 
25
- Answer.create(reply_type, node, query.repository)
26
- end
25
+ # returns an existing one or nil if there is no
26
+ def answer
27
+ node = query.value(QV.has_answer)
28
+ return nil if node.nil?
27
29
 
28
- # creates a new answer or returns an existing one
29
- def answer!
30
- answer || create_answer
31
- end
30
+ Answer.create(reply_type, node, query.repository)
31
+ end
32
32
 
33
- private
33
+ # creates a new answer or returns an existing one
34
+ def answer!
35
+ answer || create_answer
36
+ end
34
37
 
35
- def repository; query.repository; end
38
+ private
36
39
 
37
- def create_answer
38
- node = generate_answer_node
39
- repository << [ node, RDF.type, QV.answer_type ]
40
- answer_query = NodeQuery.new(node, repository)
41
- answer_query.set_value(QV.answers, query.node)
42
- query.set_value(QV.has_answer, node)
40
+ def repository; query.repository; end
43
41
 
44
- answer
45
- end
42
+ def create_answer
43
+ node = generate_answer_node
44
+ repository << [ node, RDF.type, QV.answer_type ]
45
+ repository << [ node, QV.answers, query.node ]
46
+ repository << [ query.node, QV.has_answer, node ]
47
+
48
+ answer
49
+ end
46
50
 
47
- def generate_answer_node
48
- question_uri_hash = query.node.to_s.hash.to_s
49
- ('answer' + question_uri_hash).to_sym
51
+ def generate_answer_node
52
+ question_uri_hash = query.node.to_s.hash.to_s
53
+ ('answer' + question_uri_hash).to_sym
54
+ end
50
55
  end
51
56
  end
@@ -1,14 +1,18 @@
1
- class QuestionReplyType
2
- def self.to_sym(query)
3
- case query.value(QV.replyType)
4
- when QV.Location
5
- :location
6
- when QV.Temperature
7
- :temperature
8
- when QV.Select
9
- :select
10
- else
11
- nil
1
+ module Model
2
+ class QuestionReplyType
3
+ def self.to_sym(query)
4
+ case query.value(QV.replyType)
5
+ when QV.Number
6
+ :number
7
+ when QV.Location
8
+ :location
9
+ when QV.Temperature
10
+ :temperature
11
+ when QV.Select
12
+ :select
13
+ else
14
+ nil
15
+ end
12
16
  end
13
17
  end
14
18
  end
@@ -1,17 +1,23 @@
1
1
  require_relative '../services/question_references'
2
2
 
3
- class Requirement
4
- attr_reader :query
3
+ module Model
4
+ class Requirement
5
+ attr_reader :query
5
6
 
6
- def initialize(node, repository)
7
- @query = NodeQuery.new(node, repository)
8
- end
7
+ def initialize(node, repository)
8
+ @query = NodeQuery.new(node, repository)
9
+ end
9
10
 
10
- def description
11
- query.value(LV.description)
12
- end
11
+ def uri
12
+ query.node_uri
13
+ end
14
+
15
+ def description
16
+ query.value(LV.description)
17
+ end
13
18
 
14
- def satisfied?
15
- query.value(LV.satisfied) == true
19
+ def satisfied?
20
+ query.value(LV.satisfied) == true
21
+ end
16
22
  end
17
23
  end
@@ -0,0 +1,30 @@
1
+ module Model
2
+ class Scenario
3
+ attr_reader :query
4
+
5
+ def initialize(node, repository)
6
+ @query = NodeQuery.new(node, repository)
7
+ end
8
+
9
+ def description
10
+ query.value(LV.description)
11
+ end
12
+
13
+ def value(key)
14
+ camelized_key = camelize(key.to_s)
15
+ query.value(LV.uri_for(camelized_key)).to_s
16
+ end
17
+
18
+ private
19
+
20
+ def camelize(str)
21
+ str.split('_').each_with_index.map do |part, i|
22
+ if i == 0
23
+ part
24
+ else
25
+ part.capitalize
26
+ end
27
+ end.join
28
+ end
29
+ end
30
+ end
@@ -1,32 +1,34 @@
1
- class Selection
2
- attr_reader :query
1
+ module Model
2
+ class Selection
3
+ attr_reader :query
3
4
 
4
- include Answer
5
+ include Answer
5
6
 
6
- def initialize(node, repository)
7
- @query = NodeQuery.new(node, repository)
8
- end
7
+ def initialize(node, repository)
8
+ @query = NodeQuery.new(node, repository)
9
+ end
9
10
 
10
- def options
11
- if question.query.value_exists?(QV.options)
12
- RDF::List.new(question.query.value(QV.options), query.repository)
13
- elsif question.query.value_exists?(QV.option)
14
- question.query.values(QV.option)
15
- else
16
- nil
11
+ def options
12
+ if question.query.value_exists?(QV.options)
13
+ RDF::List.new(question.query.value(QV.options), query.repository)
14
+ elsif question.query.value_exists?(QV.option)
15
+ question.query.values(QV.option)
16
+ else
17
+ nil
18
+ end
17
19
  end
18
- end
19
20
 
20
- def selected
21
- query.value(LV.selected)
22
- end
21
+ def selected
22
+ query.value(LV.selected)
23
+ end
23
24
 
24
- def selected=(value)
25
- # in case it is an array, store all the answers
26
- if value.is_a? Array
27
- value.each { |v| query.set_value(LV.selected, v, false) }
28
- else
29
- query.set_value(LV.selected, value)
25
+ def selected=(value)
26
+ # in case it is an array, store all the answers
27
+ if value.is_a? Array
28
+ value.each { |v| query.set_value(LV.selected, v, false) }
29
+ else
30
+ query.set_value(LV.selected, value)
31
+ end
30
32
  end
31
33
  end
32
34
  end
@@ -1,19 +1,21 @@
1
- class StateChange
2
- attr_reader :query
1
+ module Model
2
+ class StateChange
3
+ attr_reader :query
3
4
 
4
- def initialize(node, repository)
5
- @query = NodeQuery.new(node, repository)
6
- end
5
+ def initialize(node, repository)
6
+ @query = NodeQuery.new(node, repository)
7
+ end
7
8
 
8
- def apply
9
- new_repo = query.repository.clone
10
- new_repo.load_and_process_n3(state_rules)
11
- new_repo
12
- end
9
+ def apply
10
+ new_repo = query.repository.clone
11
+ new_repo.load_and_process_n3(state_rules)
12
+ new_repo
13
+ end
13
14
 
14
- private
15
+ private
15
16
 
16
- def state_rules
17
- File.read(File.dirname(__FILE__) + '/../descriptions/states.n3')
17
+ def state_rules
18
+ File.read(File.dirname(__FILE__) + '/../descriptions/states.n3')
19
+ end
18
20
  end
19
21
  end
@@ -1,17 +1,19 @@
1
- class Temperature
2
- attr_reader :query
1
+ module Model
2
+ class Temperature
3
+ attr_reader :query
3
4
 
4
- include Answer
5
+ include Answer
5
6
 
6
- def initialize(node, repository)
7
- @query = NodeQuery.new(node, repository)
8
- end
7
+ def initialize(node, repository)
8
+ @query = NodeQuery.new(node, repository)
9
+ end
9
10
 
10
- def in_celsius
11
- query.value(LV.in_celsius)
12
- end
11
+ def in_celsius
12
+ query.value(LV.in_celsius)
13
+ end
13
14
 
14
- def in_celsius=(value)
15
- query.set_value(LV.in_celsius, value)
15
+ def in_celsius=(value)
16
+ query.set_value(LV.in_celsius, value)
17
+ end
16
18
  end
17
19
  end
@@ -1,17 +1,19 @@
1
- class HttpVocabulary
2
- def self.method_missing(name, *arguments, &block)
3
- uri_for(name)
4
- end
1
+ module Model
2
+ class HttpVocabulary
3
+ def self.method_missing(name, *arguments, &block)
4
+ uri_for(name)
5
+ end
5
6
 
6
- def self.method_name
7
- uri_for('methodName')
8
- end
7
+ def self.method_name
8
+ uri_for('methodName')
9
+ end
9
10
 
10
- def self.request_uri
11
- uri_for('requestURI')
12
- end
11
+ def self.request_uri
12
+ uri_for('requestURI')
13
+ end
13
14
 
14
- def self.uri_for(name)
15
- RDF::URI("http://www.w3.org/2011/http##{name}")
15
+ def self.uri_for(name)
16
+ RDF::URI("http://www.w3.org/2011/http##{name}")
17
+ end
16
18
  end
17
19
  end
@@ -1,26 +1,32 @@
1
1
  require 'rdf'
2
2
 
3
- class LocalVocabulary
4
- def self.method_missing(name, *arguments, &block)
5
- uri_for(name)
6
- end
3
+ module Model
4
+ class LocalVocabulary
5
+ def self.method_missing(name, *arguments, &block)
6
+ uri_for(name)
7
+ end
7
8
 
8
- def self.name
9
- uri_for('name')
10
- end
9
+ def self.name
10
+ uri_for('name')
11
+ end
11
12
 
12
- def self.question_type
13
- uri_for('question')
14
- end
13
+ def self.question_type
14
+ uri_for('question')
15
+ end
15
16
 
16
- def self.answer_type
17
- uri_for('answer')
18
- end
17
+ def self.answer_type
18
+ uri_for('answer')
19
+ end
19
20
 
20
- def self.uri_for(name)
21
- RDF::URI("http://matus.tomlein.org/case/#{name}")
21
+ def self.uri_for(name)
22
+ RDF::URI("#{prefix}#{name}")
23
+ end
24
+
25
+ def self.prefix
26
+ 'http://matus.tomlein.org/case/'
27
+ end
22
28
  end
23
- end
24
29
 
25
- LV = LocalVocabulary
26
- QV = LocalVocabulary
30
+ LV = LocalVocabulary
31
+ QV = LocalVocabulary
32
+ end
@@ -1,15 +1,17 @@
1
- class MathVocabulary
2
- def self.method_missing(name, *arguments, &block)
3
- uri_for(name)
4
- end
1
+ module Model
2
+ class MathVocabulary
3
+ def self.method_missing(name, *arguments, &block)
4
+ uri_for(name)
5
+ end
5
6
 
6
- def self.less_than
7
- uri_for('lessThan')
8
- end
7
+ def self.less_than
8
+ uri_for('lessThan')
9
+ end
9
10
 
10
- def self.uri_for(name)
11
- RDF::URI("http://www.w3.org/2000/10/swap/math##{name}")
11
+ def self.uri_for(name)
12
+ RDF::URI("http://www.w3.org/2000/10/swap/math##{name}")
13
+ end
12
14
  end
13
- end
14
15
 
15
- MV = MathVocabulary
16
+ MV = MathVocabulary
17
+ end
@@ -1,11 +1,13 @@
1
- class RDFVocabulary
2
- def self.method_missing(name, *arguments, &block)
3
- uri_for(name)
4
- end
1
+ module Model
2
+ class RDFVocabulary
3
+ def self.method_missing(name, *arguments, &block)
4
+ uri_for(name)
5
+ end
5
6
 
6
- def self.uri_for(name)
7
- RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns##{name}")
7
+ def self.uri_for(name)
8
+ RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns##{name}")
9
+ end
8
10
  end
9
- end
10
11
 
11
- RDFV = RDFVocabulary
12
+ RDFV = RDFVocabulary
13
+ end
@@ -1,9 +1,11 @@
1
- class StateVocabulary
2
- def self.method_missing(name, *arguments, &block)
3
- uri_for(name)
4
- end
1
+ module Model
2
+ class StateVocabulary
3
+ def self.method_missing(name, *arguments, &block)
4
+ uri_for(name)
5
+ end
5
6
 
6
- def self.uri_for(name)
7
- RDF::URI("http://matus.tomlein.org/case/#{name}")
7
+ def self.uri_for(name)
8
+ RDF::URI("http://matus.tomlein.org/case/#{name}")
9
+ end
8
10
  end
9
11
  end