Exemplo n.º 1
0
def renderFOAFNetwork(graph,nicks,nsMap = {}):
    try:    
        bglGraph = bgl.Digraph()
    except:
        raise NotImplementedError("Boost Graph Library & Python bindings not installed.  See: see: http://www.osl.iu.edu/~dgregor/bgl-python/")
    namespace_manager = NamespaceManager(Graph())
    vertexMaps   = {}
    edgeMaps     = {}
    for prefix,uri in nsMap.items():
        namespace_manager.bind(prefix, uri, override=False)

    visitedNodes = {}
    edges = []
    idx = 0
    edgeIDX = 0
    excludedPeople = Set()
    #collect exluded people
    print nicks
    for s,p,o in graph.triples((None,FOAF_NS.knows,None)):
        for node in [s,o]:
            if not node in visitedNodes:
                included = False
                _nicks = []
                for s2,p2,nick in graph.triples((node,FOAF_NS.nick,None)):                    
                    _nicks.append(str(nick))
                    if str(nick) in nicks:
                        included = True
                        break
                if not included:
                    print "exluding ", node
                    print "\t",_nicks, nicks
                    excludedPeople.add(node)
    
    for s,p,o in graph.triples((None,FOAF_NS.knows,None)):
        if s in excludedPeople:
            print "skipping ", s
            continue
        for node in [s,o]:
            if not node in visitedNodes:
                idx += 1
                visitedNodes[node] = generateBGLNode((node,graph),bglGraph,vertexMaps,namespace_manager,str(idx),node not in excludedPeople)
        if (s,o) not in edges:
            edgeIDX += 1
            edge             = bglGraph.add_edge(visitedNodes[s],visitedNodes[o])
            labelMap         = edgeMaps.get('label',bglGraph.edge_property_map('string'))
            labelMap[edge]   = "foaf:knows"
            idMap            = edgeMaps.get('ids',bglGraph.edge_property_map('string'))
            idMap[edge]      = str(edgeIDX)
            
            edgeMaps['ids']   = idMap
            edgeMaps['label'] = labelMap
            bglGraph.edge_properties['label'] = labelMap
            bglGraph.edge_properties['ids']   = idMap
            edges.append((s,o))
    return bglGraph
Exemplo n.º 2
0
    def testExpand(self):
        EX = Namespace("http://example.com/")
        namespace_manager = NamespaceManager(Graph())
        namespace_manager.bind('ex', EX, override=False)
        self.testGraph.namespace_manager = namespace_manager

        man = Class(EX.Man)
        boy = Class(EX.Boy)
        woman = Class(EX.Woman)
        girl = Class(EX.Girl)
        male = Class(EX.Male)
        female = Class(EX.Female)
        human = Class(EX.Human)
        animal = Class(EX.Animal)
        cat = Class(EX.Cat)
        dog = Class(EX.Dog)
        animal = Class(EX.Animal)

        animal = cat | dog | human
        human += man
        human += boy
        human += woman
        human += girl
        male += man
        male += boy
        female += woman
        female += girl

        testClass = human & ~female
        self.assertEquals(repr(testClass),
                          '( ex:Human and ( not ex:Female ) )')
        newtestClass = ComplementExpansion(testClass, debug=True)
        self.assertTrue(
            repr(newtestClass)
            in ['( ex:Boy or ex:Man )', '( ex:Man or ex:Boy )'],
            repr(newtestClass))

        testClass2 = animal & ~(male | female)
        self.assertEquals(
            repr(testClass2),
            '( ( ex:Cat or ex:Dog or ex:Human ) and ( not ( ex:Male or ex:Female ) ) )'
        )
        newtestClass2 = ComplementExpansion(testClass2, debug=True)
        testClass2Repr = repr(newtestClass2)
        self.assertTrue(
            testClass2Repr in ['( ex:Cat or ex:Dog )', '( ex:Dog or ex:Cat )'],
            testClass2Repr)
