Exemplo n.º 1
0
def tptp(filename, m, options=[]):

    m = ClifModuleSet(filename)
    if '-cumulate' in options:
        # translate into a single tptp single_file
        single_file = m.get_single_tptp_file()
        print ""
        print "+++++++++++++++++++++"
        print "Files created:"
        print ""
        print single_file
        print "+++++++++++++++++++++"
    elif '-module' in options:
        single_file = m.get_top_module().get_tptp_file_name()
        print ""
        print "+++++++++++++++++++++"
        print "Files created:"
        print ""
        print single_file
        print "+++++++++++++++++++++"
        
    else:
        files = m.get_tptp_files()
        print ""
        print "+++++++++++++++++++++"
        print "Files created:"
        print ""
        for single_file in files:
            print single_file
        print "+++++++++++++++++++++"    
Exemplo n.º 2
0
def tptp(filename, m, options=[]):

    m = ClifModuleSet(filename)
    if '-cumulate' in options:
        # translate into a single tptp single_file
        single_file = m.get_single_tptp_file()
        print("")
        print("+++++++++++++++++++++")
        print("Files created:")
        print("")
        print(single_file)
        print("+++++++++++++++++++++")
    elif '-module' in options:
        single_file = m.get_top_module().get_tptp_file_name()
        print("")
        print("+++++++++++++++++++++")
        print("Files created:")
        print("")
        print(single_file)
        print("+++++++++++++++++++++")

    else:
        files = m.get_tptp_files()
        print("")
        print("+++++++++++++++++++++")
        print("Files created:")
        print("")
        for single_file in files:
            print(single_file)
        print("+++++++++++++++++++++")    
Exemplo n.º 3
0
def consistent(filename, options=[]):  
    m = ClifModuleSet(filename)
     
    if '-module' in options:
        results = m.run_consistency_check_by_subset(abort=True, abort_signal=ClifModuleSet.CONSISTENT)
    elif '-depth' in options:
        results = m.run_consistency_check_by_depth(abort=True, abort_signal=ClifModuleSet.CONSISTENT)
    elif '-simple' in options:
        results = m.run_simple_consistency_check()
    else:
        results = m.run_full_consistency_check(abort=True, abort_signal=ClifModuleSet.CONSISTENT)
        
    if len(results)==0:
        logging.getLogger(__name__).info("+++ CONSISTENCY CHECK TERMINATED: NO MODULES FOUND IN " +str(m.get_imports()) +"\n")
    else:
        for (r, value, _) in results:
            if value==-1:
                logging.getLogger(__name__).info("+++ CONSISTENCY CHECK TERMINATED: INCONSISTENCY FOUND IN " +str(r) +"\n")
                return (False, m)
        result_sets = [r[0] for r in results]
        result_sets.sort(lambda x,y: cmp(len(x), len(y)))
#        print result_sets[0]
#        print results
#        print "+++++" + str(value)
        if results[0][1]==1:
            logging.getLogger(__name__).info("+++ CONSISTENCY CHECK TERMINATED: PROVED CONSISTENCY OF " +str(result_sets[0]) +"\n")
            return (True, m)
        else:
            logging.getLogger(__name__).info("+++ CONSISTENCY CHECK TERMINATED: NO RESULT FOR CONSISTENCY OF " +str(result_sets[0]) +"\n")
            if len(result_sets)>1:
                for (r, value, _) in results:
                    if value==1:
                        logging.getLogger(__name__).info("+++ CONSISTENCY CHECK TERMINATED: PROVED CONSISTENCY OF SUBONTOLOGY " +str(r[0]) +"\n")
            return (None, m)
Exemplo n.º 4
0
def tptp_all(folder, options=[]):
    for directory, subdirs, files in os.walk(folder):
        if any(ignore in directory for ignore in ignores):
            pass
        else:
            for single_file in files:
                if single_file.endswith(ending):
                    filename = os.path.join(directory, single_file)
                    print filename
                    m = ClifModuleSet(filename)
                    clif_to_tptp.tptp(filename, m, options)
