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.
@@ -1,37 +1,39 @@
1
- class ApplicationByCapabilityFinder
2
- def initialize(ontology)
3
- @ontology = ontology
4
- end
1
+ module Model
2
+ class ApplicationByCapabilityFinder
3
+ def initialize(ontology)
4
+ @ontology = ontology
5
+ end
5
6
 
6
- def find(options)
7
- solutions = RDF::Query.execute(@ontology, {
8
- application: {
9
- LV.hasDeploymentPlan => {
10
- LV.capability => options
7
+ def find(options)
8
+ solutions = RDF::Query.execute(@ontology, {
9
+ application: {
10
+ LV.hasDeploymentPlan => {
11
+ LV.capability => options
12
+ }
11
13
  }
12
- }
13
- })
14
+ })
14
15
 
15
- solutions.map do |solution|
16
- solution[:application]
17
- end.uniq.map do |node|
18
- Application.new(node, @ontology)
16
+ solutions.map do |solution|
17
+ solution[:application]
18
+ end.uniq.map do |node|
19
+ Application.new(node, @ontology)
20
+ end
19
21
  end
20
- end
21
22
 
22
- def find_by_type(type)
23
- find({ RDF.type => type })
24
- end
23
+ def find_by_type(type)
24
+ find({ RDF.type => type })
25
+ end
25
26
 
26
- def find_by_description(description)
27
- find({ LV.description => description })
28
- end
27
+ def find_by_description(description)
28
+ find({ LV.description => description })
29
+ end
29
30
 
30
- def find_by_actuator(actuator)
31
- find({ LV.actuator => actuator })
32
- end
31
+ def find_by_actuator(actuator)
32
+ find({ LV.actuator => actuator })
33
+ end
33
34
 
34
- def find_by_sensor(sensor)
35
- find({ LV.sensor => sensor })
35
+ def find_by_sensor(sensor)
36
+ find({ LV.sensor => sensor })
37
+ end
36
38
  end
37
39
  end
@@ -1,19 +1,21 @@
1
- class EyeSerializer
2
- def self.serialize_implication(facts, precondition, postcondition)
3
- pre = serialize_graph(precondition)
4
- post = serialize_graph(postcondition)
5
- "#{facts.dump(:ntriples)}{#{pre}} => {#{post}}.\n"
6
- end
1
+ module Model
2
+ class EyeSerializer
3
+ def self.serialize_implication(facts, precondition, postcondition)
4
+ pre = serialize_graph(precondition)
5
+ post = serialize_graph(postcondition)
6
+ "#{facts.dump(:ntriples)}{#{pre}} => {#{post}}.\n"
7
+ end
7
8
 
8
- def self.serialize_graph(graph)
9
- statements = []
10
- graph.each_statement do |statement|
11
- statements << RDF::Statement.new({
12
- subject: statement.subject,
13
- predicate: statement.predicate,
14
- object: statement.object
15
- }).to_s + "\n"
9
+ def self.serialize_graph(graph)
10
+ statements = []
11
+ graph.each_statement do |statement|
12
+ statements << RDF::Statement.new({
13
+ subject: statement.subject,
14
+ predicate: statement.predicate,
15
+ object: statement.object
16
+ }).to_s + "\n"
17
+ end
18
+ statements.join
16
19
  end
17
- statements.join
18
20
  end
19
21
  end
@@ -0,0 +1,12 @@
1
+ module Model
2
+ class Helper
3
+ # convert string to underscore case
4
+ def self.underscore(str)
5
+ str.gsub(/::/, '/').
6
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
7
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
8
+ tr("-", "_").
9
+ downcase
10
+ end
11
+ end
12
+ end
@@ -1,34 +1,36 @@
1
1
  require 'securerandom'
2
2
 
3
- class NodeBuilder
4
- def self.create(properties, repository)
5
- node = generate_node
6
- query = NodeQuery.new node, repository
3
+ module Model
4
+ class NodeBuilder
5
+ def self.create(properties, repository)
6
+ node = generate_node
7
+ query = NodeQuery.new node, repository
7
8
 
8
- properties.each do |key, v|
9
- values = if v.is_a?(Array)
10
- v
11
- else
12
- [ v ]
13
- end
9
+ properties.each do |key, v|
10
+ values = if v.is_a?(Array)
11
+ v
12
+ else
13
+ [ v ]
14
+ end
14
15
 
15
- values.each do |value|
16
- query.set_value(key,
17
- if value.is_a?(Hash)
18
- create(value, repository)
19
- else
20
- value
21
- end, false)
16
+ values.each do |value|
17
+ query.set_value(key,
18
+ if value.is_a?(Hash)
19
+ create(value, repository)
20
+ else
21
+ value
22
+ end, false)
23
+ end
22
24
  end
23
- end
24
25
 
25
- node
26
- end
26
+ node
27
+ end
27
28
 
28
- private
29
+ private
29
30
 
30
- def self.generate_node
31
- id = SecureRandom.hex
32
- RDF::URI("http://example.org/#{id}")
31
+ def self.generate_node
32
+ id = SecureRandom.hex
33
+ RDF::URI("http://example.org/#{id}")
34
+ end
33
35
  end
34
36
  end
@@ -1,55 +1,61 @@
1
- class NodeQuery
2
- attr_reader :node, :repository
1
+ module Model
2
+ class NodeQuery
3
+ attr_reader :node, :repository
3
4
 
4
- def initialize(node, repository)
5
- @node = node
6
- @repository = repository
7
- end
5
+ def initialize(node, repository)
6
+ @node = node
7
+ @repository = repository
8
+ end
8
9
 
9
- def value(predicate)
10
- values(predicate).first
11
- end
10
+ def node_uri
11
+ node.to_s
12
+ end
12
13
 
13
- def values(predicate)
14
- results = repository.query([ node, predicate, nil ])
15
- results.map {|s| s.object }
16
- end
14
+ def value(predicate)
15
+ values(predicate).first
16
+ end
17
17
 
18
- def value_exists?(predicate)
19
- values(predicate).any?
20
- end
18
+ def values(predicate)
19
+ results = repository.query([ node, predicate, nil ])
20
+ results.map {|s| s.object }
21
+ end
21
22
 
22
- def set_value(predicate, object, replace = true)
23
- delete_value(predicate) if replace
24
- @repository << [ node, predicate, object ]
25
- end
23
+ def value_exists?(predicate)
24
+ values(predicate).any?
25
+ end
26
26
 
27
- def predicates_and_objects
28
- values = {}
29
- repository.query([ node, nil, nil ]).each do |s|
30
- if values.has_key? s.predicate
31
- if values[s.predicate].is_a? Array
32
- values[s.predicate] << s.object
27
+ def set_value(predicate, object, replace = true)
28
+ delete_value(predicate) if replace
29
+ @repository << [ node, predicate, object ]
30
+ end
31
+
32
+ def predicates_and_objects
33
+ values = {}
34
+ repository.query([ node, nil, nil ]).each do |s|
35
+ if values.has_key? s.predicate
36
+ if values[s.predicate].is_a? Array
37
+ values[s.predicate] << s.object
38
+ else
39
+ values[s.predicate] = [
40
+ values[s.predicate], s.object
41
+ ]
42
+ end
33
43
  else
34
- values[s.predicate] = [
35
- values[s.predicate], s.object
36
- ]
44
+ values[s.predicate] = s.object
37
45
  end
38
- else
39
- values[s.predicate] = s.object
40
46
  end
47
+ values
41
48
  end
42
- values
43
- end
44
49
 
45
- private
50
+ private
46
51
 
47
- def delete_value(predicate)
48
- # makes sure not to delete any values in subgraphs
49
- repository.query([ node, predicate, nil ]).select do |s|
50
- s.graph_name.nil?
51
- end.each do |s|
52
- repository.delete(s)
52
+ def delete_value(predicate)
53
+ # makes sure not to delete any values in subgraphs
54
+ repository.query([ node, predicate, nil ]).select do |s|
55
+ s.graph_name.nil?
56
+ end.each do |s|
57
+ repository.delete(s)
58
+ end
53
59
  end
54
60
  end
55
61
  end
@@ -1,77 +1,79 @@
1
1
  require 'rdf/n3'
2
2
  require 'tempfile'
3
3
 
4
- class Reasoner
5
- attr_reader :repository
4
+ module Model
5
+ class Reasoner
6
+ attr_reader :repository
6
7
 
7
- def initialize(repository)
8
- @repository = repository
9
- end
8
+ def initialize(repository)
9
+ @repository = repository
10
+ end
10
11
 
11
- # implication doesn't work well when a blank node is used in the
12
- # condition, e.g.:
13
- # {?device1 <http://...#type> _:vehicle .} => {...}
14
- # will always result in the postcondition, because of _:vehicle
15
- def imply(precondition, postcondition)
16
- eye_input = EyeSerializer.serialize_implication(
17
- repository.facts_only,
18
- repository.graph(precondition),
19
- repository.graph(postcondition)
20
- )
12
+ # implication doesn't work well when a blank node is used in the
13
+ # condition, e.g.:
14
+ # {?device1 <http://...#type> _:vehicle .} => {...}
15
+ # will always result in the postcondition, because of _:vehicle
16
+ def imply(precondition, postcondition)
17
+ eye_input = EyeSerializer.serialize_implication(
18
+ repository.facts_only,
19
+ repository.graph(precondition),
20
+ repository.graph(postcondition)
21
+ )
21
22
 
22
- eye_output = run_eye eye_input
23
- parse_eye_output eye_output
24
- end
23
+ eye_output = run_eye eye_input
24
+ parse_eye_output eye_output
25
+ end
25
26
 
26
- # makes sure that the graph with name condition is valid
27
- # to do that, it creates a temporary graph (:helper_graph) and tries to imply
28
- # condition => helper_graph
29
- # it creates a new temp_reasoner object so as not to modify the current repository
30
- # with the helper_graph
31
- def check_condition(condition)
32
- temp_repository = @repository.clone
33
- temp_repository << [ LV.condition, LV.was, LV.true, :helper_graph ]
27
+ # makes sure that the graph with name condition is valid
28
+ # to do that, it creates a temporary graph (:helper_graph) and tries to imply
29
+ # condition => helper_graph
30
+ # it creates a new temp_reasoner object so as not to modify the current repository
31
+ # with the helper_graph
32
+ def check_condition(condition)
33
+ temp_repository = @repository.clone
34
+ temp_repository << [ LV.condition, LV.was, LV.true, :helper_graph ]
34
35
 
35
- temp_reasoner = Reasoner.new temp_repository
36
- result = temp_reasoner.imply(condition, :helper_graph)
36
+ temp_reasoner = Reasoner.new temp_repository
37
+ result = temp_reasoner.imply(condition, :helper_graph)
37
38
 
38
- result.query([ LV.condition, LV.was, LV.true ]).any?
39
- end
39
+ result.query([ LV.condition, LV.was, LV.true ]).any?
40
+ end
40
41
 
41
- def load_and_process_n3(n3_input)
42
- input = [
43
- n3_input,
44
- EyeSerializer.serialize_graph(repository.facts_only)
45
- ].join
42
+ def load_and_process_n3(n3_input)
43
+ input = [
44
+ n3_input,
45
+ EyeSerializer.serialize_graph(repository.facts_only)
46
+ ].join
46
47
 
47
- eye_output = run_eye input
48
- parse_eye_output eye_output, @repository
49
- @repository
50
- end
48
+ eye_output = run_eye input
49
+ parse_eye_output eye_output, @repository
50
+ @repository
51
+ end
51
52
 
52
- private
53
+ private
53
54
 
54
- def run_eye(input)
55
- data = Tempfile.new('data')
56
- data.write(input)
57
- data.close
58
- output = `eye --nope --pass #{data.path} 2> /dev/null`
59
- data.unlink
55
+ def run_eye(input)
56
+ data = Tempfile.new('data')
57
+ data.write(input)
58
+ data.close
59
+ output = `eye --nope --pass #{data.path} 2> /dev/null`
60
+ data.unlink
60
61
 
61
- output
62
- end
62
+ output
63
+ end
63
64
 
64
- def parse_eye_output(output, repo = nil)
65
- output = remove_incompatible_prefix_from_eye_output output
66
- repo = RDF::Repository.new if repo.nil?
67
- reader = RDF::Reader.for(:n3).new(output)
68
- reader.each_statement { |statement| repo << statement }
69
- repo
70
- end
65
+ def parse_eye_output(output, repo = nil)
66
+ output = remove_incompatible_prefix_from_eye_output output
67
+ repo = RDF::Repository.new if repo.nil?
68
+ reader = RDF::Reader.for(:n3).new(output)
69
+ reader.each_statement { |statement| repo << statement }
70
+ repo
71
+ end
71
72
 
72
- def remove_incompatible_prefix_from_eye_output(output)
73
- output.gsub(/PREFIX .+\n/) do |match|
74
- match.sub('PREFIX ', '@prefix ') + '.'
73
+ def remove_incompatible_prefix_from_eye_output(output)
74
+ output.gsub(/PREFIX .+\n/) do |match|
75
+ match.sub('PREFIX ', '@prefix ') + '.'
76
+ end
75
77
  end
76
78
  end
77
79
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require_relative '../lib/case_model'
2
2
  require 'webmock/rspec'
3
3
 
4
+ include Model
5
+
4
6
  RSpec.configure do |config|
5
7
  config.expect_with :rspec do |c|
6
8
  c.syntax = :expect
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: case_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matus Tomlein
@@ -9,7 +9,63 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-01-28 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdf-n3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.99.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.99.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdf-vocab
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.7.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.7.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.22.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.22.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.0
13
69
  description: Built for the Context-Aware Software Ecosystem. Resolves application
14
70
  model requirements using semantic reasoning.
15
71
  email: matus@cs.au.dk
@@ -33,11 +89,16 @@ files:
33
89
  - lib/models/device.rb
34
90
  - lib/models/ecosystem_host.rb
35
91
  - lib/models/http_request.rb
92
+ - lib/models/installation.rb
93
+ - lib/models/installation_used_device.rb
36
94
  - lib/models/location.rb
95
+ - lib/models/number.rb
37
96
  - lib/models/ontology.rb
97
+ - lib/models/property.rb
38
98
  - lib/models/question.rb
39
99
  - lib/models/question_reply_type.rb
40
100
  - lib/models/requirement.rb
101
+ - lib/models/scenario.rb
41
102
  - lib/models/selection.rb
42
103
  - lib/models/state_change.rb
43
104
  - lib/models/temperature.rb
@@ -48,6 +109,7 @@ files:
48
109
  - lib/ontology/state_vocabulary.rb
49
110
  - lib/services/application_by_capability_finder.rb
50
111
  - lib/services/eye_serializer.rb
112
+ - lib/services/helper.rb
51
113
  - lib/services/node_builder.rb
52
114
  - lib/services/node_query.rb
53
115
  - lib/services/question_references.rb