Exemplo n.º 3
0
def renderNetwork(network,nsMap = {}):
    """
    Takes an instance of a compiled ReteNetwork and a namespace mapping (for constructing QNames
    for rule pattern terms) and returns a BGL Digraph instance representing the Rete network
    #(from which GraphViz diagrams can be generated)
    """
    from FuXi.Rete import BuiltInAlphaNode
    from BetaNode import LEFT_MEMORY, RIGHT_MEMORY, LEFT_UNLINKING
    dot=Dot(graph_type='digraph')
    namespace_manager = NamespaceManager(Graph())
    for prefix,uri in nsMap.items():
        namespace_manager.bind(prefix, uri, override=False)

    visitedNodes = {}
    edges = []
    idx = 0
    for node in network.nodes.values():
        if not node in visitedNodes:
            idx += 1
            visitedNodes[node] = generateBGLNode(dot,node,namespace_manager,str(idx))
            dot.add_node(visitedNodes[node])
    nodeIdxs = {}                        
    for node in network.nodes.values():
        for mem in node.descendentMemory:
            if not mem:
                continue
            bNode = mem.successor
        for bNode in node.descendentBetaNodes:
            for idx,otherNode in enumerate([bNode.leftNode,bNode.rightNode]):
                if node == otherNode and (node,otherNode) not in edges:
                    for i in [node,bNode]:
                        if i not in visitedNodes:
                            idx += 1
                            nodeIdxs[i] = idx 
                            visitedNodes[i] = generateBGLNode(dot,i,namespace_manager,str(idx))
                            dot.add_node(visitedNodes[i])
                    edge = Edge(visitedNodes[node],
                                visitedNodes[bNode],
                                label=idx==0 and 'left' or 'right')
                    dot.add_edge(edge)                                        
                    edges.append((node,bNode))
                    
    return dot
Exemplo n.º 4
0
def renderNetwork(network,nsMap = {}):
    """
    Takes an instance of a compiled ReteNetwork and a namespace mapping (for constructing QNames
    for rule pattern terms) and returns a BGL Digraph instance representing the Rete network
    #(from which GraphViz diagrams can be generated)
    """
    from FuXi.Rete import BuiltInAlphaNode
    from BetaNode import LEFT_MEMORY, RIGHT_MEMORY, LEFT_UNLINKING
    dot=Dot(graph_type='digraph')
    namespace_manager = NamespaceManager(Graph())
    for prefix,uri in nsMap.items():
        namespace_manager.bind(prefix, uri, override=False)

    visitedNodes = {}
    edges = []
    idx = 0
    for node in network.nodes.values():
        if not node in visitedNodes:
            idx += 1
            visitedNodes[node] = generateBGLNode(dot,node,namespace_manager,str(idx))
            dot.add_node(visitedNodes[node])
    nodeIdxs = {}                        
    for node in network.nodes.values():
        for mem in node.descendentMemory:
            if not mem:
                continue
            bNode = mem.successor
        for bNode in node.descendentBetaNodes:
            for idx,otherNode in enumerate([bNode.leftNode,bNode.rightNode]):
                if node == otherNode and (node,otherNode) not in edges:
                    for i in [node,bNode]:
                        if i not in visitedNodes:
                            idx += 1
                            nodeIdxs[i] = idx 
                            visitedNodes[i] = generateBGLNode(dot,i,namespace_manager,str(idx))
                            dot.add_node(visitedNodes[i])
                    edge = Edge(visitedNodes[node],
                                visitedNodes[bNode],
                                label=idx==0 and 'left' or 'right')
                    dot.add_edge(edge)                                        
                    edges.append((node,bNode))
                    
    return dot
Exemplo n.º 5
0
    def testExpand(self):
        EX = Namespace("http://example.com/")
        namespace_manager = NamespaceManager(Graph())
        namespace_manager.bind('ex', EX, override=False)
        self.testGraph.namespace_manager = namespace_manager    
        
        man   = Class(EX.Man)
        boy   = Class(EX.Boy)
        woman = Class(EX.Woman)
        girl  = Class(EX.Girl)
        male  = Class(EX.Male)
        female= Class(EX.Female)
        human = Class(EX.Human)        
        animal = Class(EX.Animal)
        cat = Class(EX.Cat)
        dog = Class(EX.Dog)
        animal = Class(EX.Animal)
        
        animal = cat | dog | human
        human += man
        human += boy
        human += woman
        human += girl
        male   += man
        male   += boy
        female += woman
        female += girl
        
        testClass = human & ~ female
        self.assertEquals(repr(testClass),'( ex:Human and ( not ex:Female ) )')
        newtestClass = ComplementExpansion(testClass,debug=True)      
        self.assertTrue(repr(newtestClass) in ['( ex:Boy or ex:Man )','( ex:Man or ex:Boy )'],repr(newtestClass))

        testClass2 = animal & ~ (male | female)
        self.assertEquals(repr(testClass2),
                          '( ( ex:Cat or ex:Dog or ex:Human ) and ( not ( ex:Male or ex:Female ) ) )')
        newtestClass2 = ComplementExpansion(testClass2,debug=True)  
        testClass2Repr = repr(newtestClass2)
        self.assertTrue(testClass2Repr in ['( ex:Cat or ex:Dog )','( ex:Dog or ex:Cat )'],testClass2Repr)