def consistent_all(folder, options=[]):
    good = 0
    bad = 0
    neutral = 0
    good_files = []
    bad_files = []
    neutral_files = []
    for directory, subdirs, files in os.walk(folder):
        if any(ignore in directory for ignore in ignores):
            pass
        else:
            for single_file in files:
                if single_file.endswith(ending):
                    filename = os.path.join(directory, single_file)
                    print filename
                    m = ClifModuleSet(filename)
                    (result,
                     _) = check_consistency.consistent(filename, m, options)
                    if result is True:
                        good += 1
                        good_files.append(filename)
                    elif result is False:
                        bad += 1
                        bad_files.append(filename)
                    else:
                        neutral += 1
                        neutral_files.append(filename)
    print "\n------------ CONSISTENCY CHECK (ALL) RESULTS ------------\n"
    logging.getLogger(__name__).info(
        "------------ CONSISTENCY CHECK (ALL) RESULTS ------------\n")
    print str(good + bad + neutral) + " files in total"
    logging.getLogger(__name__).info(
        str(good + bad + neutral) + " files in total \n")
    print str(good) + " consistent theories:"
    logging.getLogger(__name__).info(str(good) + " consistent:\n")
    for line in good_files:
        logging.getLogger(__name__).info(" - " + line)
        print " - " + line
    print str(neutral) + " unknown theories: "
    logging.getLogger(__name__).info(str(neutral) + " unknown:\n")
    for line in neutral_files:
        logging.getLogger(__name__).info(" - " + line)
        print " - " + line
    print str(bad) + " inconsistent theories:"
    logging.getLogger(__name__).info(str(bad) + " inconsistent: \n")
    for line in bad_files:
        logging.getLogger(__name__).info(" - " + line)
        print " - " + line
Exemplo n.º 6
0
        print results
        print "+++++" + str(value)
        if results[0][1] == 1:
            logging.getLogger(__name__).info(
                "+++ CONSISTENCY CHECK TERMINATED: PROVED CONSISTENCY OF " +
                str(result_sets[0]) + "\n")
            return (True, m)
        else:
            logging.getLogger(__name__).info(
                "+++ CONSISTENCY CHECK TERMINATED: NO RESULT FOR CONSISTENCY OF "
                + str(result_sets[0]) + "\n")
            if len(result_sets) > 1:
                for (r, value, _) in results:
                    if value == 1:
                        logging.getLogger(__name__).info(
                            "+++ CONSISTENCY CHECK TERMINATED: PROVED CONSISTENCY OF SUBONTOLOGY "
                            + str(r[0]) + "\n")
    return (None, m)


if __name__ == '__main__':
    licence.print_terms()
    # global variables

    options = sys.argv
    options.reverse()
    options.pop()
    filename = options.pop()
    m = ClifModuleSet(filename)
    derp, clif = consistent(filename, m, options)
Exemplo n.º 7
0
def prove(lemmas_filename, summary_file, axioms_filename=None, options=[]):

    if axioms_filename is None:
        m = ClifModuleSet(lemmas_filename)
        #print "REMOVING " + m.get_top_module().module_name
        m.remove_module(m.get_top_module())
    else:
        m = ClifModuleSet(axioms_filename)

    lemmas = ClifLemmaSet(lemmas_filename)

    lemma_modules = lemmas.get_lemmas()

    for l in lemma_modules:
        logging.getLogger(__name__).debug("LEMMA MODULE: " + l.module_name +
                                          " TPTP_SENTENCE " + l.tptp_sentence)

    for l in lemma_modules:
        m.add_lemma_module(l)

        #print str(m.get_imports())
        l.output = ClifModuleSet.UNKNOWN

        if '-module' in options:
            results = m.run_consistency_check_by_subset(
                abort_signal=ClifModuleSet.PROOF, increasing=True)
            for (i, r) in results.iteritems():
                if r == ClifModuleSet.PROOF:
                    l.output = ClifModuleSet.PROOF
                    logging.getLogger(__name__).info("+++ LEMMA PROVED " +
                                                     l.module_name +
                                                     "from AXIOMS: " + str(i) +
                                                     "\n")
            if l.output != ClifModuleSet.PROOF:
                l.output = run_simple_check(m)

        elif '-depth' in options:
            results = m.run_consistency_check_by_depth(
                abort_signal=ClifModuleSet.PROOF, increasing=True)
            for (i, r) in results.iteritems():
                if r == ClifModuleSet.PROOF:
                    l.output = ClifModuleSet.PROOF
                    logging.getLogger(__name__).info("+++ LEMMA PROVED " +
                                                     l.module_name +
                                                     "from AXIOMS: " + str(i) +
                                                     "\n")
            if l.output != ClifModuleSet.PROOF:
                l.output = run_simple_check(m)

        elif '-simple' in options:
            l.output = run_simple_check(m)
        else:
            results = m.run_full_consistency_check()
            for (i, r) in results.iteritems():
                if len(results) == 1 and r == ClifModuleSet.COUNTEREXAMPLE:
                    l.output = ClifModuleSet.COUNTEREXAMPLE
                    logging.getLogger(__name__).info(
                        "+++ SENTENCE REFUTED " +
                        m.get_lemma_module().module_name + " in AXIOMS: " +
                        str(m.get_axioms()) + "\n")
                if r == ClifModuleSet.PROOF:
                    l.output = ClifModuleSet.PROOF
                    logging.getLogger(__name__).info("+++ LEMMA PROVED " +
                                                     l.module_name +
                                                     "from AXIOMS: " + str(i) +
                                                     "\n")
            if l.output != ClifModuleSet.PROOF:
                l.output = ClifModuleSet.UNKNOWN
                logging.getLogger(
                    __name__).info("+++ SENTENCE NEITHER PROVED NOR REFUTED " +
                                   m.get_lemma_module().module_name +
                                   " in AXIOMS: " + str(m.get_axioms()) + "\n")

    proofs = 0
    counterexamples = 0
    unknown = 0

    # write results to summary single_file
    single_file = open(summary_file, "a")
    for l in lemma_modules:
        if l.output == ClifModuleSet.PROOF: proofs += 1
        elif l.output == ClifModuleSet.COUNTEREXAMPLE: counterexamples += 1
        else: unknown += 1
        single_file.write(str(l.output) + " " + l.module_name + "\n")
    single_file.flush()
    single_file.close()

    return (proofs, counterexamples, unknown)
