Пример #1
0
  def test_load(self):
    with open("TestData.json") as file:
      net = json.load(file)

    t = Topology()
    t.load(net)

    self.assertTrue(t.existsNode(Node("bird")))
    self.assertTrue(t.existsNode(Node("pelican")))
    self.assertTrue(len(t.nodes), 4)
    self.assertEqual(len(t.relations),4)
    self.assertEqual(t.nodes[1], Node("pelican"))
    self.assertEqual(t.nodes[1].attributes[0].type, NodeAttributeType("size"))
    self.assertEqual(t.nodes[1].attributes[0].value, "big")
    self.assertEqual(t.nodes[1].attributes[1].type, NodeAttributeType("color"))
    self.assertEqual(t.nodes[1].attributes[1].value, "white")

    r = Relation(RelationType("is_a"), Node("pelican"), Node("bird"))

    self.assertEqual(t.relations[0], r)

    r2 = Relation(RelationType("has"), Node("bird"), Node("wings"))
    r2.createAttribute(RelationAttributeType("amount"), 2)

    self.assertEqual(t.relations[1], r2)
Пример #2
0
 def test_relationType(self):
     rt = RelationType("is-a")
     rt2 = RelationType("is-a")
     self.assertEqual(rt.name, "is-a")
     self.assertEqual(str(rt), "is-a")
     self.assertEqual(repr(rt), "RelationType(name = 'is-a')")
     self.assertIs(rt, rt2)
Пример #3
0
def createRelation(env, type, source, target):
    relationtype = RelationType(type)
    relation = Relation(type, Node(source), Node(target))
    # todo: do relations need to be saved in the environment?
    # todo: + what name should they get?

    return success
Пример #4
0
    def test_calculateDistance_differentOrMissingAttributes(self):
        #               emporer penguin:    emporer penguin
        #   wings       2                   2
        #   color       blackwhite          blackwhite
        #   size        big                 -
        #   population  -                   notsomany

        rt = RelationType("is_a")
        at = RelationAttributeType("wings")
        at2 = RelationAttributeType("color")
        at3 = RelationAttributeType("size")
        at4 = RelationAttributeType("population")
        source = Node("EmporerPenguin")
        target = Node("Bird")
        r = Relation(rt, source, target)
        a = r.createAttribute(at, 2)
        a2 = r.createAttribute(at2, "blackwhite")
        a3 = r.createAttribute(at3, "big")
        r2 = Relation(rt, source, target)
        a4 = r2.createAttribute(at, 2)
        a5 = r2.createAttribute(at2, "blackwhite")
        a6 = r2.createAttribute(at4, "notsomany")

        dist = gd.calculateRelationDistance(r, r2)
        self.assertEqual(dist, 0.125)
Пример #5
0
    def load(self, data):
        '''
    This function loads a topology from a dictionary.
    All previous nodes and relations, if existing, are wiped out.
    '''
        self.nodes.clear()
        self.relations.clear()

        attributetypes = data.get("attributetypes")
        relationtypes = data.get("relationtypes")
        for node in data.get("nodes"):  # load nodes
            self.nodes.append(Node(node))
        if "node_attributes" in data:
            for node_attribute in data.get(
                    "node_attributes"):  # load node attributes
                at = NodeAttributeType(attributetypes[node_attribute[0]])
                index = node_attribute[1]
                value = node_attribute[2]
                self.nodes[index].createAttribute(at, value)
        for relation in data.get("relations"):  # load relations
            rt = RelationType(relationtypes[relation[0]])
            source = self.nodes[relation[1]]
            target = self.nodes[relation[2]]
            self.relations.append(Relation(rt, source, target))
        for relation_attribute in data.get(
                "relation_attributes"):  # load relation attributes
            at = RelationAttributeType(attributetypes[relation_attribute[0]])
            index = relation_attribute[1]
            value = relation_attribute[2]
            self.relations[index].createAttribute(at, value)
Пример #6
0
    def test_basicViewOperations(self):
        n = Node("penguin")
        t = buildTopology()
        t.insertNode(n)
        r = Relation(RelationType("is_a"), n, Node("bird"))
        t.insertRelation(r)
        v = View(t)

        with self.assertRaises(AssertionError):
            v.includeNode(Node("beatle"))  # does not exist in topology t

        self.assertEqual(len(v.nodes), 0)
        self.assertEqual(len(v.relations), 0)

        v.includeNode(n)
        v.includeRelation(r)

        self.assertIn(n, v.nodes)
        self.assertIn(r, v.relations)

        v.mend()

        self.assertIn(
            Node("bird"), v.nodes
        )  # after mend the Node(name="bird") should be in the view, too
Пример #7
0
    def test_RelationMatching(self):
        rt = RelationType("is_a")
        at = RelationAttributeType("wings")
        at2 = RelationAttributeType("color")
        at3 = RelationAttributeType("size")
        at4 = RelationAttributeType("population")
        source = Node("EmporerPenguin")
        target = Node("Bird")
        target2 = Node("Thing")
        r = Relation(rt, source, target)
        r.createAttribute(at, 2)
        r.createAttribute(at2, "blackwhite")
        r.createAttribute(at3, "big")
        r2 = Relation(rt, source, target)
        r2.createAttribute(at, 2)
        r2.createAttribute(at2, "blackwhite")
        r2.createAttribute(at4, "notsomany")
        r3 = Relation(rt, source, target2)
        r3.createAttribute(at, 4)
        r3.createAttribute(at2, "black")
        r3.createAttribute(at3, "enormous")

        t = Topology()
        t.insertNode(source)
        t.insertNode(target)
        t.insertNode(target2)
        t.insertRelation(r3)
        t.insertRelation(r)
        t.insertRelation(r2)

        matchedRelations = gd.matchRelations(r, source, t)
        self.assertEqual(matchedRelations[0], r)
        self.assertEqual(matchedRelations[1], r2)
        self.assertEqual(matchedRelations[2], r3)
Пример #8
0
    def test_firstparsertests(self):
        # load static semantic network consisting of card names and so on
        # still missing: things like "game" and "turn"

        with open("homerock.json") as file:
            net1 = json.load(file)

        g1 = Topology()
        g1.load(net1)
        g2 = Topology()

        #with open("hearthstone_log_parseTransitionsTest.txt") as file:
        with open("hearthstone_2016_06_20_15_30_29.log") as file:
            g2 = parseLog(file, g1)

        test = g2.toJsonNamedTriples()
        with open("test.json", "w") as file:
            json.dump(test, file)

        relationtype_is_a = RelationType("is_a")
        relationtype_has = RelationType("has")
        water_elemental = Node("Water Elemental")
        friendly_deck = Node("Location_Friendly_Deck_0")
        minion_0 = Node("Minion_0")
        relation1 = Relation(relationtype_is_a, minion_0, water_elemental)
        relation2 = Relation(relationtype_has, friendly_deck, minion_0)
        self.assertTrue(
            g2.existsRelation(relation1),
            "The relation 'Minion_0 is_a Water Elemental' should exist")
        self.assertTrue(
            g2.existsRelation(relation2),
            "The relation 'Location_Friendly_Deck_0 has Minion_0' should exist"
        )
        id = g2.getNodeByName("Minion_0").getAttributeValue(
            NodeAttributeType("id"))
        self.assertEqual(
            id, 17, "minion_0 should have the ID 17, but has {0}".format(id))
        id = g2.getNodeByName("Minion_1").getAttributeValue(
            NodeAttributeType("id"))
        self.assertEqual(
            id, 29, "minion_1 should have the ID 23, but has {0}".format(id))
        id = g2.getNodeByName("Minion_2").getAttributeValue(
            NodeAttributeType("id"))
        self.assertEqual(
            id, 13, "minion_2 should have the ID 13, but has {0}".format(id))
Пример #9
0
 def test_SameAttributeTypeTwice(self):
     rt = RelationType("has")
     at = RelationAttributeType("amount")
     source = Node("bird")
     target = Node("wing")
     r = Relation(rt, source, target)
     a = r.createAttribute(at, 2)
     with self.assertRaises(AssertionError):
         a2 = r.createAttribute(at, 10)
Пример #10
0
 def test_hasAttributeOfType(self):
     rt = RelationType("has")
     at = RelationAttributeType("amount")
     source = Node("bird")
     target = Node("wing")
     r = Relation(rt, source, target)
     a = r.createAttribute(at, 2)
     x = r.hasAttributeOfType(at)
     self.assertTrue(x)
Пример #11
0
def relationGetAttributeValue(env, topology, type, source, target,
                              attributeType):
    tmpRelation = Relation(RelationType(type), Node(source), Node(target))
    t = env.vars[topology]
    assert t is not None
    r = t.tryGetRelation(tmpRelation)
    assert r is not None
    value = r.getAttributeValue(RelationAttributeType(attributeType))

    return {"result": value}
Пример #12
0
def relationHasAttribute(env, topology, type, source, target, attributeType,
                         value):
    tmpRelation = Relation(RelationType(type), Node(source), Node(target))
    attr = Attribute(RelationAttributeType(attributeType), value)
    t = env.vars[topology]
    assert t is not None
    r = t.tryGetRelation(tmpRelation)
    assert r is not None
    result = r.hasAttribute(attr)

    return {"result": result}
Пример #13
0
    def test_calculateDistance_differentSource(self):
        rt = RelationType("is_a")
        source = Node("EmporerPenguin")
        source2 = Node("LittlePenguin")
        target = Node("Bird")

        r = Relation(rt, source, target)
        r2 = Relation(rt, source2, target)

        dist = gd.calculateRelationDistance(r, r2)
        self.assertEqual(dist, 0.25)
Пример #14
0
    def test_calculateDistance_differentTarget(self):
        rt = RelationType("is_a")
        source = Node("Anna")
        target = Node("Student")
        target2 = Node("Employee")

        r = Relation(rt, source, target)
        r2 = Relation(rt, source, target2)

        dist = gd.calculateRelationDistance(r, r2)
        self.assertEqual(dist, 0.25)
Пример #15
0
 def test_relation(self):
     rt = RelationType("is-a")
     source = Node("pelican")
     target = Node("bird")
     r = Relation(rt, source, target)
     self.assertEqual(r.type, rt)
     self.assertEqual(r.source, source)
     self.assertEqual(r.target, target)
     self.assertEqual(str(r), "pelican is-a bird")
     self.assertEqual(
         repr(r),
         "Relation(type = 'is-a', source = 'pelican', target = 'bird')")
Пример #16
0
    def test_relationWithAttributes(self):
        rt = RelationType("has")
        at = RelationAttributeType("amount")
        source = Node("bird")
        target = Node("wing")
        r = Relation(rt, source, target)
        a = r.createAttribute(at, 2)

        self.assertIs(r.attributes[0], a)
        self.assertIs(a.type, at)
        self.assertEqual(a.value, 2)
        self.assertEqual(str(a), "amount: 2")
        self.assertEqual(repr(a), "Attribute(type = 'amount', value = '2')")
Пример #17
0
def createRelation(topology,
                   source,
                   relationType,
                   target,
                   attributeType=None,
                   attributeValue=None):
    relationtype = RelationType(relationType)
    relation = Relation(relationtype, source, target)
    if attributeType != None:
        relationAttributeType = RelationAttributeType(attributeType)
        relation.createAttribute(relationAttributeType, attributeValue)
    topology.insertRelation(relation)
    return relation
Пример #18
0
def relationHasAttributeOfType(env, topology, type, source, target,
                               attributeType):
    tmpRelation = Relation(RelationType(type), Node(source), Node(target))
    t = env.vars[topology]
    assert t is not None
    r = None
    for rel in t.relations:
        if rel.source == tmpRelation.source and rel.target == tmpRelation.target and rel.type == tmpRelation.type:
            r = rel
    assert r is not None
    result = r.hasAttributeOfType(RelationAttributeType(attributeType))

    return {"result": result}
Пример #19
0
    def test_calculateAttributeDistance_sameRelation(self):
        rt = RelationType("is_a")
        at = RelationAttributeType("wings")
        at2 = RelationAttributeType("color")
        at3 = RelationAttributeType("size")
        source = Node("EmporerPenguin")
        target = Node("Bird")
        r = Relation(rt, source, target)
        a = r.createAttribute(at, 2)
        a2 = r.createAttribute(at2, "blackwhite")
        a3 = r.createAttribute(at3, "big")

        dist = gd.calculateRelationDistance(r, r)
        self.assertEqual(dist, 0)
Пример #20
0
    def test_calculateDistance_Symmetry(self):
        rt = RelationType("is_a")
        at = RelationAttributeType("wings")
        at2 = RelationAttributeType("size")
        source = Node("EmporerPenguin")
        source2 = Node("LittlePenguin")
        target = Node("Bird")
        r = Relation(rt, source, target)
        r2 = Relation(rt, source2, target)
        r.createAttribute(at, 2)
        r2.createAttribute(at, 2)
        r.createAttribute(at2, "big")
        r2.createAttribute(at2, "small")

        dist1 = gd.calculateRelationDistance(r, r2)
        dist2 = gd.calculateRelationDistance(r2, r)
        self.assertEqual(dist1, dist2)
Пример #21
0
    def hasAnyRelation(topology, node):
        for rel in data:
            type = RelationType(rel["type"]) if rel["type"] != None else None
            target = Node(rel["target"]) if rel["target"] != None else None
            attrs = [
                Attribute(RelationAttributeType(a["type"]), a[value])
                for a in rel["attributes"]
            ] if rel["attributes"] != None else None
            relations = [r for r in topology.relations if r.source == node]

            for r in relations:
                if (type == None or r.type == type) and (
                        target == None
                        or r.target == target) and (attrs == None or all(
                            r.hasAttribute(a) for a in attrs)):
                    return True
        return False
Пример #22
0
def relationCreateAttribute(env,
                            topology,
                            relationType,
                            source,
                            target,
                            attributeType,
                            value,
                            attributes=None):
    tmpRelation = Relation(RelationType(relationType), Node(source),
                           Node(target))
    for type, value in attributes:
        tmpRelation.createAttribute(RelationAttributeType(type), value)
    t = env.vars[topology]
    assert t is not None
    r = t.tryGetRelation(tmpRelation)
    assert r is not None
    r.createAttribute(RelationAttributeType(attributeType), value)

    return success