Exemplo n.º 6
0
 def canonicalTerm(self,term):
     if isinstance(term,URIRef):
         if self.prolog is not None:
             namespace_manager = NamespaceManager(Graph())
             for prefix,uri in self.prolog.prefixBindings.items():
                 namespace_manager.bind(prefix, uri, override=False)
             try:    
                 prefix,uri,localName=namespace_manager.compute_qname(term)
             except:
                 return term
             if prefix not in self.prolog.prefixBindings:
                 return term
             else:
                 return u':'.join([prefix,localName])
         else:
             return term
     elif isinstance(term,Literal):
         return term.n3()
     elif isinstance(term,BNode):
         return term.n3()
     else:
         assert isinstance(term,Variable)
         return term.n3()
Exemplo n.º 7
0
class QNameManager(object):
    def __init__(self,nsDict=None):
        self.nsDict = nsDict and nsDict or {}
        self.nsMgr = NamespaceManager(Graph())
        self.nsMgr.bind('owl','http://www.w3.org/2002/07/owl#')
        self.nsMgr.bind('math','http://www.w3.org/2000/10/swap/math#')
        
    def bind(self,prefix,namespace):
        self.nsMgr.bind(prefix,namespace)
Exemplo n.º 8
0
 def canonicalTerm(self, term):
     if isinstance(term, URIRef):
         if self.prolog is not None:
             namespace_manager = NamespaceManager(Graph())
             for prefix, uri in self.prolog.prefixBindings.items():
                 namespace_manager.bind(prefix, uri, override=False)
             try:
                 prefix, uri, localName = namespace_manager.compute_qname(
                     term)
             except:
                 return term
             if prefix not in self.prolog.prefixBindings:
                 return term
             else:
                 return u':'.join([prefix, localName])
         else:
             return term
     elif isinstance(term, Literal):
         return term.n3()
     elif isinstance(term, BNode):
         return term.n3()
     else:
         assert isinstance(term, Variable)
         return term.n3()
Exemplo n.º 9
0
def defineProperties():
        #namespaces definition
        aeroOntology = Namespace('http://purl.obolibrary.org/obo/aero.owl')
        skosNS = Namespace('http://www.w3.org/2004/02/skos/core#')
        umlsNS = Namespace('http://bioportal.bioontology.org/ontologies/umls/')
        aeroNS = Namespace('http://purl.obolibrary.org/obo/')
        OntologyNS = Namespace('http://purl.org/vaers/')
        medraNS = Namespace('http://purl.bioontology.org/ontology/MDR/')


        namespace_manager = NamespaceManager(Graph())
        namespace_manager.bind('obo', aeroNS, override=False)
        namespace_manager.bind('owl', OWL_NS, override=False)
        namespace_manager.bind('aero', aeroOntology, override=False)
        namespace_manager.bind('skos-core',skosNS, override=False)
        namespace_manager.bind('umls', umlsNS, override=False)


        #create the main graph
        g = Graph()
        g.namespace_manager = namespace_manager
        #this tells you that all objects will be created in the g graph -> no need to pass an extra parameter to each
        Individual.factoryGraph = g
        return g