def nontrivially_consistent(filename, m, options=[]):
	(consistent, m) = check_consistency.consistent(filename, m, options)
	
	if consistent==None or consistent==True:  # no need to check nontrivial consistency if it is not consistent at all      
		#m = ClifModuleSet(filename)
		definitional_modules = []
		if "-simple" in options:
			i = m.get_top_module()
			if "-defs" not in options or i.is_simple_definition():
				definitional_modules.append(i)
		else:
			for i in m.get_imports():
				if "-defs" not in options or i.is_simple_definition():
					definitional_modules.append(i)
		
		weak = "strong"
		defs = ""
		if "-weak" in options:
			weak = "weak"
		if "-defs" in options:
			defs = "definitional "
		print "\n+++++++++++++++++++++\nProving "+weak +" nontrivial consistency for all " + str(len(definitional_modules))  + " " + defs + "modules of "+ m.get_module_name() +":\n+++++++++++++++++++++"
		for n in definitional_modules:
			print n.module_name
		print "+++++++++++++++++++++\n"
		
		if len(definitional_modules)==0:
			print "NO DEFINITIONS FOUND TO CHECK NONTRIVIAL CONSISTENCY FOR."
		
		for i in definitional_modules:
			if "-defs" not in options or i.is_simple_definition():
				if "-defs" in options:
					if "-all" in options:
						defined_symbols = m.get_defined_nonlogical_symbols()
					else:
						defined_symbols = i.get_defined_symbols()
				else: # not just definitions
					if "-all" in options:
						defined_symbols = m.get_nonlogical_symbols()
					else:
						defined_symbols = i.get_nonlogical_symbols()

				symbol_string = ""
				for (symbol, arity) in defined_symbols:
					symbol_string += symbol + '('+ str(arity) + ') '

				print "\n+++++++++++++++++++++\nProving "+weak +" nontrivial consistency of nonlogical symbols " + symbol_string + " in module " + i.module_name + "\n+++++++++++++++++++++\n"
				
				#for (symbol, arity) in defined_symbols:
					#print "Symbol " + str(symbol) + " has arity " + str(arity)
				
				# need to create new CL file that imports the definition module and adds a sentence stating that n distinct elements in this relation exist

				module_name_modifier = "" 
				if "-all" in options:
					module_name_modifier += "_all"
				if "-weak" in options:
					module_name_modifier += "_weak"
				(module_name, path) = filemgt.get_path_with_ending_for_nontrivial_consistency_checks(i.module_name+module_name_modifier)

				now = datetime.datetime.now()
				
				clif_file = open(path, 'w')
				clif_file.write("/** AUTOMATICALLY CREATED BY MACLEOD ON " + now.strftime("%a %b %d %H:%M:%S %Y")+'**/\n\n')
				clif_file.write('(' + clif.CLIF_TEXT + ' ' + module_name + '\n\n')
				clif_file.write('(' + clif.CLIF_IMPORT + ' ' + i.module_name + filemgt.read_config('cl','ending') + ')\n\n')
				
				# constructing a sentence of the form:
				# (exists (x1 x2 ...)
				#    (and
				#        (SYMBOL x1 x2 ...)
				#        (not (= x1 x2))
				#        (not (= ...
				#  )  )
				#
				# The assumption here is that there must exist a possibility that all the participating elements are distinct. 
				# If this weren't the case, the n-ary predicate could be reduced to a (n-1)-ary predicate.  This may be overly simplistic, but works for most of the case.
				# In particular, it fails if a binary relation is strictly reflexive, i.e. holds only for individual elements.
				# For predicates with n>2 this should probably be relaxed to:
				# every pairwise position of elements can be distinct.   
				for (symbol, arity) in defined_symbols:
					if arity>0:
						if "-weak" in options: # weak nontrivial consistency: each entity is independent from all others
							for n in range(arity):                            
								clif_file.write(construct_existential_sentence(symbol, arity, negation=False, all_distinct=False, position=n) + '\n\n')
								clif_file.write(construct_existential_sentence(symbol, arity, negation=True, all_distinct=False, position=n) + '\n\n')
								
						else: # strong nontrivial consistency: all participating entities have to be disjoint
							clif_file.write(construct_existential_sentence(symbol, arity, negation=False, all_distinct=True) + '\n\n')
							clif_file.write(construct_existential_sentence(symbol, arity, negation=True, all_distinct=True) + '\n\n')

				clif_file.write(')\n') # closing "cl-module"
					
				clif_file.close()
				
				m2 = ClifModuleSet(path)
				check_consistency.consistent(path, m2, options=options)            
