8 The following methods can be used for generating Sokoban atoms from a file/url:
9 - add_socoban_level_to_atomspace_from_file
10 - add_socoban_level_to_atomspace_from_url
12 Use levels from http://www.sourcecode.se/sokoban/levels.php
15 __author__ =
'keyvan-m-sadeghi'
17 from xml.dom.minidom
import parseString
18 from opencog.atomspace
import AtomSpace, TruthValue, Atom
19 from opencog.atomspace
import types
24 levels[level_index].append_to_atomspace(atomspace)
28 Url should point to 'http://www.sourcecode.se/sokoban/' database
29 e.g. 'httpd://www.sourcecode.se/sokoban/dnldlevel.php?LevelFile=100Boxes.slc'
33 levels[level_index].append_to_atomspace(atomspace)
36 global TRUTH_VALUE, TIME, BLOCK, MOVABLE_BLOCK, MOVABLE_OBJECT, SOKOBAN_GOAL
37 TRUTH_VALUE = TruthValue(1,1)
38 TIME = atomspace.add_node(types.TimeNode,
'0')
39 BLOCK = atomspace.add_node(types.ConceptNode,
'block')
40 MOVABLE_BLOCK = atomspace.add_node(types.ConceptNode,
'MovableBlock')
41 MOVABLE_OBJECT = atomspace.add_node(types.ConceptNode,
'MovableObject')
42 SOKOBAN_GOAL = atomspace.add_node(types.ConceptNode,
'SokobanGoal')
43 atomspace.add_link(types.InheritanceLink, [MOVABLE_BLOCK, MOVABLE_OBJECT], TRUTH_VALUE)
53 data = open_xml_stream.read()
54 open_xml_stream.close()
55 levelTags = parseString(data).getElementsByTagName(
'Level')
57 for levelTag
in levelTags:
58 levels.append(
Level(levelTag))
64 def __init__(self, x = 0, y = 0, charachter = ' '):
76 return self.atomspace.add_node(types.PetNode,
'OAC_MrSokobanRobot')
79 box = self.atomspace.add_node(types.ObjectNode,
'id_movable_block' +
80 str(self.x) +
'_' + str(self.
y) +
'_99')
81 self.atomspace.add_link(types.InheritanceLink, [box, MOVABLE_BLOCK], TRUTH_VALUE)
85 wall = self.atomspace.add_node(types.ObjectNode,
'id_CHUNK_0_0_0_BLOCK' +
86 str(self.x) +
'_' + str(self.
y) +
'_99')
87 self.atomspace.add_link(types.InheritanceLink, [wall, BLOCK], TRUTH_VALUE)
91 goal = self.atomspace.add_node(types.ObjectNode,
'id_SokobanGoal_' +
92 str(self.x) +
'_' + str(self.
y) +
'_99')
94 self.atomspace.add_link(types.InheritanceLink, [goal, SOKOBAN_GOAL], TRUTH_VALUE)
100 _method_by_charachter = {
'@':(_add_to_atomspace_as_player,),
101 '+':(_add_to_atomspace_as_player, _add_to_atomspace_as_goal),
102 '$':(_add_to_atomspace_as_box,),
103 '*':(_add_to_atomspace_as_box, _add_to_atomspace_as_goal),
104 '#':(_add_to_atomspace_as_wall,),
105 '.':(_add_to_atomspace_as_goal,),
110 atomspace, truth_value, time = self.
atomspace, TRUTH_VALUE, TIME
112 x = atomspace.add_node(types.NumberNode, str(self.x + 1.5))
113 y = atomspace.add_node(types.NumberNode, str(self.
y + 1.5))
114 z = atomspace.add_node(types.NumberNode,
'99.5')
116 list = atomspace.add_link(types.ListLink, [node, x, y, z])
117 predicate = atomspace.add_node(types.PredicateNode,
"AGISIM_position")
119 evaluation = atomspace.add_link(types.EvaluationLink, [predicate, list])
120 at_time = atomspace.add_link(types.AtTimeLink, [time, evaluation], truth_value)
121 atomspace.add_link(types.LatestLink, [at_time])
125 return "'" + self.
character +
"' at" +
' X=' + str(self.x) +
' Y=' + str(self.
y)
137 self.
Height = int(xml_tag.getAttribute(
'Height'))
138 self.
Width = int(xml_tag.getAttribute(
'Width'))
140 xmlRows = xml_tag.getElementsByTagName(
'L')
142 for i, xmlRow
in enumerate(xmlRows):
144 rowString = str(xmlRow.toxml().\
145 replace(
'<L>',
'').replace(
'</L>',
''))
147 for j
in range(self.
Width):
148 if j == len(rowString):
150 character = rowString[j]
151 if character ==
'@' or character ==
'+':
153 block =
Block(i, j, character)
154 blockRow.append(block)
156 self.Rows.append(blockRow)
160 block.append_to_atomspace(atomspace)
163 for row
in self.
Rows:
168 return self.
Rows[index]
170 if __name__ ==
'__main__':
171 atomspace = AtomSpace()
173 'http://www.sourcecode.se/sokoban/dnldlevel.php?LevelFile=100Boxes.slc', 0)
174 atomspace.print_list()
def _add_globals_to_atomspace
def _give_position_to_node
def _add_to_atomspace_as_wall
def generate_levels_from_file
def generate_levels_from_url
def add_socoban_level_to_atomspace_from_file
def _add_to_atomspace_as_box
def _add_to_atomspace_as_goal
def _add_to_atomspace_as_player
dictionary _method_by_charachter
def add_socoban_level_to_atomspace_from_url