Exemplo n.º 10
0
def main():
  from optparse import OptionParser

  parser = OptionParser()
  parser.add_option('--stdin', type="choice",
    choices = ['xml', 'trix', 'n3', 'nt', 'rdfa'],
    help = 'Parse RDF from STDIN (useful for piping) with given format')
  parser.add_option('-x', '--xml', action='append',
    help = 'Append to the list of RDF/XML documents to parse')
  parser.add_option('-t', '--trix', action='append',
    help = 'Append to the list of TriX documents to parse')
  parser.add_option('-n', '--n3', action='append',
    help = 'Append to the list of N3 documents to parse')
  parser.add_option('--nt', action='append',
    help = 'Append to the list of NT documents to parse')
  parser.add_option('-a', '--rdfa', action='append',
    help = 'Append to the list of RDFa documents to parse')

  parser.add_option('-o', '--output', type="choice",
    choices = ['n3', 'xml', 'pretty-xml', 'TriX', 'turtle', 'nt'],
    help = 'Format of the final serialized RDF graph')

  parser.add_option('-m', '--ns', action='append',
    help = 'Register a namespace binding (QName prefix to a base URI)')

  parser.add_option('-r', '--rules', action='append',
    help = 'Append to the list of fact files to use to perform reasoning')
  parser.add_option('-i', '--inferred',
    help = 'URI to use for the graph containing any inferred triples')

  parser.set_defaults(
      xml=[], trix=[], n3=[], nt=[], rdfa=[], ns=[],
      output='n3'
    )

  (options, args) = parser.parse_args()

  store = plugin.get(RDFLIB_STORE,Store)()        
  store.open(RDFLIB_CONNECTION)

  namespace_manager = NamespaceManager(Graph())
  for prefixDef in options.ns:
    prefix, uri = prefixDef.split('=')
    namespace_manager.bind(prefix, uri, override=False)    

  factGraph = ConjunctiveGraph(store) 
  for graphRef in options.xml:
    factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef),
                    format='xml')
  for graphRef in options.trix:
    factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef),
                    format='trix')
  for graphRef in options.n3:
    factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef),
                    format='n3')
  for graphRef in options.nt:
    factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef),
                    format='nt')
  for graphRef in options.rdfa:
    factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef),
                    format='rdfa')
  if options.stdin:
    factGraph.parse(sys.stdin, format=options.stdin)

  if options.inferred and len(options.rules) > 0:
    inferredURI = URIRef(options.inferred)
    ruleStore = N3RuleStore()
    ruleGraph = Graph(ruleStore)
    for ruleFile in options.rules:
      ruleGraph.parse(ruleFile, format='n3')
    tokenSet = generateTokenSet(factGraph)
    deltaGraph = Graph(store=factGraph.store,
                       identifier=inferredURI)
    network = ReteNetwork(ruleStore,
                          inferredTarget=deltaGraph)
    network.feedFactsToAdd(tokenSet)

  print factGraph.serialize(destination=None, format=options.output,
                            base=None)
  store.rollback()
Exemplo n.º 11
0
def main():
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option(
        '--stdin',
        type="choice",
        choices=['xml', 'trix', 'n3', 'nt', 'rdfa'],
        help='Parse RDF from STDIN (useful for piping) with given format')
    parser.add_option('-x',
                      '--xml',
                      action='append',
                      help='Append to the list of RDF/XML documents to parse')
    parser.add_option('-t',
                      '--trix',
                      action='append',
                      help='Append to the list of TriX documents to parse')
    parser.add_option('-n',
                      '--n3',
                      action='append',
                      help='Append to the list of N3 documents to parse')
    parser.add_option('--nt',
                      action='append',
                      help='Append to the list of NT documents to parse')
    parser.add_option('-a',
                      '--rdfa',
                      action='append',
                      help='Append to the list of RDFa documents to parse')

    parser.add_option(
        '-o',
        '--output',
        type="choice",
        choices=['n3', 'xml', 'pretty-xml', 'TriX', 'turtle', 'nt'],
        help='Format of the final serialized RDF graph')

    parser.add_option(
        '-m',
        '--ns',
        action='append',
        help='Register a namespace binding (QName prefix to a base URI)')

    parser.add_option(
        '-r',
        '--rules',
        action='append',
        help='Append to the list of fact files to use to perform reasoning')
    parser.add_option(
        '-i',
        '--inferred',
        help='URI to use for the graph containing any inferred triples')

    parser.set_defaults(xml=[],
                        trix=[],
                        n3=[],
                        nt=[],
                        rdfa=[],
                        ns=[],
                        output='n3')

    (options, args) = parser.parse_args()

    store = plugin.get(RDFLIB_STORE, Store)()
    store.open(RDFLIB_CONNECTION)

    namespace_manager = NamespaceManager(Graph())
    for prefixDef in options.ns:
        prefix, uri = prefixDef.split('=')
        namespace_manager.bind(prefix, uri, override=False)

    factGraph = ConjunctiveGraph(store)
    for graphRef in options.xml:
        factGraph.parse(graphRef,
                        publicID=Uri.OsPathToUri(graphRef),
                        format='xml')
    for graphRef in options.trix:
        factGraph.parse(graphRef,
                        publicID=Uri.OsPathToUri(graphRef),
                        format='trix')
    for graphRef in options.n3:
        factGraph.parse(graphRef,
                        publicID=Uri.OsPathToUri(graphRef),
                        format='n3')
    for graphRef in options.nt:
        factGraph.parse(graphRef,
                        publicID=Uri.OsPathToUri(graphRef),
                        format='nt')
    for graphRef in options.rdfa:
        factGraph.parse(graphRef,
                        publicID=Uri.OsPathToUri(graphRef),
                        format='rdfa')
    if options.stdin:
        factGraph.parse(sys.stdin, format=options.stdin)

    if options.inferred and len(options.rules) > 0:
        inferredURI = URIRef(options.inferred)
        ruleStore = N3RuleStore()
        ruleGraph = Graph(ruleStore)
        for ruleFile in options.rules:
            ruleGraph.parse(ruleFile, format='n3')
        tokenSet = generateTokenSet(factGraph)
        deltaGraph = Graph(store=factGraph.store, identifier=inferredURI)
        network = ReteNetwork(ruleStore, inferredTarget=deltaGraph)
        network.feedFactsToAdd(tokenSet)

    print factGraph.serialize(destination=None,
                              format=options.output,
                              base=None)
    store.rollback()
