User:GeneaBot/Code/object constraint violation

From Wikidata
Jump to navigation Jump to search
# -*- coding: utf-8  -*-
import pywikibot
from object_display import *
from util_pwb       import *


ConstraintViolationInit = False
fctCheckConstraints = dict()



class ConstraintViolation:
    def __init__(self, ident, pProperty, pValue, pQualifiers, item, claimsToAdd, pClaimTarget, isQualifier=False, isSource = False):
        DisplayMessages.call("O", "ConstraintViolation." + ident + ".__init__", 5, "pProperty  =" + str(pProperty) + \
                                                       "\npValue     =" + str(pValue) + \
                                                       "\npQualifiers=" + repr(pQualifiers) + \
                                                       "\nitem       =" + repr(item) + \
                                                       "\nclaimsToAdd=" + str(claimsToAdd) + \
                                                       "\nisQualifier=" + str(isQualifier) + \
                                                       "\nisSource   =" + str(isSource))
        self.objectName     = "ConstraintViolation." + ident
        self.property       = pProperty
        self.value          = pValue
        self.qualifiers     = pQualifiers
        self.item           = item
        self.claimsToAdd    = claimsToAdd
        self.isQualifier    = isQualifier
        self.isSource       = isSource
        self.claimTarget    = pClaimTarget
        self.violation      = []
        CheckConstraints(self)
    #
    #
    #
    def __str__(self):
        text = " ; ".join(self.violation)
        DisplayMessages.call("O", self.objectName + ".__str__", 5, "return " + text)
        return text
    #
    #
    #
    #
    def ConflictsWith(self, Property, List=[]):
        DisplayMessages.call("O", self.objectName + ".ConflictsWith", 5, "Property=" + Property + ", List=" + str(List))
        text = ""
        if not self.value == "None":
            if Property in self.item.claims:
                DisplayMessages.debug("O", self.objectName + ".ConflictsWith", 9, Property + " present in claims")
                if List == []:
                    text = 'Claim "' + GetIdentAndLabel(self.property, 'P') + \
                                   '"="' + GetIdentAndLabel(self.value, 'Q') + '" must not be in item with ' + \
                                   'claim using "' + GetIdentAndLabel(Property, 'P') + '"'
                else:
                    for claim in self.item.claims[Property]:
                        value = ExtractItem(claim)
                        DisplayMessages.debug("O", self.objectName + ".ConflictsWith", 9, Property + "=" + value)
                        if  value in List:
                            text = 'Claim "' + GetIdentAndLabel(self.property, 'P') + \
                                   '"="' + GetIdentAndLabel(self.value, 'Q') + '" must not be in item with ' + \
                                   'claim "' + GetIdentAndLabel(Property, 'P') + \
                                   '"="' + GetIdentAndLabel(self.value, 'Q') + '"'
        if not text == "":
            self.violation.append(text)
            DisplayMessages.debug("O", self.objectName + ".ConflictsWith", 6, "violation=" + text)
    #
    #
    #
    #
    def DiffWithinRange(self, BaseProperty, Min, Max):
        # ====> Not implement
        DisplayMessages.call("O", self.objectName + ".DiffWithinRange",7,  "Not implement")
    #
    #
    #
    #
    def Item(self, Property, List = []):
        DisplayMessages.call("O", self.objectName + ".Item", 5, "Property=" + Property + ", List=" + str(List) )
        text = ""
        if not self.value == "None":
            PropPresent = False
            if Property in self.item.claims:
                PropPresent = True
            if Property in self.claimsToAdd:
                PropPresent = True
            if PropPresent:
                if not List == []:
                    ValueOK = False
                    if Property in self.item.claims:
                        for value in self.item.claims[Property]:
                            if value in List:
                                ValueOK = True
                    if Property in self.claimsToAdd:
                        for value in self.claimsToAdd[Property]:
                            if value in List:
                                ValueOK = True
                    if not ValueOK:
                        text = 'Property "' + GetIdentAndLabel(Property, 'P') + '" has value that is not in list of expected values'
            else:
                text = 'Property "' + GetIdentAndLabel(Property, 'P') + '" is not in item'
        #
        if not text == "":
            self.violation.append(text)
            DisplayMessages.debug("O", self.objectName + ".Item", 6, "violation : " + text )
    #
    #
    #
    #
    def OneOf(self, ListValues):
        DisplayMessages.call("O", self.objectName + ".OneOf", 5, "ListValues=" + str(ListValues))
        if not self.value == "None":
            if not self.value in ListValues:
                text = "value is not in list of excepted values"
                self.violation.append(text)
                DisplayMessages.debug("O", self.objectName + ".OneOf", 6, "violation : " + text )
    #
    #
    #
    #
    def Qualifier(self):
        DisplayMessages.call("O", self.objectName + ".Qualifier", 5, "")
        if not self.value == "None":
            if not self.isQualifier:
                text = '"' + GetIdentAndLabel(self.property, 'P') + '" must only be used as qualifier property'
                self.violation.append(text)
                DisplayMessages.debug("O", self.objectName + ".Qualifier", 6, "violation : " + text )
    #
    #
    #
    #
    def Qualifiers(self, List):
        # ====> Not implement
        DisplayMessages.call("O", self.objectName + ".Qualifiers", 5, "Not implement")
    #
    #
    #
    #
    def SingleValue(self):
        DisplayMessages.call("O", self.objectName + ".SingleValue", 5, "")
        text = ""
    
        if self.property in self.item.claims:
            DisplayMessages.debug("O", self.objectName + ".SingleValue", 8, "Value to add : " + self.value)
            DisplayMessages.debug("O", self.objectName + ".SingleValue", 8, self.property + " is in claims : " + \
                        str(len(self.item.claims[self.property])) + " value(s) "  )
            if self.value == "None":
                text = "already:" + ExtractItem(self.item.claims[self.property][0])
            elif len(self.item.claims[self.property]) >= 2:
                text = GetIdentAndLabel(self.value, 'Q')  + '" has already several values for property "' + \
                       GetIdentAndLabel(self.property, 'P') + '" that must have a single value'
            elif len(self.item.claims[self.property]) == 1:
                firstVal = ExtractItem(self.item.claims[self.property][0])
                DisplayMessages.debug("O", self.objectName + ".SingleValue", 8, str(firstVal))
                if not self.value == firstVal:
                    text = 'property "' + GetIdentAndLabel(self.property, 'P') + '" has already a value (' + GetLinkAndLabel(firstVal, 'Q') + ') that must be single'
        #
        if not text == "":
            self.violation.append(text)
            DisplayMessages.debug("O", self.objectName + ".SingleValue", 6, "violation : " + text )
    #
    #
    #
    #
    def Source(self):
        DisplayMessages.call("O", self.objectName + ".Source", 5, "")
        if not self.value == "None":
            if not self.isSource:
                text = '"' + GetIdentAndLabel(self.property, 'P') + '" must only be used as source property'
                self.violation.append(text)
                DisplayMessages.debug("O", self.objectName + ".Source", 6, "violation : " + text )
    #
    #
    #
    #
    def TargetRequiredClaim(self, Property, Values, Exceptions):
        DisplayMessages.call("O", self.objectName + ".TargetRequiredClaim", 5, "self.value=" + self.value + "Property=" + Property + ", Values=" + str(Values) + ", Exceptions=" + str(Exceptions))
        text = ""
        if not self.value == "None":
            Target = pywikibot.ItemPage(WikiData.GetRepositery(), self.value)
            try:
                Target.get()
            except NotImplementedError:
                DisplayMessages.warning("O", self.objectName + ".TargetRequiredClaim", "unable to read item " + self.value + " (NotImplementedError) : constraint violation not checked")
            else:
                if Property in Target.claims:
                    if not Values == []:
                        value = ExtractItem(Target.claims[Property][0])
                        if not value in Exceptions:
                            if not value in Values:
                                text = 'Target of "' + GetIdentAndLabel(self.property, 'P') + \
                                        '" has statement "' + GetIdentAndLabel(Property, 'P') + \
                                        '" with wrong value (' + value + ')'
                elif Property in self.claimTarget:
                    if not Values == []:
                        value = self.claimTarget[Property][0]
                        if not value in Exceptions:
                            if not value in Values:
                                text = 'Target of "' + GetIdentAndLabel(self.property, 'P') + \
                                        '" has statement "' + GetIdentAndLabel(Property, 'P') + \
                                        '" with wrong value (' + value + ')'
                else:
                    text = 'Target of "' + GetIdentAndLabel(self.property, 'P') + \
                            '" has no statement with "' + GetIdentAndLabel(Property, 'P') + '"'
        #
        if not text == "":
            self.violation.append(text)
            DisplayMessages.debug("O", self.objectName + ".TargetRequiredClaim", 6, "violation : " + text )
    #
    #
    #
    #
    def Type(self, ListClass, Relation):
        DisplayMessages.call("O", self.objectName + ".Type", 5, "ListClass=" + str(ListClass) + ", Relation=" + Relation)
        text = ""
        if not self.value == "None":
            Property = ''
            if Relation == "instance":
                Property = 'P31'
            elif Relation == "subclass":
                Property = 'P279'
            else:
                DisplayMessages.warning("O", self.objectName + ".Type", "bad value for parameter Relation " + Relation )
            
            PropPresent = False
            ValueOK = False
            if not Property == '':
                if Property in self.item.claims:
                    PropPresent = True
                    ListValue = self.item.claims[Property]
                    DisplayMessages.debug("O", self.objectName + ".Type", 9, "list value of " + Property + " from wikidata " + str(ListValue) )
                    for Value in ListValue:
                        if not ValueOK:
                            ValueOK = CheckSubClassOf(ExtractItem(Value), ListClass)

                if Property in self.claimsToAdd:
                    PropPresent = True
                    ListValue = self.claimsToAdd[Property]
                    DisplayMessages.debug("O", self.objectName + ".Type", 9, "list value of " + Property + " from claimsToAdd " + str(ListValue) )
                    for Value in ListValue:
                        if not ValueOK:
                            ValueOK = CheckSubClassOf(Value, ListClass)

                if not PropPresent:
                    text = 'item has no claim with property "' + GetIdentAndLabel(Property, 'P') + '"'
                else:
                    if not ValueOK:
                        text = Relation + ' of item is not a subclass of ' + str(ListClass)
        #
        if not text == "":
            self.violation.append(text)
            DisplayMessages.debug("O", self.objectName + ".SingleValue", 6, "violation : " + text )
    #
    #
    #
    #
    def ValueOnly(self):
        DisplayMessages.call("O", self.objectName + ".ValueOnly", 5, "")
        if not self.value == "None":
            
            if self.isQualifier:
                text = '"' + GetIdentAndLabel(Property, 'P') + '" is used as qualifier property'
                self.violation.append(text)
                DisplayMessages.debug("O", self.objectName + ".ValueOnly", 6, "violation : " + text )
                
            if self.isSource:
                text = '"' + GetIdentAndLabel(Property, 'P') + '" is used as source property'
                self.violation.append(text)
                DisplayMessages.debug("O", self.objectName + ".ValueOnly", 6, "violation : " + text )
    #
    #
    #
    #
    def ValueType(self, ListClass, Relation):
        DisplayMessages.call("O", self.objectName + ".ValueType", 5, "self.value=" + self.value + ", ListClass=" + str(ListClass) + ", Relation=" + Relation)
        text = ""
        if not self.value == "None":

            Property = ''
            if Relation == "instance":
                Property = 'P31'
            elif Relation == "subclass":
                Property = 'P279'
            else:
                DisplayMessages.warning("O", self.objectName + ".ValueType", "bad value for parameter Relation " + Relation )

            Target = pywikibot.ItemPage(WikiData.GetRepositery(), self.value)
            try:
                Target.get()
            except NotImplementedError:
                DisplayMessages.warning("O", self.objectName + ".ValueType", "unable to read item " + self.value + " (NotImplementedError) : constraint violation not checked")
            else:
                PropPresent = False
                ValueOK = False
                if not Property == '':
                    if Property in Target.claims:
                        PropPresent = True
                        ListValue = Target.claims[Property]
                        for Value in ListValue:
                            if not ValueOK:
                                ValueOK = CheckSubClassOf(ExtractItem(Value), ListClass)
            
                    if not PropPresent:
                        if not self.claimTarget == {}:
                            if Property in self.claimTarget:
                                PropPresent = True
                                ListValue = self.claimTarget[Property]
                                for Value in ListValue:
                                    if not ValueOK:
                                        ValueOK = CheckSubClassOf(Value, ListClass)
                
                    if not PropPresent:
                        text = 'Target item has no claim with property "' + GetIdentAndLabel(Property, 'P') + '"'
                    else:
                        if not ValueOK:
                            text = Relation + ' of Target item is not a subclass of [' 
                            deb = True
                            for Class in ListClass:
                                if not deb:
                                    text += ", "
                                text += '{{Q|' + Class + '}}'
                            text += ']'
        #
        if not text == "":
            self.violation.append(text)
            DisplayMessages.debug("O", self.objectName + ".SingleValue", 6, "violation : " + text )




    



