#!/usr/bin/env python # -*- coding: utf-8 -*- """ Knowledge Closure -- a Linked Data checker by kennyluck """ import sys sys.path.append("/afs/csail.mit.edu/u/k/kennyluck/public/cwm-1.2.1") import os #@@ change this to suitable folder if os.environ['HTTP_HOST'] == 'dig.csail.mit.edu': WRITABLE_DIRECTORY = "/afs/csail.mit.edu/group/dig/www/data/2008/webdav/Knowledge_Closure/" else: WRITABLE_DIRECTORY = "" from swap.myStore import symbol, Namespace from swap import uripath RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") import swap import cgi #import cgitb import datetime sys.stderr = open(WRITABLE_DIRECTORY + "log.txt", 'w') #Very stupid of I, cgitb!!! #sys.excepthook = cgitb.Hook(file=sys.stderr) def DEBUG(string): swap.diag.progress(string + "\n") from util import print_str, print_result, send_initial_document,\ isNeglectable, setExaminedThing, resource,\ LOADING, finish, FOAF, load_local, LINK, OWL from core import visible_formula from labeler import label, NoLabelError from xml.sax.saxutils import escape #XML escape template_stack = open("check_template.xhtml").read().split("%s") examined_URI = cgi.FieldStorage()['uri'].value examined_thing = symbol(examined_URI) setExaminedThing(examined_thing) examined_document = examined_thing.document() template_stack.reverse() # HTML Header print_str(template_stack.pop()) print_str(examined_URI) print_str(template_stack.pop()) # Basic Tests from kc_test import Check_Info_Test, Browsable_Test #build_visible_formula_for(examined_thing) # check_info(examined_thing) #Test_0 initial_test = Check_Info_Test(examined_thing) initial_test.run() #It's wrong to do muilti-threading here, so run() not startup() del initial_test try: print_str(escape(label(examined_thing))) except NoLabelError: print_str(escape("<"+ examined_thing.uriref() +">")) print_str(template_stack.pop()) types = visible_formula[examined_thing].each(examined_thing, RDF.type) load_local("decor/README.n3") #for the logos of the types implied_document = False # if not types: if hasattr(examined_thing, '_semantics'): #@@ not working correctly types.append(LINK.Document) #(redirection issue) implied_document = True #print_str("

Warning: The thing does not have a type

") logo, type = None, None for type in types: logo = sys.kb.any(type, FOAF.logo, None) if logo: break logo = logo or sys.kb.any(OWL.Thing, FOAF.logo, None) #get the unknown icon #get the content negotiable uri general_resource = os.path.splitext(logo.uriref())[0] print_str(uripath.refTo(uripath.base(), general_resource)) print_str(template_stack.pop()) if implied_document: # (implied) Document print_str("(implied) ") try: if not type: print_str("(unknown)") else: print_str(escape(label(type) + " <" + type.uriref() + "> ")) except NoLabelError: print_str(escape("<" + type.uriref() + "> ")) del logo, type #print_str("

Type: " + ", ".join(map(label, types)) + "

\n ") print_str(template_stack.pop()) ## Examine Basic Connection #start from the exmained_document's semantics not the knowledge base #but from the thing from swap.RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 former, later = None, None for statement in visible_formula[examined_thing].\ statementsMatching(subj=examined_thing): the_thing = statement[OBJ] if isinstance(the_thing, resource) and not isNeglectable(the_thing): former = Check_Info_Test(the_thing) later = Browsable_Test(statement, OBJ) #@It is possible that the_thing == examined_thing (not usual) former.addNextTest(later) for statement in visible_formula[examined_thing].\ statementsMatching(obj=examined_thing): the_thing = statement[SUBJ] if isinstance(the_thing, resource) and not isNeglectable(the_thing): former = Check_Info_Test(the_thing) later = Browsable_Test(statement, SUBJ) former.addNextTest(later) del former, later #remove these Thread instance binding #These are the preparations for the tests, for detailed implementation, #see for test in Check_Info_Test.examples: print_result("Looking up <%s>" % test.thing_to_test.uriref(), test, LOADING) # Browsable Graph Tests print_str(template_stack.pop()) for test in Browsable_Test.examples: s = test.statement_to_test print_result("Checking triple <%s> <%s> <%s>" % (s[SUBJ].uriref(), s[PRED].uriref(), s[OBJ].uriref()), test, LOADING) #General Review ....finally from util import load_local, n3String from kc_test import General_Review from swap.RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 from swap.why import Because from conflict import display_rule load_local("assumption.n3") # the assumptions thinking = load_local("thinking.n3", True) for st in thinking.statementsMatching(pred=sys.kb.store.implies): General_Review(st.asFormula(why=Because("registering custom rules"))) print_str(template_stack.pop()) for test in General_Review.examples: print_result("Thinking over", test, LOADING, display_rule(test.rule)) send_initial_document(template_stack[0]) DEBUG("[%s] The initial page is sent out" % str(datetime.datetime.now())) #<> #fire all the intial tests for test in Check_Info_Test.examples: test.start() for test in General_Review.examples: DEBUG("[%s] Starting thinking thread" % str(datetime.datetime.now())) test.start() #wait for the results of all the tests, and quit the program for test in Check_Info_Test.examples: test.join() for test in Browsable_Test.examples: test.join() for test in General_Review.examples: test.join() #Finish this today finish() sys.exit(0)