Last active
March 18, 2025 00:38
-
-
Save lmmx/f36d63b42840d7168f678dc09ebab793 to your computer and use it in GitHub Desktop.
FlatZinc-JSON format (JSONSchema) converted to Pydantic models https://www.minizinc.org/schemas/fznjson/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pyproject.toml | |
uv.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from enum import Enum | |
from typing import Any | |
from pydantic import BaseModel, Field, RootModel | |
class Type(Enum): | |
bool = "bool" | |
float = "float" | |
int = "int" | |
set_of_int = "set of int" | |
class Method(Enum): | |
satisfy = "satisfy" | |
minimize = "minimize" | |
maximize = "maximize" | |
class Identifier(RootModel): | |
root: str = Field(..., pattern="[A-Za-z][A-Za-z0-9_]*") | |
class Literal1(BaseModel): | |
set: list[list[float]] | |
class Literal2(BaseModel): | |
string: str | |
class Literal(RootModel): | |
root: float | Identifier | bool | Literal1 | Literal2 | |
class Annotations(RootModel): | |
root: list[Annotation] | |
class Argument(RootModel): | |
root: Union[Literal, list[Literal]] | |
class Domain(RootModel): | |
root: list[float] = Field(min_length=2, max_length=2) | |
class Variables(BaseModel): | |
type: Type | |
domain: Domain | None = None | |
rhs: Literal | None = None | |
introduced: bool | None = None | |
defined: bool | None = None | |
ann: Annotations | None = None | |
class Arrays(BaseModel): | |
a: list[Literal] | |
ann: Annotations | None = None | |
introduced: bool | None = None | |
defined: bool | None = None | |
class Constraint(BaseModel): | |
id: Identifier | |
args: list[Argument] | |
ann: Annotations | None = None | |
defines: Identifier | None = None | |
class Solve(BaseModel): | |
method: Method | |
objective: Literal | None = None | |
ann: Annotations | None = None | |
class FlatZincJSON(BaseModel): | |
""" | |
A JSON representation of a FlatZinc model | |
""" | |
version: str | |
variables: dict[str, Variables] | |
arrays: dict[str, Arrays] | |
constraints: list[Constraint] | |
output: list[Identifier] | |
solve: Solve | |
class Annotation(RootModel): | |
root: AnnotationCall | str | |
class AnnotationArgument(RootModel): | |
root: AnnotationLiterals | AnnotationLiteral | |
class AnnotationCall(BaseModel): | |
id: Identifier | |
args: list[AnnotationArgument] | |
class AnnotationLiterals(RootModel): | |
root: list[AnnotationLiteral] | |
class AnnotationLiteral(RootModel): | |
root: Literal | AnnotationCall |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"$id": "https://www.minizinc.org/schemas/fznjson", | |
"title": "FlatZincJSON", | |
"description": "A JSON representation of a FlatZinc model", | |
"$defs": { | |
"identifier": { "type": "string", "pattern": "[A-Za-z][A-Za-z0-9_]*" }, | |
"literal": { | |
"oneOf": [ | |
{ "type": "number" }, | |
{ "$ref": "#/$defs/identifier" }, | |
{ "type": "boolean" }, | |
{ | |
"type": "object", | |
"properties": { | |
"set": { | |
"type": "array", | |
"items": { | |
"type": "array", | |
"items": [{ "type": "number" }, { "type": "number" }] | |
} | |
} | |
}, | |
"required": ["set"] | |
}, | |
{ | |
"type": "object", | |
"properties": { | |
"string": { "type": "string" } | |
}, | |
"required": ["string"] | |
} | |
] | |
}, | |
"literals": { "type": "array", "items": { "$ref": "#/$defs/literal" } }, | |
"argument": { | |
"oneOf": [{ "$ref": "#/$defs/literals" }, { "$ref": "#/$defs/literal" }] | |
}, | |
"annotation": { | |
"oneOf": [{ "$ref": "#/$defs/annotationCall" }, { "type": "string" }] | |
}, | |
"annotations": { | |
"type": "array", | |
"items": { "$ref": "#/$defs/annotation" } | |
}, | |
"annotationArgument": { | |
"oneOf": [ | |
{ "$ref": "#/$defs/annotationLiterals" }, | |
{ "$ref": "#/$defs/annotationLiteral" } | |
] | |
}, | |
"annotationCall": { | |
"type": "object", | |
"properties": { | |
"id": { "$ref": "#/$defs/identifier" }, | |
"args": { | |
"type": "array", | |
"items": { "$ref": "#/$defs/annotationArgument" } | |
} | |
}, | |
"required": ["id", "args"] | |
}, | |
"annotationLiterals": { | |
"type": "array", | |
"items": { "$ref": "#/$defs/annotationLiteral" } | |
}, | |
"annotationLiteral": { | |
"oneOf": [ | |
{ "$ref": "#/$defs/literal" }, | |
{ "$ref": "#/$defs/annotationCall" } | |
] | |
}, | |
"domain": { | |
"type": "array", | |
"items": { | |
"type": "array", | |
"items": [{ "type": "number" }, { "type": "number" }] | |
} | |
} | |
}, | |
"type": "object", | |
"properties": { | |
"version": { "type": "string" }, | |
"variables": { | |
"type": "object", | |
"patternProperties": { | |
"[A-Za-z][A-Za-z0-9_]*": { | |
"type": "object", | |
"properties": { | |
"type": { | |
"enum": ["bool", "float", "int", "set of int"] | |
}, | |
"domain": { "$ref": "#/$defs/domain" }, | |
"rhs": { "$ref": "#/$defs/literal" }, | |
"introduced": { | |
"type": "boolean" | |
}, | |
"defined": { | |
"type": "boolean" | |
}, | |
"ann": { "$ref": "#/$defs/annotations" } | |
}, | |
"required": ["type"] | |
} | |
} | |
}, | |
"arrays": { | |
"type": "object", | |
"patternProperties": { | |
"[A-Za-z][A-Za-z0-9_]*": { | |
"type": "object", | |
"properties": { | |
"a": { | |
"type": "array", | |
"items": { "$ref": "#/$defs/literal" } | |
}, | |
"ann": { "$ref": "#/$defs/annotations" }, | |
"introduced": { | |
"type": "boolean" | |
}, | |
"defined": { | |
"type": "boolean" | |
} | |
}, | |
"required": ["a"] | |
} | |
} | |
}, | |
"constraints": { | |
"type": "array", | |
"items": { | |
"type": "object", | |
"properties": { | |
"id": { "$ref": "#/$defs/identifier" }, | |
"args": { | |
"type": "array", | |
"items": { "$ref": "#/$defs/argument" } | |
}, | |
"ann": { "$ref": "#/$defs/annotations" }, | |
"defines": { "$ref": "#/$defs/identifier" } | |
}, | |
"required": ["id", "args"] | |
} | |
}, | |
"output": { | |
"type": "array", | |
"items": { "$ref": "#/$defs/identifier" } | |
}, | |
"solve": { | |
"type": "object", | |
"properties": { | |
"method": { | |
"enum": ["satisfy", "minimize", "maximize"] | |
}, | |
"objective": { "$ref": "#/$defs/literal" }, | |
"ann": { "$ref": "#/$defs/annotations" } | |
}, | |
"required": ["method"] | |
} | |
}, | |
"required": [ | |
"version", | |
"variables", | |
"arrays", | |
"output", | |
"constraints", | |
"solve" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment