예제 #1
0
    def test_segmark(self):

        ms = comm.SegmentationMark(23)
        md = comm.Record({'foo': 1, 'bar': 2, 'baz': 3})

        c = sync_backend.ConditionSegmark('d')
        self.assertTrue(c.test(ms))
        self.assertEqual(c.locals, {'d': 23})

        c = sync_backend.ConditionSegmark('d')
        self.assertFalse(c.test(md))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionSegmark('d', ['tree'])
        self.assertFalse(c.test(ms))
        self.assertEqual(c.locals, {})

        ms['tree'] = -1
        ms['foobar'] = -2

        c = sync_backend.ConditionSegmark('d', ['tree'])
        self.assertTrue(c.test(ms))
        self.assertEqual(c.locals, {'d': 23, 'tree': -1})

        c = sync_backend.ConditionSegmark('d', ['tree'], 'rest')
        self.assertTrue(c.test(ms))
        self.assertEqual(c.locals, {
            'd': 23,
            'tree': -1,
            'rest': comm.Record({'foobar': -2})
        })
예제 #2
0
    def test_locals(self):

        m1 = comm.Record({'foo': 1, 'bar': 2, 'baz': 3})
        m2 = comm.Record({'baz': 3})

        c = sync_backend.ConditionData(['foo', 'bar'])
        self.assertTrue(c.test(m1))
        self.assertEqual(c.locals, {'foo': 1, 'bar': 2})

        self.assertFalse(c.test(m2))
        self.assertEqual(c.locals, {})
예제 #3
0
    def test_data(self):

        ms = comm.SegmentationMark(23)
        m = comm.Record({'foo': 1, 'bar': 2, 'baz': 3})

        c = sync_backend.ConditionData()
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData()
        self.assertFalse(c.test(ms))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData([])
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData(['foo'])
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {'foo': 1})

        c = sync_backend.ConditionData(['tree'])
        self.assertFalse(c.test(m))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData(['foo'], 'rest')
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {
            'foo': 1,
            'rest': comm.Record({
                'bar': 2,
                'baz': 3
            })
        })

        c = sync_backend.ConditionData(['foo', 'bar', 'baz'], 'rest')
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {
            'foo': 1,
            'bar': 2,
            'baz': 3,
            'rest': comm.Record()
        })
예제 #4
0
    def test_store(self):

        v = StoreVar('foo')
        self.assertIs(v.get(), None)

        with self.assertRaises(TypeError):
            v.set({'1': 1})

        m = comm.Record({'foo': 'bar'})
        v.set(m)
        self.assertEqual(v.get(), m)
        self.assertEqual(id(v.get()), id(m))

        self.assertTrue(re.match('store\(%s \= (.+?)\)' % 'foo', repr(v)))
예제 #5
0
    def test_this(self):

        exp = TermThis()

        with self.assertRaises(AssertionError):
            exp.compute({})

        with self.assertRaises(AssertionError):
            exp.compute({'__this__': 35})

        #---

        m = comm.Record({'foo': 'bar'})
        this = exp.compute({'__this__': m, 'zz': 100})

        self.assertEqual(id(this), id(m))
        self.assertIn('foo', this)
