Esempio n. 1
0
 def evaluateObject(self,   subj_py):
     if verbosity() > 80: progress("strTime:parse input:"+`subj_py`)
     value, format = subj_py
     try:
         return  str(calendar.timegm(time.strptime(value, format)))
     except:
         return None
Esempio n. 2
0
 def resetRenames(reset = True):
     if reset:
         if diag.chatty_flag > 20:
             progress("Resetting all renamed vars maps ---------------------------------")
         Formula._renameVarsMaps.append({})
     else:
         Formula._renameVarsMaps.pop()
Esempio n. 3
0
def webget(addr, referer=None, types=[]):
    """Open a URI for reading; return a file-like object with .headers
    cf http://www.w3.org/TR/2004/REC-webarch-20041215/#dereference-uri
    """

    if diag.chatty_flag > 7: progress("Accessing: " + addr)
    if sandBoxed():
        if addr[:5] == 'file:':
            raise SecurityError('local file access prohibited')


#    addr = cacheHack(addr)

# work around python stdlib bugs with data: URIs
# buggy in 2.4.2 with CStringIO
    if addr[:5] == 'data:':
        # return open_data(addr)
        return urllib.urlopen(addr)

    req = urllib2.Request(addr)

    req.add_header('Accept', ','.join(types))

    if referer:  #consistently misspelt
        req.add_header('Referer', referer)

    req.add_header('User-Agent', 'cwm/1.2 (AIR reasoner/1)')

    stream = urllib2.urlopen(req)

    if print_all_file_names:
        diag.file_list.append(addr)

    return stream
Esempio n. 4
0
    def object(self, str, i, res):
        j = self.subject(str, i, res)
        if j >= 0:
            return j
        else:
            j = self.skipSpace(str, i)
            if j < 0:
                return -1
            else:
                i = j

            if str[i] == '"':
                if str[i : i + 3] == '"""':
                    delim = '"""'
                else:
                    delim = '"'
                i = i + len(delim)

                pairFudge = self.strconst(str, i, delim)
                j = pairFudge[0]
                s = pairFudge[1]

                res.append(self._store.literal(s))
                progress("New string const ", s, j)
                return j
            else:
                return -1
Esempio n. 5
0
def report(statement, why):
    """Report a new statement to the reason tracking software
    
    This module stores the reasons.  The store should call this
    method to report new data. See the KBReasonTracker class

    Most formulas don't need collectors. Let's see if we can change that.
    """
    if why is dontAsk: return
    
    f = statement.context()
    collectors = proofsOf.get(f, [])

    if collectors == []:
        return None
        if diag.chatty_flag>50:
            progress("Adding %s. New collector for  %s" % (statement, why))
        collector = KBReasonTracker(f)
        proofsOf[f] = [ collector ]

    elif len(collectors) != 1:
        raise RuntimeError("""More than one tracker for formula %s.
            Means formula must already have been closed, so shouldn't be
            added to.""" % f)
            
    else: collector = collectors[0]
    
    return collector.newStatement(statement, why)
Esempio n. 6
0
def webget(addr, referer=None, types=[]):
    """Open a URI for reading; return a file-like object with .headers
    cf http://www.w3.org/TR/2004/REC-webarch-20041215/#dereference-uri
    """

    if diag.chatty_flag > 7: progress("Accessing: " + addr)
    if sandBoxed():
        if addr[:5] == 'file:':
            raise SecurityError('local file access prohibited')

#    addr = cacheHack(addr)

    # work around python stdlib bugs with data: URIs
    # buggy in 2.4.2 with CStringIO
    if addr[:5] == 'data:':
        # return open_data(addr)
        return urllib.urlopen(addr)

    req = urllib2.Request(addr)

    if types:
        req.add_header('Accept', ','.join(types))

    if referer: #consistently misspelt
        req.add_header('Referer', referer)

    stream =  urllib2.urlopen(req)

    if print_all_file_names:
        diag.file_list.append(addr)

    return stream
Esempio n. 7
0
    def _propertyAttr(self, ns, name, value):
        "Parse a propertrAttr production.  7.2.25"
        if verbosity() > 50: progress("_propertyAttr ns=%s  name=%s  value=%s"%
                            (ns, name, value))
        if self._subject == None:  # Property as attribute
            self._subject = self.newBlankNode()

            self.sink.makeStatement((self._context,
                                    self._predicate,
                                    self._subject,
                                    self._subject ), why=self._reason2)

        if not ns:
            if "L" not in self.flags:  # assume local?
                raise BadSyntax(sys.exc_info(), "No namespace on property attribute %s=%s" % (name, value))
            ns = self._thisDoc + "#"

        pred = ns + name
        if pred == RDF_NS_URI + "type":  # special case
            obj = self.sink.newSymbol(self.uriref(value)) # SYN#7.2.11 step 2/3
        else:
            obj = self.sink.newLiteral(value, self._datatype, self._language)

        self.sink.makeStatement((self._context,
                                    self.sink.newSymbol(self.uriref(pred)),
                                    self._subject,
                                    obj), why=self._reason2)
        self._state = STATE_NOVALUE  # NOT looking for value
        return
