ontology-united 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef0bf5863b23a554f1dc5a86bd0be1d4307a1de3
4
- data.tar.gz: 5220f4ed1b6c8515344fe4484eba00cdd525891b
3
+ metadata.gz: ab561ca8455f27d70cbdd4cb9c20351da04f6a4e
4
+ data.tar.gz: ff6f3bb6c32f3cb2b9fd3862e10cc2b140ab9269
5
5
  SHA512:
6
- metadata.gz: b6a60f8c8ed792e131f0197af0220292a136db44e5d148e36f6caeb4445052f36f94d3f77b5bfd90c666cb545e909038fd1b45912e30775e6410a4448ce23866
7
- data.tar.gz: ba969589d3eda647f2ecc4b48a2030072082183d75435203c8ee94d6c9f66c4f40c57ecdded28fd8cfd07f1dd5e8799f3088519b67c31f673a1de8e81f8a9d83
6
+ metadata.gz: 758a5eac126d84d58d6a46901b43299ec9040e7b4f4f1166c3c2e8e6333cbb389caef40a7ce4e017c065ba71eef119a8dc62877c967e49c441901844f9d03aec
7
+ data.tar.gz: 6890fcda4eccbfc2b0d89aac0b8448e94bcd040d3fbdf0faa70c5fccb462b739e4749395a0a36d10c8975088c403f87a588f4368e515ef473d09c0a20ff32090
data/README.md CHANGED
@@ -31,3 +31,157 @@ Or install it yourself as:
31
31
 
32
32
  $ gem install ontology-united
33
33
 
34
+ ## Usage
35
+
36
+ First you'll need to require the gem (unless you are using bundler):
37
+ ```ruby
38
+ require 'ontology-united'
39
+ ```
40
+
41
+ After doing this you'll be able to create an ontology in two different ways.
42
+ Either by explicitly using the defined method:
43
+ ```ruby
44
+ OntologyUnited::DSL::OntologyDSL.define('MyOntologyName') do
45
+ end
46
+ ```
47
+
48
+ or by including the convience module, which will give you a more
49
+ useful/shorter method call:
50
+ ```ruby
51
+ # you'll only need to do this once per namespace
52
+ include OntologyUnited::Convience
53
+
54
+ define_ontology('MyOntologyName') do
55
+ end
56
+ ```
57
+
58
+ ### Building your ontology
59
+
60
+ Usually there is more than one way to building your ontology, i will
61
+ elaborate on the benefits and drawbacks of each method. However i will now
62
+ use the convenience method of building an ontology.
63
+
64
+ #### Prefix
65
+
66
+ In order to benefit from ontology-united you'll first need to create a
67
+ prefix. As the block passed to `define_ontology` is `instance_eval`ed on the
68
+ ontology, self will be the current ontology. So you can create a prefix
69
+ which points to the current ontology.
70
+ ```ruby
71
+ define_ontology('MyOntologyName') do
72
+ this = prefix('thisOntology', self)
73
+ end
74
+ ```
75
+
76
+ However technically you can also pass an IRI (internationalized resource
77
+ identifier) instead of `self`. But then you're pretty much on your own.
78
+ You'll need to handle iri-management yourself.
79
+ ```ruby
80
+ # Handling iri-management yourself:
81
+ define_ontology('MyOntologyName') do
82
+ this = prefix('thisOntology', 'http://example.com/MyOntologyName#')
83
+ end
84
+ ```
85
+
86
+ #### Class (OWL)
87
+
88
+ ```ruby
89
+ define_ontology('MyOntologyName') do
90
+ this = prefix('thisOntology', self)
91
+ this.class('ASampleClass')
92
+ end
93
+ ```
94
+
95
+ The `.class` method on a prefix, will create a prefixed class inside of the
96
+ current ontology. If you do not want to rely on prefixes you can also use
97
+ `ontology_class()` but then the argument has to be an IRI instead of a name.
98
+ And again (as with prefixes) your currently on your own for
99
+ iri-management.
100
+
101
+
102
+ ```ruby
103
+ # Handling iri-management yourself:
104
+ define_ontology('MyOntologyName') do
105
+ ontology_class('http://example.com/MyOntology#ASampleClass')
106
+ end
107
+ ```
108
+
109
+ By the way, you do not need to store ontology classes inside variables in
110
+ order to use them again (i.e. in sentences). You can just *define* them
111
+ again. The system will figure out by itself, that you actually meant the
112
+ same class.
113
+
114
+ #### Sentence
115
+
116
+ Currently ontology-united only supports `sub_class_of` sentences. This will
117
+ be extended in the future, but it *has* to be enough for now.
118
+
119
+ ```ruby
120
+ define_ontology('MyOntologyName') do
121
+ this = prefix('thisOntology', self)
122
+ this.class('ASampleClass').sub_class_of this.class('AParentClass')
123
+ end
124
+ ```
125
+
126
+ #### Imports
127
+
128
+ You can define ontologies inside of other ontologies, through the inner
129
+ `define`-method, which works exactly as the standard `define` method (also
130
+ called `define_ontology` in convenience mode).
131
+
132
+ ```ruby
133
+ define_ontology('Foo') do
134
+ this = prefix('this', self)
135
+ define('Bar') do
136
+ rr = prefix('rr', self)
137
+ rr.class('SomeBar')
138
+ end
139
+ this.class('Bar').sub_class_of this.class('Foo')
140
+ this.class('something')
141
+ this.class('something').sub_class_of this.class('Foo')
142
+ end
143
+ ```
144
+
145
+ As you might've guessed this is pretty useful when using imports, as this
146
+ would allow you to import an ontology which you just defined *in-place*.
147
+ Let's take a look at this:
148
+
149
+ ```ruby
150
+ define_ontology('Foo') do
151
+ import define('Bar') do
152
+ rr = prefix('rr', self)
153
+ rr.class('SomeBar')
154
+ end
155
+ this.class('Bar').sub_class_of this.class('Foo')
156
+ end
157
+ ```
158
+
159
+ The `import` method is key, and that is basically it.
160
+
161
+
162
+ ### Serialization
163
+
164
+ Let's assume we have an ontology:
165
+
166
+ ```ruby
167
+ ontology = define_ontology('Foo') do
168
+ rr = prefix('rr', self)
169
+ imports define('Bar') do
170
+ rr = prefix('rr', self)
171
+ rr.class('SomeBar')
172
+ end
173
+ rr.class('Bar').sub_class_of rr.class('Foo')
174
+ rr.class('something')
175
+ rr.class('something').sub_class_of rr.class('Foo')
176
+ end
177
+ ```
178
+
179
+ If we want to work with it, we can query it for its IRI like this:
180
+ `ontology.iri`, which will return a `file://`-schema IRI.
181
+
182
+ If we want access to the `Tempfile` (which ontology-united uses internally),
183
+ we can call `ontology.file`.
184
+
185
+ Both methods will ensure that the corresponding file is created and
186
+ serialized, including all imported ontologies.
187
+
@@ -4,6 +4,7 @@ require 'set'
4
4
  module OntologyUnited
