openmath 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +26 -5
  3. data/lib/openmath/cd/collection.rb +92 -0
  4. data/lib/openmath/cd/configuration.rb +65 -0
  5. data/lib/openmath/cd/definition.rb +42 -0
  6. data/lib/openmath/cd/document.rb +50 -0
  7. data/lib/openmath/cd/example.rb +22 -0
  8. data/lib/openmath/cd/fmp.rb +21 -0
  9. data/lib/openmath/cd/namespace.rb +13 -0
  10. data/lib/openmath/cd/validation.rb +10 -0
  11. data/lib/openmath/cd.rb +27 -0
  12. data/lib/openmath/evaluation/binding_table.rb +58 -0
  13. data/lib/openmath/evaluation/error_value.rb +27 -0
  14. data/lib/openmath/evaluation/errors.rb +65 -0
  15. data/lib/openmath/evaluation/evaluator.rb +141 -0
  16. data/lib/openmath/evaluation/operations.rb +126 -0
  17. data/lib/openmath/evaluation.rb +29 -0
  18. data/lib/openmath/v2.rb +1 -1
  19. data/lib/openmath/v3/common_elements.rb +48 -0
  20. data/lib/openmath/v3/configuration.rb +35 -0
  21. data/lib/openmath/v3/oma.rb +9 -0
  22. data/lib/openmath/v3/omatp.rb +9 -0
  23. data/lib/openmath/v3/omattr.rb +9 -0
  24. data/lib/openmath/v3/omb.rb +9 -0
  25. data/lib/openmath/v3/ombind.rb +9 -0
  26. data/lib/openmath/v3/ombvar.rb +9 -0
  27. data/lib/openmath/v3/ome.rb +9 -0
  28. data/lib/openmath/v3/omf.rb +9 -0
  29. data/lib/openmath/v3/omforeign.rb +9 -0
  30. data/lib/openmath/v3/omi.rb +9 -0
  31. data/lib/openmath/v3/omobj.rb +9 -0
  32. data/lib/openmath/v3/omr.rb +9 -0
  33. data/lib/openmath/v3/oms.rb +9 -0
  34. data/lib/openmath/v3/omstr.rb +9 -0
  35. data/lib/openmath/v3/omv.rb +9 -0
  36. data/lib/openmath/v3.rb +55 -0
  37. data/lib/openmath/validation.rb +62 -0
  38. data/lib/openmath/version.rb +1 -1
  39. data/lib/openmath.rb +28 -1
  40. data/schemas/PROVENANCE.md +17 -0
  41. data/schemas/om3-common.rng +257 -0
  42. data/schemas/omcd2.rng +180 -0
  43. data/schemas/openmath2.rnc +88 -0
  44. data/schemas/openmath2.rng +248 -0
  45. data/schemas/openmath3.rng +19 -0
  46. metadata +60 -9
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+
5
+ module Openmath
6
+ module Evaluation
7
+ # The tree walk: literals evaluate to themselves, applications resolve
8
+ # their operator symbol through the binding table and dispatch to the
9
+ # named operation, error constructors evaluate to error values, and
10
+ # everything outside the evaluated surface refuses with a typed error.
11
+ module Evaluator
12
+ module_function
13
+
14
+ # Evaluates an OpenMath object under the variable bindings. Variables
15
+ # bind by name (PM 1004's binding domain: numbers, booleans, strings).
16
+ def evaluate(object, variables: {}, table: BindingTable.default)
17
+ case object
18
+ when Openmath::V2::Omobj, Openmath::V3::Omobj
19
+ single(object, variables, table)
20
+ when Openmath::V2::Omi, Openmath::V3::Omi
21
+ integer_literal(object)
22
+ when Openmath::V2::Omf, Openmath::V3::Omf
23
+ float_literal(object)
24
+ when Openmath::V2::Omstr, Openmath::V3::Omstr
25
+ object.value.join
26
+ when Openmath::V2::Omb, Openmath::V3::Omb
27
+ require_base64(object)
28
+ when Openmath::V2::Omv, Openmath::V3::Omv
29
+ lookup_variable(object, variables)
30
+ when Openmath::V2::Oma, Openmath::V3::Oma
31
+ apply(object, variables, table)
32
+ when Openmath::V2::Ome, Openmath::V3::Ome
33
+ error_value(object, variables, table)
34
+ else
35
+ raise UnsupportedExpressionError, element_name(object)
36
+ end
37
+ end
38
+
39
+ # The children of a container in document order: element_order names
40
+ # each element as parsed; the per-type collections hold the values in
41
+ # the same relative order, so a cursor per element name reconstructs
42
+ # the sequence.
43
+ CHILD_ATTRIBUTES = {
44
+ "OMS" => :oms_value, "OMV" => :omv_value, "OMI" => :omi_value,
45
+ "OMB" => :omb_value, "OMSTR" => :omstr_value, "OMF" => :omf_value,
46
+ "OMA" => :oma_value, "OMBIND" => :ombind_value,
47
+ "OMBVAR" => :ombvar_value, "OME" => :ome_value,
48
+ "OMATTR" => :omattr_value, "OMATP" => :omatp_value,
49
+ "OMFOREIGN" => :omforeign_value, "OMR" => :omr_value,
50
+ }.freeze
51
+
52
+ def ordered_children(container)
53
+ cursors = Hash.new(0)
54
+ ordered = container.element_order.filter_map do |element|
55
+ attribute = CHILD_ATTRIBUTES[element.name]
56
+ next unless attribute
57
+
58
+ values = container.public_send(attribute)
59
+ index = cursors[element.name]
60
+ cursors[element.name] += 1
61
+ values[index]
62
+ end
63
+ # Mixed-content elements can drop out of the order tracking; append
64
+ # any values the order walk did not consume, in collection order.
65
+ CHILD_ATTRIBUTES.each do |name, attribute|
66
+ values = container.public_send(attribute)
67
+ consumed = cursors[name]
68
+ ordered.concat(values.drop(consumed)) if values && values.size > consumed
69
+ end
70
+ ordered
71
+ end
72
+
73
+ def element_name(object)
74
+ object.is_a?(Openmath::V2::Ombind) || object.is_a?(Openmath::V3::Ombind) ? "OMBIND" : object.class.name
75
+ end
76
+
77
+ def single(omobj, variables, table)
78
+ elements = ordered_children(omobj)
79
+ raise UnsupportedExpressionError, "empty OMOBJ" if elements.empty?
80
+ raise UnsupportedExpressionError, "OMOBJ with multiple children" if elements.size > 1
81
+
82
+ evaluate(elements.first, variables: variables, table: table)
83
+ end
84
+
85
+ def integer_literal(node)
86
+ text = node.value.join.strip
87
+ Integer(text.delete(" "))
88
+ end
89
+
90
+ def float_literal(node)
91
+ return Float(node.dec) if node.dec
92
+ return [node.hex].pack("H*").unpack1("G") if node.hex
93
+
94
+ raise OperandTypeError.new("OMF", "neither dec nor hex")
95
+ end
96
+
97
+ def require_base64(node)
98
+ Base64.decode64(node.value.join)
99
+ rescue ArgumentError
100
+ raise OperandTypeError.new("base64 content", node.value.join)
101
+ end
102
+
103
+ def lookup_variable(node, variables)
104
+ name = node.name
105
+ return variables[name] if variables.key?(name)
106
+ return variables[name.to_sym] if variables.key?(name.to_sym)
107
+
108
+ raise UnboundSymbolError.new("(variable)", name)
109
+ end
110
+
111
+ def apply(oma, variables, table)
112
+ children = ordered_children(oma)
113
+ operator = children.first
114
+ unless operator.is_a?(Openmath::V2::Oms) || operator.is_a?(Openmath::V3::Oms)
115
+ raise UnsupportedExpressionError, "application without an OMS operator"
116
+ end
117
+
118
+ binding = table.lookup(operator.cd, operator.name)
119
+ raise UnboundSymbolError.new(operator.cd, operator.name) unless binding
120
+
121
+ implementation, arity_spec = Operations.implementation(binding.operation)
122
+ arguments = children[1..].map { |child| evaluate(child, variables: variables, table: table) }
123
+ unless arity_spec.cover?(arguments.size)
124
+ raise ArityError.new(binding.operation, arity_spec, arguments.size)
125
+ end
126
+
127
+ implementation.call(arguments)
128
+ end
129
+
130
+ def error_value(ome, variables, table)
131
+ symbol = ome.oms_value.first
132
+ raise UnsupportedExpressionError, "error without a leading OMS" unless symbol
133
+
134
+ content = ordered_children(ome).drop(1).map do |child|
135
+ evaluate(child, variables: variables, table: table)
136
+ end
137
+ ErrorValue.new(symbol.cd, symbol.name, content)
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module Evaluation
5
+ # The operations the binding table can name, with their signatures and
6
+ # the exactness discipline: integers and rationals stay exact; a float
7
+ # operand contaminates the result to the limited class; divided_by of
8
+ # exact operands is an exact rational, never a float.
9
+ module Operations
10
+ module_function
11
+
12
+ # [implementation, arity spec] where arity spec is an Integer or a
13
+ # Range. The implementation receives the evaluated arguments.
14
+ REGISTRY = {
15
+ identity: [->(args) { args.fetch(0) }, 1..1],
16
+ rational: [->(args) { rational(args) }, 2..2],
17
+ plus: [->(args) { numeric_fold(args, :+) }, 1..],
18
+ times: [->(args) { numeric_fold(args, :*) }, 1..],
19
+ minus: [->(args) { numeric_pair(args) { |a, b| a - b } }, 2..2],
20
+ unary_minus: [->(args) { require_numeric(args); -args.fetch(0) }, 1..1],
21
+ divided_by: [->(args) { divided_by(args) }, 2..2],
22
+ power: [->(args) { power(args) }, 2..2],
23
+ abs: [->(args) { args.fetch(0).abs }, 1..1],
24
+ max: [->(args) { require_numeric(args); args.max }, 1..],
25
+ min: [->(args) { require_numeric(args); args.min }, 1..],
26
+ eq: [->(args) { compare_chain(args, :==) }, 2..],
27
+ neq: [->(args) { args[0] != args[1] }, 2..2],
28
+ gt: [->(args) { compare_chain(args, :>) }, 2..],
29
+ lt: [->(args) { compare_chain(args, :<) }, 2..],
30
+ geq: [->(args) { compare_chain(args, :>=) }, 2..],
31
+ leq: [->(args) { compare_chain(args, :<=) }, 2..],
32
+ and: [->(args) { boolean_fold(args) { |a, b| a && b } }, 1..],
33
+ or: [->(args) { boolean_fold(args) { |a, b| a || b } }, 1..],
34
+ xor: [->(args) { boolean_fold(args, :^) }, 2..],
35
+ not: [->(args) { !args.fetch(0) }, 1..1],
36
+ implies: [->(args) { !args.fetch(0) || args.fetch(1) }, 2..2],
37
+ }.freeze
38
+
39
+ def implementation(operation)
40
+ entry = REGISTRY[operation]
41
+ return entry if entry
42
+
43
+ raise UnimplementedOperationError, operation
44
+ end
45
+
46
+ def arity(operation)
47
+ REGISTRY.fetch(operation).last
48
+ end
49
+
50
+ def rational(args)
51
+ numerator, denominator = args
52
+ if numerator.is_a?(Float) || denominator.is_a?(Float)
53
+ require_contaminated(numerator, denominator)
54
+ numerator / denominator
55
+ else
56
+ Rational(numerator, denominator)
57
+ end
58
+ end
59
+
60
+ def numeric_fold(args, operator)
61
+ require_numeric(args)
62
+ args.reduce { |a, b| a.public_send(operator, b) }
63
+ end
64
+
65
+ def numeric_pair(args, &block)
66
+ require_numeric(args)
67
+ block.call(args.fetch(0), args.fetch(1))
68
+ end
69
+
70
+ def divided_by(args)
71
+ require_numeric(args)
72
+ numerator, denominator = args
73
+ if numerator.is_a?(Float) || denominator.is_a?(Float)
74
+ numerator / denominator
75
+ elsif denominator.zero?
76
+ raise ZeroDivisionError, "exact division by zero"
77
+ else
78
+ Rational(numerator, denominator)
79
+ end
80
+ end
81
+
82
+ def power(args)
83
+ base, exponent = args
84
+ if base.is_a?(Float) || exponent.is_a?(Float)
85
+ base**exponent
86
+ elsif exponent.is_a?(Integer) && exponent >= 0
87
+ base**exponent
88
+ else
89
+ Rational(base)**exponent
90
+ end
91
+ end
92
+
93
+ def compare_chain(args, operator)
94
+ require_numeric(args)
95
+ args.each_cons(2).all? { |a, b| a.public_send(operator, b) }
96
+ end
97
+
98
+ def boolean_fold(args, operator = nil)
99
+ require_boolean(args)
100
+ return args.reduce { |a, b| yield a, b } unless operator
101
+
102
+ args.reduce { |a, b| a.public_send(operator, b) }
103
+ end
104
+
105
+ def require_numeric(args)
106
+ args.each do |arg|
107
+ unless arg.is_a?(Numeric)
108
+ raise OperandTypeError.new("arithmetic", arg)
109
+ end
110
+ end
111
+ end
112
+
113
+ def require_boolean(args)
114
+ args.each do |arg|
115
+ raise OperandTypeError.new("logic", arg) unless arg == true || arg == false
116
+ end
117
+ end
118
+
119
+ def require_contaminated(*args)
120
+ args.each do |arg|
121
+ raise OperandTypeError.new("float arithmetic", arg) unless arg.is_a?(Numeric)
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ # Semantic evaluation: content-dictionary symbols resolve through the
5
+ # binding table and evaluate under the exactness discipline — integers
6
+ # and rationals stay exact, floats contaminate to the limited class,
7
+ # unbound symbols refuse with a typed error naming the symbol.
8
+ module Evaluation
9
+ autoload :Error, "openmath/evaluation/errors"
10
+ autoload :UnboundSymbolError, "openmath/evaluation/errors"
11
+ autoload :UnsupportedExpressionError, "openmath/evaluation/errors"
12
+ autoload :ArityError, "openmath/evaluation/errors"
13
+ autoload :OperandTypeError, "openmath/evaluation/errors"
14
+ autoload :UnimplementedOperationError, "openmath/evaluation/errors"
15
+ autoload :BindingTable, "openmath/evaluation/binding_table"
16
+ autoload :Operations, "openmath/evaluation/operations"
17
+ autoload :ErrorValue, "openmath/evaluation/error_value"
18
+ autoload :Evaluator, "openmath/evaluation/evaluator"
19
+
20
+ module_function
21
+
22
+ # Evaluates an OpenMath object (or XML text) under the variable
23
+ # bindings. The binding table is the shipped artifact unless given.
24
+ def evaluate(object, variables: {}, table: BindingTable.default, version: nil)
25
+ object = Openmath.parse(object, version: version || 2) if object.is_a?(String)
26
+ Evaluator.evaluate(object, variables: variables, table: table)
27
+ end
28
+ end
29
+ end
data/lib/openmath/v2.rb CHANGED
@@ -48,7 +48,7 @@ module Openmath
48
48
 
