Skip to content

Commit

Permalink
Merge pull request opencog#47 from keyvan-m-sadeghi/blending
Browse files Browse the repository at this point in the history
Blending
  • Loading branch information
keyvan-m-sadeghi committed Nov 14, 2012
2 parents c2c4837 + b86d214 commit 90e5717
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
24 changes: 14 additions & 10 deletions opencog/python/blending/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

from opencog.atomspace import AtomSpace, Node, find_links_upward
from opencog.cogserver import MindAgent
from random import random as rand
from random import randrange

class DummyBlendingAgent(MindAgent):

def run(self, atomspace):
# Define a set to add the source ConceptNodes to which we will retrieve from the AtomSpace
nodes = []

# Request all nodes of type 'ConceptNode' from the AtomSpace and loop through them
for node in atomspace.get_atoms_by_type("ConceptNode"):
for node in atomspace.get_atoms_by_type('ConceptNode'):
# Append each ConceptNode to our own nodes set

nodes.append(node)
# Check to make sure we actually got back some nodes of the type we want (ConceptNode)
if len(nodes) <= 1:
# Otherwise we might as well stop here
return

# Randomly select a number (limited by the size of our node set) for the first node
first_node_index = rand(0,len(nodes))
first_node_index = randrange(0,len(nodes))
# Randomly select a number for the second node with the same limitations
second_node_index = rand(0,len(nodes))
second_node_index = randrange(0,len(nodes))
# Check to see we didn't unfortunately get the same number twice
while second_node_index == first_node_index:
# If we did, pick a new second number
second_node_index = rand(0,len(nodes))
second_node_index = randrange(0,len(nodes))

# Retrieve the first blending node by its index and keep it in C1
C1 = nodes[first_node_index]
Expand All @@ -38,23 +40,25 @@ def run(self, atomspace):
# Loop through the links in C1
for link in find_links_upward(C1):
# Check to see if it's a SimilarityLink
if link.type_name == "SimilarityLink":
if link.type_name == 'SimilarityLink':
# If so, add it to C1_links
C1_links.append(link)
# Loop through the links in C2
for link in find_links_upward(C2):
# Check to see if it's a SimilarityLink
if link.type_name == "SimilarityLink":
if link.type_name == 'SimilarityLink':
# If so, add it to C2_links
C2_links.append(link)


C3 = atomspace.add_node("ConceptNode", C1.name + C2.name)
C3 = atomspace.add_node('ConceptNode', C1.name + C2.name)
print C3
links = C1_links + C2_links
for i in range(len(links)):
link = links[rand(0,len(links))]
link = links[randrange(0,len(links))]
for node in link.out:
atomspace.add_link("SimilarityLink", node)
link =atomspace.add_link('SimilarityLink', (C3,node))
print link



Expand Down
21 changes: 21 additions & 0 deletions opencog/python/examples/bat_man.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ConceptNode "bat")
(ConceptNode "man")

(SimilarityLink (stv 1.0 1.0)
(ConceptNode "bat")
(ConceptNode "flying_thing")
)
(SimilarityLink (stv 1.0 1.0)
(ConceptNode "bat")
(ConceptNode "blood_drinker")
)


(SimilarityLink (stv 1.0 1.0)
(ConceptNode "man")
(ConceptNode "two_leg_creatures")
)
(SimilarityLink (stv 0 0)
(ConceptNode "man")
(ConceptNode "blood_drinker")
)
6 changes: 5 additions & 1 deletion opencog/python/examples/blending_agent_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
from blending.agents import DummyBlendingAgent
from opencog.atomspace import AtomSpace
from opencog.cogserver import Server
from load_scm_file import load_scm_file

# Check if git shows the branch
if __name__ == '__main__':
server = Server()
server.add_mind_agent(DummyBlendingAgent())
server.run(AtomSpace())
a = AtomSpace()
load_scm_file(a, '../examples/bat_man.scm')
a.print_list()
server.run(a)
7 changes: 6 additions & 1 deletion opencog/python/opencog/atomspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def value(self):
def __init__(self):
self._tv = DEFAULT_TV
self._av = DEFAULT_AV
self._in = []

# legacy Cython-style interface
self.out = []
Expand Down Expand Up @@ -75,7 +76,7 @@ def setav(self,av):

# Needs to be updated by the atomspace
def getincoming(self):
return iter([])
return iter(self._in)
incoming = property(getincoming)

def __cmp__(self,other):
Expand Down Expand Up @@ -215,6 +216,10 @@ def _add(self,atom):
# if isinstance(atom, Link):
# for o in atom.out:
# assert self._atom_object_is_in_atomspace(o)
# if isinstance(atom, Link):
# for o in atom.out:
# assert isinstance(o, Atom)
# o._in.append(atom)

self.atoms_by_id.add(atom)
self.atoms_by_signature[atom._signature()] = atom
Expand Down

0 comments on commit 90e5717

Please sign in to comment.