Esempio n. 8
0
 def evaluateSubject(self, obj_py):
     if verbosity() > 80: progress("Concat input:"+`obj_py`)
     str = ""
     for x in obj_py:
         if not isString(x): return None # Can't
         str = str + x 
     return str
Esempio n. 9
0
def report(statement, why):
    """Report a new statement to the reason tracking software
    
    This module stores the reasons.  The store should call this
    method to report new data. See the KBReasonTracker class

    Most formulas don't need collectors. Let's see if we can change that.
    """
    if why is dontAsk: return

    f = statement.context()
    collectors = proofsOf.get(f, [])

    if collectors == []:
        return None
        if diag.chatty_flag > 50:
            progress("Adding %s. New collector for  %s" % (statement, why))
        collector = KBReasonTracker(f)
        proofsOf[f] = [collector]

    elif len(collectors) != 1:
        raise RuntimeError("""More than one tracker for formula %s.
            Means formula must already have been closed, so shouldn't be
            added to.""" % f)

    else:
        collector = collectors[0]

    return collector.newStatement(statement, why)
Esempio n. 10
0
 def evaluateObject(self,   subj_py):
     if verbosity() > 80: progress("strTime:parse input:"+`subj_py`)
     str, format = subj_py
     try:
         return  isodate.fullString(int(calendar.timegm(time.strptime(str, format))));
     except:
         return None
Esempio n. 11
0
    def explain(self, ko, flags):
        """Describe this reason to an RDF store
        Returns the value of this reason as interned in the store.
        """
        me = self.me.get(ko, None)
        if me != None: return me  #  Only do this once
        me = self.meIn(ko)
        if diag.chatty_flag > 49:
            progress("Premise reason=%s ko=%s" % (self, me))
        ko.add(subj=me, pred=rdf.type, obj=reason.Premise, why=dontAsk)
        ko.add(subj=me,
               pred=reason.text,
               obj=ko.newLiteral(self._string),
               why=dontAsk)

        if not self.statements:
            pass


##          raise RuntimeError("No given data for Premise %s" % self)
        else:
            prem = _subsetFormula(self.statements)
            standIn = formulaStandIn(ko, prem, flags=flags)
            ko.add(me, reason.gives, standIn, why=dontAsk)
            if diag.chatty_flag > 59:
                progress("Premise (%s) is:\n%s" %
                         (self._string, prem.n3String()))
        return me
Esempio n. 12
0
 def evaluateObject(self, subj_py):
     """Subject is format string or empty string for std formatting.
     Returns reformatted. @@@@ Ignores TZ"""
     if verbosity() > 80: progress("time:localTime input:"+`subj_py`)
     format = subj_py
     if format =="" : return   isodate.asString(time.time())
     return   time.strftime(format, time.localtime(time.time()))
Esempio n. 13
0
File: thing.py Progetto: weyls/swap
    def unify(self, other, vars, existentials, bindings):
        """Unify this which may contain variables with the other,
            which may contain existentials but not variables.
            Return 0 if impossible.
            Return [(var1, val1), (var2,val2)...] if match"""
        if verbosity() > 90:
            progress("Unifying list %s with %s vars=%s" %
                     (self.value(), other.value(), vars))
        if not isinstance(other, NonEmptyList): return 0
        if other is self: return bindings

        lb = len(bindings)
        nb = self.first.unify(other.first, vars, existentials, bindings)
        if nb == 0: return 0
        if len(nb) > lb:
            vars2 = vars[:]
            existentials2 = existentials[:]
            bindings2 = bindings[:]
            for var, val in nb[lb:]:
                if var in vars2:
                    vars2.remove(var)
                    bindings2.append((var, val))
                else:
                    existentials2.remove(var)
            o = other.rest.substitution(nb)
            s = self.rest.substitution(nb)
            return s.unify(o, vars2, existentials2, bindings2)
        else:
            return self.rest.unify(other.rest, vars, existentials, bindings)
Esempio n. 14
0
    def bind(self, prefix, uri):
        """Pass on a binding hint for later use in output.

        This really is just a hint. The parser calls bind to pass on
        the prefix which it came across, as this is a useful hint for
        a human readable prefix for output of the same
        namespace. Otherwise, output processors will have to invent or
        avoid using namespaces, which will look ugly.
        
        """

        if ':' not in uri:
            # @@ should raise an exception, but sax callbacks crash.
            warn("@@URI must be absolute: %s" % uri)

        # If we don't have a prefix for this ns...
        if self.prefixes.get(uri, None) == None:
            if self.namespaces.get(prefix, None) == None:  # For conventions
                self.prefixes[uri] = prefix
                self.namespaces[prefix] = uri
                if verbosity() > 29:
                    progress("RDFSink.bind:  prefix %s: to <%s>. " %
                             (prefix, uri))
            else:
                self.bind(prefix + "_", uri)  # Recursion unnecessary