5
5
  module DSL
6
6
  class Ontology < BaseDSL
7
+ DEFAULT_EXTENSION = :owl
7
8
 
8
9
  attr_reader_with_default :the_classes, :the_imports,
9
10
  :the_sentences, :the_prefixes, default: Set
@@ -39,10 +40,14 @@ module OntologyUnited
39
40
  OntologyDSL.define(*args, &block)
40
41
  end
41
42
 
43
+ def redefine(ontology = self, &block)
44
+ OntologyDSL.redefine(ontology, &block)
45
+ end
46
+
42
47
  def imports(arg, &block)
43
- if block || arg.is_a?(Ontology)
48
+ if block && arg.is_a?(Ontology)
44
49
  ontology = arg
45
- arg = define(ontology.name, &block)
50
+ arg = redefine(&block)
46
51
  end
47
52
  the_import = OntologyImport.new(arg)
48
53
  the_imports << the_import
@@ -71,14 +76,21 @@ module OntologyUnited
71
76
  end
72
77
 
73
78
  def file
74
- @file ||= create_tempfile
79
+ create_tempfile if @file.nil?
80
+ @file
81
+ end
82
+
83
+ def extension
84
+ ".#{DEFAULT_EXTENSION}"
75
85
  end
76
86
 
77
87
  def create_tempfile
78
- filename = name.match(%r(([^/]+)\.\w+$))[1]
79
- file = Tempfile.new([filename, '.owl'])
88
+ filename = name
89
+ file = Tempfile.new([filename, extension])
80
90
  @iri = "file://#{file.path}"
91
+ @file = file
81
92
  file.write(self.to_s)
93
+ file.flush
82
94
  file.rewind
83
95
  file
84
96
  end
@@ -4,6 +4,10 @@ module OntologyUnited
4
4
 
5
5
  def self.define(name, &block)
6
6
  ontology = Ontology.new(name)
7
+ redefine(ontology, &block)
8
+ end
9
+
10
+ def self.redefine(ontology, &block)
7
11
  stack.push(ontology)
8
12
  if block
9
13
  if block.arity == 1
@@ -46,11 +46,11 @@ module OntologyUnited::Serializer
46
46
 
47
47
  def class_definition?(ontology_class)
48
48
  # if no parent is set
49
- parent_current.nil? ||
49
+ parent.nil? ||
50
50
  # or the parent is an ontology itself
51
- ontology?(parent_current) ||
51
+ ontology?(parent) ||
52
52
  # or it is the first symbol of a sentence