Exemplo n.º 12
0
 def __init__(self,ruleStore,name = None,
              initialWorkingMemory = None, 
              inferredTarget = None,
              nsMap = {},
              graphVizOutFile=None,
              dontFinalize=False,
              goal=None,
              rulePrioritizer=None,
              alphaNodePrioritizer=None):
     self.leanCheck = {}
     self.goal = goal        
     self.nsMap = nsMap
     self.name = name and name or BNode()
     self.nodes = {}
     self.alphaPatternHash = {}
     self.ruleSet = set()
     for alphaPattern in xcombine(('1','0'),('1','0'),('1','0')):
         self.alphaPatternHash[tuple(alphaPattern)] = {}
     if inferredTarget is None:
         self.inferredFacts = Graph()
         namespace_manager = NamespaceManager(self.inferredFacts)
         for k,v in nsMap.items():
             namespace_manager.bind(k, v)    
         self.inferredFacts.namespace_manager = namespace_manager    
     else:            
         self.inferredFacts = inferredTarget
     self.workingMemory = initialWorkingMemory and initialWorkingMemory or set()
     self.proofTracers = {}
     self.terminalNodes  = set()
     self.instanciations = {}        
     start = time.time()
     self.ruleStore=ruleStore
     self.justifications = {}
     self.dischargedBindings = {}
     if not dontFinalize:
         self.ruleStore._finalize()
     self.filteredFacts = Graph()
     self.rulePrioritizer      = rulePrioritizer
     self.alphaNodePrioritizer = alphaNodePrioritizer
     
     #'Universal truths' for a rule set are rules where the LHS is empty.  
     # Rather than automatically adding them to the working set, alpha nodes are 'notified'
     # of them, so they can be checked for while performing inter element tests.
     self.universalTruths = []
     from FuXi.Horn.HornRules import Ruleset
     self.rules=set()
     self.negRules = set()
     for rule in Ruleset(n3Rules=self.ruleStore.rules,nsMapping=self.nsMap):
         import warnings
         warnings.warn(
       "Rules in a network should be built *after* construction via "+
       " self.buildNetworkClause(HornFromN3(n3graph)) for instance",
                       DeprecationWarning,2)            
         self.buildNetworkFromClause(rule)
     self.alphaNodes = [node for node in self.nodes.values() if isinstance(node,AlphaNode)]
     self.alphaBuiltInNodes = [node for node in self.nodes.values() if isinstance(node,BuiltInAlphaNode)]
     self._setupDefaultRules()
     if initialWorkingMemory:            
         start = time.time()          
         self.feedFactsToAdd(initialWorkingMemory)
         print >>sys.stderr,"Time to calculate closure on working memory: %s m seconds"%((time.time() - start) * 1000)            
     if graphVizOutFile:
         print >>sys.stderr,"Writing out RETE network to ", graphVizOutFile
         renderNetwork(self,nsMap=nsMap).write(graphVizOutFile)