49
49
  def parse(input)
50
50
  register_models!
51
- Omobj.from_xml(input)
51
+ Omobj.from_xml(input, register: Configuration::CONTEXT_ID)
52
52
  end
53
53
  end
54
54
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ # The shared parent of the OpenMath 3 element classes. The OM3 draft's
6
+ # object grammar is the same shape as OpenMath 2's (the 2009 effort
7
+ # schema, vendored as schemas/openmath3.rng), so the Base modules are
8
+ # shared and only the class hierarchy and registry context differ.
9
+ class CommonElements < Openmath::CommonElements
10
+ def self.lutaml_default_register
11
+ Configuration::CONTEXT_ID
12
+ end
13
+
14
+ attribute :oms_value, :oms, collection: true
15
+ attribute :omv_value, :omv, collection: true
16
+ attribute :omi_value, :omi, collection: true
17
+ attribute :omb_value, :omb, collection: true
18
+ attribute :omstr_value, :omstr, collection: true
19
+ attribute :omf_value, :omf, collection: true
20
+ attribute :oma_value, :oma, collection: true
21
+ attribute :ombind_value, :ombind, collection: true
22
+ attribute :ombvar_value, :ombvar, collection: true
23
+ attribute :ome_value, :ome, collection: true
24
+ attribute :omattr_value, :omattr, collection: true
25
+ attribute :omatp_value, :omatp, collection: true
26
+ attribute :omforeign_value, :omforeign, collection: true
27
+ attribute :omr_value, :omr, collection: true
28
+
29
+ xml do
30
+ namespace Openmath::Namespace
31
+ map_element "OMS", to: :oms_value
32
+ map_element "OMV", to: :omv_value
33
+ map_element "OMI", to: :omi_value
34
+ map_element "OMB", to: :omb_value
35
+ map_element "OMSTR", to: :omstr_value
36
+ map_element "OMF", to: :omf_value
37
+ map_element "OMA", to: :oma_value
38
+ map_element "OMBIND", to: :ombind_value
39
+ map_element "OMBVAR", to: :ombvar_value
40
+ map_element "OME", to: :ome_value
41
+ map_element "OMATTR", to: :omattr_value
42
+ map_element "OMATP", to: :omatp_value
43
+ map_element "OMFOREIGN", to: :omforeign_value
44
+ map_element "OMR", to: :omr_value
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ # Registers the version's element classes under this version's LutaML
6
+ # context, so the symbol-typed child attributes resolve (the Mml
7
+ # pattern).
8
+ module Configuration
9
+ CONTEXT_ID = :openmath_v3
10
+
11
+ extend self
12
+
13
+ def context
14
+ Lutaml::Model::GlobalContext.context(CONTEXT_ID) || create_context!
15
+ end
16
+
17
+ def register_model(klass, id:)
18
+ create_context! unless Lutaml::Model::GlobalContext.context(CONTEXT_ID)
19
+ Lutaml::Model::GlobalContext.context(CONTEXT_ID)
20
+ .registry.register(id.to_sym, klass)
21
+ klass
22
+ end
23
+
24
+ def create_context!
25
+ Lutaml::Model::GlobalContext.register_context(
26
+ Lutaml::Model::TypeContext.new(
27
+ id: CONTEXT_ID,
28
+ registry: Lutaml::Model::TypeRegistry.new,
29
+ ),
30
+ )
31
+ Lutaml::Model::GlobalContext.context(CONTEXT_ID)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Oma < CommonElements
6
+ include Base::Oma
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omatp < CommonElements
6
+ include Base::Omatp
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omattr < CommonElements
6
+ include Base::Omattr
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omb < CommonElements
6
+ include Base::Omb
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Ombind < CommonElements
6
+ include Base::Ombind
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Ombvar < CommonElements
6
+ include Base::Ombvar
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Ome < CommonElements
6
+ include Base::Ome
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omf < CommonElements
6
+ include Base::Omf
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omforeign < CommonElements
6
+ include Base::Omforeign
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omi < CommonElements
6
+ include Base::Omi
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omobj < CommonElements
6
+ include Base::Omobj
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omr < CommonElements
6
+ include Base::Omr
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Oms < CommonElements
6
+ include Base::Oms
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omstr < CommonElements
6
+ include Base::Omstr
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module V3
5
+ class Omv < CommonElements
6
+ include Base::Omv
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ # OpenMath 3: the draft standard (the object grammar is the same shape as
5
+ # OpenMath 2's; the versioned hierarchy carries the distinction).
6
+ module V3
7
+ autoload :Configuration, "openmath/v3/configuration"
8
+ autoload :CommonElements, "openmath/v3/common_elements"
9
+ autoload :Oms, "openmath/v3/oms"
10
+ autoload :Omv, "openmath/v3/omv"
11
+ autoload :Omi, "openmath/v3/omi"
12
+ autoload :Omb, "openmath/v3/omb"
13
+ autoload :Omstr, "openmath/v3/omstr"
14
+ autoload :Omf, "openmath/v3/omf"
15
+ autoload :Oma, "openmath/v3/oma"
16
+ autoload :Ombind, "openmath/v3/ombind"
17
+ autoload :Ombvar, "openmath/v3/ombvar"
18
+ autoload :Ome, "openmath/v3/ome"
19
+ autoload :Omattr, "openmath/v3/omattr"
20
+ autoload :Omatp, "openmath/v3/omatp"
21
+ autoload :Omr, "openmath/v3/omr"
22
+ autoload :Omforeign, "openmath/v3/omforeign"
23
+ autoload :Omobj, "openmath/v3/omobj"
24
+
25
+ module_function
26
+
27
+ # Registers every element class with this version's context, once per
28
+ # process (the Mml::VersionedParser pattern).
29
+ def register_models!
30
+ return if @registered
31
+
32
+ Configuration.register_model(Oms, id: :oms)
33
+ Configuration.register_model(Omv, id: :omv)
34
+ Configuration.register_model(Omi, id: :omi)
35
+ Configuration.register_model(Omb, id: :omb)
36
+ Configuration.register_model(Omstr, id: :omstr)
37
+ Configuration.register_model(Omf, id: :omf)
38
+ Configuration.register_model(Oma, id: :oma)
39
+ Configuration.register_model(Ombind, id: :ombind)
40
+ Configuration.register_model(Ombvar, id: :ombvar)
41
+ Configuration.register_model(Ome, id: :ome)
42
+ Configuration.register_model(Omattr, id: :omattr)
43
+ Configuration.register_model(Omatp, id: :omatp)
44
+ Configuration.register_model(Omforeign, id: :omforeign)
45
+ Configuration.register_model(Omr, id: :omr)
46
+ Configuration.register_model(Omobj, id: :omobj)
47
+ @registered = true
48
+ end
49
+
50
+ def parse(input)
51
+ register_models!
52
+ Omobj.from_xml(input, register: Configuration::CONTEXT_ID)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module Openmath
6
+ module Validation
7
+ # A schema violation: the message plus the node path it occurred at.
8
+ Error = Struct.new(:message, :path) do
9
+ alias to_s message
10
+ end
11
+
12
+ class ValidationError < StandardError
13
+ attr_reader :errors
14
+
15
+ def initialize(errors)
16
+ @errors = errors
17
+ super(errors.map(&:message).join("; "))
18
+ end
19
+ end
20
+
21
+ SCHEMA_FILES = {
22
+ 2 => "openmath2.rng",
23
+ 3 => "openmath3.rng",
24
+ }.freeze
25
+
26
+ module_function
27
+
28
+ # The vendored RelaxNG schema for the version, parsed once per process.
29
+ def schema(version)
30
+ file = SCHEMA_FILES.fetch(version) do
31
+ raise ArgumentError, "unsupported OpenMath version #{version.inspect}"
32
+ end
33
+ @schemas ||= {}
34
+ @schemas[version] ||= Nokogiri::XML::RelaxNG(
35
+ File.open(File.expand_path("../../schemas/#{file}", __dir__)),
36
+ )
37
+ end
38
+
39
+ # Validates the document against the version's schema. Returns the list
40
+ # of Error objects; empty when the document is valid.
41
+ def validate(xml, version: 2)
42
+ document = Nokogiri::XML(xml) { |config| config.norecover }
43
+ raise ValidationError, [Error.new("malformed XML", "/")] if document.errors.any?
44
+
45
+ schema(version).validate(document).map do |e|
46
+ Error.new(e.message, e.path)
47
+ end
48
+ end
49
+
50
+ # Validates and raises ValidationError carrying every violation.
51
+ def validate!(xml, version: 2)
52
+ errors = validate(xml, version: version)
53
+ raise ValidationError, errors unless errors.empty?
54
+
55
+ true
56
+ end
57
+
58
+ def valid?(xml, version: 2)
59
+ validate(xml, version: version).empty?
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Openmath
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end