Esempio n. 15
0
    def unify(self, other, vars, existentials,  bindings):
        """Unify this which may contain variables with the other,
            which may contain existentials but not variables.
            Return 0 if impossible.
            Return [(var1, val1), (var2,val2)...] if match"""
        if verbosity() > 90: progress("Unifying list %s with %s vars=%s"%(self.value(), other.value(),vars))
        if not isinstance(other, NonEmptyList): return 0
        if other is self: return bindings
        

        lb = len(bindings)
        nb = self.first.unify(other.first, vars, existentials, bindings)
        if nb == 0: return 0
        if len(nb) > lb:
            vars2 = vars[:]
            existentials2 = existentials[:]
            bindings2 = bindings[:]
            for var, val in nb[lb:]:
                if var in vars2:
                    vars2.remove(var)
                    bindings2.append((var, val))
                else:
                    existentials2.remove(var)
            o = other.rest.substitution(nb)
            s = self.rest.substitution(nb)
            return s.unify(o, vars2, existentials2, bindings2)
        else:
            return self.rest.unify(other.rest, vars, existentials,  bindings)
def doCommand(year, inputURI="/dev/stdin"):
        import sys
        global sax2rdf
        import thing
	from thing import load
	global kb
        #from thing import chatty
        #import sax2rdf
	import os
	from thing import Literal
	
# Load the data:
	 
	progress("Data from", inputURI)
	kb=load(inputURI)
#	print "# Size of kb: ", len(kb)

	totals = {}
	for s in kb.statements:
	    obj = s.object()
	    if isinstance(obj, Literal)
		try:
		    value = int(obj.string)
		except:
		    continue
		tot = totals.get(s.predicate(), 0) + value
		totals[s.predicate()] = tot

	ko = kb.newFormula()
	for pred in totals.keys():
	    ko.add(subj=pred, pred=qu.total, obj=ko.newSymbol(`totals[pred]`))
	print ko.close().n3String()
Esempio n. 17
0
    def conclude(self, bindings, evidence = [], extraBNodes=Set()):
        """When a match found in a query, add conclusions to target formula,
        and also remove retractions.

        Returns the number of statements added."""
        if diag.chatty_flag > 25: progress(
            "Insertions will now be made into %s. Bindings %s" % (self.workingContext, bindings))
        result = Query.conclude(self, bindings, evidence)
        
        # delete statements
        if diag.chatty_flag > 25: progress(
            "Insertions made, deletions will now be made. Bindings %s" % bindings)
        for st in self.retraction:
            s, p, o = st.spo()
            subj = self.doSubst(s, bindings)
            pred = self.doSubst(p, bindings)
            obj = self.doSubst(o, bindings)
            ss = self.workingContext.statementsMatching(
                    subj = subj,  pred = pred, obj = obj)
            if len(ss) != 1:
                progress("""Error: %i matches removing statement {%s %s %s} 
                    bound as {%s %s %s} from %s:\n%s\n""" %
                        (len(ss),s,p,o, subj.uriref(), pred.uriref(), obj.uriref(), self.workingContext, ss
                        ))
                progress(self.workingContext.debugString())
                raise RuntimeError(
                    """Error: %i matches removing statement {%s %s %s} 
                    bound as {%s %s %s} from %s""" %
                        (len(ss),s,p,o, `subj`, pred.uriref(), `obj`, self.workingContext))
            if diag.chatty_flag > 25: progress("Deleting %s" % ss[0])
            self.workingContext.removeStatement(ss[0])
        self.justOne = 1  # drop out of here when done
        return 1     # Success --  behave as a test and drop out
Esempio n. 18
0
 def _scan(self, x, context=None):
     #       progress("Scanning ", x, " &&&&&&&&")
     #       assert self.context._redirections.get(x, None) is None, "Should not be redirected: "+`x`
     if verbosity() > 98:
         progress(
             "scanning %s a %s in context %s" % (` x `, ` x.__class__ `, ` context `),
             x.generated(),
             self._inContext.get(x, "--"),
         )
     if isinstance(x, NonEmptyList) or isinstance(x, N3Set):
         for y in x:
             self._scanObj(context, y)
     if isinstance(x, Formula):
         for s in x.statements:
             for p in PRED, SUBJ, OBJ:
                 y = s[p]
                 if isinstance(y, AnonymousVariable) or (isinstance(y, Fragment) and y.generated()):
                     z = self._inContext.get(y, None)
                     if z == "many":
                         continue  # forget it
                     if z is None:
                         self._inContext[y] = x
                     elif z is not x:
                         self._inContext[y] = "many"
                         continue
                     z = self._occurringAs[p].get(y, 0)
                     self._occurringAs[p][y] = z + 1
                 #                       progress("&&&&&&&&& %s now occurs %i times as %s" %(`y`, z+1, "CPSO"[p]))
                 #                   else:
                 #                       progress("&&&&&&&&& yyyy  %s has class %s " %(`y`, `y.__class__`))
                 if x is not y:
                     self._scan(y, x)
         self._breakloops(x)
Esempio n. 19
0
def doCommand(inputURI="/dev/stdin"):
    import sys
    global sax2rdf
    global kb
    import os

    # Load the data:

    progress("Data from", inputURI)
    kb = load(inputURI)
    #       print "# Size of kb: ", len(kb)

    totals = {}
    for s in kb.statements:
        obj = s.object()
        if isinstance(obj, Literal):
            try:
                value = float(obj.string)
            except:
                continue
            tot = totals.get(s.predicate(), 0) + value
            totals[s.predicate()] = tot

    ko = kb.newFormula()
    for pred in totals.keys():
        ko.add(subj=pred,
               pred=qu.total,
               obj=ko.newLiteral("%7.2f" % totals[pred]))
    print ko.close().n3String()
