def test_link_form_cast(self): # Given form = _LinkRequestForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('link_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('link_lane'))) items.add( Slot.create_slot(Text.create_from('prio'), Num.create_from(1))) items.add( Slot.create_slot(Text.create_from('rate'), Num.create_from(3))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('link_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('link'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _LinkRequest) self.assertEqual('link_node', actual._node_uri) self.assertEqual('link_lane', actual._lane_uri) self.assertEqual(1, actual._prio) self.assertEqual(3, actual._rate) self.assertEqual(body, actual._body.get_item(0))
def test_unlinked_form_cast(self): # Given form = _UnlinkedResponseForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('unlinked_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('unlinked_lane'))) items.add( Slot.create_slot(Text.create_from('prio'), Num.create_from(20))) items.add( Slot.create_slot(Text.create_from('rate'), Num.create_from(25))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('unlinked_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('unlinked'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _UnlinkedResponse) self.assertEqual('unlinked_node', actual._node_uri) self.assertEqual('unlinked_lane', actual._lane_uri) self.assertEqual(20, actual._prio) self.assertEqual(25, actual._rate) self.assertEqual(body, actual._body.get_item(0))
def test_write_item_slot(self): # Given item = Slot.create_slot(Text.create_from('Age'), Num.create_from(32)) writer = _ReconWriter() # When actual = writer._write_item(item) # Then self.assertIsInstance(actual, str) self.assertEqual('Age:32', actual)
def _mold(self, envelope: Optional['_LinkAddressedEnvelope']) -> 'Value': if envelope is not None: headers = _Record.create()._add_slot('node', envelope._node_uri)._add_slot('lane', envelope._lane_uri) prio = envelope._prio if prio != 0 and not math.isnan(prio): headers._add_slot('prio', Num.create_from(prio)) rate = envelope._rate if rate != 0 and not math.isnan(rate): headers._add_slot('rate', Num.create_from(rate)) return Attr.create_attr(self._tag, headers)._concat(envelope._body) else: return _Item.extant()
async def test_write_sync_body_int(self): # Given envelope = SyncRequest('bar/baz/', 'foo/bar', body=Num.create_from(42)) expected = '@sync(node:"bar/baz/",lane:"foo/bar")42' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
def test_write_value_number(self): # Given number = Num.create_from(-13.1) writer = _ReconWriter() # When actual = writer._write_value(number) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(5, actual._size) self.assertEqual('-13.1', actual._value) self.assertEqual('1', actual._last_char)
async def test_write_event_body_int(self): # Given envelope = EventMessage('/this/is/spam', 'hello', body=Num.create_from(1224)) expected = '@event(node:"/this/is/spam",lane:hello)1224' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
async def test_write_command_body_int(self): # Given envelope = CommandMessage('dead/parrot', 'spam/ham', body=Num.create_from(911)) expected = '@command(node:"dead/parrot",lane:"spam/ham")911' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
def test_sync_form_cast(self): # Given form = SyncRequestForm() items = RecordMap.create() items.add(Slot.create_slot(Text.create_from('node'), Text.create_from('sync_node'))) items.add(Slot.create_slot(Text.create_from('lane'), Text.create_from('sync_lane'))) items.add(Slot.create_slot(Text.create_from('prio'), Num.create_from(33.12))) items.add(Slot.create_slot(Text.create_from('rate'), Num.create_from(12.33))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('sync_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) record_map.add(body) # When actual = form.cast(record_map) # Then self.assertIsInstance(actual, SyncRequest) self.assertEqual('sync_node', actual.node_uri) self.assertEqual('sync_lane', actual.lane_uri) self.assertEqual(33.12, actual.prio) self.assertEqual(12.33, actual.rate) self.assertEqual(body, actual.body.get_item(0))
def test_write_command_body_float(self): # Given envelope = _CommandMessage('dead/parrot', 'spam/ham', body=Num.create_from(-911.119)) expected = '@command(node:"dead/parrot",lane:"spam/ham")-911.119' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_write_event_body_float(self): # Given envelope = _EventMessage('/this/is/spam', 'hello', body=Num.create_from(33.12)) expected = '@event(node:"/this/is/spam",lane:hello)33.12' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_write_unlinked_body_float(self): # Given envelope = _UnlinkedResponse('/unit/5', '/unit/3', body=Num.create_from(-100.0)) expected = '@unlinked(node:"/unit/5",lane:"/unit/3")-100.0' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_write_sync_body_float(self): # Given envelope = _SyncRequest('bar/baz/', 'foo/bar', body=Num.create_from(33.22)) expected = '@sync(node:"bar/baz/",lane:"foo/bar")33.22' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_write_link_body_float(self): # Given envelope = _LinkRequest('/unit/5', '/unit/3', body=Num.create_from(-100.0)) expected = '@link(node:"/unit/5",lane:"/unit/3")-100.0' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_write_synced_body_float(self): # Given envelope = _SyncedResponse('foo/bar/baz', 'baz/bar/foo', body=Num.create_from(-13.04)) expected = '@synced(node:"foo/bar/baz",lane:"baz/bar/foo")-13.04' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
async def test_write_linked_body_int(self): # Given envelope = LinkedResponse('/unit/5', '/unit/3', body=Num.create_from(-100)) expected = '@linked(node:"/unit/5",lane:"/unit/3")-100' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
async def test_write_event_body_object(self): # Given body = RecordMap.create() body.add( Attr.create_attr(Text.create_from('Person'), Extant.get_extant())) body.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Bar'))) body.add(Slot.create_slot(Text.create_from('age'), Num.create_from(14))) body.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(-5.9))) envelope = EventMessage('/this/is/spam', 'hello', body=body) expected = '@event(node:"/this/is/spam",lane:hello)@Person{name:Bar,age:14,salary:-5.9}' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
def test_write_event_body_nested(self): # Given friend = RecordMap.create() friend.add( Attr.create_attr(Text.create_from('Person'), _Extant._get_extant())) friend.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Sam/Spam'))) friend.add( Slot.create_slot(Text.create_from('age'), Num.create_from(1))) friend.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(22))) body = RecordMap.create() body.add( Attr.create_attr(Text.create_from('Person'), _Extant._get_extant())) body.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Par'))) body.add(Slot.create_slot(Text.create_from('age'), Num.create_from(11))) body.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(-5.9))) body.add(Slot.create_slot(Text.create_from('friend'), friend)) envelope = _EventMessage('/this/is/spam', 'hello', body=body) person = '@Person{name:Par,age:11,salary:-5.9,friend:@Person{name:"Sam/Spam",age:1,salary:22}}' expected = '@event(node:"/this/is/spam",lane:hello)' + person # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
# limitations under the License. # Setting the value of a value lane on a remote agent. import time from swimai import SwimClient from swimai.structures import Num async def custom_on_event_callback(event): print(f'link received event: {event}') with SwimClient(debug=True) as swim_client: host_uri = 'ws://localhost:9001' node_uri = '/unit/foo' lane_uri = 'publishValue' event_downlink = swim_client.downlink_event() event_downlink.set_host_uri('ws://localhost:9001') event_downlink.set_node_uri(node_uri) event_downlink.set_lane_uri(lane_uri) event_downlink.on_event(custom_on_event_callback) event_downlink.open() new_value = 'Hello from Python!' swim_client.command(host_uri, node_uri, "publish", Num.create_from(13)) print('Stopping the client in 5 seconds') time.sleep(2)
from swimai import SwimClient from swimai.structures import Num async def custom_on_event(event): print(f'Link received event: {event}') if __name__ == '__main__': with SwimClient() as swim_client: host_uri = 'ws://localhost:9001' node_uri = '/unit/foo' event_downlink = swim_client.downlink_event() event_downlink.set_host_uri(host_uri) event_downlink.set_node_uri(node_uri) event_downlink.set_lane_uri("publishValue") event_downlink.on_event(custom_on_event) event_downlink.open() msg = Num.create_from(9035768) # command() `msg` TO # the "publish" lane OF # the agent addressable by `/unit/foo` RUNNING ON # the plane with hostUri "warp://localhost:9001" swim_client.command(host_uri, node_uri, 'publish', msg) print('Stopping the client in 2 seconds') time.sleep(2)
def _create_number(value: Union[float, int]) -> 'Num': return Num.create_from(value)