예제 #1
0
    def test_b_post_and_get_link(self):
        jswan = self.mkswan()
        janimal = self.mkanimal()

        # Create a test link between swan and animal
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.5, 'count': 0.4}}
        atom = {
            'type': 'InheritanceLink',
            'truthvalue': truthvalue,
            'outgoing': [jswan['handle'], janimal['handle']]
        }

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(atom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['type'] == atom['type']
        assert post_result['truthvalue']['type'] == truthvalue['type']
        assert_almost_equals(float(
            post_result['truthvalue']['details']['strength']),
                             truthvalue['details']['strength'],
                             places=5)
        assert_almost_equals(float(
            post_result['truthvalue']['details']['count']),
                             truthvalue['details']['count'],
                             places=5)
        assert jswan['handle'] in post_result['outgoing']
        assert janimal['handle'] in post_result['outgoing']

        # Compare to the values created in the AtomSpace
        swan_animal = self.atomspace.add_link(
            types.InheritanceLink, [self.swan, self.animal],
            TruthValue(0.5, count_to_confidence(0.4)))

        atomspace_result = swan_animal
        assert types.__dict__.get(post_result['type']) == atomspace_result.type
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv

        # Get by handle and compare
        handle = post_result['handle']
        get_response_handle = \
            self.client.get(self.uri + 'atoms/' + str(handle))
        get_result_handle = \
            json.loads(get_response_handle.data)['result']['atoms'][0]
        assert post_result == get_result_handle

        # Check if the link is in the incoming set of each of the nodes
        jswan = self.get_atom(jswan['handle'])
        janimal = self.get_atom(janimal['handle'])
        for h in post_result['outgoing']:
            assert post_result['handle'] in jswan['incoming']
            assert post_result['handle'] in janimal['incoming']
예제 #2
0
    def test_a_post_and_get_node(self):
        # Create a test node
        truthvalue = {'type': 'simple',
                      'details': {'strength': 0.08, 'count': 0.2}}
        jatom = {'type': 'ConceptNode',
                'name': 'giant_frog',
                'truthvalue': truthvalue}

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(jatom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['type'] == jatom['type']
        assert post_result['name'] == jatom['name']
        assert post_result['truthvalue']['type'] == truthvalue['type']
        assert_almost_equals(
            float(post_result['truthvalue']['details']['strength']),
            truthvalue['details']['strength'], places=5)
        assert_almost_equals(
            float(post_result['truthvalue']['details']['count']),
            truthvalue['details']['count'], places=5)

        # Compare to the values created in the AtomSpace
        frog = self.atomspace.add_node(
            types.ConceptNode, 'giant_frog',
            TruthValue(.08, count_to_confidence(0.2)))
        atomspace_result = frog
        assert post_result['name'] == atomspace_result.name
        assert types.__dict__.get(post_result['type']) == atomspace_result.type
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv

        # Get by handle and compare
        handle = post_result['handle']
        get_response_handle = \
            self.client.get(self.uri + 'atoms/' + str(handle))
        get_result_handle = \
            json.loads(get_response_handle.data)['result']['atoms'][0]
        assert post_result == get_result_handle

        # Get by name and compare
        name = post_result['name']
        get_response_name = self.client.get(self.uri + 'atoms?name=' + name)
        get_result_name = \
            json.loads(get_response_name.data)['result']['atoms'][0]
        assert post_result == get_result_name

        # Get by name and type and compare
        type = post_result['type']
        get_response_name_type = \
            self.client.get(self.uri + 'atoms?name=' + name + '&type=' + type)
        get_result_name_type = \
            json.loads(get_response_name_type.data)['result']['atoms'][0]
        assert post_result == get_result_name_type
예제 #3
0
    def test_b_post_and_get_link(self):
        jswan = self.mkswan()
        janimal = self.mkanimal()

        # Create a test link between swan and animal
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.5, 'count': 0.4}}
        atom = {'type': 'InheritanceLink', 'truthvalue': truthvalue,
                'outgoing': [jswan['handle'], janimal['handle']]}

        post_response = self.client.post(
            self.uri + 'atoms', data=json.dumps(atom), headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['type'] == atom['type']
        assert post_result['truthvalue']['type'] == truthvalue['type']
        assert_almost_equals(
            float(post_result['truthvalue']['details']['strength']),
            truthvalue['details']['strength'], places=5)
        assert_almost_equals(
            float(post_result['truthvalue']['details']['count']),
            truthvalue['details']['count'], places=5)
        assert jswan['handle'] in post_result['outgoing']
        assert janimal['handle'] in post_result['outgoing']

        # Compare to the values created in the AtomSpace
        swan_animal = self.atomspace.add_link(
            types.InheritanceLink, [self.swan, self.animal],
            TruthValue(0.5, count_to_confidence(0.4)))

        atomspace_result = swan_animal
        assert types.__dict__.get(post_result['type']) == atomspace_result.type
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv

        # Get by handle and compare
        handle = post_result['handle']
        get_response_handle = \
            self.client.get(self.uri + 'atoms/' + str(handle))
        get_result_handle = \
            json.loads(get_response_handle.data)['result']['atoms'][0]
        assert post_result == get_result_handle

        # Check if the link is in the incoming set of each of the nodes
        jswan = self.get_atom(jswan['handle'])
        janimal = self.get_atom(janimal['handle'])
        for h in post_result['outgoing']:
            assert post_result['handle'] in jswan['incoming']
            assert post_result['handle'] in janimal['incoming']
예제 #4
0
    def test_e_post_revise_existing_node(self):
        # Attempt to create a node, where a node already exists with that name
        # and type. Should revise existing node.
        existing_atom = self.bird

        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.1, 'count': 0.95}}
        atom = \
            {'type': 'ConceptNode', 'name': 'bird', 'truthvalue': truthvalue}

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(atom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert_almost_equals(float(
            post_result['truthvalue']['details']['strength']),
                             truthvalue['details']['strength'],
                             places=5)
        assert_almost_equals(float(
            post_result['truthvalue']['details']['count']),
                             truthvalue['details']['count'],
                             places=5)

        # Compare to the values updated in the AtomSpace
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
               == existing_atom.tv
예제 #5
0
    def test_f_post_revise_existing_link(self):
        # Attempt to create a link, where a link already exists with that name
        # and outgoing set.
        # Should revise existing link.
        existing_atom = self.bird_animal
        jbird_animal = self.mkbird_animal()
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.1, 'count': 0.95}}
        outgoing = jbird_animal['outgoing']
        atom = {'type': 'InheritanceLink',
                'truthvalue': truthvalue,
                'outgoing': outgoing}

        post_response = self.client.post(
            self.uri + 'atoms', data=json.dumps(atom), headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['outgoing'] == outgoing
        assert_almost_equals(
            float(post_result['truthvalue']['details']['strength']),
            truthvalue['details']['strength'], places=5)
        assert_almost_equals(
            float(post_result['truthvalue']['details']['count']),
            truthvalue['details']['count'], places=5)

        # Compare to the values updated in the AtomSpace
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
               == existing_atom.tv
예제 #6
0
    def test_e_post_revise_existing_node(self):
        # Attempt to create a node, where a node already exists with that name
        # and type. Should revise existing node.
        existing_atom = self.bird

        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.1, 'count': 0.95}}
        atom = \
            {'type': 'ConceptNode', 'name': 'bird', 'truthvalue': truthvalue}

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(atom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert_almost_equals(
            float(post_result['truthvalue']['details']['strength']),
            truthvalue['details']['strength'], places=5)
        assert_almost_equals(
            float(post_result['truthvalue']['details']['count']),
            truthvalue['details']['count'], places=5)

        # Compare to the values updated in the AtomSpace
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
               == existing_atom.tv
예제 #7
0
    def test_post_and_get_link(self):
        # Create a test link between swan and animal
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.5, 'count': 0.4}}
        atom = {
            'type': 'InheritanceLink',
            'truthvalue': truthvalue,
            'outgoing': [self.swan.value(),
                         self.animal.value()]
        }

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(atom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['type'] == atom['type']
        assert post_result['truthvalue']['type'] == truthvalue['type']
        assert_almost_equals(float(
            post_result['truthvalue']['details']['strength']),
                             truthvalue['details']['strength'],
                             places=5)
        assert_almost_equals(float(
            post_result['truthvalue']['details']['count']),
                             truthvalue['details']['count'],
                             places=5)
        assert self.swan.value() in post_result['outgoing']
        assert self.animal.value() in post_result['outgoing']

        # Compare to the values created in the AtomSpace
        atomspace_result = Atom(post_result['handle'], self.atomspace)
        assert Atom(post_result['handle'], self.atomspace).value() == \
            atomspace_result.value()
        assert types.__dict__.get(post_result['type']) == atomspace_result.type
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv

        # Get by handle and compare
        handle = post_result['handle']
        get_response_handle = \
            self.client.get(self.uri + 'atoms/' + str(handle))
        get_result_handle = \
            json.loads(get_response_handle.data)['result']['atoms'][0]
        assert post_result == get_result_handle

        # Check if the link is in the incoming set of each of the nodes
        for h in post_result['outgoing']:
            assert post_result['handle'] \
                in [atom.value() for atom in Atom(h, self.atomspace).incoming]
예제 #8
0
    def test_post_and_get_link(self):
        # Create a test link between swan and animal
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.5, 'count': 0.4}}
        atom = {'type': 'InheritanceLink', 'truthvalue': truthvalue,
                'outgoing': [self.swan.value(), self.animal.value()]}

        post_response = self.client.post(
            self.uri + 'atoms', data=json.dumps(atom), headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['type'] == atom['type']
        assert post_result['truthvalue']['type'] == truthvalue['type']
        assert_almost_equals(
            float(post_result['truthvalue']['details']['strength']),
            truthvalue['details']['strength'], places=5)
        assert_almost_equals(
            float(post_result['truthvalue']['details']['count']),
            truthvalue['details']['count'], places=5)
        assert self.swan.value() in post_result['outgoing']
        assert self.animal.value() in post_result['outgoing']

        # Compare to the values created in the AtomSpace
        atomspace_result = Atom(post_result['handle'], self.atomspace)
        assert Atom(post_result['handle'], self.atomspace).value() == \
            atomspace_result.value()
        assert types.__dict__.get(post_result['type']) == atomspace_result.type
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv

        # Get by handle and compare
        handle = post_result['handle']
        get_response_handle = \
            self.client.get(self.uri + 'atoms/' + str(handle))
        get_result_handle = \
            json.loads(get_response_handle.data)['result']['atoms'][0]
        assert post_result == get_result_handle

        # Check if the link is in the incoming set of each of the nodes
        for h in post_result['outgoing']:
            assert post_result['handle'] \
                in [atom.value() for atom in Atom(h, self.atomspace).incoming]
예제 #9
0
파일: mappers.py 프로젝트: zhanglix/opencog
    def parse(data):
        if 'truthvalue' in data:
            if 'type' in data['truthvalue']:
                tv_type = data['truthvalue']['type']
            else:
                abort(
                    400, 'Invalid request: truthvalue object requires a '
                    'type parameter')
        else:
            abort(
                400, 'Invalid request: required parameter truthvalue '
                'is missing')

        # @todo: Cython bindings implementation does not provide support for
        # other TruthValue types yet
        # (see: opencog\cython\opencog\atomspace_details.pyx,
        #       opencog\cython\opencog\atomspace.pxd)
        if tv_type != 'simple':
            if tv_type in ['count', 'indefinite']:
                # @todo: check error type
                abort(
                    400, 'Invalid request: truthvalue type \'' + tv_type +
                    '\' is not supported')
            else:
                # @todo: check error type
                abort(
                    400, 'Invalid request: type \'' + tv_type +
                    '\' is not a valid truthvalue type')

        if 'details' in data['truthvalue']:
            if 'strength' in data['truthvalue']['details'] \
                and 'count' in data['truthvalue']['details']:
                tv = TruthValue(
                    data['truthvalue']['details']['strength'],
                    count_to_confidence(
                        data['truthvalue']['details']['count']))
            else:
                abort(
                    400, 'Invalid request: truthvalue details object '
                    'requires both a strength and count parameter')
        else:
            abort(
                400, 'Invalid request: truthvalue object requires a '
                'details parameter')

        return tv
예제 #10
0
    def test_put_and_get_tv_av_link(self):
        atom = self.bird_animal
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.9, 'count': 0.95}}
        attentionvalue = {'sti': 6, 'lti': 3, 'vlti': True}
        atom_update = \
            {'truthvalue': truthvalue, 'attentionvalue': attentionvalue}
        put_response =\
            self.client.put(
                self.uri + 'atoms/' + str(atom.value()),
                data=json.dumps(atom_update),
                headers=self.headers)
        put_result = json.loads(put_response.data)['atoms']

        # Verify values returned by the PUT request
        assert put_result['handle'] == atom.value()
        assert_almost_equals(float(
            put_result['truthvalue']['details']['strength']),
                             truthvalue['details']['strength'],
                             places=5)
        assert_almost_equals(float(
            put_result['truthvalue']['details']['count']),
                             truthvalue['details']['count'],
                             places=5)
        assert put_result['attentionvalue']['sti'] == attentionvalue['sti']
        assert put_result['attentionvalue']['lti'] == attentionvalue['lti']
        assert put_result['attentionvalue']['vlti'] == attentionvalue['vlti']

        # Compare to the values updated in the AtomSpace
        atomspace_result = Atom(put_result['handle'], self.atomspace)
        assert Atom(put_result['handle'], self.atomspace).value() == \
            atomspace_result.value()
        assert types.__dict__.get(put_result['type']) == atomspace_result.type
        assert TruthValue(
            float(put_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(put_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv
        assert put_result['attentionvalue'] == atomspace_result.av

        # Get by handle and compare
        get_response = \
            self.client.get(self.uri + 'atoms/' + str(atom.value()))
        get_result = json.loads(get_response.data)['result']['atoms'][0]
        assert put_result == get_result
예제 #11
0
    def test_put_and_get_tv_av_link(self):
        atom = self.bird_animal
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.9, 'count': 0.95}}
        attentionvalue = {'sti': 6, 'lti': 3, 'vlti': True}
        atom_update = \
            {'truthvalue': truthvalue, 'attentionvalue': attentionvalue}
        put_response =\
            self.client.put(
                self.uri + 'atoms/' + str(atom.value()),
                data=json.dumps(atom_update),
                headers=self.headers)
        put_result = json.loads(put_response.data)['atoms']

        # Verify values returned by the PUT request
        assert put_result['handle'] == atom.value()
        assert_almost_equals(
            float(put_result['truthvalue']['details']['strength']),
            truthvalue['details']['strength'], places=5)
        assert_almost_equals(
            float(put_result['truthvalue']['details']['count']),
            truthvalue['details']['count'], places=5)
        assert put_result['attentionvalue']['sti'] == attentionvalue['sti']
        assert put_result['attentionvalue']['lti'] == attentionvalue['lti']
        assert put_result['attentionvalue']['vlti'] == attentionvalue['vlti']

        # Compare to the values updated in the AtomSpace
        atomspace_result = Atom(put_result['handle'], self.atomspace)
        assert Atom(put_result['handle'], self.atomspace).value() == \
            atomspace_result.value()
        assert types.__dict__.get(put_result['type']) == atomspace_result.type
        assert TruthValue(
            float(put_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(put_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv
        assert put_result['attentionvalue'] == atomspace_result.av

        # Get by handle and compare
        get_response = \
            self.client.get(self.uri + 'atoms/' + str(atom.value()))
        get_result = json.loads(get_response.data)['result']['atoms'][0]
        assert put_result == get_result
예제 #12
0
파일: mappers.py 프로젝트: TScottJ/opencog
    def parse(data):
        if 'truthvalue' in data:
            if 'type' in data['truthvalue']:
                tv_type = data['truthvalue']['type']
            else:
                abort(400, 'Invalid request: truthvalue object requires a '
                           'type parameter')
        else:
            abort(400, 'Invalid request: required parameter truthvalue '
                       'is missing')

        # @todo: Cython bindings implementation does not provide support for
        # other TruthValue types yet
        # (see: opencog\cython\opencog\atomspace_details.pyx,
        #       opencog\cython\opencog\atomspace.pxd)
        if tv_type != 'simple':
            if tv_type in ['count', 'indefinite']:
                # @todo: check error type
                abort(400, 'Invalid request: truthvalue type \'' +
                           tv_type + '\' is not supported')
            else:
                # @todo: check error type
                abort(400, 'Invalid request: type \'' + tv_type +
                           '\' is not a valid truthvalue type')

        if 'details' in data['truthvalue']:
            if 'strength' in data['truthvalue']['details'] \
                and 'count' in data['truthvalue']['details']:
                tv = TruthValue(data['truthvalue']['details']['strength'],
                                count_to_confidence(
                                    data['truthvalue']['details']['count']))
            else:
                abort(400, 'Invalid request: truthvalue details object '
                           'requires both a strength and count parameter')
        else:
            abort(400, 'Invalid request: truthvalue object requires a '
                       'details parameter')

        return tv
예제 #13
0
    def test_f_post_revise_existing_link(self):
        # Attempt to create a link, where a link already exists with that name
        # and outgoing set.
        # Should revise existing link.
        existing_atom = self.bird_animal
        jbird_animal = self.mkbird_animal()
        truthvalue = \
            {'type': 'simple', 'details': {'strength': 0.1, 'count': 0.95}}
        outgoing = jbird_animal['outgoing']
        atom = {
            'type': 'InheritanceLink',
            'truthvalue': truthvalue,
            'outgoing': outgoing
        }

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(atom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['outgoing'] == outgoing
        assert_almost_equals(float(
            post_result['truthvalue']['details']['strength']),
                             truthvalue['details']['strength'],
                             places=5)
        assert_almost_equals(float(
            post_result['truthvalue']['details']['count']),
                             truthvalue['details']['count'],
                             places=5)

        # Compare to the values updated in the AtomSpace
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
               == existing_atom.tv
예제 #14
0
    def test_a_post_and_get_node(self):
        # Create a test node
        truthvalue = {
            'type': 'simple',
            'details': {
                'strength': 0.08,
                'count': 0.2
            }
        }
        jatom = {
            'type': 'ConceptNode',
            'name': 'giant_frog',
            'truthvalue': truthvalue
        }

        post_response = self.client.post(self.uri + 'atoms',
                                         data=json.dumps(jatom),
                                         headers=self.headers)
        post_result = json.loads(post_response.data)['atoms']

        # Verify values returned by the POST request
        assert post_result['type'] == jatom['type']
        assert post_result['name'] == jatom['name']
        assert post_result['truthvalue']['type'] == truthvalue['type']
        assert_almost_equals(float(
            post_result['truthvalue']['details']['strength']),
                             truthvalue['details']['strength'],
                             places=5)
        assert_almost_equals(float(
            post_result['truthvalue']['details']['count']),
                             truthvalue['details']['count'],
                             places=5)

        # Compare to the values created in the AtomSpace
        frog = self.atomspace.add_node(
            types.ConceptNode, 'giant_frog',
            TruthValue(.08, count_to_confidence(0.2)))
        atomspace_result = frog
        assert post_result['name'] == atomspace_result.name
        assert types.__dict__.get(post_result['type']) == atomspace_result.type
        assert TruthValue(
            float(post_result['truthvalue']['details']['strength']),
            count_to_confidence(
                float(post_result['truthvalue']['details']['count']))) \
            == atomspace_result.tv

        # Get by handle and compare
        handle = post_result['handle']
        get_response_handle = \
            self.client.get(self.uri + 'atoms/' + str(handle))
        get_result_handle = \
            json.loads(get_response_handle.data)['result']['atoms'][0]
        assert post_result == get_result_handle

        # Get by name and compare
        name = post_result['name']
        get_response_name = self.client.get(self.uri + 'atoms?name=' + name)
        get_result_name = \
            json.loads(get_response_name.data)['result']['atoms'][0]
        assert post_result == get_result_name

        # Get by name and type and compare
        type = post_result['type']
        get_response_name_type = \
            self.client.get(self.uri + 'atoms?name=' + name + '&type=' + type)
        get_result_name_type = \
            json.loads(get_response_name_type.data)['result']['atoms'][0]
        assert post_result == get_result_name_type