Exemplo n.º 9
0
def prove (lemmas_filename, summary_file, axioms_filename=None, options=[]):
        
    if axioms_filename is None:
        m = ClifModuleSet(lemmas_filename)
        #print "REMOVING " + m.get_top_module().module_name
        m.remove_module(m.get_top_module())
    else:
        m = ClifModuleSet(axioms_filename)
        
    lemmas = ClifLemmaSet(lemmas_filename)

    lemma_modules = lemmas.get_lemmas()
    
    for l in lemma_modules:
        logging.getLogger(__name__).debug("LEMMA MODULE: " + l.module_name + " TPTP_SENTENCE " + l.tptp_sentence)

    for l in lemma_modules:
        m.add_lemma_module(l)
        
        #print str(m.get_imports())
        l.output = ClifModuleSet.UNKNOWN
        
        if '-module' in options:
            results = m.run_consistency_check_by_subset(abort_signal = ClifModuleSet.PROOF, increasing=True)
            for (i, r) in results.iteritems():
                if r==ClifModuleSet.PROOF:
                    l.output = ClifModuleSet.PROOF
                    logging.getLogger(__name__).info("+++ LEMMA PROVED " +l.module_name + "from AXIOMS: " + str(i)  +"\n")
            if l.output != ClifModuleSet.PROOF:
                l.output = run_simple_check(m)
                
        elif '-depth' in options:
            results = m.run_consistency_check_by_depth(abort_signal = ClifModuleSet.PROOF, increasing=True)
            for (i, r) in results.iteritems():
                if r==ClifModuleSet.PROOF:
                    l.output = ClifModuleSet.PROOF
                    logging.getLogger(__name__).info("+++ LEMMA PROVED " +l.module_name + "from AXIOMS: " + str(i)  +"\n")
            if l.output != ClifModuleSet.PROOF:
                l.output = run_simple_check(m)
        
        elif '-simple' in options:
            l.output = run_simple_check(m)
        else:
            results = m.run_full_consistency_check()
            for (i, r) in results.iteritems():
                if len(results)==1 and r==ClifModuleSet.COUNTEREXAMPLE:
                    l.output = ClifModuleSet.COUNTEREXAMPLE
                    logging.getLogger(__name__).info("+++ SENTENCE REFUTED " +m.get_lemma_module().module_name+ " in AXIOMS: " + str(m.get_axioms())  +"\n")
                if r==ClifModuleSet.PROOF:
                    l.output = ClifModuleSet.PROOF
                    logging.getLogger(__name__).info("+++ LEMMA PROVED " +l.module_name + "from AXIOMS: " + str(i)  +"\n")
            if l.output != ClifModuleSet.PROOF:
                l.output = ClifModuleSet.UNKNOWN
                logging.getLogger(__name__).info("+++ SENTENCE NEITHER PROVED NOR REFUTED " +m.get_lemma_module().module_name + " in AXIOMS: " + str(m.get_axioms())  +"\n")

    proofs = 0
    counterexamples = 0
    unknown = 0
    
    # write results to summary single_file
    single_file = open(summary_file, "a")
    for l in lemma_modules:
        if l.output == ClifModuleSet.PROOF: proofs += 1
        elif l.output == ClifModuleSet.COUNTEREXAMPLE: counterexamples += 1
        else: unknown += 1
        single_file.write(str(l.output) + " " + l.module_name + "\n")
    single_file.flush()
    single_file.close()
    
    return (proofs, counterexamples, unknown)