53
- sentence?(parent_current) && parent_current.sentence.first == ontology_class
53
+ sentence?(parent) && parent.sentence.first == ontology_class
54
54
  end
55
55
 
56
56
  end
@@ -1,25 +1,36 @@
1
1
  module OntologyUnited
2
2
  module Serializer
3
3
  class SerializerBase
4
- attr_accessor :current, :parent_current
5
4
 
6
5
  def initialize(current: nil)
7
- self.current = current
8
- self.parent_current = nil
6
+ stack.push(current) if current
9
7
  end
10
8
 
11
9
  def process(subject, &block)
12
- previous_parent = mark!(subject)
10
+ mark!(subject)
13
11
  result = block.call
14
- mark!(parent_current, previous_parent)
12
+ unmark!
15
13
  result
16
14
  end
17
15
 
18
- def mark!(subject, parent=nil)
19
- previous_parent = parent_current
20
- parent_current = parent || current
21
- current = subject
22
- previous_parent
16
+ def mark!(subject)
17
+ stack.push(subject)
18
+ end
19
+
20
+ def unmark!
21
+ stack.pop
22
+ end
23
+
24
+ def stack
25
+ @stack ||= []
26
+ end
27
+
28
+ def current
29
+ stack[-1]
30
+ end
31
+
32
+ def parent
33
+ stack[-2]
23
34
  end
24
35
 
25
36
  def join(elements, sep)
@@ -38,7 +49,7 @@ module OntologyUnited
38
49
  subject.is_a?(OntologyUnited::DSL::OntologyClass)
39
50
  end
40
51
 
41
- def class?(subject)
52
+ def sentence?(subject)
42
53
  subject.is_a?(OntologyUnited::DSL::OntologySentence)
43
54
  end
44
55
 
@@ -1,3 +1,3 @@
1
1
  module OntologyUnited
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -9,9 +9,9 @@ describe OntologyUnited do
9
9
 
10
10
  context 'when issueing a standard definition' do
11
11
  let(:ontology) do
12
- OntologyUnited::DSL::OntologyDSL.define('Foo.owl') do
12
+ OntologyUnited::DSL::OntologyDSL.define('Foo') do
13
13
  rr = prefix('rr', self)
14
- imports define('Bar.owl') do
14
+ imports define('Bar') do
15
15
  rr = prefix('rr', self)
16
16
  rr.class('SomeBar')
17
17
  end
@@ -5,7 +5,7 @@ describe OntologyUnited::Serializer::OWL::Manchester do
5
5
  let(:name) { 'OntologyName' }
6
6
  let(:list) do
7
7
  the_prefix, ontology_class, sentence = nil
8
- o = OntologyUnited::DSL::OntologyDSL.define("#{name}.owl") do
8
+ o = OntologyUnited::DSL::OntologyDSL.define(name) do
9
9
  the_prefix = prefix('some', self)
10
10
  ontology_class = the_prefix.class('Foobar')
11
11
  sentence = ontology_class.sub_class_of the_prefix.class('NoBar')
@@ -53,8 +53,36 @@ describe OntologyUnited::Serializer::OWL::Manchester do
53
53
  let(:serialized) { sentence.to_s(serializer: manchester.new) }
54
54
 
55
55
  it 'should be a valid manchester syntax sentence definition' do
56
- expect(serialized).to eq("Class: some:Foobar SubClassOf: Class: some:NoBar")
56
+ expect(serialized).to eq("Class: some:Foobar SubClassOf: some:NoBar")
57
57
  end
58
58
  end
59
59
 
60
+ context 'when serializing ontologies with imports' do
61
+ let(:manchester) { OntologyUnited::Serializer::OWL::Manchester }
62
+ let(:name) { 'OntologyName' }
63
+ let(:list) do
64
+ referenced, the_import = nil
65
+ o = OntologyUnited::DSL::OntologyDSL.define(name) do
66
+ referenced = define('OtherOne') do
67
+ ontohub = prefix('ontohub', self)
68
+ ontohub.class('some_class')
69
+ end
70
+ the_import = imports referenced
71
+ end
72
+ [o, referenced, the_import]
73
+ end
74
+ let(:ontology) { list[0] }
75
+ let(:referenced) { list[1] }
76
+ let(:import) { list[2] }
77
+
78
+ context 'when serializing an import' do
79
+ let!(:serialized) { import.to_s(serializer: manchester.new) }
80
+
81
+ it 'should be a valid manchester syntax import definition' do
82
+ expect(serialized).to eq("Import: <#{referenced.iri}>")
83
+ end
84
+ end
85
+
86
+ end
87
+
60
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ontology-united
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Reddehase
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-24 00:00:00.000000000 Z
11
+ date: 2014-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler