示例#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_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)
示例#3
0
文件: Parser.py 项目: GretelF/semNets
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
示例#4
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)
示例#5
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)
示例#6
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)
示例#7
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)
示例#8
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
示例#9
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)
示例#10
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)
示例#11
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
示例#12
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')")
示例#13
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')")
示例#14
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)
示例#15
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)
示例#16
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
示例#17
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
示例#18
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)
示例#19
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)
示例#20
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))
示例#21
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)
示例#22
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}
示例#23
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
示例#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 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}
示例#26
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')")
示例#27
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}
示例#28
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)
示例#29
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)
示例#30
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)
示例#31
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
示例#32
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)
示例#33
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
示例#34
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
示例#35
0
def existsRelationInTopology(env, topology, type, source, target):
    t = env.vars[topology]
    relation = Relation(RelationType(type), Node(source), Node(target))
    result = t.existsRelation(relation)

    return {"result": result}
示例#36
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)