Esempio n. 20
0
def doCommand(inputURI="/dev/stdin"):
        import sys
        global sax2rdf
        global kb
        import os

# Load the data:
         
        progress("Data from", inputURI)
        kb=load(inputURI)
#       print "# Size of kb: ", len(kb)

        totals = {}
        for s in kb.statements:
            obj = s.object()
            if isinstance(obj, Literal):
                try:
                    value = float(obj.string)
                except:
                    continue
                tot = totals.get(s.predicate(), 0) + value
                totals[s.predicate()] = tot

        ko = kb.newFormula()
        for pred in totals.keys():
            ko.add(subj=pred, pred=qu.total, obj=ko.newLiteral("%7.2f" % totals[pred]))
        print ko.close().n3String()
Esempio n. 21
0
 def evaluateObject(self, subj_py):
     """params are ISO time string, format string. Returns reformatted. Ignores TZ@@"""
     if verbosity() > 80: progress("strTime:format input:"+`subj_py`)
     str, format = subj_py
     try:
         return  time.strftime(format, time.gmtime(isodate.parse(str)))
     except:
         return None
Esempio n. 22
0
def evalXPath(*args, **keywords):
    try:
        from xml.xpath import Evaluate as localEvalXPath 
    except ImportError:
        progress("Try getting python-xml from http://downloads.sourceforge.net/pyxml/PyXML-0.8.4.tar.gz")
        localEvalXPath = progress
    globals()['evalXPath'] = localEvalXPath
    return localEvalXPath(*args, **keywords)
Esempio n. 23
0
 def evaluateObject(self,  subj_py):
     """params are epoch-seconds time string, format string. Returns reformatted"""
     if verbosity() > 80: progress("strTime:format input:"+`subj_py`)
     str, format = subj_py
     try:
         return  time.strftime(format, time.gmtime(int(str)))
     except:
         return None
Esempio n. 24
0
 def resetRenames(reset=True):
     if reset:
         if diag.chatty_flag > 20:
             progress(
                 "Resetting all renamed vars maps ---------------------------------"
             )
         Formula._renameVarsMaps.append({})
     else:
         Formula._renameVarsMaps.pop()
Esempio n. 25
0
 def _addData(self, data, theType, filter):
     progress("@@ _addData data=%s theType=%s filter=%s " %(data, theType, filter))
     progress("@@@ type ", type(data))
     if type(data[0]) is not type([]): data = [data]   # resolve ambiguous calling convention
     try:
         data[0].isdigit() # Lord, there's got to be a better way. @@@   (@@ to do what? --tim)
         data = [data]
     except AttributeError, e:
         data = data  #  @@? what does this do - tim   mean "pass"?
Esempio n. 26
0
def evalXPath(*args, **keywords):
    try:
        from xml.xpath import Evaluate as localEvalXPath
    except ImportError:
        progress(
            "Try getting python-xml from http://downloads.sourceforge.net/pyxml/PyXML-0.8.4.tar.gz"
        )
        localEvalXPath = progress
    globals()['evalXPath'] = localEvalXPath
    return localEvalXPath(*args, **keywords)
Esempio n. 27
0
    def declareUniversal(self, v, key=None):
        if False and key is not AnonymousUniversal:
            raise RuntimeError("""We have now disallowed the calling of declareUniversal.
For future reference, use newUniversal
""")
        if verbosity() > 90: progress("Declare universal:", v)
        if v not in self._universalVariables:
            self._universalVariables.add(v)
            if self.occurringIn(Set([self.newSymbol(v.uriref())])):
                raise ValueError("Internal error: declareUniversal: %s?" % v)
Esempio n. 28
0
 def evaluateObject(self,  subj_py):
     """Subject is  (empty string for standard formatting or) format string.
     Returns formatted."""
     if verbosity() > 80: progress("time:gmTime input:"+`subj_py`)
     format = subj_py
     if format =="" : format="%Y-%m-%dT%H:%M:%SZ"
     try:
         return time.strftime(format, time.gmtime(time.time()))
     except:
         return isodate.asString(time())
Esempio n. 29
0
 def occurringIn(self, vars):
     "Which variables in the list occur in this list?"
     set = []
     if verbosity() > 98: progress("----occuringIn: ", `self`)
     x = self
     while not isinstance(x, EmptyList):
         y = x.first
         x = x.rest
         set = merge(set, y.occurringIn(vars))
     return set
Esempio n. 30
0
    def declareUniversal(self, v, key=None):
#        if key is not AnonymousUniversal:
#            raise RuntimeError("""We have now disallowed the calling of declareUniversal.
#For future reference, use newUniversal
#""")
        if verbosity() > 90: progress("Declare universal:", v)
        if v not in self._universalVariables:
            self._universalVariables.add(v)
            if self.occurringIn(Set([self.newSymbol(v.uriref())])):
                raise ValueError("Are you trying to confuse me with %s?" % v)
