Example #1
0
    def readGraph(self):

        tree = ET.parse(self._filename)
        root = tree.getroot()

        #Get our graph name
        graph_name = root.attrib.get('name')

        #Process the nodes
        node_list = []
        for node in root.iter('node'):

            story_node_name = node.attrib['name']
            node_attributes = {}

            #Get the target
            node_target = node.find('target').text

            #Get the node type
            node_attributes['Node_Type'] = node.find('nodetype').text

            #Get the attributes
            for attribute in node.findall('attr'):
                attribute_type = attribute.attrib.get('type')
                attribute_name = attribute.attrib.get('name')
                attribute_value = attribute.find('value').text
                if attribute_type == 'bool':
                    node_attributes[attribute_name] = (
                        attribute_value == "True")
                elif attribute_type == 'int':
                    node_attributes[attribute_name] = int(attribute_value)
                elif attribute_type == 'float':
                    node_attributes[attribute_name] = float(attribute_value)
                else:
                    node_attributes[attribute_name] = attribute_value

            new_node = StoryNode(story_node_name, node_attributes,
                                 self._graph.get_node_from_name(node_target))

            if not node.attrib['modification'] == 'None':
                modification_filename = self._path + "Modifications/" + node.attrib[
                    'modification']
                modification_reader = XMLModificationReader(
                    modification_filename)
                modification = modification_reader.readModification()
                new_node.set_modification(modification)

            node_list.append(new_node)

        #Create the Return Graph
        returnGraph = StoryGraph(graph_name, node_list)

        #Begin Making the connections
        for connection in root.iter('connection'):
            connect_from = connection.attrib.get('from')
            from_node = returnGraph.get_node_from_name(connect_from)

            connect_to = connection.attrib.get('to')
            to_node = returnGraph.get_node_from_name(connect_to)

            relation = connection.find('relation').attrib

            if relation.keys()[0] == "none":
                relation = {}

            returnGraph.connect(from_node, relation, to_node)

        return returnGraph
mod_a = {"Method" : {"name" : SocialNode.move_player_to_node, "self" : {"ref" : "Player"}, "args" : [{"ref" : "Recipient"}]}}
Move_2 = [mod_a]

# Kill Victim
mod_1 = {"Method" : {"name" : SocialNode.murder, "self" : {"ref" : "Recipient"}, "args" : [{"ref" : "Player"}]}}
mod_2 = {"Attribute" : {"name" : {"ref" : "Recipient"}, "key" : "alive", "value" : False}}
mod_3 = {"Method" : {"name" : SocialNode.set_other_nodes_relations, "self" : {"ref" : "Recipient"}, "args" : [["Friends", "Loves"], "Hates", {"ref" : "Invoker"}, "MurderBlackmailer_of_"]}}
mod_4 = {"Method" : {"name" : SocialNode.set_other_nodes_relations, "self" : {"ref" : "Recipient"}, "args" : [["Hates", "Enemies"], "Friends", {"ref" : "Invoker"}, "MurderBlackmailer_of_"]}}

Kill_Modification = [mod_1, mod_2, mod_3, mod_4]

#----------------------------------
# Story Results
#----------------------------------
One = StoryNode("MurderBlackmailer_Request_from_Invoker", {"Node_Type" : "Info"}, A)
One.set_modification(Move_1)
Two = StoryNode("Go_To_Victim", {"Node_Type" : "Go_To"}, B)
Two.set_modification(Move_2)
Three = StoryNode("Kill_Victim", {"Node_Type" : "MurderBlackmailer"}, B)
Three.set_modification(Kill_Modification)
Four = StoryNode("Return_to_Invoker", {"Node_Type" : "Return"}, A)
Four.set_modification(Move_1)

MurderBlackmailer_Story = StoryGraph("MurderBlackmailer_Story", [One, Two, Three, Four])
MurderBlackmailer_Story.connect(One, {}, Two)
MurderBlackmailer_Story.connect(Two, {}, Three)
MurderBlackmailer_Story.connect(Three, {}, Four)

MurderBlackmailer_Rule = RewriteRule(None, MurderBlackmailer, MurderBlackmailer_Story, None, "Murder_Blackmailer")

#----------------------------------------------------
Example #3
0
    def readGraph(self):
        
        tree = ET.parse(self._filename)
        root = tree.getroot()
        
        #Get our graph name
        graph_name = root.attrib.get('name')
        
        #Process the nodes
        node_list = []
        for node in root.iter('node'):
            
            story_node_name = node.attrib['name']
            node_attributes = {}
            
            #Get the target
            node_target = node.find('target').text

            #Get the node type
            node_attributes['Node_Type'] = node.find('nodetype').text
              
            #Get the attributes
            for attribute in node.findall('attr'):
                attribute_type = attribute.attrib.get('type')
                attribute_name = attribute.attrib.get('name')
                attribute_value = attribute.find('value').text
                if attribute_type == 'bool':
                    node_attributes[attribute_name] = (attribute_value == "True")
                elif attribute_type == 'int':
                    node_attributes[attribute_name] = int(attribute_value)
                elif attribute_type == 'float':
                    node_attributes[attribute_name] = float(attribute_value)
                else:
                    node_attributes[attribute_name] = attribute_value
                    
            new_node = StoryNode(story_node_name, node_attributes, self._graph.get_node_from_name(node_target))
           
            if not node.attrib['modification'] == 'None':
                modification_filename = self._path + "Modifications/" + node.attrib['modification']
                modification_reader = XMLModificationReader(modification_filename)
                modification = modification_reader.readModification()        
                new_node.set_modification(modification)

            node_list.append(new_node)
            
        #Create the Return Graph
        returnGraph = StoryGraph(graph_name, node_list)
        
        #Begin Making the connections
        for connection in root.iter('connection'):
            connect_from = connection.attrib.get('from')
            from_node = returnGraph.get_node_from_name(connect_from)
            
            connect_to = connection.attrib.get('to')
            to_node= returnGraph.get_node_from_name(connect_to)
            
            relation = connection.find('relation').attrib
            
            if relation.keys()[0] == "none":
                relation = {}
                
            returnGraph.connect(from_node, relation, to_node)
        
        return returnGraph