예제 #1
0
    def determine_remap_target(self,g,c,s,var_bindings):
        full_path = None

        if type(s) == Literal: return None
        node_type = get_property(g, s, rdf.type)                
        subject_uri = str(s)
        
        if type(s) == BNode:
            assert node_type != None, "%s is a bnode with no type"%s.n3()
            t = RecordObject[node_type]
            if (t.path == None): return None

        elif type(s) == URIRef:
            if subject_uri.startswith("urn:smart_external_id:"):
                full_path = self.internal_id(c, s)
                assert full_path or node_type != None, "%s is a new external URI node with no type"%s.n3()
                if full_path == None:
                    t = RecordObject[node_type]
                    assert t.path != None, "Non-gettable type %s shouldn't be a URI node."%t
            elif subject_uri.startswith(smart_path("")):
                return None
            else:
                self.assert_never_a_subject(g,s)
                return None
                    
        # If we got here, we need to remap the node "s".
        if full_path == None:
            full_path = t.determine_full_path(var_bindings)
        return full_path
예제 #2
0
def augment_data(g, var_bindings, new_nodes):
    # Quick and dirty CodedValue augmentation to start. 
    # TODO: generalize this mechanism to allow plug-in extensibility!
    
    code_q = """
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX sp: <http://smartplatforms.org/terms#>
            SELECT ?c
            WHERE { ?cv rdf:type sp:CodedValue .
                    ?cv sp:code ?c. }"""
        
    codes = set(g.query(code_q))
    print "queried ", code_q, codes
    codes = filter(lambda s: type(s) == URIRef and \
                           not str(s).startswith("urn:smart_external_id:") and \
                       not str(s).startswith(smart_path("")), codes)
    
    # For any URI nodes referencing external vocabularies...
    for c in codes:
        augment_code_uri(g,c)


    # Attach each data element (med, problem, lab, etc), to the 
    # base record URI with the sp:hasDataElement predicate.
    recordURI = URIRef(smart_path("/records/%s"%var_bindings['record_id']))
    for n in new_nodes:
        node_type = get_property(g, n, rdf.type)

        # Make sure this is a "medical data element" type
        t = ontology[node_type]
        if (t.base_path == None): continue
        if (not t.base_path.startswith("/records")): continue
        if (n == recordURI): continue # don't assert that the record has itself as an element

        g.add((recordURI, sp.hasMedicalDataElement, n))
        g.add((recordURI, rdf.type, sp.MedicalRecord))