Esempio n. 31
0
 def evaluateObject(self,  subj_py):
     if verbosity() > 80: progress("os:argv input:"+`subj_py`)
     if  self.store.argv:  # Not None or []. was also: isString(subj_py) and
         try:
             argnum = int(subj_py) -1
         except ValueError:
             if verbosity() > 30:
                 progress("os:argv input is not a number: "+`subj_py`)
             return None
         if argnum < len(self.store.argv):
             return self.store.argv[argnum]
    def evaluateObject(self, subj_py):
        if verbosity() > 80: progress("scrape input:" + ` subj_py `)

        str, pat = subj_py
        patc = re.compile(pat)
        m = patc.search(str)

        if m:
            if verbosity() > 80: progress("scrape matched:" + m.group(1))
            return m.group(1)
        if verbosity() > 80: progress("scrape didn't match")
Esempio n. 33
0
 def occurringIn(self, vars):
     "Which variables in the list occur in this?"
     set = Set()
     if verbosity() > 98: progress("----occuringIn: ", `self`)
     for p in PRED, SUBJ, OBJ:
         y = self[p]
         if y is self:
             pass
         else:
             set = set | y.occurringIn(vars)
     return set
Esempio n. 34
0
    def evalObj(self, subj, queue, bindings, proof, query):
        progress("@@fn:doc", subj)

        # fn:doc takes a string, but the llyn cache keys of symbols
        sym = subj.store.newSymbol(subj.value())

        try:
            lit = loadToStore(sym, ["application/xml", "text/xml"])
        except IOError, e:
            progress("@@ioerror", e)
            return None  # hmm... is built-in API evolving to support exceptions?
Esempio n. 35
0
 def explain(self, ko, flags):
     """Describe this reason to an RDF store
     Returns the value of this reason as interned in the store.
     """
     me = self.me.get(ko, None)
     if me != None: return me  #  Only do this once
     me = self.meIn(ko)
     if diag.chatty_flag>49: progress("CommandLine reason=%s ko=%s"%(self,me))
     ko.add(subj=me, pred=rdf.type, obj=reason.CommandLine, why=dontAsk)
     ko.add(subj=me, pred=reason.args, obj=self._string, why=dontAsk)
     return me
Esempio n. 36
0
    def declareUniversal(self, v, key=None):
        if False and key is not AnonymousUniversal:
            raise RuntimeError(
                """We have now disallowed the calling of declareUniversal.
For future reference, use newUniversal
""")
        if verbosity() > 90: progress("Declare universal:", v)
        if v not in self._universalVariables:
            self._universalVariables.add(v)
            if self.occurringIn(Set([self.newSymbol(v.uriref())])):
                raise ValueError("Internal error: declareUniversal: %s?" % v)
Esempio n. 37
0
def substitute(form, dict):
    "Substitute values from dictionary into $vvv$ places in form string"
    str = form[:]
    r = re.compile(r"^(.*)(\$[_A-Za-z0-9]*\$)(.*)$", re.MULTILINE)
    while 1:
        m = r.search(str)
        if m == None: break
        start, end = m.start(2), m.end(2)
        progress("Matched start, end", start, end, str[start:end])
        str = str[:start] + dict[str[start:end][1:-1]] + str[end:]
    return str
Esempio n. 38
0
 def unify(self, other, vars, existentials,  bindings):
     """Unify this which may contain variables with the other,
         which may contain existentials but not variables.
         Return 0 if impossible.
         Return [(var1, val1), (var2,val2)...] if match"""
     if verbosity() > 97: progress("Unifying symbol %s with %s vars=%s"%(self, other,vars))
     if self is other: return bindings
     if self in vars+existentials:
         if verbosity() > 80: progress("Unifying term MATCHED %s to %s"%(self,other))
         return bindings + [(self, other)]
     return 0
Esempio n. 39
0
    def evalObj(self, subj, queue, bindings, proof, query):
        progress("@@fn:doc", subj)

        # fn:doc takes a string, but the llyn cache keys of symbols
        sym = subj.store.newSymbol(subj.value())

        try:
            lit = loadToStore(sym, ["application/xml", "text/xml"])
        except IOError, e:
            progress("@@ioerror", e)
            return None # hmm... is built-in API evolving to support exceptions?
Esempio n. 40
0
def substitute(form, dict):
    "Substitute values from dictionary into $vvv$ places in form string"
    str = form[:]
    r = re.compile(r"^(.*)(\$[_A-Za-z0-9]*\$)(.*)$", re.MULTILINE)
    while 1:
        m = r.search(str)
        if m == None: break
        start, end = m.start(2), m.end(2)
        progress( "Matched start, end", start, end, str[start:end])
        str = str[:start] + dict[str[start:end][1:-1]] + str[end:]
    return str
Esempio n. 41
0
 def explain(self, ko, flags):
     """Describe this reason to an RDF store
     Returns the value of this reason as interned in the store.
     """
     me = self.me.get(ko, None)
     if me != None: return me  #  Only do this once
     me = self.meIn(ko)
     if diag.chatty_flag > 49:
         progress("CommandLine reason=%s ko=%s" % (self, me))
     ko.add(subj=me, pred=rdf.type, obj=reason.CommandLine, why=dontAsk)
     ko.add(subj=me, pred=reason.args, obj=self._string, why=dontAsk)
     return me