예제 #6
0
    def test_data_exp(self):

        t_var = TermVar('foo')
        t_pair = TermPair('bar', TermVar('baz'))
        t_this = TermThis()
        t_var_zzz = TermVar('zzz')

        fcode = '10'
        f = eval('lambda: ' + fcode)
        f.code = fcode

        m_sm_10_exp = SegmentationMarkExp(IntExp(f))

        m_sm_10 = comm.SegmentationMark(10)
        m_sm_4 = comm.SegmentationMark(4)

        m_rec_empty = comm.Record()
        m_rec_12 = comm.Record({'1': 'one', '2': 'two'})
        m_rec_3 = comm.Record({'3': 'three'})

        m_rec_empty_exp = RecordExp(m_rec_empty.content)
        m_rec_12_exp = RecordExp(m_rec_12.content)
        m_rec_3_exp = RecordExp(m_rec_3.content)

        with self.assertRaises(TypeError):
            DataExp(42)

        with self.assertRaises(TypeError):
            DataExp('abc')

        with self.assertRaises(TypeError):
            DataExp([t_var, 'ololo'])

        with self.assertRaises(TypeError):
            DataExp([t_var, t_this], 42)

        # No init, terms: []
        dexp = DataExp([])
        result = dexp.compute({})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {})

        #---

        # Init: record, terms: []
        dexp = DataExp([], m_rec_12_exp)
        result = dexp.compute({})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {'1': 'one', '2': 'two'})

        #---

        # Init: record, terms: [records]
        dexp = DataExp([t_var, t_this], m_rec_empty_exp)
        result = dexp.compute({'__this__': m_rec_12, 'foo': m_rec_3})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {
            '1': 'one',
            '2': 'two',
            '3': 'three'
        })

        #---

        # Init: record, terms: [record, segmark]
        dexp = DataExp([t_var, t_this], m_rec_empty_exp)
        result = dexp.compute({'__this__': m_sm_4, 'foo': m_rec_3})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {'3': 'three', '__n__': 4})

        #---

        # Init: segmentation mark, terms: [records, segmark]
        dexp = DataExp([t_var, t_this, t_var_zzz], m_sm_10_exp)
        result = dexp.compute({
            '__this__': m_rec_12,
            'foo': m_rec_3,
            'zzz': m_sm_4
        })

        self.assertIs(type(result), comm.SegmentationMark)
        self.assertEqual(result.n, m_sm_10.n)

        (self.assertIn(i, result) for i in ('1', '2', '3'))

        #---

        # No init, terms: [records]
        dexp = DataExp([t_var, t_pair])
        result = dexp.compute({'baz': 100, 'foo': m_rec_3})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {'3': 'three', 'bar': 100})

        #---

        dexp = DataExp([t_var, t_this, t_var_zzz])

        # Non-record type in a union.
        with self.assertRaises(RuntimeError):
            dexp.compute({'__this__': m_sm_10, 'foo': 42, 'zzz': m_sm_4})

        # Two segmentation marks in a union.
        with self.assertRaises(RuntimeError):
            dexp.compute({'__this__': m_sm_10, 'foo': m_rec_3, 'zzz': m_sm_4})

        # No init, segmentation mark term
        result = dexp.compute({
            '__this__': m_rec_12,
            'foo': m_rec_3,
            'zzz': m_sm_4
        })

        self.assertIs(type(result), comm.SegmentationMark)
        self.assertEqual(result.n, m_sm_4.n)

        (self.assertIn(i, result) for i in ('1', '2', '3'))
예제 #7
0
파일: morph.py 프로젝트: mkuznets/astrakahn
        n = msg['chunks'].pop()

        out_msg['lst'] = msg['lst'][:n]
        msg['lst'] = msg['lst'][n:]
        del out_msg['chunks']

        return ('continue', {0: [out_msg]}, msg)


def c_Reduce(m1, m2):
    '1MU'
    m1['sum'] += m2['sum']
    return ('partial', {}, m1)


__input__ = {'in': [], '_in': []}

import random
import communication as comm

__input__['in'].append(
    comm.Record({
        'lst': [random.random() for i in range(100)],
        '__nfrag__': 7
    }))
__input__['in'].append(comm.SegmentationMark(0))
__input__['_in'].append(comm.SegmentationMark(0))

import astrakahn
astrakahn.start()
예제 #8
0
파일: fact.py 프로젝트: mkuznets/astrakahn
def c_Gen(m):
    '2I'
    n = m['n']
    if n == 1:
        return ('terminate', {}, None)
    else:
        sn = n
        n -= 1
        return ('continue', {0: [{'n': sn}]}, {'n': n})


def c_Reduce(m1, m2):
    '1DU'
    a = m1['n']
    b = m2['n']
    c = a * b
    return ('partial', {}, {'n': c})


__input__ = {'in': []}

from random import randint

for i in range(100):
    __input__['in'].append(comm.Record({'n': randint(2, 100)}))

__input__['in'].append(comm.SegmentationMark(0))

import astrakahn
astrakahn.start()
예제 #9
0
        send this => c;
      }
  }
}
'''

#------------------------------------------------------------------------------

if __name__ == '__main__':

    import astrakahn
    import communication as comm

    __input__ = {
        'a': [
            comm.Record({'A': 1}),
            comm.Record({'A': 2}),
            comm.Record({'A': 3}),
            comm.SegmentationMark(1),
            comm.Record({'C': 1}),
            comm.Record({'C': 2}),
            comm.Record({'C': 3}),
            comm.SegmentationMark(0),
        ],
        'b': [
            comm.Record({'B': 1}),
            comm.Record({'B': 2}),
            comm.SegmentationMark(1),
            comm.Record({'B': 3}),
            comm.Record({'B': 4}),
            comm.SegmentationMark(0),
예제 #10
0
      in1.(n || tail) {
        send (tail || n: [n+inc]) => out2;
      }
  }
}
'''

#------------------------------------------------------------------------------

import communication as comm

__input__ = {
    'in1': [
        comm.Record({
            'n': 0,
            'inc': 10,
            'a': 100
        }),
        comm.Record({
            'n': 0,
            'inc': 12,
            'a': 100
        }),
        comm.Record({
            'n': 0,
            'inc': 109,
            'a': 100
        }),
        comm.Record({
            'n': 0,
            'inc': 10,