def CheckConstraints(Item):
    DisplayMessages.call("U", "CheckConstraints", 5, "Item=" + repr(Item) )
    global ConstraintViolationInit
    global fctCheckConstraints
    
    if not ConstraintViolationInit:
        InitConstraintViolation()

    if Item.property in fctCheckConstraints:
        text = fctCheckConstraints[Item.property](Item)
    else:
        text = "No function to test constraint violation for property " + Item.property
        DisplayMessages.warning("U", "CheckConstraints", text)
    return text



def InitConstraintViolation():
    DisplayMessages.call("U", "InitConstraintViolation", 5, "" )
    global fctCheckConstraints
    fctCheckConstraints['P7']   = CheckConstraints_P7       # brother
    fctCheckConstraints['P9']   = CheckConstraints_P9       # sister
    fctCheckConstraints['P19']  = CheckConstraints_P19      # place of birth
    fctCheckConstraints['P20']  = CheckConstraints_P20      # place of death
    fctCheckConstraints['P21']  = CheckConstraints_P21      # sex or gender
    fctCheckConstraints['P22']  = CheckConstraints_P22      # father
    fctCheckConstraints['P25']  = CheckConstraints_P25      # mother
    fctCheckConstraints['P26']  = CheckConstraints_P26      # spouse
    fctCheckConstraints['P31']  = CheckConstraints_P31      # instance of
    fctCheckConstraints['P39']  = CheckConstraints_P39      # position held
    fctCheckConstraints['P40']  = CheckConstraints_P40      # child
    fctCheckConstraints['P53']  = CheckConstraints_P53      # noble family
    fctCheckConstraints['P94']  = CheckConstraints_P94      # coat of arms image
    fctCheckConstraints['P97']  = CheckConstraints_P97      # noble title
    fctCheckConstraints['P106'] = CheckConstraints_P106     # occupation
    fctCheckConstraints['P227'] = CheckConstraints_P227     # GND identifier
    fctCheckConstraints['P279'] = CheckConstraints_P279     # subclass of
    fctCheckConstraints['P361'] = CheckConstraints_P361     # part of
    fctCheckConstraints['P460'] = CheckConstraints_P460     # said to be the same as
    fctCheckConstraints['P527'] = CheckConstraints_P527     # has part
    fctCheckConstraints['P569'] = CheckConstraints_P569     # date of birth
    fctCheckConstraints['P570'] = CheckConstraints_P570     # date of death
    fctCheckConstraints['P571'] = CheckConstraints_P571     # inception (date of creation)
    fctCheckConstraints['P735'] = CheckConstraints_P735     # given name
    fctCheckConstraints['P1001'] = CheckConstraints_P1001   # jurisdiction
    
    #Qualifiers
    fctCheckConstraints['P155'] = CheckConstraints_P155     # followed by
    fctCheckConstraints['P156'] = CheckConstraints_P156     # follows
    fctCheckConstraints['P580'] = CheckConstraints_P580     # date of begin
    fctCheckConstraints['P582'] = CheckConstraints_P582     # date of end
    fctCheckConstraints['P642'] = CheckConstraints_P642     # within the scope of
    
    # Sources
    fctCheckConstraints['P143'] = CheckConstraints_P143     # imported from
    fctCheckConstraints['P248'] = CheckConstraints_P248     # affirmed by
    fctCheckConstraints['P304'] = CheckConstraints_P304     # page
    fctCheckConstraints['P478'] = CheckConstraints_P478     # tome
    fctCheckConstraints['P813'] = CheckConstraints_P813     # retrieved
    fctCheckConstraints['P854'] = CheckConstraints_P854     # url
    
    ConstraintViolationInit = True



    