Esempio n. 42
0
 def newStatement(self, s, why):
     if verbosity() > 80 and why is not dontAsk:
         progress("Believing %s because of %s"%(s, why))
     import formula
     for x in s.quad[1:]:
         if isinstance(x, formula.Formula):
             if x.canonical is not x:
                 raise RuntimeError(x)
     assert why is not self
     self.reasonForStatement[s]=why
     if isinstance(why, (Premise, BecauseOfRule, BecauseOfData)):
         why.statements.add(s)
Esempio n. 43
0
 def newStatement(self, s, why):
     if verbosity() > 80 and why is not dontAsk:
         progress("Believing %s because of %s" % (s, why))
     import formula
     for x in s.quad[1:]:
         if isinstance(x, formula.Formula):
             if x.canonical is not x:
                 raise RuntimeError(x)
     assert why is not self
     self.reasonForStatement[s] = why
     if isinstance(why, (Premise, BecauseOfRule, BecauseOfData)):
         why.statements.add(s)
Esempio n. 44
0
def smushedFormula(F, G):
    """The formula F has been replaced by G
    
    Because this module tracks formula in stores, if ever the address
    of a formula is changed, that is (currently) when it is 
    canonicalized, then the fact must be reported here.
    """
    progress("why: Formula %s has been replaced by %s" % (F, G))
    pF = proofsOf[F]
    pG = proofsOf[G]
    proofsOf[G] = pF + pG
    raise RuntimeError("@@@@ temp flag - we got to line 71 of why.py")
    del proofsOf[F]
Esempio n. 45
0
    def evaluateObject(self, subj_py):
        #        raise Error
        store = self.store
        if verbosity() > 80: progress("search input:" + ` subj_py `)

        str, pat = subj_py
        patc = re.compile(pat)
        m = patc.search(str)

        if m:
            if verbosity() > 80: progress("search matched:" + m.group(1))
            return m.groups()
        if verbosity() > 80: progress("search didn't match")
Esempio n. 46
0
def smushedFormula(F, G):
    """The formula F has been replaced by G
    
    Because this module tracks formula in stores, if ever the address
    of a formula is changed, that is (currently) when it is 
    canonicalized, then the fact must be reported here.
    """
    progress("why: Formula %s has been replaced by %s" %(F,G))
    pF = proofsOf[F]
    pG = proofsOf[G]
    proofsOf[G] = pF + pG
    raise RuntimeError("@@@@ temp flag - we got to line 71 of why.py")
    del proofsOf[F]
def SparqlQuery(query, items, serviceURI):
    """Perform remote query as client on remote store.
        See $SWAP/query.py
    """
    ##    diag.chatty_flag = 99    # @@@@@@
    if diag.chatty_flag > 10:
        progress(
            "SPARQL Query on service %s,\n\tvariables: %s;\n\texistentials: %s"
            % (serviceURI, query.variables, query.existentials()))
        for item in items:
            progress("\tSparql query line: %s" % ( ` item `))

#    s = query.n3String()
#    progress("QUERY IS ", s)

## To make it easy to map the variables used to the ones
## we get back, use strings, and go both ways.
    vars = {}
    reverseVars = {}
    for var in query.variables:
        varString = makeVar()
        vars[var] = varString
        reverseVars[varString] = var

    patterns = []
    for item in items:
        p = []
        for part in SUBJ, PRED, OBJ:
            term = item[part]
            p.append(vars.get(term, representationOf(term)))
        patterns.append(' '.join([x for x in p]))

    queryString = "SELECT * WHERE { %s }" % ('. '.join(patterns))
    url = serviceURI + '?' + urllib.urlencode({'query': queryString})

    response = urllib.urlopen(url)
    results = parseSparqlResults(query.store, response)
    nbs = []
    for binding in results:
        newBinding = Env()
        for key, val in binding.items():
            var = reverseVars['?' + key]
            newBinding = newBinding.bind(var, (val, newBinding.id))
        nbs.append((newBinding, None))


#    raise NotImplementedError(results)
    if diag.chatty_flag > 50:
        progress('remote query done, nbs=%s' % nbs)
    return nbs  # No bindings for testing
