📘 Resource stores

📘 Resource stores#

Resource stores are a convenient means to serialize, deserialize, and share resources such as components, characteristics, descriptions, strategies and measures:

from galactic.algebras.concept.core import ItemUniverse
from galactic.algebras.convex.characteristics.core import (
    Characteristic,
    Component,
    Integer,
)
from galactic.algebras.convex.descriptions.core import (
    Description,
    PredicateUniverse,
    Context,
    GaloisConnection,
)
from galactic.algebras.convex.strategies.core import (
    Strategy,
)
from galactic.algebras.convex.strategies.examples.arithmetic.core import (
    MultipleStrategy,
)
from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
    MultipleDescription,
    DivisorDescription,
)
from galactic.helpers.resource import ResourceStore

characteristic = Integer(components=(Component(),))
descriptions = [
    DivisorDescription(space=(characteristic,)),
    MultipleDescription(space=(characteristic,)),
]
strategy = MultipleStrategy(space=(characteristic,))
store = ResourceStore()
store["characteristic.1"] = characteristic
store["description.1"] = descriptions[0]
store["description.2"] = descriptions[1]
store["strategy.1"] = strategy
store.dump()
{'characteristic': {'1': {'$type': 'core.characteristic.Integer',
   'components': [{'$type': 'core.Component'}]}},
 'description': {'1': {'$type': 'examples.core.description.Divisor',
   'space': [{'$ref': 'characteristic.1'}]},
  '2': {'$type': 'examples.core.description.Multiple',
   'space': [{'$ref': 'characteristic.1'}]}},
 'strategy': {'1': {'$type': 'examples.core.strategy.Multiple',
   'space': [{'$ref': 'characteristic.1'}]}}}

The galactic.io.resources.toml package provides a resource store implementation to serialize/deserialize resources in TOML format. The store can be used as follows:

import tempfile
from galactic.io.resources.core import ResourcesFileRegistry
text = """
[characteristics.1]
"$type" = "core.characteristic.Integer"
components = [{"$type" = "core.Component"}]

[descriptions.1]
"$type" = "examples.core.description.Divisor"
space = [{"$ref" = "characteristics.1"}]

[descriptions.2]
"$type" = "examples.core.description.Multiple"
space = [{"$ref" = "characteristics.1"}]

[strategies.1]
"$type" = "examples.core.strategy.Multiple"
space = [{"$ref" = "characteristics.1"}]
"""
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".toml") as file:
    file.write(text)
    file.seek(0)
    config = ResourcesFileRegistry().create(file)
    display(config)

store = ResourceStore()
store.load(config)
store["strategies.1"]
{'characteristics': {'1': {'$type': 'core.characteristic.Integer',
   'components': [{'$type': 'core.Component'}]}},
 'descriptions': {'1': {'$type': 'examples.core.description.Divisor',
   'space': [{'$ref': 'characteristics.1'}]},
  '2': {'$type': 'examples.core.description.Multiple',
   'space': [{'$ref': 'characteristics.1'}]}},
 'strategies': {'1': {'$type': 'examples.core.strategy.Multiple',
   'space': [{'$ref': 'characteristics.1'}]}}}
<galactic.algebras.convex.strategies.examples.arithmetic.core.MultipleStrategy object at 0x7c9775eeaf10>