-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathcreate_atoms_lowlevel.py
executable file
·57 lines (44 loc) · 1.55 KB
/
create_atoms_lowlevel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/env python3
#
# create_atoms_lowlevel.py
#
"""
Example of how to use the low-level interfaces into the AtomSpace.
This provides the fastest python API for dumping raw data into the
AtomSpace. However, it is a bit hard to use; the Atomese-style
python is easier. See `create_atoms.py` for a simpler variant.
"""
from opencog.atomspace import AtomSpace, Atom
from opencog.type_constructors import FloatValue, StringValue
from opencog.atomspace import types
a = AtomSpace()
# Add three Nodes
concept_type = types.ConceptNode
A = a.add_node(concept_type, 'Apple')
B = a.add_node(concept_type, 'Berry')
C = a.add_node(concept_type, 'Comestible')
# Create a Value holding a vector of numbers.
weights = FloatValue([1, 0.8, 42, 3.14159])
# Create a key
key = a.add_node(types.PredicateNode, 'my favorite keyname')
# Place some weights on the Nodes:
A.set_value(key, weights)
B.set_value(key, FloatValue([0.5, 0.333, 66, 2.71828]))
C.set_value(key, StringValue(["just", "some", "words"]))
# Get the weights:
A.get_value(key)
B.get_value(key)
C.get_value(key)
# Weights convert to python lists:
print("Ello, there!", list(C.get_value(key)))
# Add three InheritanceLinks, asserting that apples are berries
# and that berries are edible.
inh_type = types.InheritanceLink
a.add_link(inh_type, [A, B])
a.add_link(inh_type, [B, C])
a.add_link(inh_type, [A, C])
# A verbose way of writing M = MeetLink(VariableNode('x"))
V = a.add_node(types.VariableNode, "x")
M = a.add_link(types.MeetLink, [V])
print("The atomspace contains:\n\n", M.execute())
# THE END. That's All, Folks!