Esempio n. 48
0
File: thing.py Progetto: weyls/swap
 def unify(self, other, vars, existentials, bindings):
     """Unify this which may contain variables with the other,
         which may contain existentials but not variables.
         Return 0 if impossible.
         Return [(var1, val1), (var2,val2)...] if match"""
     if verbosity() > 97:
         progress("Unifying symbol %s with %s vars=%s" %
                  (self, other, vars))
     if self is other: return bindings
     if self in vars + existentials:
         if verbosity() > 80:
             progress("Unifying term MATCHED %s to %s" % (self, other))
         return bindings + [(self, other)]
     return 0
    def reification(self, sink, bnodeMap={}, why=None):
        """Describe myself in RDF to the given context
        
        
        """
        list = [].__class__
        try:
            return bnodeMap[self]
        except KeyError:
            F = sink.newBlankNode()
            bnodeMap[self] = F
        rei = sink.newSymbol(reifyNS[:-1])
        myMap = bnodeMap
        ooo = sink.newSymbol(owlOneOf)
        es = list(self.existentials())
        es.sort(Term.compareAnyTerm)
        us = list(self.universals())
        us.sort(Term.compareAnyTerm)
        for vars, vocab in ((es, rei["existentials"]), (us,
                                                        rei["universals"])):
            if diag.chatty_flag > 54:
                progress("vars=", vars)
                progress("vars=", [v.uriref() for v in vars])
            list = sink.store.nil.newList([
                x.reification(sink, myMap, why) for x in vars
            ])  # sink.newLiteral(x.uriref())
            klass = sink.newBlankNode()
            sink.add(klass, ooo, list)
            sink.add(F, vocab, klass, why)

        #The great list of statements
        statementList = []
        for s in self.statements:
            subj = sink.newBlankNode()
            sink.add(subj, rei["subject"],
                     s[SUBJ].reification(sink, myMap, why), why)
            sink.add(subj, rei["predicate"],
                     s[PRED].reification(sink, myMap, why), why)
            sink.add(subj, rei["object"], s[OBJ].reification(sink, myMap, why),
                     why)
            statementList.append(subj)

    #The great class of statements
        StatementClass = sink.newBlankNode()
        realStatementList = sink.store.nil.newList(statementList)
        sink.add(StatementClass, ooo, realStatementList, why)
        #We now know something!
        sink.add(F, rei["statements"], StatementClass, why)

        return F
Esempio n. 50
0
 def __init__(self, context, subj, conclusion, pred, obj, reason):
     BecauseBuiltIn.__init__(self, context, subj, pred, obj)
     self.reason = []
     for statement in reason:
         if isinstance(statement, Reason):
             if isinstance(statement, BecauseBuiltIn):
                 if statement._context is conclusion:
                     self.reason.append(statement)
                     progress(statement)
                     progress("Is anybody home?")
         else:
             if statement.context() is conclusion:
                 self.reason.append(statement)
     self.conclusion = conclusion
     assert isTopLevel(conclusion)
Esempio n. 51
0
    def explain(self, ko, flags):
        me = self.me.get(ko, None)
        if me != None: return me  #  Only do this once
        me = self.meIn(ko)

        g = self.formula
        e = g.existentials()
        if g.occurringIn(e) != e: raise RuntimeError(g.debugString())

        qed = ko.newBlankNode(why=dontAsk)
        standIn = formulaStandIn(ko, self.formula, flags)
        ko.add(subj=me, pred=reason.gives, obj=standIn, why=dontAsk)

        statementsForReason = {}  # reverse index: group by reason
        for s, rea in sorted(self.reasonForStatement.items()):
            x = statementsForReason.get(rea, None)
            if x is None: statementsForReason[rea] = [s]
            else: x.append(s)
        if diag.chatty_flag > 29:
            progress(
                "Collector %s (->%s), explaining %i reasons for total of %i statements:-"
                % (self, me, len(statementsForReason),
                   len(self.reasonForStatement)))
            progress("reasonForStatement", self.reasonForStatement)
            progress("statementsForReason", statementsForReason)
        # @@ Should special case (no conjunction) if only one r
        if len(statementsForReason) != 1:
            ko.add(subj=me, pred=rdf.type, obj=reason.Conjunction, why=dontAsk)

        for r, ss in statementsForReason.items():
            assert r is not self, ("Loop in reasons!", self, id(self), s)
            try:
                r1 = r.explain(ko, flags=flags)
            except:
                print ss
                raise
            if diag.chatty_flag > 29:
                progress("\tExplaining reason %s (->%s) for %i statements" %
                         (r, r1, len(ss)))
                for s in ss:
                    progress("\t  Statement %s" % (s))
            if len(statementsForReason) == 1:  ## No need for conjunction
                ko.substituteEqualsInPlace({r1: me}, why=dontAsk)
            else:
                ko.add(me, reason.component, r1, why=dontAsk)
        return me
Esempio n. 52
0
def cacheHack(addr):
    """ If on a plane, hack remote w3.org access to local access
    """
    real = "http://www.w3.org/"
    local = "/devel/WWW/"
    suffixes = ["", ".rdf", ".n3"]
    if addr.startswith(real):
        rest = local + addr[len(real):]
        for s in suffixes:
            fn = rest + s
            try:
                os.stat(fn)
                progress("Offline: Using local copy %s" % fn)
                return "file://" + fn
            except OSError:
                continue
    return addr
Esempio n. 53
0
 def _scanObj(self, context, x):
     "Does this appear in just one context, and if so counts how many times as object"
     z = self._inContext.get(x, None)
     if z == "many": return  # forget it
     if z is None:
         self._inContext[x] = context
     elif z is not context:
         self._inContext[x] = "many"
         return
     if isinstance(x, NonEmptyList) or isinstance(x, N3Set):
         for y in x:
             self._scanObj(context, y)
     if isinstance(x, AnonymousVariable) or (isinstance(x, Fragment)
                                             and x.generated()):
         y = self._occurringAs[OBJ].get(x, 0) + 1
         self._occurringAs[OBJ][x] = y
         if verbosity() > 98:
             progress("scan: %s, a %s, now has %i occurrences as %s" %
                      (x, x.__class__, y, "CPSOqqqqq"[y]))
