def predictSolnCard(self, cardAName, cardBName, cardCName, cardDName): pp = pprint.PrettyPrinter(indent=4) D = Card(cardDName) #Predict number of nodes in D nodesInA = len(self.cards[cardAName].nodes) nodesInB = len(self.cards[cardBName].nodes) nodesInC = len(self.cards[cardCName].nodes) nodesInD = nodesInC - (nodesInA - nodesInB) #################### got 1 que #return ansName # apply triangular properties and relations # A--B # | # C # # - Regus #for i in range(1, nodesInOption): i=-1 # deducting for 2x2 ABRel = self.correspondenceList[cardAName+','+cardBName] ACRel = self.correspondenceList[cardAName+','+cardCName] # from ACRel.related, remove the sets which contain elements from AB.deleted validNodesInC = [] for (x,y,w) in ACRel['related']: if x not in ABRel['deleted']: # find the related pair of x in ABRel[related] and match it with x for (p, q,r) in ABRel['related']: if p == x: #got it. form CBRel validNodesInC.append((y, p, q, 0)) print "CABRelation nodes in C:" pp.pprint(validNodesInC) #also add newly added nodes #for x in ABRel['added']: # validNodesInC.append(x) i=-1 for (c, a, b, w) in validNodesInC: i+=1 c = self.nodes[c] a = self.nodes[a] b = self.nodes[b] dNode = Node(D.name+"_"+str(i), {}, {}, {}) print "Initialized dNode"+dNode.name dNode.printNode() D.addNode(dNode) print "Creating nodes in "+dNode.name +"("+c.name+","+a.name+","+b.name+")" # predict properties: for property in b.properties: print "Recreating property: "+b.name+"."+ property+"="+b.properties[property]+ " in "+dNode.name # predict value of d.property based on the property type valA = a.properties[property] valB = b.properties[property] valC = c.properties[property] if valA.isdigit() and valB.isdigit() and valC.isdigit(): valD = self.predictNumericProperty(property, int(valA), int(valB), int(valC)) print "Predicting number: "+valC+"+("+valB+"-"+valA+")="+str(valD) print "int D."+property+" = "+str(valD) dNode.addProperties(property, str(valD) ) # Predict Boolean property elif self.isBool(valA): if valA == valB: dNode.addProperties(property, valC) else: dNode.addProperties(property, self.invertBoolProperty(valC)) else: dNode.addProperties(property, self.predictFromChain(valA, valB, valC)) #predict relations: for relation in b.relations: dNode.addRelation(relation, b.relations[relation]) dNode.addProperties('inCard', D.name) dNode.printNode() self.addNode(dNode.name, dNode.properties, {}) self.addCard(D) return D.name
def formSemanticNet(self, problem): graph = Graph() inCard = {} pp = pprint.PrettyPrinter(indent=4) #populate all inCard[obects] = figure, before parsing other things # example inCards[b] = A for figName, figure in problem.figures.iteritems(): # print "Figure: "+figName # pp.pprint(figure.objects) for objName, obj in figure.objects.iteritems(): inCard[obj.name] = figure.name #pp.pprint(obj.attributes) # print "inCards are: " # pp.pprint(inCard) graph.inCard = inCard print "done building inCards. Initiating SemNet construction..." # now we can identify which attribute is an attribute and which attribute is a relation for figName, figure in problem.figures.iteritems(): # print "Parsing Figure: "+figName # print figure.visualFilename # pp.pprint(figure.objects) card = Card(figure.name) for objName, obj in figure.objects.iteritems(): properties = {} properties['inCard'] = figure.name # add the additional property to identify the parent figure relations = {} #clientForMigration.php #print "Object attributes:" #pprint.pprint(obj.attributes) for attrib, val in obj.attributes.iteritems(): vals = val.split(",") isNode = True #print "Adding "+attrib+"==>"+val for v in vals: if v in inCard: # relations are saved in the format: relation['inside'] = 'b', # where inside is the relation and 'b; is the target node if attrib in relations: relations[attrib].append(v) else: relations[attrib] = [v] else: isNode = False break if not isNode: # properties are saved in the format: properties['filled'] = 'yes' properties[attrib] = val # got all relations and properties of the object. form the node now. graph.addNode(obj.name, relations, properties) card.addNode(graph.getNode(obj.name)) # end of for graph.addCard(card) return graph