Skip to content

Commit 6afa12d

Browse files
committed
example -> examples in schema
1 parent 650add1 commit 6afa12d

File tree

5 files changed

+119
-31
lines changed

5 files changed

+119
-31
lines changed

mof.schema.json

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
}
207207
}, {
208208
"description": "A real-valued numeric constant",
209-
"example": "{\"head\": \"real\", \"value\": 1.0}",
209+
"examples": ["{\"head\": \"real\", \"value\": 1.0}"],
210210
"required": ["value"],
211211
"properties": {
212212
"head": {
@@ -218,7 +218,7 @@
218218
}
219219
}, {
220220
"description": "A complex-valued numeric constant",
221-
"example": "{\"head\": \"complex\", \"real\": 1.0, \"imag\": 2.0}",
221+
"examples": ["{\"head\": \"complex\", \"real\": 1.0, \"imag\": 2.0}"],
222222
"required": ["real", "imag"],
223223
"properties": {
224224
"head": {
@@ -233,7 +233,7 @@
233233
}
234234
}, {
235235
"description": "A reference to an optimization variable",
236-
"example": "{\"head\": \"variable\", \"name\": \"x\"}",
236+
"examples": ["{\"head\": \"variable\", \"name\": \"x\"}"],
237237
"required": ["name"],
238238
"properties": {
239239
"head": {
@@ -245,7 +245,7 @@
245245
}
246246
}, {
247247
"description": "A pointer to a (1-indexed) element in the `node_list` field in a nonlinear function",
248-
"example": "{\"head\": \"node\", \"index\": 2}",
248+
"examples": ["{\"head\": \"node\", \"index\": 2}"],
249249
"required": ["index"],
250250
"properties": {
251251
"head": {
@@ -264,7 +264,7 @@
264264
"required": ["head"],
265265
"oneOf": [{
266266
"description": "The scalar variable `variable`.",
267-
"example": "{\"head\": \"SingleVariable\", \"variable\": \"x\"}",
267+
"examples": ["{\"head\": \"SingleVariable\", \"variable\": \"x\"}"],
268268
"required": ["variable"],
269269
"properties": {
270270
"head": {
@@ -276,7 +276,7 @@
276276
}
277277
}, {
278278
"description": "The function `a'x + b`, where `a` is a sparse vector specified by a list of `ScalarAffineTerm`s in `terms` and `b` is the scalar in `constant`. Duplicate variables in `terms` are accepted, and the corresponding coefficients are summed together.",
279-
"example": "{\"head\": \"ScalarAffineFunction\", \"constant\": 1.0, \"terms\": [{\"coefficient\": 2.5, \"variable\": \"x\"}]}",
279+
"examples": ["{\"head\": \"ScalarAffineFunction\", \"constant\": 1.0, \"terms\": [{\"coefficient\": 2.5, \"variable\": \"x\"}]}"],
280280
"required": ["constant", "terms"],
281281
"properties": {
282282
"head": {
@@ -294,7 +294,7 @@
294294
}
295295
}, {
296296
"description": "The function `0.5x'Qx + a'x + b`, where `a` is a sparse vector of `ScalarAffineTerm`s in `affine_terms`, `b` is the scalar `constant`, and `Q` is a symmetric matrix specified by a list of `ScalarQuadraticTerm`s in `quadratic_terms`. Duplicate indices in `affine_terms` and `quadratic` are accepted, and the corresponding coefficients are summed together. Mirrored indices in `quadratic_terms` (i.e., `(i,j)` and `(j, i)`) are considered duplicates; only one need to be specified.",
297-
"example": "{\"head\": \"ScalarAffineFunction\", \"constant\": 1.0, \"affine_terms\": [{\"coefficient\": 2.5, \"variable\": \"x\"}], \"quadratic_terms\": [{\"coefficient\": 2.0, \"variable_1\": \"x\", \"variable_2\": \"y\"}]}",
297+
"examples": ["{\"head\": \"ScalarAffineFunction\", \"constant\": 1.0, \"affine_terms\": [{\"coefficient\": 2.5, \"variable\": \"x\"}], \"quadratic_terms\": [{\"coefficient\": 2.0, \"variable_1\": \"x\", \"variable_2\": \"y\"}]}"],
298298
"required": ["constant", "affine_terms", "quadratic_terms"],
299299
"properties": {
300300
"head": {
@@ -341,7 +341,7 @@
341341
"required": ["head"],
342342
"oneOf": [{
343343
"description": "An ordered list of variables.",
344-
"example": "{\"head\": \"VectorOfVariables\", \"variables\": [\"x\", \"y\"]}",
344+
"examples": ["{\"head\": \"VectorOfVariables\", \"variables\": [\"x\", \"y\"]}"],
345345
"required": ["variables"],
346346
"properties": {
347347
"head": {
@@ -356,7 +356,7 @@
356356
}
357357
}, {
358358
"description": "The function `Ax + b`, where `A` is a sparse matrix specified by a list of `VectorAffineTerm`s in `terms` and `b` is a dense vector specified by `constants`.",
359-
"example": "{\"head\": \"VectorAffineFunction\", \"constants\": [1.0], \"terms\": [{\"output_index\": 1, \"scalar_term\": {\"coefficient\": 2.5, \"variable\": \"x\"}}]}",
359+
"examples": ["{\"head\": \"VectorAffineFunction\", \"constants\": [1.0], \"terms\": [{\"output_index\": 1, \"scalar_term\": {\"coefficient\": 2.5, \"variable\": \"x\"}}]}"],
360360
"required": ["constants", "terms"],
361361
"properties": {
362362
"head": {
@@ -409,7 +409,7 @@
409409
"required": ["head"],
410410
"oneOf": [{
411411
"description": "(-∞, upper]",
412-
"example": "{\"head\": \"LessThan\", \"upper\": 2.1}",
412+
"examples": ["{\"head\": \"LessThan\", \"upper\": 2.1}"],
413413
"required": ["upper"],
414414
"properties": {
415415
"head": {
@@ -421,7 +421,7 @@
421421
}
422422
}, {
423423
"description": "[lower, ∞)",
424-
"example": "{\"head\": \"GreaterThan\", \"lower\": 2.1}",
424+
"examples": ["{\"head\": \"GreaterThan\", \"lower\": 2.1}"],
425425
"required": ["lower"],
426426
"properties": {
427427
"head": {
@@ -433,7 +433,7 @@
433433
}
434434
}, {
435435
"description": "{value}",
436-
"example": "{\"head\": \"EqualTo\", \"value\": 2.1}",
436+
"examples": ["{\"head\": \"EqualTo\", \"value\": 2.1}"],
437437
"required": ["value"],
438438
"properties": {
439439
"head": {
@@ -445,7 +445,7 @@
445445
}
446446
}, {
447447
"description": "[lower, upper]",
448-
"example": "{\"head\": \"Interval\", \"lower\": 2.1, \"upper\": 3.4}",
448+
"examples": ["{\"head\": \"Interval\", \"lower\": 2.1, \"upper\": 3.4}"],
449449
"required": ["lower", "upper"],
450450
"properties": {
451451
"head": {
@@ -460,7 +460,7 @@
460460
}
461461
}, {
462462
"description": "{0} ∪ {lower, lower + 1, ..., upper}",
463-
"example": "{\"head\": \"Semiinteger\", \"lower\": 2, \"upper\": 4}",
463+
"examples": ["{\"head\": \"Semiinteger\", \"lower\": 2, \"upper\": 4}"],
464464
"required": ["lower", "upper"],
465465
"properties": {
466466
"head": {
@@ -475,7 +475,7 @@
475475
}
476476
}, {
477477
"description": "{0} ∪ [lower, upper]",
478-
"example": "{\"head\": \"Semicontinuous\", \"lower\": 2.1, \"upper\": 3.4}",
478+
"examples": ["{\"head\": \"Semicontinuous\", \"lower\": 2.1, \"upper\": 3.4}"],
479479
"required": ["lower", "upper"],
480480
"properties": {
481481
"head": {
@@ -490,15 +490,15 @@
490490
}
491491
}, {
492492
"description": "{0, 1}",
493-
"example": "{\"head\": \"ZeroOne\"}",
493+
"examples": ["{\"head\": \"ZeroOne\"}"],
494494
"properties": {
495495
"head": {
496496
"const": "ZeroOne"
497497
}
498498
}
499499
}, {
500500
"description": "",
501-
"example": "{\"head\": \"Integer\"}",
501+
"examples": ["{\"head\": \"Integer\"}"],
502502
"properties": {
503503
"head": {
504504
"const": "Integer"
@@ -512,23 +512,23 @@
512512
"required": ["head"],
513513
"oneOf": [{
514514
"description": "[x, y, z] ∈ {R³: y * exp(x / y) ≤ z, y ≥ 0}",
515-
"example": "{\"head\": \"ExponentialCone\"}",
515+
"examples": ["{\"head\": \"ExponentialCone\"}"],
516516
"properties": {
517517
"head": {
518518
"const": "ExponentialCone"
519519
}
520520
}
521521
}, {
522522
"description": "[u, v, w] ∈ {R³: -u * exp(v / u) ≤ exp(1) * w, u < 0}",
523-
"example": "{\"head\": \"DualExponentialCone\"}",
523+
"examples": ["{\"head\": \"DualExponentialCone\"}"],
524524
"properties": {
525525
"head": {
526526
"const": "DualExponentialCone"
527527
}
528528
}
529529
}, {
530530
"description": "A special ordered set of type I.",
531-
"example": "{\"head\": \"SOS1\", \"weights\": [1, 3, 2]}",
531+
"examples": ["{\"head\": \"SOS1\", \"weights\": [1, 3, 2]}"],
532532
"required": ["weights"],
533533
"properties": {
534534
"head": {
@@ -543,7 +543,7 @@
543543
}
544544
}, {
545545
"description": "A special ordered set of type II.",
546-
"example": "{\"head\": \"SOS2\", \"weights\": [1, 3, 2]}",
546+
"examples": ["{\"head\": \"SOS2\", \"weights\": [1, 3, 2]}"],
547547
"required": ["weights"],
548548
"properties": {
549549
"head": {
@@ -558,7 +558,7 @@
558558
}
559559
}, {
560560
"description": "[t, x] ∈ {R^{dimension}: t ≤ (Πxᵢ)^{1 / (dimension-1)}}",
561-
"example": "{\"head\": \"GeometricMeanCone\", \"dimension\": 3}",
561+
"examples": ["{\"head\": \"GeometricMeanCone\", \"dimension\": 3}"],
562562
"required": ["dimension"],
563563
"properties": {
564564
"head": {
@@ -571,7 +571,7 @@
571571
}
572572
}, {
573573
"description": "[t, x] ∈ {R^{dimension} : t ≥ ||x||₂",
574-
"example": "{\"head\": \"SecondOrderCone\", \"dimension\": 3}",
574+
"examples": ["{\"head\": \"SecondOrderCone\", \"dimension\": 3}"],
575575
"required": ["dimension"],
576576
"properties": {
577577
"head": {
@@ -584,7 +584,7 @@
584584
}
585585
}, {
586586
"description": "[t, u, x] ∈ {R^{dimension} : 2tu ≥ (||x||₂)²; t, u ≥ 0}",
587-
"example": "{\"head\": \"RotatedSecondOrderCone\", \"dimension\": 3}",
587+
"examples": ["{\"head\": \"RotatedSecondOrderCone\", \"dimension\": 3}"],
588588
"required": ["dimension"],
589589
"properties": {
590590
"head": {
@@ -597,7 +597,7 @@
597597
}
598598
}, {
599599
"description": "{0}^{dimension}",
600-
"example": "{\"head\": \"Zeros\", \"dimension\": 3}",
600+
"examples": ["{\"head\": \"Zeros\", \"dimension\": 3}"],
601601
"required": ["dimension"],
602602
"properties": {
603603
"head": {
@@ -610,7 +610,7 @@
610610
}
611611
}, {
612612
"description": "R^{dimension}",
613-
"example": "{\"head\": \"Reals\", \"dimension\": 3}",
613+
"examples": ["{\"head\": \"Reals\", \"dimension\": 3}"],
614614
"required": ["dimension"],
615615
"properties": {
616616
"head": {
@@ -623,7 +623,7 @@
623623
}
624624
}, {
625625
"description": "R₋^{dimension}",
626-
"example": "{\"head\": \"Nonpositives\", \"dimension\": 3}",
626+
"examples": ["{\"head\": \"Nonpositives\", \"dimension\": 3}"],
627627
"required": ["dimension"],
628628
"properties": {
629629
"head": {
@@ -636,7 +636,7 @@
636636
}
637637
}, {
638638
"description": "R₊^{dimension}",
639-
"example": "{\"head\": \"Nonnegatives\", \"dimension\": 3}",
639+
"examples": ["{\"head\": \"Nonnegatives\", \"dimension\": 3}"],
640640
"required": ["dimension"],
641641
"properties": {
642642
"head": {
@@ -665,7 +665,7 @@
665665
}
666666
}, {
667667
"description": "[x, y, z] ∈ {R³: x^{exponent} y^{1-exponent} ≥ |z|; x, y ≥ 0}",
668-
"example": "{\"head\": \"PowerCone\", \"exponent\": 2.0}",
668+
"examples": ["{\"head\": \"PowerCone\", \"exponent\": 2.0}"],
669669
"required": ["exponent"],
670670
"properties": {
671671
"head": {
@@ -677,7 +677,7 @@
677677
}
678678
}, {
679679
"description": "[u, v, w] ∈ {R³: (u / exponent)^{exponent} (v / (1-exponent))^{1-exponent} ≥ |w|; u, v ≥ 0}",
680-
"example": "{\"head\": \"DualPowerCone\", \"exponent\": 2.0}",
680+
"examples": ["{\"head\": \"DualPowerCone\", \"exponent\": 2.0}"],
681681
"required": ["exponent"],
682682
"properties": {
683683
"head": {

src/mof/generate-static-schema.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"encoding/base64"
7+
"io/ioutil"
8+
"os"
9+
)
10+
11+
func main() {
12+
filename := os.Args[1]
13+
bytes, err := ioutil.ReadFile(filename)
14+
if err != nil {
15+
panic(err)
16+
}
17+
base64str := base64.StdEncoding.EncodeToString(bytes)
18+
packageString :=
19+
"// Code generated by go generate; DO NOT EDIT.\n" +
20+
"package main\n" +
21+
"\n" +
22+
"var jsonSchema64 = \"" + base64str + "\""
23+
if err := ioutil.WriteFile("staticSchema.go", []byte(packageString), os.ModePerm); err != nil {
24+
panic(err)
25+
}
26+
}

src/mof/mof.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"flag"
7+
"fmt"
8+
"io/ioutil"
9+
"github.com/qri-io/jsonschema"
10+
)
11+
12+
//go:generate go run generate-static-schema.go ..\\..\\mof.schema.json
13+
func main() {
14+
validateFlag := flag.Bool(
15+
"validate", true,
16+
"Validate the input JSON file against the MathOptFormat schema")
17+
flag.Parse()
18+
19+
tail := flag.Args()
20+
if len(tail) != 1 {
21+
fmt.Println("Invalid arguments")
22+
flag.PrintDefaults()
23+
return
24+
}
25+
filename := tail[0]
26+
27+
if *validateFlag {
28+
if err := validateFile(filename); err != nil {
29+
fmt.Printf("%s is not a valid MOF file\nThe error is:\n", filename)
30+
fmt.Println(err)
31+
} else {
32+
fmt.Printf("Success! %s conforms to the MathOptFormat schema", filename)
33+
}
34+
}
35+
return
36+
}
37+
38+
func validateFile(filename string) error {
39+
schemaBytes, err := base64.StdEncoding.DecodeString(jsonSchema64)
40+
if err != nil {
41+
fmt.Println("Unable to decode JSON schema")
42+
return err
43+
}
44+
rs := &jsonschema.RootSchema{}
45+
if err := json.Unmarshal(schemaBytes, rs); err != nil {
46+
fmt.Println("Unable to unmarshall schema")
47+
return err
48+
}
49+
modelData, err := ioutil.ReadFile(filename)
50+
if err != nil {
51+
fmt.Printf("Unable to read %s\n", filename)
52+
return err
53+
}
54+
if errs, _ := rs.ValidateBytes(modelData); len(errs) > 0 {
55+
fmt.Printf("Error validating file")
56+
for _, err = range errs {
57+
fmt.Println(err.Error())
58+
}
59+
return errs[0]
60+
}
61+
return nil
62+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func processNonlinear(data map[string]interface{}) string {
157157
"##### Functions",
158158
"",
159159
"| Name | Arity |",
160-
"| ---- | ----- |"}
160+
"| ---- | ----- |"}
161161

162162
leafStrings := []string{
163163
"#### Nonlinear functions",

0 commit comments

Comments
 (0)