lingologos-struc / tools /make_nodes.py
tailot's picture
update structures bundle
024d660 verified
Raw History Blame Contribute Delete
10.9 kB
#!/usr/bin/env python3
"""Write the thirteen node folders of the structures topology.
Three files per node, the same shape the math and physics datasets have:
``grammar.gf`` the controlled language, as documentation. The reference implementations are
``lingologos_struc/parse_a.py`` (road A, question head first) and ``lingologos_struc/parse_b.py``
(road B, declaration first); the file states the language and the role this node
plays in it.
``ontology.smt2`` what Z3 is told before this node is checked: the timeout, and nothing else. A
structure has no physical constant to remember -- every number of a claim was read
from the declaration of the problem -- so the ontology supplies only the clock.
``seed_payload.json`` the envelope this node starts with. Only the arbiter starts with a problem,
because the arbiter is the node the first arc of the routing matrix points at;
the others are idle.
Run it after changing the language or the sample problem::
python struc/tools/make_nodes.py
"""
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from lingologos_struc import agents # noqa: E402
NODES = ROOT / "nodes"
# The question the topology ships with. tools/ask.py replaces it with the question being measured.
SAMPLE_PROBLEM = (
"reactions frame nodes [ 1 0 0 ; 2 2 0 ; 3 4 0 ] members [ 1 1 2 ; 2 2 3 ] "
"props [ 1 210000000 1/5 2/1875 ; 2 210000000 1/5 2/1875 ] "
"supports [ 1 pin ; 3 roller 0 1 ] loads [ member 1 udl 0 -15 ; member 2 udl 0 -15 ]")
SAMPLE_TREE = ("Reactions (Frame (Nodes (Row 1 0 0) (Row 2 2 0) (Row 3 4 0)) "
"(Members (Row 1 1 2) (Row 2 2 3)) "
"(Props (Row 1 210000000 1/5 2/1875) (Row 2 210000000 1/5 2/1875)) "
"(Supports (Pin 1) (Roller 3 0 1)) "
"(Loads (Udl 1 0 -15) (Udl 2 0 -15)) (Hinges None))")
ROLE = {
"reader_a": "First reader: reads the sentence along road A, the question head first, and reports "
"the structure and every number it read.",
"reader_b": "Second reader: reads the same sentence along road B, the declaration first, and "
"reports the same reading by the other route.",
"axioms_a": "Axioms of the deformation: says which idealisations the question rests on -- small "
"displacements, linear elasticity, plane sections staying plane -- and pins the "
"numbers that keep them legitimate.",
"axioms_b": "Axioms of the regime: says which law the question is inside -- Jourawski for shear, "
"Euler for a slender column, a closed form for a section -- and pins the guard numbers "
"that keep that law inside its regime.",
"convention_a": "Convention of the signs: rotates every member into its own axis, pins the exact "
"direction cosines, and checks that the answer is written with the sign convention "
"of this dataset.",
"convention_b": "Convention of the units: audits the dimension of every number the reading carries "
"and pins the audit, so a millimetre mixed with a metre cannot pass for a number.",
"equivalence": "Comparator: says whether the two readings, the two roads and the two conventions "
"close over the same statement, and writes the sentences the family decided to "
"delegate to mathematics.",
"coverage": "Coverer: echoes every number of the delegated sentences against the facts of the "
"question, and says what mathematics is asked to settle.",
"solver_a": "Solver of the stiffness: solves the frame by the direct stiffness method, asks "
"mathematics the delegated sentences, and reports the numbers of that road.",
"solver_b": "Solver of the forces: solves the same frame by the method of the forces and by the "
"theorems of the virtual work, and reports the numbers of the other road.",
"verifier": "Verifier: rebuilds the conjunction of every claim the constructive nodes closed with "
"and reports whether that conjunction is consistent.",
"adversary": "Adversary: takes the reported answer, moves one of its two roads by exactly one, and "
"reports whether the chain would have noticed the move.",
"arbiter": "Arbiter: applies the decision rule of this dataset to the records of the chain and "
"states the status, the assurance and the answer in one line.",
}
GRAMMAR = """-- Node %s (%s): %s
-- StrucLang: abstract syntax of the controlled language of the structures topology.
-- It documents the language; the reference implementations are lingologos_struc/parse_a.py (road A, question
-- head first) and lingologos_struc/parse_b.py (road B, declaration first). Both roads have to reach the same
-- canonical reading, which is what the equivalence node compares.
-- NOTE: this specification has not been compiled with the GF toolchain in the authoring environment.
-- In the abstractSyntaxTree string literals and numbers are written bare (2, 1/5, -15) instead of
-- (Lit 2), a bracketed list of rows is written (Rows (Row 1 0 0) (Row 2 2 0)), and every rational is
-- exact: a decimal point in a sentence of this language is a mistake, not a rounding.
abstract StrucLang = {
cat
Problem ; Frame ; Section ; Rows ; Row ; Support ; Load ; Dir ; Law ; Ends ; Opt ;
fun
-- the eight questions of the fragment
Classify : Frame -> Problem ;
Reactions : Frame -> Problem ;
Cut : Num -> Num -> Frame -> Problem ;
Peak : Law -> Frame -> Problem ;
DeflectNode : Num -> Dir -> Frame -> Problem ;
DeflectMember : Num -> Num -> Dir -> Frame -> Problem ;
Verify : Law -> Num -> Num -> Section -> Num -> Frame -> Problem ;
Properties : Section -> Problem ;
Euler : Ends -> Ends -> Num -> Num -> Section -> Problem ;
-- the declaration of a planar frame: five nodes and eight members at most, exact cosines only
MkFrame : Rows -> Rows -> Rows -> Rows -> Rows -> Opt -> Frame ;
-- a section is one rectangle, or a pile of rectangles given by width, height and centre
Rect : Num -> Num -> Section ;
RectAt : Num -> Num -> Num -> Row ;
Shapes : Rows -> Section ;
-- rows of the frame block: id x y | id from to | id E A I | id kind | member kind ...
Node : Num -> Num -> Num -> Row ;
Member : Num -> Num -> Num -> Row ;
Prop : Num -> Num -> Num -> Num -> Row ;
Pin : Num -> Support ;
Roller : Num -> Num -> Num -> Support ;
Fixed : Num -> Support ;
Udl : Num -> Num -> Num -> Load ;
Point : Num -> Num -> Num -> Num -> Load ;
Moment : Num -> Num -> Num -> Num -> Load ;
NodeForce : Num -> Num -> Num -> Load ;
Bending, Shear, Axial, VonMises : Law ;
Pinned, FixedEnd : Ends ;
Vertical, Horizontal, Rotation : Dir ;
Some : Num -> Opt ;
None : Opt ;
RowsOf : Row -> Rows -> Rows ;
Empty : Rows ;
Num : String ;
}
concrete StrucLangEng of StrucLang = {
lincat
Problem, Frame, Section, Rows, Row, Support, Load, Dir, Law, Ends, Opt, Num = Str ;
lin
Classify f = "classify" ++ f ;
Reactions f = "reactions" ++ f ;
Cut m p f = "cut" ++ "member" ++ m ++ "at" ++ p ++ f ;
Peak l f = "peak" ++ l ++ f ;
DeflectNode n d f = "deflect" ++ "node" ++ n ++ d ++ f ;
DeflectMember m p d f = "deflect" ++ "member" ++ m ++ "at" ++ p ++ d ++ f ;
Verify l m p s q f = "verify" ++ l ++ "member" ++ m ++ "at" ++ p
++ "section" ++ s ++ "limit" ++ q ++ f ;
Properties s = "properties" ++ "section" ++ s ;
Euler e1 e2 l e s = "euler" ++ "ends" ++ e1 ++ e2 ++ "length" ++ l
++ "modulus" ++ e ++ "section" ++ s ;
MkFrame n m p s l h = "frame" ++ "nodes" ++ n ++ "members" ++ m ++ "props" ++ p
++ "supports" ++ s ++ "loads" ++ l ++ h ;
Rect w h = "rect" ++ w ++ h ;
RectAt w h c = w ++ h ++ c ;
Shapes r = "shapes" ++ "[" ++ r ++ "]" ;
Node i x y = "[" ++ i ++ x ++ y ++ "]" ;
Member i a b = "[" ++ i ++ a ++ b ++ "]" ;
Prop i e a ii = "[" ++ i ++ e ++ a ++ ii ++ "]" ;
Pin i = i ++ "pin" ;
Roller i x y = i ++ "roller" ++ x ++ y ;
Fixed i = i ++ "fixed" ;
Udl m a b = "[" ++ m ++ "udl" ++ a ++ b ++ "]" ;
Point m p x y = "[" ++ m ++ "point" ++ p ++ x ++ y ++ "]" ;
Moment m p x y = "[" ++ m ++ "couple" ++ p ++ x ++ y ++ "]" ;
NodeForce n x y = "[" ++ n ++ "force" ++ x ++ y ++ "]" ;
Bending = "bending" ; Shear = "shear" ; Axial = "axial" ; VonMises = "vonmises" ;
Pinned = "pinned" ; FixedEnd = "fixed" ;
Vertical = "vertical" ; Horizontal = "horizontal" ; Rotation = "rotation" ;
Some n = "hinges" ++ "[" ++ n ++ "]" ;
None = "" ;
RowsOf r rest = r ++ rest ;
Empty = "" ;
Num = "a number written as an exact ratio: 2, -15, 1/5, 21/10" ;
}
"""
def grammar(node: str) -> str:
return (GRAMMAR % (node, node, ROLE[node])) + "\n"
def ontology(node: str) -> str:
return """; SMT-LIB 2 ontology for the structures topology node %s.
; Real arithmetic is built into Z3, and a structure has no physical constant to remember: every
; number a node pins was read from the declaration of the problem, so the claim carries its own
; declarations and the ontology fixes only the clock, because one hard nonlinear query (the square
; of a slenderness, the square of a combined stress) must not block a whole session.
(set-option :timeout 8000)
""" % node + "\n"
def seed(node: str, idle: bool) -> str:
payload = {
"sender": node,
"turn": 0,
"volatileState": {"internalTimeTick": 0},
"payload": {
"naturalLanguage": "Idle until the problem arrives." if idle else SAMPLE_PROBLEM,
"abstractSyntaxTree": "Idle" if idle else SAMPLE_TREE,
"smtConstraints": {
"declarations": [],
"assertions": ["(assert true)"],
"z3Status": "sat",
},
},
}
return json.dumps(payload, indent=2) + "\n"
def main() -> int:
written = 0
for node in agents.ALL_AGENTS:
folder = NODES / node
folder.mkdir(parents=True, exist_ok=True)
(folder / "grammar.gf").write_text(grammar(node), encoding="utf-8")
(folder / "ontology.smt2").write_text(ontology(node), encoding="utf-8")
(folder / "seed_payload.json").write_text(seed(node, idle=node != "arbiter"), encoding="utf-8")
written += 3
print("wrote %s" % folder.relative_to(ROOT.parent))
print("%d files over %d nodes, sample problem: %s"
% (written, len(agents.ALL_AGENTS), SAMPLE_PROBLEM[:60]))
return 0
if __name__ == "__main__":
sys.exit(main())