Пример #23
0
    def missingAllRelations(topology, node):
        for rel in data:
            type = RelationType(rel["type"]) if rel["type"] != None else None
            target = Node(rel["target"]) if rel["target"] != None else None
            attrs = [
                Attribute(RelationAttributeType(a["type"]), a["value"])
                for a in rel["attributes"]
            ] if rel["attributes"] != None else None
            relations = [r for r in topology.relations if r.source == node]

            for r in relations:
                # if a relation is found, that does exist within the topology: return False
                if (type == None or r.type == type) and (
                        target == None
                        or r.target == target) and (attrs == None or all(
                            r.hasAttribute(a) for a in attrs)):
                    return False
        # if none of the given relations is found within the topology: return True
        return True
Пример #24
0
    def test_calculateDistance_customWeights(self):
        rt = RelationType("is_a")
        at = RelationAttributeType("wings")
        at2 = RelationAttributeType("size")
        source = Node("EmporerPenguin")
        source2 = Node("LittlePenguin")
        target = Node("Bird")

        r = Relation(rt, source, target)
        r2 = Relation(rt, source2, target)

        r.createAttribute(at, 2)
        r.createAttribute(at2, "big")
        r2.createAttribute(at2, "small")
        dist = gd.calculateRelationDistance(r,
                                            r2,
                                            wSource=0.5,
                                            wDifferentStringValue=0.3,
                                            wMissingAttribute=0.7)
        self.assertEqual(dist, 0.75)
Пример #25
0
    def hasAllRelations(topology, node):
        for rel in data:
            type = RelationType(rel["type"]) if rel["type"] != None else None
            target = Node(rel["target"]) if rel["target"] != None else None
            attrs = [
                Attribute(RelationAttributeType(a["type"]), a["value"])
                for a in rel["attributes"]
            ] if rel["attributes"] != None else None
            relations = [r for r in topology.relations if r.source == node]

            relationInRelationList = False
            for r in relations:
                if (type == None or r.type == type) and (
                        target == None
                        or r.target == target) and (attrs == None or all(
                            r.hasAttribute(a) for a in attrs)):
                    relationInRelationList = True

            # if the current relation does not exist within the topology: return False
            if not relationInRelationList:
                return False
        # if none of the given relations is missing in the topology: return True
        return True
Пример #26
0
    def missingAnyRelation(topology, node):
        for rel in data:
            type = RelationType(rel["type"]) if rel["type"] != None else None
            target = Node(rel["target"]) if rel["target"] != None else None
            attrs = [
                Attribute(RelationAttributeType(a["type"]), a["value"])
                for a in rel["attributes"]
            ] if rel["attributes"] != None else None
            relationInRelationList = False  # Flag

            relation = [r for r in topology.relations if r.source == node]
            for r in relations:
                if (type != None and r.type == type) or (
                        target != None
                        and r.target == target) or (attrs != None and any(
                            r.hasAttribute(a) for a in attrs)):
                    relationInRelationList = True
                    break

            # If a relation is found, that does not exist within the topology: return True
            if not relationInRelationList:
                return True
        # It the topology is not missing any of the given relations: return False
        return False
Пример #27
0
  def test_insertNodesAndRelations(self):
    t = Topology()
    n0 = Node("bird")
    n1 = Node("pelican")
    rt = RelationType("is_a")
    r = Relation(rt, n0, n1)

    self.assertFalse(t.existsNode(n0))
    self.assertFalse(t.existsNode(n1))
    self.assertFalse(t.existsRelation(r))

    t.insertNode(n0)
    t.insertNode(n1)
    t.insertRelation(r)

    self.assertTrue(t.existsNode(n0))
    self.assertTrue(t.existsNode(n1))
    self.assertTrue(t.existsRelation(r))

    with self.assertRaises(AssertionError):
      t.insertNode(n0)

    t.insertRelation(r)
    self.assertTrue(len(t.relations), 2)
Пример #28
0
def insertNodeInTopology(env, topology, type, source, target):
    t = env.vars[topology]
    relation = Relation(RelationType(type), Node(source), Node(target))
    t.insertRelation(relation)

    return success
Пример #29
0
def deleteRelationInTopology(env, topology, type, source, target):
    t = env.vars[topology]
    relation = Relation(RelationType(type), Node(source), Node(target))
    t.deleteRelation(relation)

    return success
Пример #30
0
def excludeRelationInView(env, view, type, source, target):
    v = env.vars[view]
    r = Relation(RelationType(type), Node(source), Node(target))
    v.excludeRelation(r)

    return success