Exemplo n.º 13
0
def main():
    from optparse import OptionParser
    op = OptionParser('usage: %prog [options] factFile1 factFile2 ... factFileN')
    op.add_option('--closure', 
                  action='store_true',
                  default=False,
      help = 'Whether or not to serialize the inferred triples'+ 
             ' along with the original triples.  Otherwise '+
              '(the default behavior), serialize only the inferred triples')
    op.add_option('--output', 
                  default='n3',
                  metavar='RDF_FORMAT',
                  choices = ['xml', 
                             'TriX', 
                             'n3', 
                             'nt',
                             'rif',
                             'rif-xml',
                             'conflict',
                             'man-owl'],
      help = "Serialize the inferred triples and/or original RDF triples to STDOUT "+
             "using the specified RDF syntax ('xml','pretty-xml','nt','turtle', "+
             "or 'n3') or to print a summary of the conflict set (from the RETE "+
             "network) if the value of this option is 'conflict'.  If the the "+
             " value is 'rif' or 'rif-xml', Then the rules used for inference "+
             "will be serialized as RIF.  Finally if the value is 'man-owl', then "+
             "the RDF facts are assumed to be OWL/RDF and serialized via Manchester OWL "+
             "syntax.  The default is %default")
    op.add_option('--class',
                  dest='classes',
                  action='append',
                  default=[],
                  metavar='QNAME', 
      help = 'Used with --output=man-owl to determine which '+
             'classes within the entire OWL/RDF are targetted for serialization'+
             '.  Can be used more than once')
    op.add_option('--property',
                  action='append',
                  dest='properties',
                  default=[],
                  metavar='QNAME', 
      help = 'Used with --output=man-owl or --extract to determine which '+
             'properties are serialized / extracted.  Can be used more than once')
    op.add_option('--normalize', 
                  action='store_true',
                  default=False,
      help = "Used with --output=man-owl to attempt to determine if the ontology is 'normalized' [Rector, A. 2003]"+
      "The default is %default")
    op.add_option('--input-format', 
                  default='xml',
                  dest='inputFormat',
                  metavar='RDF_FORMAT',
                  choices = ['xml', 'trix', 'n3', 'nt', 'rdfa'],
      help = "The format of the RDF document(s) which serve as the initial facts "+
             " for the RETE network. One of 'xml','n3','trix', 'nt', "+
             "or 'rdfa'.  The default is %default")
    op.add_option('--pDSemantics', 
                  action='store_true',
                  default=False,
      help = 'Used with --dlp to add pD semantics ruleset for semantics not covered '+
      'by DLP but can be expressed in definite Datalog Logic Programming'+
      ' The default is %default')
    op.add_option('--stdin', 
                  action='store_true',
                  default=False,
      help = 'Parse STDIN as an RDF graph to contribute to the initial facts. The default is %default ')
    op.add_option('--ns', 
                  action='append',
                  default=[],
                  metavar="PREFIX=URI",
      help = 'Register a namespace binding (QName prefix to a base URI).  This '+
             'can be used more than once')
    op.add_option('--rules', 
                  default=[],
                  action='append',
                  default=[],
                  metavar='PATH_OR_URI',
      help = 'The Notation 3 documents to use as rulesets for the RETE network'+
      '.  Can be specified more than once')
    op.add_option('--ruleFacts', 
                  action='store_true',
                  default=False,
      help = "Determines whether or not to attempt to parse initial facts from "+
      "the rule graph.  The default is %default")
    op.add_option('--dlp', 
                  action='store_true',
                  default=False,
      help = 'Use Description Logic Programming (DLP) to extract rules from OWL/RDF.  The default is %default')
    (options, facts) = op.parse_args()
    
    nsBinds = {'iw':'http://inferenceweb.stanford.edu/2004/07/iw.owl#'}
    for nsBind in options.ns:
        pref,nsUri = nsBind.split('=')
        nsBinds[pref]=nsUri
    
    namespace_manager = NamespaceManager(Graph())
    factGraph = Graph() 
    ruleSet = Ruleset()

    for fileN in options.rules:
        if options.ruleFacts:
            factGraph.parse(fileN,format='n3')
            print >>sys.stderr,"Parsing RDF facts from ", fileN
        rs = HornFromN3(fileN)
        nsBinds.update(rs.nsMapping)
        ruleSet.formulae.extend(rs)
        #ruleGraph.parse(fileN,format='n3')
    ruleSet.nsMapping = nsBinds

    for prefix,uri in nsBinds.items():
        namespace_manager.bind(prefix, uri, override=False)
    closureDeltaGraph = Graph()
    closureDeltaGraph.namespace_manager = namespace_manager
    factGraph.namespace_manager = namespace_manager

    for fileN in facts:
        factGraph.parse(fileN,format=options.inputFormat)
        
    if options.stdin:
        factGraph.parse(sys.stdin,format=options.inputFormat)
                
    workingMemory = generateTokenSet(factGraph)

    rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
    network.inferredFacts = closureDeltaGraph
    network.nsMap = nsBinds
    
    if options.dlp:
        dlp=setupDescriptionLogicProgramming(factGraph,
                                             addPDSemantics=options.pDSemantics,
                                             constructNetwork=False)        
        ruleSet.formulae.extend(dlp)
    if options.output == 'rif':
         for rule in ruleSet:
             print rule
    elif options.output == 'man-owl':
        cGraph = network.closureGraph(factGraph,readOnly=False)
        cGraph.namespace_manager = namespace_manager
        Individual.factoryGraph = cGraph
        if options.classes:
            mapping = dict(namespace_manager.namespaces())
            for c in options.classes:
                pref,uri=c.split(':')
                print Class(URIRef(mapping[pref]+uri)).__repr__(True)
        elif options.properties:
            mapping = dict(namespace_manager.namespaces())
            for p in options.properties:
                pref,uri=p.split(':')
                print Property(URIRef(mapping[pref]+uri))
        else:
            for p in AllProperties(cGraph):
                print p.identifier
                print repr(p)
            for c in AllClasses(cGraph):
                if options.normalize:
                    if c.isPrimitive():
                        primAnc = [sc for sc in c.subClassOf if sc.isPrimitive()] 
                        if len(primAnc)>1:
                            warnings.warn("Branches of primitive skeleton taxonomy"+
                              " should form trees: %s has %s primitive parents: %s"%(
                             c.qname,len(primAnc),primAnc),UserWarning,1)
                        children = [desc for desc in c.subSumpteeIds()]
                        for child in children:
                            for otherChild in [o for o in children if o is not child]:
                                if not otherChild in [c.identifier 
                                          for c in Class(child).disjointWith]:# and\
                                    warnings.warn("Primitive children (of %s) "+
                                          "must be mutually disjoint: %s and %s"%(
                                      c.qname,
                                      Class(child).qname,
                                      Class(otherChild).qname),UserWarning,1)
                if not isinstance(c.identifier,BNode):
                    print c.__repr__(True)
    for rule in ruleSet:
        network.buildNetworkFromClause(rule)
    start = time.time()  
    network.feedFactsToAdd(workingMemory)
    sTime = time.time() - start
    if sTime > 1:
        sTimeStr = "%s seconds"%sTime
    else:
        sTime = sTime * 1000
        sTimeStr = "%s milli seconds"%sTime
    print >>sys.stderr,"Time to calculate closure on working memory: ",sTimeStr
    print >>sys.stderr, network
    if options.output == 'conflict':
        network.reportConflictSet()
    elif options.output not in ['rif','rif-xml','man-owl']:
        if options.closure:
            cGraph = network.closureGraph(factGraph)
            cGraph.namespace_manager = namespace_manager
            print cGraph.serialize(destination=None, 
                                   format=options.output, 
                                   base=None)
        else:
            print network.inferredFacts.serialize(destination=None, 
                                                  format=options.output, 
                                                  base=None)