# Property:P7   =  brother

def CheckConstraints_P7(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P7", 5, "item=" + Constraint.objectName)
    
    Constraint.ValueType          ( [ 'Q5', 'Q95074', 'Q21070568' ], 'instance' )
    Constraint.TargetRequiredClaim( 'P21', [ 'Q6581097', 'Q44148', 'Q2449503' ], [])
    Constraint.Type               ( [ 'Q5', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Item               ( 'P19' )
    Constraint.Item               ( 'P569' )
    Constraint.Qualifiers         ( [ 'P1039' ] )
    Constraint.ValueOnly          ( )





# Property:P9   =  sister

def CheckConstraints_P9(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P9", 5, "item=" + Constraint.objectName)
    
    Constraint.ValueType          ( [ 'Q5', 'Q95074', 'Q21070568' ], 'instance' )
    Constraint.TargetRequiredClaim( 'P21', [ 'Q6581072', 'Q43445', 'Q1052281' ], [])
    Constraint.Type               ( [ 'Q5', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Item               ( 'P19' )
    Constraint.Item               ( 'P569' )
    Constraint.Qualifiers         ( [ 'P1039' ] )
    Constraint.ValueOnly          ( )




# Property:P19  =  place of birth

def CheckConstraints_P19(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P19", 5, "item=" + Constraint.objectName)
    
    Constraint.SingleValue        ()
    Constraint.ConflictsWith      ( 'P31', ['Q1456832', 'Q4167410' ]) 
    Constraint.TargetRequiredClaim( 'P17', [], [])
    Constraint.TargetRequiredClaim( 'P131', [], [])
    Constraint.Qualifiers         ( [ 'P17', 'P131', 'P805', 'P1480' ] )
    Constraint.Type               ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Item               ( 'P569' )
    Constraint.ValueType          ( [ 'Q2221906' ], 'instance' )





# Property:P20  =  place of death

def CheckConstraints_P20(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P20", 5, "item=" + Constraint.objectName)
    
    Constraint.SingleValue        ()
    Constraint.Type               ( [ 'Q5', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Item               ( 'P19' )
    Constraint.Item               ( 'P569' )
    Constraint.Item               ( 'P570' )
    Constraint.Qualifiers         ( [ 'P17', 'P131', 'P805', 'P1480' ] )




# Property:P21  =  sex or gender

def CheckConstraints_P21(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P21", 5, "item=" + Constraint.objectName)
    
    Constraint.OneOf              ([ 'Q6581097', 'Q6581072', 'Q1097630', 'Q303479', 'Q189125', 'Q1052281', 'Q2449503', 'Q48270', 'Q1399232', 'Q3277905', 'Q746411', 'Q350374', 'Q660882', 'Q44148', 'Q43445', 'None' ])
    Constraint.SingleValue        ()
    Constraint.ConflictsWith      ( 'P31', ['Q14756018', 'Q2301325', 'Q3418125', 'Q1141470', 'Q31184', 'Q16334295', 'Q281643', 'Q10648343', 'Q164950', 'Q13417114', 'Q8436', 'Q15618652', 'Q16979650', 'Q874405' ]) 
    Constraint.ConflictsWith      ( 'P31', ['Q14073567', 'Q3046146' ]) 
    Constraint.ConflictsWith      ( 'P625') 
    Constraint.ConflictsWith      ( 'P17') 
    Constraint.ConflictsWith      ( 'P131') 
    Constraint.ConflictsWith      ( 'P31', ['Q4167836', 'Q13406463', 'Q4164871', 'Q8261', 'Q386724', 'Q1371849', 'Q273057', 'Q101352', 'Q132821', 'Q4' ]) 
    Constraint.ConflictsWith      ( 'P360') 
    Constraint.ConflictsWith      ( 'P364') 
    Constraint.ConflictsWith      ( 'P50') 
    Constraint.ConflictsWith      ( 'P31', ['Q4167410', 'Q15623926', 'Q202444', 'Q215380', 'Q571' ])
    Constraint.ConflictsWith      ( 'P279') 
    Constraint.ConflictsWith      ( 'P527') 
    Constraint.Qualifiers         ( [ 'P580', 'P582', 'P31' ] )




# Property:P22  =  father

def CheckConstraints_P22(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P22", 5, "item=" + Constraint.objectName)

    Constraint.ConflictsWith      ( 'P31', ['Q4167410', 'Q101352', 'Q101352', 'Q12308941', 'Q11879590', 'Q3409032', 'Q202444', 'Q577' ]) 
    Constraint.SingleValue        ()
    Constraint.TargetRequiredClaim( 'P21', [ 'Q6581097', 'Q44148', 'Q1052281' ], [])
    Constraint.Item               ( 'P21' )
    Constraint.ValueType          ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Type               ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Qualifiers         ( [ 'P1039' ] )
    Constraint.ValueOnly          ()
    Constraint.Item               ( 'P19' )
    Constraint.Item               ( 'P569' )





# Property:P25  =  mother

def CheckConstraints_P25(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P25", 5, "item=" + Constraint.objectName)

    Constraint.ConflictsWith      ( 'P31', ['Q4167410', 'Q101352', 'Q12308941', 'Q11879590', 'Q3409032', 'Q202444', 'Q577' ]) 
    Constraint.SingleValue        ()
    Constraint.TargetRequiredClaim( 'P21', [ 'Q6581072', 'Q43445' ], [])
    Constraint.Item               ( 'P21' )
    Constraint.ValueType          ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Type               ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Qualifiers         ( [ 'P1039' ] )
    Constraint.ValueOnly          ()
    Constraint.Item               ( 'P19' )
    Constraint.Item               ( 'P569' )





# Property:P26  =  spouse

def CheckConstraints_P26(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P26", 5, "item=" + Constraint.objectName)

    Constraint.ConflictsWith      ( 'P31', ['Q4167410', 'Q101352', 'Q12308941', 'Q11879590', 'Q3409032', 'Q202444', 'Q577' ])
    Constraint.Type               ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Qualifiers         ( [ 'P580', 'P582' ] )
    Constraint.ValueOnly          ()





# Property:P31  =  instance of

def CheckConstraints_P31(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P31", 5, "item=" + Constraint.objectName)

    Constraint.TargetRequiredClaim( 'P279', [], [ 'Q35120' ])
    Constraint.ConflictsWith      ( 'P31', ['Q8441', 'Q467', 'Q6581097', 'Q6581072' ]) 
    Constraint.Qualifiers         ( [ 'P460', 'P642', 'P580', 'P361', 'P582' ] )




# Property:P39  =  position held

def CheckConstraints_P39(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P39", 5, "item=" + Constraint.objectName)
    
    Constraint.Type               ( [ 'Q5', 'Q21070598', 'Q95074', 'Q146', 'Q144' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.ValueType          ( [ 'Q4164871' ], 'instance' )
    Constraint.Qualifiers         ( [ 'P580', 'P582', 'P1365', 'P1366', 'P642', 'P805', 'P768', 'P1545', 'P794', 'P748', 'P1734', 'P1534' ] )
    Constraint.TargetRequiredClaim( 'P1001', [], [])
    Constraint.TargetRequiredClaim( 'P571' , [], [])



# Property:P40  =  child

def CheckConstraints_P40(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P40", 5, "item=" + Constraint.objectName)
    
    Constraint.ConflictsWith      ( 'P31', ['Q4167410', 'Q101352', 'Q12308941', 'Q11879590', 'Q3409032', 'Q202444', 'Q577' ])
    Constraint.ValueType          ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.Type               ( [ 'Q215627', 'Q95074' ], 'instance' )
    Constraint.TargetRequiredClaim( 'P21', [], [])
    Constraint.Item               ( 'P21' )
    Constraint.Qualifiers         ( [ 'P1039' ] )
    Constraint.ValueOnly          ()





# Property:P53  =  noble family

def CheckConstraints_P53(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P53", 5, "item=" + Constraint.objectName)
    
    Constraint.Type               ( [ 'Q5', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.ValueType          ( [ 'Q13417114', 'Q19791817' ], 'instance' )
    Constraint.SingleValue        ()
    Constraint.Qualifiers         ( [ ] )






# Property:P94  =  coat of arms image

def CheckConstraints_P94(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P94", 5, "item=" + Constraint.objectName)
    #Not implement
    






# Property:P97  =  noble title

def CheckConstraints_P97(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P97", 5, "item=" + Constraint.objectName)
    
    Constraint.Type               ( [ 'Q5', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.ValueType          ( [ 'Q5737899' ], 'instance' )
    Constraint.Qualifiers         ( [ 'P580', 'P582', 'P156', 'P155', 'P1545' ] )





# Property:P106 =  occupation

def CheckConstraints_P106(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P106", 5, "item=" + Constraint.objectName)
    
    Constraint.ValueType          ( [ 'Q12737077', 'Q17305127' ], 'instance' )
    Constraint.Type               ( [ 'Q5', 'Q21070598', 'Q95074' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Qualifiers         ( [ 'P101', 'P580', 'P582', 'P642', 'P1365', 'P1366' ] )





# Property:P227 =  identifiant GND

def CheckConstraints_P227(Constraint):
    # ====> Not implement
    DisplayMessages.call("U", "CheckConstraints_P227", 5, "item=" + Constraint.objectName)



    
# Property:P279 =  subclass of

def CheckConstraints_P279(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P279", 5, "item=" + Constraint.objectName)
    Constraint.TargetRequiredClaim( 'P279', [], [ 'Q35120' ] )






# Property:P361 =  part of

def CheckConstraints_P361(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P361", 5, "item=" + Constraint.objectName)
    Constraint.ConflictsWith      ( 'P31', ['Q191067', 'Q3330467', 'Q18610453' ])




# Property:P460 =  said to be the same as
    
def CheckConstraints_P460(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P460", 5, "item=" + Constraint.objectName)
    Constraint.Qualifiers         ( [ ] )





# Property:P527 =  has part

def CheckConstraints_P527(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P527", 5, "item=" + Constraint.objectName)





# Property:P569 =  date of birth

def CheckConstraints_P569(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P569", 5, "item=" + Constraint.objectName)
    
    Constraint.ConflictsWith      ( 'P527') 
    Constraint.ConflictsWith      ( 'P31', ['Q14756018', 'Q14073567', 'Q4167410' ])
    Constraint.ConflictsWith      ( 'P625') 
    Constraint.SingleValue        ()
    Constraint.Type               ( [ 'Q5', 'Q95074', 'Q144', 'Q726', 'Q146', 'Q33602', 'Q36611', 'Q80174', 'Q7378', 'Q4126704', 'Q33609', 'Q26843', 'Q193327', 'Q19537', 'Q36341', 'Q133006', 'Q41050', 'Q237260', 'Q34505' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Item               ( 'P19' )
    Constraint.Qualifiers         ( [ 'P459', 'P805', 'P1319', 'P1326', 'P1480' ] )





# Property:P570 =  date of death

def CheckConstraints_P570(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P570", 5, "item=" + Constraint.objectName)
    
    Constraint.DiffWithinRange    ('P569', 0, 150)
    Constraint.SingleValue        ()
    Constraint.Type               ( [ 'Q5', 'Q95074', 'Q144', 'Q726', 'Q146', 'Q33602', 'Q36611', 'Q80174', 'Q7378', 'Q4126704', 'Q33609', 'Q26843', 'Q193327', 'Q19537', 'Q36341', 'Q133006', 'Q41050', 'Q237260', 'Q34505' ], 'instance' )
    Constraint.Item               ( 'P21' )
    Constraint.Item               ( 'P19' )
    Constraint.Item               ( 'P569' )
    Constraint.Item               ( 'P20' )
    Constraint.Qualifiers         ( [ 'P459', 'P805', 'P1319', 'P1326', 'P1480', 'P2241' ] )






# Property:P571 =  inception (date of creation)

def CheckConstraints_P571(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P571", 5, "item=" + Constraint.objectName)

    Constraint.SingleValue()
    






# Property:P735 =  given name

def CheckConstraints_P735(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P735", 5, "item=" + Constraint.objectName)
    
    Constraint.ConflictsWith      ( 'P31', [ 'Q4167836', 'Q101352', 'Q12308941', 'Q11879590', 'Q3409032', 'Q202444', 'Q43229', 'Q1092563', 'Q157031', 'Q14756018', 'Q2301325', 'Q1141470', 'Q31184', 'Q281643', 'Q16979650', 'Q36279', '3331189', 'Q11424', 'Q571', 'Q17329259' ]) 
    Constraint.ConflictsWith      ( 'P31', [ 'Q14073567', 'Q3046146', 'Q16334295', 'Q10648343' ]) 
    Constraint.ConflictsWith      ( 'P1560') 
    Constraint.ConflictsWith      ( 'P1533') 
    Constraint.ConflictsWith      ( 'P921')
    Constraint.Type               ( [ 'Q18336849' ], 'instance' )
    Constraint.ValueType          ( [ 'Q202444' ], 'instance' )
    #Constraint.TargetRequiredClaim( 'P31', [ 'Q12308941', 'Q11879590', 'Q202444', 'Q3409032' ], [] )
    #Constraint.Type               ( [ 'Q5', 'Q95074', 'Q61002', 'Q127843', 'Q16934977' ], 'instance' )
    Constraint.Item               ( 'P31' , ['Q5', 'Q15632617', 'Q95074', 'Q61002', 'Q15410431', 'Q15298259', 'Q15966903', 'Q12405827', 'Q20643955', 'Q13127686'])
    Constraint.Qualifiers         ( [ 'P138', 'P31', 'P407', 'P580', 'P582', 'P518', ' P585', ' P1545', ' P794', 'P1449' ] )
    Constraint.ValueOnly          ()
    Constraint.ConflictsWith      ( 'P17') 
    Constraint.ConflictsWith      ( 'P131') 
    Constraint.ConflictsWith      ( 'P625') 
    Constraint.ConflictsWith      ( 'P31', [ 'Q19389637' ]) 
    Constraint.Type               ( [ 'Q215627' ], 'instance' )






# Property:P1001 =  jurisdiction

def CheckConstraints_P1001(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P1001", 5, "item=" + Constraint.objectName)

    Constraint.Type( ['Q4164871' ,'Q40231' ,'Q7748' ,'Q41487' ,'Q23691' ,'Q7188'], 'instance')
    Constraint.ValueType          ( [ 'Q56061' ], 'instance')
    Constraint.TargetRequiredClaim( 'P17', [], [])






# Property:P155 =  followed by

def CheckConstraints_P155(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P155", 5, "item=" + Constraint.objectName)
    Constraint.Qualifier()




# Property:P156 =  follows

def CheckConstraints_P156(Constraint):
    # ====> Not implement
    DisplayMessages.call("U", "CheckConstraints_P156", 5, "Not implement")
    return ""




# Property:P580 =  date of begin

def CheckConstraints_P580(Constraint):
    # ====> Not implement
    DisplayMessages.call("U", "CheckConstraints_P580", 5, "Not implement")




# Property:P582 =  date of end

def CheckConstraints_P582(Constraint):
    # ====> Not implement
    DisplayMessages.call("U", "CheckConstraints_P582", 5, "Not implement")




# Property:P642 =  within the scope of

def CheckConstraints_P642(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P642", 5, "Not implement")
    Constraint.Qualifier()




# Property:P143 =  imported from

def CheckConstraints_P143(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P143", 5, "item=" + Constraint.objectName)
    Constraint.Source()




# Property:P248 =  affirmed by

def CheckConstraints_P248(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P248", 5, "item=" + Constraint.objectName)
    Constraint.Source             ()
    Constraint.ValueType          ( [ 'Q11028' ], 'instance' )





# Property:P304 =  page

def CheckConstraints_P304(Constraint):
    # ====> Not implement
    DisplayMessages.call("U", "CheckConstraints_P304", 5, "Not implement")
    



# Property:P478 =  tome
   
def CheckConstraints_P478(Constraint):
    # ====> Not implement
    DisplayMessages.call("U", "CheckConstraints_P478", 5, "Not implement")
    




# Property:P813 =  retrieved

def CheckConstraints_P813(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P813", 5, "item=" + Constraint.objectName)
    #{{Constraint:Single value}}
    #{{Constraint:Range|min=2001|max=now}}
    Constraint.Source             ()





# Property:P854 =  url

def CheckConstraints_P854(Constraint):
    DisplayMessages.call("U", "CheckConstraints_P813", 5, "item=" + Constraint.objectName)
    Constraint.Source             ()










def CheckSubClassOf(Value, ListClass, Deepth = 20):
    DisplayMessages.call("U", "CheckSubClassOf", 5, "Value=" + Value + ", ListClass=" + str(ListClass) + ", Deepth=" + str(Deepth) )
    if Value in ListClass:
        return True
    if Deepth == 0 or Value == 'None':
        return False
    item = pywikibot.ItemPage(WikiData.GetRepositery(), Value)
    try:
        item.get()
    except NotImplementedError:
        DisplayMessages.warning("U", "CheckSubClassOf", "unable to read item " + Value + " (NotImplementedError) : constraint violation not checked")
        return True
    else:
        if item.claims:
            if 'P279' in item.claims.keys():
                ListValue = item.claims['P279']
                for Value in ListValue:
                    if CheckSubClassOf(ExtractItem(Value), ListClass, Deepth-1):
                        return True
    return False



def TextCorrect(text):
    if len(text) >= 3:
        if text[:3] == " ; ":
            text = text[3:]
    return text
            









def testCheckConstraints(NumTest, pIdent, pProperty, pValue, pQualifiers, claimsToAdd):
    print( "" )
    print( "---------------------------" )
    text = "test " + str(NumTest) + ":\n"
    text += "CheckConstraints(pIdent=" + pIdent + ", property=" + pProperty + ", value=" + pValue
    text += ", claimsToAdd=" + str(claimsToAdd)
    text += ")"
    print( text )
    print( "" )
    item = pywikibot.ItemPage(WikiData.GetRepositery(), pIdent)
    item.get()
    Item = ConstraintViolation(pIdent, pProperty, pValue, pQualifiers, item, claimsToAdd, {}, False, False)
    print( '"' + str(Item) + '"' )




def mainTestCheckConstraints(*args):
    param = CallParameter(*args)
    NumCase = param.GetValue('case', 0)
    DisplayMessages.SetFileName("")
    
    if NumCase == 0 or NumCase == 1 :
        testCheckConstraints(1, 'Q729206', 'P31', 'Q5', [], {})
    if NumCase == 0 or NumCase == 2 :
        testCheckConstraints(2, 'Q729206', 'P31', 'Q102310', [], {})
    if NumCase == 0 or NumCase == 3 :
        testCheckConstraints(3, 'Q729206', 'P21', 'Q6581097', [], {})
    if NumCase == 0 or NumCase == 4 :
        testCheckConstraints(4, 'Q729206', 'P21', 'Q6581072', [], {})
    if NumCase == 0 or NumCase == 5 :
        testCheckConstraints(5, 'Q729206', 'P21', 'Q14756018', [], {})
    if NumCase == 0 or NumCase == 6 :
        testCheckConstraints(6, 'Q2197', 'P21', 'Q6581097', [], {})
    if NumCase == 0 or NumCase == 7 :
        testCheckConstraints(7, 'Q188044', 'P19', 'Q220437', [], {})
    if NumCase == 0 or NumCase == 8 :
        testCheckConstraints(8, 'Q729206', 'P19', 'None', [], {})
    DisplayMessages.End()




if __name__ == "__main__":
    mainTestCheckConstraints()