Esempio n. 54
0
def explainStatement(s, ko, ss=None, flags=""):
    si = describeStatement([s], ko, flags=flags)

    statementReason = getStatementReason(s)

    if statementReason == None:
        raise RuntimeError("""Ooops, no reason for this statement?! 
        Collector: %s
        Formula: %s
        No reason for statement: %s
        Reasons for statements we do have: %s
        Formula contents as follows:
        %s
        """ % (tracker, f, s, tracker.reasonForStatement, f.debugString()))

    if diag.chatty_flag > 49: progress("explaining statement: %s" % (s))
    ri = statementReason.explain(ko, flags=flags)
    ko.add(subj=si, pred=reason.because, obj=ri, why=dontAsk)
    return si
Esempio n. 55
0
    def conclude(self,
                 bindings,
                 evidence=[],
                 extraBNodes=Set(),
                 allBindings=None):
        """When a match found in a query, add conclusions to target formula,
        and also remove retractions.

        Returns the number of statements added."""
        if diag.chatty_flag > 25:
            progress("Insertions will now be made into %s. Bindings %s" %
                     (self.workingContext, bindings))
        result = Query.conclude(self, bindings, evidence)

        # delete statements
        if diag.chatty_flag > 25:
            progress(
                "Insertions made, deletions will now be made. Bindings %s" %
                bindings)
        for st in self.retraction:
            s, p, o = st.spo()
            subj = self.doSubst(s, bindings)
            pred = self.doSubst(p, bindings)
            obj = self.doSubst(o, bindings)
            ss = self.workingContext.statementsMatching(subj=subj,
                                                        pred=pred,
                                                        obj=obj)
            if len(ss) != 1:
                progress("""Error: %i matches removing statement {%s %s %s} 
                    bound as {%s %s %s} from %s:\n%s\n""" %
                         (len(ss), s, p, o, subj.uriref(), pred.uriref(),
                          obj.uriref(), self.workingContext, ss))
                progress(self.workingContext.debugString())
                raise RuntimeError(
                    """Error: %i matches removing statement {%s %s %s} 
                    bound as {%s %s %s} from %s""" %
                    (len(ss), s, p, o, ` subj `, pred.uriref(), ` obj
                     `, self.workingContext))
            if diag.chatty_flag > 25: progress("Deleting %s" % ss[0])
            self.workingContext.removeStatement(ss[0])
        self.justOne = 1  # drop out of here when done
        return 1  # Success --  behave as a test and drop out
Esempio n. 56
0
    def explain(self, ko, flags):
        "This is just a plain fact - or was at the time."
        me = self.me.get(ko, None)
        if me != None: return me  #  Only do this once
        me = self.meIn(ko)
        if diag.chatty_flag > 49: progress("Fact reason=%s ko=%s" % (self, me))
        fact = ko.newFormula()
        m = ko.newBlankNode(why=dontAsk)
        #        raise RuntimeError(m)
        fact.add(subj=self._subject,
                 pred=self._predicate,
                 obj=self._object,
                 why=dontAsk)
        fact = fact.close()
        ko.add(me, rdf.type, reason.Conclusion, why=dontAsk)
        standIn = formulaStandIn(ko, fact, flags=flags)
        ko.add(me, reason.gives, standIn, why=dontAsk)
        ko.add(subj=m, pred=rdf.type, obj=reason.Conjunction, why=dontAsk)
        ko.add(me, reason.because, m, why=dontAsk)
        statementsForReason = {}  # reverse index: group by reason
        for s in self.reason:
            try:
                rea = getStatementReason(s)
            except:
                print s
                raise
            x = statementsForReason.get(rea, None)
            if x is None: statementsForReason[rea] = [s]
            else: x.append(s)
        for e in statementsForReason:
            r1 = e.explain(ko, flags=flags)
            ko.add(m, reason.component, r1, why=dontAsk)
##        if 'g' in flags:
##            for x in self._subject, self._object:
##                proofs = proofsOf.get(x, None)
##                if proofs != None:
##                    ko.add(me, reason.proof, proof[0].explain(ko, flags=flags), why=dontAsk)

#       if self._proof != None:
#           ko.add(me, reason.proof, self._proof.explain(ko), why=dontAsk)
        return me
    def object(self, str, i, res):
        j = self.subject(str, i, res)
        if j >= 0:
            return j
        else:
            j = self.skipSpace(str, i)
            if j < 0: return -1
            else: i = j

            if str[i] == '"':
                if str[i:i + 3] == '"""': delim = '"""'
                else: delim = '"'
                i = i + len(delim)

                pairFudge = self.strconst(str, i, delim)
                j = pairFudge[0]
                s = pairFudge[1]

                res.append(self._store.literal(s))
                progress("New string const ", s, j)
                return j
            else:
                return -1