def readRule(self): tree = ET.parse(self._path + self._filename) root = tree.getroot() #Get our rewrite rule name ruleName = root.attrib.get('name') applyonce = (root.attrib.get('applyonce') == "true") socialConditionFilename = root.find('socialcondition').text + ".xml" socialConditionLoader = XMLSocialGraphReader(self._path + socialConditionFilename) socialCondition = socialConditionLoader.readGraph() storyConditionFilename = root.find('storycondition').text + ".xml" storyConditionLoader = XMLStoryGraphReader(socialCondition, self._path, storyConditionFilename) storyCondition = storyConditionLoader.readGraph() storyModificationFilename = root.find( 'storymodification').text + ".xml" storyModificationLoader = XMLStoryGraphReader( socialCondition, self._path, storyModificationFilename) storyModification = storyModificationLoader.readGraph() return RewriteRule(storyCondition, socialCondition, storyModification, None, ruleName, applyonce)
def readInitRule(self): tree = ET.parse(self._path + self._filename) root = tree.getroot() #Get our rewrite rule name ruleName = root.attrib.get('name') socialConditionFilename = root.find('socialcondition').text + ".xml" socialConditionLoader = XMLSocialGraphReader(self._path + socialConditionFilename) socialCondition = socialConditionLoader.readGraph() storyModificationFilename = root.find('storymodification').text + ".xml" storyModificationLoader = XMLStoryGraphReader(socialCondition, self._path, storyModificationFilename) storyModification = storyModificationLoader.readGraph() return RewriteRule(None, socialCondition, storyModification, None, ruleName)
def readTestLayout(self): layout = TestLayout() tree = ET.parse(self._filename) root = tree.getroot() layout.set_name(root.attrib.get('name')) layout.set_save_output(root.attrib['save_output'] == 'True') socialgraph = root.find('socialgraph').text filename = self._path_to_data + "SocialGraphs/" + socialgraph + '.xml' reader = XMLSocialGraphReader(filename) layout.set_social_graph(reader.readGraph()) initializationrules = root.find('initializationrules') for rule in initializationrules.iter('initializationrule'): name = rule.text filename = self._path_to_data + "Rules/InitializationRules/" + name + "/" reader = XMLInitializationRuleReader(filename, name + ".xml") layout.add_initialization_rule(reader.readInitRule()) rewriterules = root.find('rewriterules') for rule in rewriterules.iter('rewriterule'): name = rule.text filename = self._path_to_data + "Rules/RewriteRules/" + name + "/" reader = XMLRewriteRuleReader(filename, name + ".xml") layout.add_rewrite_rule(reader.readRule()) analyze_metrics = root.find('metricstoanalyze') for metric in analyze_metrics.iter('metric'): layout.add_metric_to_analyze(metric.attrib.get('name')) optimize_metrics = root.find('metricstooptimize') for metric in optimize_metrics.iter('metric'): layout.add_metric_to_optimize( [metric.attrib.get('name'), int(metric.attrib.get('weight'))]) layout.set_number_of_stories_to_generate( int(root.find('numstoriestogenerate').text)) layout.set_max_number_of_rewrites( int(root.find('maxnumberofrewrites').text)) return layout
def readTestLayout(self): layout = TestLayout() tree = ET.parse(self._filename) root = tree.getroot() layout.set_name(root.attrib.get('name')) layout.set_save_output(root.attrib['save_output'] == 'True') socialgraph = root.find('socialgraph').text filename = self._path_to_data + "SocialGraphs/" + socialgraph + '.xml' reader = XMLSocialGraphReader(filename) layout.set_social_graph(reader.readGraph()) initializationrules = root.find('initializationrules') for rule in initializationrules.iter('initializationrule'): name = rule.text filename = self._path_to_data + "Rules/InitializationRules/" + name + "/" reader = XMLInitializationRuleReader(filename, name + ".xml") layout.add_initialization_rule(reader.readInitRule()) rewriterules = root.find('rewriterules') for rule in rewriterules.iter('rewriterule'): name = rule.text filename = self._path_to_data + "Rules/RewriteRules/" + name + "/" reader = XMLRewriteRuleReader(filename, name + ".xml") layout.add_rewrite_rule(reader.readRule()) analyze_metrics = root.find('metricstoanalyze') for metric in analyze_metrics.iter('metric'): layout.add_metric_to_analyze(metric.attrib.get('name')) optimize_metrics = root.find('metricstooptimize') for metric in optimize_metrics.iter('metric'): layout.add_metric_to_optimize([metric.attrib.get('name'), int(metric.attrib.get('weight'))]) layout.set_number_of_stories_to_generate(int(root.find('numstoriestogenerate').text)) layout.set_max_number_of_rewrites(int(root.find('maxnumberofrewrites').text)) return layout
from src.ReGEN.IO.XMLSocialGraphReader import XMLSocialGraphReader from src.ReGEN.IO.XMLSocialGraphWriter import XMLSocialGraphWriter import xml.dom.minidom import random a = XMLSocialGraphReader('../../Data/SocialGraphs/Fantasy_World_Improved.xml') b = a.readGraph() temp = [] random.seed(5) for node in b.get_nodes(): for node_2 in b.get_nodes(): if not node == node_2: if node.get_attributes()['type'] == 'NPC' and node_2.get_attributes()['type'] == 'NPC': similarity = 0 if node.get_attributes()['conscientious'] == node_2.get_attributes()['conscientious']: similarity += 1 if node.get_attributes()['agreeable'] == node_2.get_attributes()['agreeable']: similarity += 1 if node.get_attributes()['neurotic'] == node_2.get_attributes()['neurotic']: similarity += 1 if node.get_attributes()['open'] == node_2.get_attributes()['open']: similarity += 1 if node.get_attributes()['extraverted'] == node_2.get_attributes()['extraverted']: similarity += 1 print similarity if similarity == 0: b.connect(node, {'Hates' : 'Personality'}, node_2) elif similarity == 1: