Esempio n. 1
0
    def insert(self , item , index):
        if self.head is None and index > 0:
            raise IndexError("OUT OF RANGE")
        elif index == 0  and self.head is None:
            self.add_First(item)
            return
        elif index == 0 and self.head.next is not None:
            prev_node = self.tail_node().next
            self.head = Node(item, self.head)
            prev_node.next = self.head
            self.length += 1
            return
        elif index == self.length:
            self.add_last(item)
            return
        counter = 0

        curr_node = self.head
        while counter < index and curr_node.next != self.head:
            prev_node = curr_node
            curr_node = curr_node.next
            counter += 1

        curr_node = Node(item , curr_node)
        prev_node.next = curr_node
        self.length += 1
Esempio n. 2
0
 def insert_after_node(self, prev_node, data):
     new_node = Node(data)
     if not prev_node:
         print("Previous mode doesn't exist")
         return
     new_node.next = prev_node.next
     prev_node.next = new_node
Esempio n. 3
0
def test_connect():
    node_1 = Node.Node("zomg")
    node_2 = Node.Node("omg")

    node_1.connectWith("energy", node_2)

    assert len(node_1.getAllOutgoingConnectionsByType("energy")) == 1
    assert len(node_2.getAllIncomingConnectionsByType("energy")) == 1
 def push(self, data):
     node = Node(data)
     if self.top:
         node.next = self.top
         self.top = node
     else:
         self.top = node
     self.size += 1
    def add_first(self, item):
        if self.head is None:
            self.head = Node(item)
            self.tail= self.head
            self.length +=1
            return
        curr_node = self.head
        self.head =Node(item, curr_node)

        self.length +=1
 def add_first(self, item):
     if self.head is None:
         self.head = Node(item, next=None, prev=None)
         self.length += 1
         return
     new_node = Node(item, self.head, None)
     self.head.prev = new_node
     self.head = new_node
     self.length += 1
     return
Esempio n. 7
0
 def add_First(self, item):
     if self.head is None:
         self.head = Node(item)
         self.head.next = self.head
         self.length += 1
         return
     prev_node = self.tail_node().next
     new_node = Node(item, self.head)
     self.head = new_node
     prev_node.next  = self.head
     self.length += 1
Esempio n. 8
0
 def append(self, value):
     newNode = Node(value)
     if not self.head:
         self.head = newNode
         self.head.next = newNode
     else:
         curr = self.head
         while curr.next != self.head:
             curr = curr.next
         curr.next = newNode
         newNode.next = self.head
Esempio n. 9
0
def test_connectNodes():
    node = Node.Node("SuchNode!")
    node_2 = Node.Node("anotherNode")

    node.connectWith("energy", node_2)

    assert len(node.getAllOutgoingConnections()) == 1
    assert len(node.getAllIncomingConnections()) == 0

    assert len(node_2.getAllOutgoingConnections()) == 0
    assert len(node_2.getAllIncomingConnections()) == 1
Esempio n. 10
0
    def prepend(self, data):
        cur = self.head
        new_node = Node(data)
        new_node.next = self.head

        if not self.head:
            new_node.next = new_node
        else:
            while cur.next != self.head:
                cur = cur.next
            cur.next = new_node
        self.head = new_node
Esempio n. 11
0
    def add_last(self , item):

        if self.head is None:
            self.head = Node(item)
            self.head.next = self.head
            self.length += 1
            return
        curr_node = self.head
        while curr_node.next != self.head:
            curr_node = curr_node.next
        curr_node.next = Node(item , self.head)
        self.length += 1
Esempio n. 12
0
    def append(self, data):
        new_node = Node(data)
        cur_node = self.head

        if not self.head:
            self.head = new_node
            new_node.next = self.head  # Idk, I can also set this to new_node abi?
            return

        while cur_node.next != self.head:
            cur_node = cur_node.next
        cur_node.next = new_node
        new_node.next = self.head
Esempio n. 13
0
 def sort_number(self):
     curr = self.head
     arr = []
     if self.get_length() == 0 or self.get_length() == 1:
         return
     while curr:
         arr.append(curr.value)
         curr = curr.next
     arr.sort()
     self.head = Node(arr[0])
     arr.pop(0)
     for x in arr:
         self.append(Node(x))
Esempio n. 14
0
def test_repair():
    node = Node.Node("zomg")

    node._health = 20

    node.repair(100)
    assert node.health == 100
Esempio n. 15
0
def test_update():
    node = Node.Node("")
    node.updateCalled.emit = MagicMock()

    node.update()

    node.updateCalled.emit.assert_called_once_with(node)
Esempio n. 16
0
def test_serialize():
    node = Node.Node("zomg")

    serialized = node.serialize()

    assert serialized["node_id"] == node.getId()
    assert serialized["temperature"] == node.temperature
Esempio n. 17
0
def test__getReservedResourceByType():
    node = Node.Node("")
    connection = MagicMock()
    connection.getReservedResource = MagicMock(return_value=209)
    node.getAllIncomingConnectionsByType = MagicMock(return_value=[connection])

    assert node._getReservedResourceByType("zomg") == 209
Esempio n. 18
0
def test_requiresReplanningOneConnectionStatisifiedButDisabled():
    node = Node.Node("")
    node.enabled = False
    node.addConnection(MagicMock(isReservationStatisfied=MagicMock(return_value=True)))
    node.addConnection(MagicMock(isReservationStatisfied=MagicMock(return_value=False)))

    assert not node.requiresReplanning() # Disabled nods should never need to be replanned!
Esempio n. 19
0
def test_deserialize():
    node = Node.Node("BLOOORPP")

    node.deserialize({"node_id": "omgzomg", "temperature": 200, "resources_received_this_tick": {}, "resources_produced_this_tick": {}, "resources_left_over": {}})

    assert node.getId() == "omgzomg"
    assert node.temperature == 200
Esempio n. 20
0
    def insert(self, item, index):
        counter = 0
        new_node = Node(item)
        curr_node = self.head
        prev_node = None
        while counter < index:
            counter += 1
            prev_node = curr_node
            curr_node = curr_node.next

        new_node.next =curr_node
        if curr_node == self.head:
            self.head = new_node
        if prev_node is not None:
            prev_node.next = new_node
        self.length += 1
Esempio n. 21
0
def test_massiveHeatDamage():
    node = Node.Node("SuchNode!")
    node._temperature = 900000000000000

    node._dealDamageFromHeat()

    assert node.health == 0  # No amount of damage should ever let the health go below 0
Esempio n. 22
0
def test_unmodifiableNodeWithModifier():
    node = Node.Node("ModifiedNode", can_be_modified=False)
    assert node.can_be_modified == False

    modifier = MagicMock(spec=Modifier.Modifier)

    node.addModifier(modifier)
    assert modifier not in node.getModifiers()
Esempio n. 23
0
def test_simpleGetters(resource_type):
    # It's not really a test, but the results should remain the same, as other child nodes depend on this
    # implementation. Call them with all resources types known to us.
    node = Node.Node("")
    assert node.preGetResource(resource_type, 200) == 0
    assert node.preGiveResource(resource_type, 200) == 0
    assert node.getResource(resource_type, 200) == 0
    assert node.giveResource(resource_type, 200) == 0
Esempio n. 24
0
def test_provideResourceToOutogingConnections(connections, amount_to_provide,
                                              resources_left):
    node = Node.Node("")

    node.getAllOutgoingConnectionsByType = MagicMock(return_value=connections)

    assert node._provideResourceToOutgoingConnections(
        "fuel", amount_to_provide) == resources_left
Esempio n. 25
0
def test_requiresReplanningOneConnectionStatisified():
    node = Node.Node("")
    node.addConnection(
        MagicMock(isReservationStatisfied=MagicMock(return_value=True)))
    node.addConnection(
        MagicMock(isReservationStatisfied=MagicMock(return_value=False)))

    assert node.requiresReplanning()
Esempio n. 26
0
 def append(self, data):
     new_node = Node(data, None, None)
     if self.head is None:
         self.head = new_node
         self.tail = self.head
     else:
         new_node.prev = self.tail
         self.tail.next = new_node
         self.tail = new_node
Esempio n. 27
0
 def append(self, data):
     new_node = Node(data)
     if self.head is None:
         self.head = new_node
         return
     last_node = self.head
     while last_node.next:
         last_node = last_node.next
     last_node.next = new_node
Esempio n. 28
0
def test__convectiveHeatTransfer(starting_temperature, outside_temperature, heat_emitted):
    node = Node.Node("")
    node.addHeat = MagicMock()
    node.outside_temp = outside_temperature
    # Force the starting temperature
    node._temperature = starting_temperature

    node._convectiveHeatTransfer()
    node.addHeat.assert_called_with(heat_emitted)
Esempio n. 29
0
    def dfs(self):

        # holds list of visited sucessors
        visitedSuccessors = []

        # the fringe of this algorithm
        fringe = []

        visitedSuccessors.append(self.startState.getIndex())
        #print(visitedSuccessors)
        fringe.append(self.agnt.getIndex())
        while self.agnt.getIndex() != self.goalState.getIndex():
            successors = self.agnt.getSuccessors()
            #print("----------")
            #print("currentState", self.agnt.getIndex())
            #print("visited", visitedSuccessors)
            #print("fringe", self.fringe)
            #print("successors", successors)

            for s in successors:
                #print(s)
                if (s not in visitedSuccessors):
                    #print("PUSHING TO FRINGE", s)
                    fringe.append(s)
                    visitedSuccessors.append(s)
                    self.agnt.setCurrentState(Node(s))

                    if len(self.agnt.getSuccessors()) == 0:
                        #print("running IF")
                        if (self.agnt.getIndex() == self.goalState.getIndex()):
                            break
                        else:
                            self.agnt.setCurrentState(
                                Node(fringe[len(fringe) - 2]))
                            fringe.pop()
                        break

                elif s == successors[len(successors) - 1]:
                    if (len(fringe)):
                        self.agnt.setCurrentState(Node(fringe[len(fringe) -
                                                              2]))
                        fringe.pop()
                    break
        return fringe
 def append(self, data):
     node = Node(data)
     if self.tail:
         self.tail.next = node
         self.tail = node
     else:
         self.head = node
         self.tail = node
     self.tail.next = self.head
     self.size += 1
Esempio n. 31
0
    def get_node(self, hostname):
	node = Node(self)
	if hasattr(self, 'nodes') and hostname in self.nodes.keys():
	    node.update(self.nodes[hostname])
	    node.__init_logfile__()
	return node			
Esempio n. 32
0
    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