Exemplo n.º 14
0
    if not options.sparqlEndpoint and facts:
        for pref, uri in factGraph.namespaces():
            nsBinds[pref] = uri

    if options.stdin:
        assert not options.sparqlEndpoint, "Cannot use --stdin with --sparqlEndpoint"
        factGraph.parse(sys.stdin, format=options.inputFormat)

    # Normalize namespace mappings
    # prune redundant, rdflib-allocated namespace prefix mappings
    newNsMgr = NamespaceManager(factGraph)
    from FuXi.Rete.Util import CollapseDictionary

    for k, v in CollapseDictionary(dict([(k, v) for k, v in factGraph.namespaces()])).items():
        newNsMgr.bind(k, v)
    factGraph.namespace_manager = newNsMgr

    if options.normalForm:
        NormalFormReduction(factGraph)

    if not options.sparqlEndpoint:
        workingMemory = generateTokenSet(factGraph)
    if options.builtins:
        import imp

        userFuncs = imp.load_source("builtins", options.builtins)
        rule_store, rule_graph, network = SetupRuleStore(
            makeNetwork=True, additionalBuiltins=userFuncs.ADDITIONAL_FILTERS
        )
    else:
Exemplo n.º 15
0
    if not options.sparqlEndpoint and facts:
        for pref, uri in factGraph.namespaces():
            nsBinds[pref] = uri

    if options.stdin:
        assert not options.sparqlEndpoint, "Cannot use --stdin with --sparqlEndpoint"
        factGraph.parse(sys.stdin, format=options.inputFormat)

    #Normalize namespace mappings
    #prune redundant, rdflib-allocated namespace prefix mappings
    newNsMgr = NamespaceManager(factGraph)
    from FuXi.Rete.Util import CollapseDictionary
    for k, v in CollapseDictionary(
            dict([(k, v) for k, v in factGraph.namespaces()])).items():
        newNsMgr.bind(k, v)
    factGraph.namespace_manager = newNsMgr

    if options.normalForm:
        NormalFormReduction(factGraph)

    if not options.sparqlEndpoint and options.naive:
        workingMemory = generateTokenSet(factGraph)
    if options.builtins:
        import imp
        userFuncs = imp.load_source('builtins', options.builtins)
        rule_store, rule_graph, network = SetupRuleStore(
            makeNetwork=True, additionalBuiltins=userFuncs.ADDITIONAL_FILTERS)
    else:
        rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
    network.inferredFacts = closureDeltaGraph
