math-spec-mapping 0.2.8__py3-none-any.whl → 0.3.0__py3-none-any.whl

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.
@@ -356,24 +356,31 @@ class MathSpec:
356
356
  return sm
357
357
 
358
358
  def _build_functional_parameters(self):
359
- opts = [x for x in self.policies.values() if len(x.policy_options) > 1]
359
+ opts = [
360
+ (x, x.policy_options)
361
+ for x in self.policies.values()
362
+ if len(x.policy_options) > 1
363
+ ]
360
364
  opts.extend(
361
365
  [
362
- x
366
+ (x, x.boundary_actions)
363
367
  for x in self.boundary_actions.values()
364
368
  if len(x.boundary_action_options) > 1
365
369
  ]
366
370
  )
367
371
  opts.extend(
368
372
  [
369
- x
373
+ (x, x.control_actions)
370
374
  for x in self.control_actions.values()
371
375
  if len(x.control_action_options) > 1
372
376
  ]
373
377
  )
374
378
  self.functional_parameters = {}
375
379
  for x in opts:
376
- self.functional_parameters["FP {}".format(x.name)] = x
380
+ x, y = x
381
+ self.functional_parameters["FP {}".format(x.name)] = {}
382
+ for y1 in y:
383
+ self.functional_parameters["FP {}".format(x.name)][y1.name] = y1
377
384
 
378
385
  def _build_parameter_types(self):
379
386
  system_parameters_types = {}
@@ -596,7 +603,145 @@ class MathSpec:
596
603
  with open(path, "w") as f:
597
604
  f.write(out)
598
605
 
606
+ def build_implementation(self, params):
607
+ return MathSpecImplementation(self, params)
608
+
599
609
 
600
610
  class MathSpecImplementation:
601
611
  def __init__(self, ms: MathSpec, params):
602
612
  self.ms = deepcopy(ms)
613
+ self.params = params
614
+ self.control_actions = self.load_control_actions()
615
+ self.boundary_actions = {}
616
+ self.policies = self.load_policies()
617
+ self.mechanisms = self.load_mechanisms()
618
+ self.load_wiring()
619
+
620
+ def load_control_actions(self):
621
+ control_actions = {}
622
+ for ca in self.ms.control_actions:
623
+ ca = self.ms.control_actions[ca]
624
+ opts = ca.control_action_options
625
+ if len(opts) == 0:
626
+ print("{} has no control action options".format(ca.name))
627
+ else:
628
+ if len(opts) == 1:
629
+ opt = opts[0]
630
+ else:
631
+ assert (
632
+ "FP {}".format(ca.name) in self.params
633
+ ), "No functional parameterization for {}".format(ca.name)
634
+ opt = self.ms.functional_parameters["FP {}".format(ca.name)][
635
+ self.params["FP {}".format(ca.name)]
636
+ ]
637
+
638
+ assert (
639
+ "python" in opt.implementations
640
+ ), "No python implementation for {} / {}".format(ca.name, opt.name)
641
+
642
+ control_actions[ca.name] = opt.implementations["python"]
643
+ return control_actions
644
+
645
+ def load_mechanisms(self):
646
+ mechanisms = {}
647
+ for m in self.ms.mechanisms:
648
+ m = self.ms.mechanisms[m]
649
+ if "python" not in m.implementations:
650
+ print("No python implementation for {}".format(m.name))
651
+ else:
652
+ mechanisms[m.name] = m.implementations["python"]
653
+ return mechanisms
654
+
655
+ def load_single_wiring(self, wiring):
656
+ components = [x.name for x in wiring.components]
657
+ if wiring.block_type == "Stack Block":
658
+
659
+ def wiring(state, params, spaces):
660
+ for component in components:
661
+ spaces = self.blocks[component](state, params, spaces)
662
+ return spaces
663
+
664
+ elif wiring.block_type == "Parallel Block":
665
+
666
+ spaces_mapping = {}
667
+ for x in wiring.components:
668
+ spaces_mapping[x.name] = []
669
+
670
+ for i, x in enumerate([x.name for x in wiring.domain_blocks]):
671
+ spaces_mapping[x].append(i)
672
+
673
+ def wiring(state, params, spaces):
674
+ codomain = []
675
+ for component in components:
676
+ spaces_i = [spaces[i] for i in spaces_mapping[component]]
677
+ spaces_i = self.blocks[component](state, params, spaces_i)
678
+ if spaces_i:
679
+ codomain.extend(spaces_i)
680
+ return codomain
681
+
682
+ else:
683
+ assert False
684
+
685
+ return wiring
686
+
687
+ def load_policies(self):
688
+ policies = {}
689
+ for p in self.ms.policies:
690
+ p = self.ms.policies[p]
691
+ opts = p.policy_options
692
+ if len(opts) == 0:
693
+ print("{} has no policy options".format(p.name))
694
+ else:
695
+ if len(opts) == 1:
696
+ opt = opts[0]
697
+ else:
698
+ assert (
699
+ "FP {}".format(p.name) in self.params
700
+ ), "No functional parameterization for {}".format(p.name)
701
+ opt = self.ms.functional_parameters["FP {}".format(p.name)][
702
+ self.params["FP {}".format(p.name)]
703
+ ]
704
+
705
+ if "python" not in opt.implementations:
706
+ print(
707
+ "No python implementation for {} / {}".format(p.name, opt.name)
708
+ )
709
+ else:
710
+ policies[p.name] = opt.implementations["python"]
711
+ return policies
712
+
713
+ def load_wiring(
714
+ self,
715
+ ):
716
+ self.blocks = {}
717
+ self.blocks.update(self.boundary_actions)
718
+ self.blocks.update(self.control_actions)
719
+ self.blocks.update(self.policies)
720
+ self.blocks.update(self.mechanisms)
721
+
722
+ self.wiring = {}
723
+
724
+ wiring = [x for x in self.ms.wiring.values()]
725
+
726
+ i = 1
727
+ while i > 0:
728
+ i = 0
729
+ hold = []
730
+ for w in wiring:
731
+ components = [x.name for x in w.components]
732
+ if all([x in self.blocks for x in components]):
733
+ i += 1
734
+ w2 = self.load_single_wiring(w)
735
+ assert w.name not in self.blocks, "{} was a repeated block".format(
736
+ w.name
737
+ )
738
+ if w2:
739
+ self.blocks[w.name] = w2
740
+ self.wiring[w.name] = w2
741
+
742
+ else:
743
+ hold.append(w)
744
+ wiring = hold
745
+ if len(wiring) > 0:
746
+ wiring = [x.name for x in wiring]
747
+ print("The following wirings were not loading: {}".format(wiring))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: math-spec-mapping
3
- Version: 0.2.8
3
+ Version: 0.3.0
4
4
  Summary: A library for easy mapping of mathematical specifications.