Exemplo n.º 16
0
        elif o == '--stdin':
            stdIn = True
        elif o == '--output':
            outMode = a
        elif o == '--ns':            
            pref,nsUri = a.split('=')
            nsBinds[pref]=nsUri
        elif o == "--input":
            factGraphs = a.split(',')
        elif o == "--help":
            usage()
            sys.exit()
        
    store = plugin.get(RDFLIB_STORE,Store)()        
    store.open(RDFLIB_CONNECTION)
    namespace_manager = NamespaceManager(Graph())
    for prefix,uri in nsBinds.items():
        namespace_manager.bind(prefix, uri, override=False)    
    factGraph = Graph(store) 
    factGraph.namespace_manager = namespace_manager
    if factGraphs:
        for fileN in factGraphs:
            factGraph.parse(fileN,format=factFormat)
    if stdIn:
        factGraph.parse(sys.stdin,format=factFormat)
    print factGraph.serialize(destination=None, format=outMode, base=None)
    store.rollback()

if __name__ == "__main__":
    main()
Exemplo n.º 17
0
def process(action, processor, manifest, project, tester,localRun = False):
    data = Graph()
    data.parse(manifest)

    results = Graph()
    abbr = NamespaceManager(results)
    abbr.bind("foaf", "http://xmlns.com/foaf/0.1/")
    abbr.bind("earl", "http://www.w3.org/ns/earl#")
    if project:
        try:
            results.parse(project)
        except IOError:
            pass
        project = URIRef(project)
    else:
        project = BNode()
    if tester:
        try:
            results.parse(tester)
        except IOError:
            pass
        tester = URIRef(tester)
    else:
        tester = BNode()
    
    nsMapping = {
        u'test':Namespace('http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#'),
        u'dc':DC_NS
    }
    GRDDL_TEST_NS = Namespace('http://www.w3.org/2001/sw/grddl-wg/td/grddl-test-vocabulary#')
    failures = set()
    reportMap = {}
    for descr,test,input,output in data.query("SELECT ?desc ?t ?i ?o WHERE { ?t test:inputDocument ?i. ?t a test:Test . ?t dc:title ?desc. ?t test:outputDocument ?o }",initNs=nsMapping):
        if DEBUG:
            print >>sys.stderr, "###", descr, "###"
            print >>sys.stderr, "\t",input
        if localRun and (test,RDF.type,GRDDL_TEST_NS.NetworkedTest) in data:
            if DEBUG:
                print >>sys.stderr, "\t Skipping networked test (local run only)"
            continue
        if action=="update":
            updateTest(processor,input,output)
        elif action=="run":
            r = runTest(processor,input,output)
            reportMap[test] = (results,tester,project,r)
            if not r:
                failures.add(test)

    hasFailure = False
    TESTING = Namespace('http://www.w3.org/2001/sw/grddl-wg/td/grddl-test-vocabulary#')
    passedByProxy = set()
    if len(failures) > 0:
        for failure in failures:
            #@@ Test subsumption RDF not currently generated by aboutTests.xsl
            subsuming = set(data.transitive_subjects(TESTING.subsumes, failure))
            s = subsuming.difference(failures)
            if len(s) > 0:
                print >>sys.stderr, "* %s failed, but subsuming test %s passed" \
                                    % (failure, list(s)[0])
                passedByProxy.add(failure)
                continue

            subsumes = set(data.transitive_objects(failure, TESTING.subsumes))
            s = subsumes.difference(failures)
            if len(s) > 0:
                print >>sys.stderr, "* %s failed, but subsumed test %s passed" \
                                    % (failure, list(s)[0])
                passedByProxy.add(failure)
                continue

            alternates = set(data.transitive_objects(
                               failure, TESTING.alternative)).union(
                         set(data.transitive_subjects(
                               TESTING.alternative, failure)))
            s = alternates.difference(failures)
            if len(s) > 0:
                print >>sys.stderr, "* %s failed, but alternate test %s passed" \
                                    % (failure, list(s)[0])
                passedByProxy.add(failure)
            else:
                print >>sys.stderr, "* %s failed" % failure
                hasFailure = True
    failures = failures.difference(passedByProxy)
    if not hasFailure and action=="run":
        print >>sys.stderr, "All tests were passed (or had an alternate test pass)!"
    for test,(results,tester,project,r) in reportMap.items():
        reportEarl(results, tester, project, test,
                   test in passedByProxy, test not in failures)
    return results