5
5
  Author-email: Sean McOwen <Sean@Block.Science>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -6,7 +6,7 @@ math_spec_mapping/Classes/Block.py,sha256=hXQO221IP-TqZm_TwFKfURpEEjZm7L1TPZDCYl
6
6
  math_spec_mapping/Classes/BoundaryAction.py,sha256=AOENCqCEfpjotnHhzUj_F2SOP0SGpkN1tNPr8Mtl6Tc,476
7
7
  math_spec_mapping/Classes/ControlAction.py,sha256=ysqpgANwdizVdwqtgZmnxXMpCqrzmVEF_6YT0M3l4Ho,526
8
8
  math_spec_mapping/Classes/Entity.py,sha256=fA0-b128_OHHxfCg4pzqyQV083EYev1HlVpy86S5igg,1226
9
- math_spec_mapping/Classes/MathSpec.py,sha256=gzTi7QSsFEWPTfHaz8YU0R7X5g4rVDFFBlKdBj4B0Pk,22244
9
+ math_spec_mapping/Classes/MathSpec.py,sha256=FTxArDTLt7PBwXE79VqhP7qIA4Gf6zgXAo_iQWOh_7U,27443
10
10
  math_spec_mapping/Classes/Mechanism.py,sha256=2sLm3wYBIeTQaMBcsJ9btqIWsbS895Ra8NY6Y9_G_Dg,379
11
11
  math_spec_mapping/Classes/Metric.py,sha256=AhPgYppOP6q49xvR8S9STxQsXUKJlTWx7wI1LfZEtww,581
12
12
  math_spec_mapping/Classes/Parameter.py,sha256=ZuJ_w0sChvRElJ4sOnXZ2EV4Ell2xXFulKLjVOpgz2E,1237
@@ -53,8 +53,8 @@ math_spec_mapping/Reports/spaces.py,sha256=-76hR5wQBv4lsG000ypBJ-OprjsNjI-rNRMYd
53
53
  math_spec_mapping/Reports/state.py,sha256=RSHDjzSiUj4ZjReWbkBW7k2njs3Ovp-q0rCC7GBfD-A,2203
54
54
  math_spec_mapping/Reports/tables.py,sha256=O0CNuqh3LMECq5uLjBOoxMUk5hUvkUK660FNnwWUxDY,1505
55
55
  math_spec_mapping/Reports/wiring.py,sha256=u9SvKWy6T-WJUEgFI6-zgZanoOaTTs_2YwmEceDLsV8,1618
56
- math_spec_mapping-0.2.8.dist-info/LICENSE,sha256=ObyEzSw8kgCaFbEfpu1zP4TrcAKLA0xhqHMZZfyh7N0,1069
57
- math_spec_mapping-0.2.8.dist-info/METADATA,sha256=hXBDv_nbuvouVAYXB0jpyKlzNmPFI9xyMjCHn8RvtiE,6012
58
- math_spec_mapping-0.2.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
59
- math_spec_mapping-0.2.8.dist-info/top_level.txt,sha256=AImhn9wgazkdV0a9vfiphtQR8uGe2nq-ZIOp-6yUk9o,18
60
- math_spec_mapping-0.2.8.dist-info/RECORD,,
56
+ math_spec_mapping-0.3.0.dist-info/LICENSE,sha256=ObyEzSw8kgCaFbEfpu1zP4TrcAKLA0xhqHMZZfyh7N0,1069
57
+ math_spec_mapping-0.3.0.dist-info/METADATA,sha256=Vc8ZOoyIufYp8gQkctaAbFV6cuSPNQnOWZw2SaYJXPI,6012
58
+ math_spec_mapping-0.3.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
59
+ math_spec_mapping-0.3.0.dist-info/top_level.txt,sha256=AImhn9wgazkdV0a9vfiphtQR8uGe2nq-ZIOp-6yUk9o,18
60
+ math_spec_mapping-0.3.0.dist-info/RECORD,,