Extracts INDRA statements from the RDF graph via SPARQL queries.
11433
def extract_statements(self):
        

        # Look for events that have an AGENT and an AFFECTED, and get the
        # start and ending text indices for each.
        query = prefixes + """
        SELECT
            ?agent_start
            ?agent_end
            ?affected_start
            ?affected_end
        WHERE {
            ?rel role:AGENT ?agent .
            ?rel role:AFFECTED ?affected .
            ?agent lf:start ?agent_start .
            ?agent lf:end ?agent_end .
            ?affected lf:start ?affected_start .
            ?affected lf:end ?affected_end .
        }
        """
        results = self.graph.query(query)
        for res in results:
            # Make a statement for each query match
            self.extract_statement_from_query_result(res)

        # Look for events that have an AGENT and a RESULT, and get the start
        # and ending text indices for each.
        query = query.replace('role:AFFECTED', 'role:RESULT')
        results = self.graph.query(query)
        for res in results:
            # Make a statement for each query match
            self.extract_statement_from_query_result(res)
1200086
def parse_rdf_payload(self, data, headers):

		'''
		
		'''

		# handle edge case for content-types not recognized by rdflib parser
		if headers['Content-Type'].startswith('text/plain'):
			logger.debug('text/plain Content-Type detected, using application/n-triples for parser')
			parse_format = 'application/n-triples'
		else:
			parse_format = headers['Content-Type']

		# clean parse format for rdf parser (see: https://www.w3.org/2008/01/rdf-media-types)
		if ';charset' in parse_format:
			parse_format = parse_format.split(';')[0]

		# parse graph
		graph = rdflib.Graph().parse(
			data=data.decode('utf-8'),
			format=parse_format)

		# return graph
		return graph
284043
def process_rdfgraph(self, rg, ont=None):
        
        # TODO: ontology metadata
        if ont is None:
            ont = Ontology()
            subjs = list(rg.subjects(RDF.type, SKOS.ConceptScheme))
            if len(subjs) == 0:
                logging.warning("No ConceptScheme")
            else:
                ont.id = self._uri2id(subjs[0])
            
        subset_map = {}
        for concept in rg.subjects(RDF.type, SKOS.Concept):
            for s in self._get_schemes(rg, concept):
                subset_map[self._uri2id(s)] = s
                
        for concept in sorted(list(rg.subjects(RDF.type, SKOS.Concept))):
            concept_uri = str(concept)
            id=self._uri2id(concept)
            logging.info("ADDING: {}".format(id))
            ont.add_node(id, self._get_label(rg,concept))
                    
            for defn in rg.objects(concept, SKOS.definition):
                if (defn.language == self.lang):
                    td = TextDefinition(id, escape_value(defn.value))
                    ont.add_text_definition(td)
                    
            for s in rg.objects(concept, SKOS.broader):
                ont.add_parent(id, self._uri2id(s))
                
            for s in rg.objects(concept, SKOS.related):
                ont.add_parent(id, self._uri2id(s), self._uri2id(SKOS.related))
                
            for m in rg.objects(concept, SKOS.exactMatch):
                ont.add_xref(id, self._uri2id(m))
                
            for m in rg.objects(concept, SKOS.altLabel):
                syn = Synonym(id, val=self._uri2id(m))
                ont.add_synonym(syn)
                
            for s in self._get_schemes(rg,concept):
                ont.add_to_subset(id, self._uri2id(s))
                
        return ont
395171
protected void readEntityIntoDataset(final IRI graphName, final RDFSyntax syntax,
            final TrellisDataset dataset) {
        try (final InputStream input = entity) {
            getServices().getIOService().read(input, syntax, getIdentifier())
                .map(skolemizeTriples(getServices().getResourceService(), getBaseUrl()))
                .filter(triple -> !RDF.type.equals(triple.getPredicate())
                        || !triple.getObject().ntriplesString().startsWith("<" + LDP.getNamespace()))
                .filter(triple -> !LDP.contains.equals(triple.getPredicate())).map(triple ->
                        rdf.createQuad(graphName, triple.getSubject(), triple.getPredicate(), triple.getObject()))
                .forEachOrdered(dataset::add);
        } catch (final RuntimeTrellisException ex) {
            throw new BadRequestException("Invalid RDF content: " + ex.getMessage());
        } catch (final IOException ex) {
            throw new WebApplicationException("Error processing input: " + ex.getMessage());
        }
    }
1184906
def transform_sparql_update(rdf, update_query):
    """"""

    logging.debug("performing SPARQL Update transformation")

    if update_query[0] == '@':  # actual query should be read from file
        update_query = file(update_query[1:]).read()

    logging.debug("update query: %s", update_query)
    rdf.update(update_query)
409856
def getPropAllSupers(self, aURI):
        
        aURI = aURI
        try:
            qres = self.rdfgraph.query(
                  """SELECT DISTINCT ?x
                     WHERE {
                         { <%s> rdfs:subPropertyOf+ ?x }
                         FILTER (!isBlank(?x))
                     }
                     """ % (aURI))
        except:
            printDebug("... warning: the 'getPropAllSupers' query failed (maybe missing SPARQL 1.1 support?)")
            qres = []
        return list(qres)
1301139
def parse_object_like_triples(self):

		'''
		
		'''

		# parse triples as object-like attributes in self.rdf.triples
		self.rdf.triples = SimpleNamespace() # prepare triples
		for s,p,o in self.rdf.graph:

			# get ns info
			ns_prefix, ns_uri, predicate = self.rdf.graph.compute_qname(p)

			# if prefix as list not yet added, add
			if not hasattr(self.rdf.triples, ns_prefix):
				setattr(self.rdf.triples, ns_prefix, SimpleNamespace())

			# same for predicate
			if not hasattr(getattr(self.rdf.triples, ns_prefix), predicate):
				setattr(getattr(self.rdf.triples, ns_prefix), predicate, [])

			# append object for this prefix
			getattr(getattr(self.rdf.triples, ns_prefix), predicate).append(o)
179915
def _get_types_from_sample(result_vars, sparql_results_json):
    
    total_bindings = len(sparql_results_json['results']['bindings'])
    homogeneous_types = {}
    for result_var in result_vars:
        var_types = set()
        var_datatypes = set()
        for i in range(0, min(total_bindings, 10)):
            binding = sparql_results_json['results']['bindings'][i]
            rdf_term = binding.get(result_var)
            if rdf_term is not None:  # skip missing values
                var_types.add(rdf_term.get('type'))
                var_datatypes.add(rdf_term.get('datatype'))
        if len(var_types) > 1 or len(var_datatypes) > 1:
            return None  # Heterogeneous types
        else:
            homogeneous_types[result_var] = {
                'type': var_types.pop() if var_types else None,
                'datatype': var_datatypes.pop() if var_datatypes else None
            }

    return homogeneous_types
604352
def entityTriples(rdfGraph, anEntity, excludeProps=False, excludeBNodes=False,
                  orderProps=[RDF, RDFS, OWL.OWLNS, DC.DCNS]):
    
    temp = []
    if not excludeProps:
        excludeProps = []

    # extract predicate/object
    for x, y, z in rdfGraph.triples((anEntity, None, None)):
        if excludeBNodes and isBlankNode(z):
            continue
        if y not in excludeProps:
            temp += [(y, z)]

    # sorting
    if type(orderProps) == type([]):
        orderedUris = sortByNamespacePrefix([y for y, z in temp], orderProps)  # order props only
        orderedUris = [(n + 1, x) for n, x in
                       enumerate(orderedUris)]  # create form: [(1, 'something'),(2,'bobby'),(3,'suzy'),(4,'crab')]
        rank = dict((key, rank) for (rank, key) in orderedUris)  # create dict to pass to sorted procedure
        temp = sorted(temp, key=lambda tup: rank.get(tup[0]))
    elif orderProps:  # default to alpha sorting unless False
        temp = sorted(temp, key=lambda tup: tup[0])

    # if niceURI:
    #	temp = [(uri2niceString(ontology, y), z) for y,z in temp]

    return temp
53878
def serialize_rdf(update_graph, signing):
    '''
    '''
    unsorted_s = update_graph.serialize(format = 'pretty-xml')
    unsorted_s = unsorted_s.replace('xmlns:rdf', 'xmlns:RDF')
    unsorted_s = unsorted_s.replace('rdf:', 'RDF:')
    unsorted_s = unsorted_s.replace('RDF:about', 'about')
    unsorted_s = unsorted_s.split('\n')
    start, end = unsorted_s[0:5], unsorted_s[-2:]
    unsorted_s = unsorted_s[5:-2]
    if signing:
        unsorted_s = [line[2:] for line in unsorted_s]
        unsorted_s.append('')
    sorting_s = []
    prev_leading_spaces = -2
    for line in unsorted_s:
        leading_spaces = len(line) - len(line.lstrip())
        if leading_spaces > prev_leading_spaces:
            sorting_s.append([line])
        elif leading_spaces == prev_leading_spaces:
            sorting_s[-1].append(line)
        elif leading_spaces < prev_leading_spaces:
            tmp_line = sorting_s.pop()
            tmp_line = '\n'.join(sorted(tmp_line))
            tmp_line = [sorting_s[-1].pop(), tmp_line, line]
            tmp_line = '\n'.join(tmp_line)
            sorting_s[-1].append(tmp_line)
        prev_leading_spaces = leading_spaces
    if signing:
        sorted_s = '\n'.join(sorting_s[0])
    else:
        sorted_s = []
        sorted_s.extend(start)
        sorted_s.extend(sorting_s[0])
        sorted_s.extend(end)
        sorted_s = '\n'.join(sorted_s)
    return sorted_s
1344855
private OntModel getGraphSparqlQuery(URI graphUri) {

        StringBuilder queryStr = new StringBuilder("CONSTRUCT { ?s ?p ?o } \n")
                .append("WHERE {\n");

        // Add named graph if necessary
        if (graphUri != null) {
            queryStr.append("GRAPH <").append(graphUri.toASCIIString()).append("> ");
        }
        queryStr.append("{ ?s ?p ?o } } \n");

        log.debug("Querying graph store: {}", queryStr.toString());

        Query query = QueryFactory.create(queryStr.toString());
        QueryExecution qe = QueryExecutionFactory.sparqlService(this.getSparqlQueryEndpoint().toASCIIString(), query);
        MonitoredQueryExecution qexec = new MonitoredQueryExecution(qe);
        // Note that we are not using the store specification here to avoid retrieving remote models
//        OntModel resultModel = ModelFactory.createOntologyModel();
        OntModel resultModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
        try {
            qexec.execConstruct(resultModel);
            return resultModel;
        } finally {
            qexec.close();
        }
    }
1352823
def _resources_from_graph(graph, uris_and_graphs)
      repo = add_data_to_repository(graph)
      resources = []

      # TODO: ? if uris_and_graphs not passed in, we could get the
      # uris from the graph, and just not create the resoruces with a graph
      # (but they won't be persistable).

      uris_and_graphs.each do |(u,g)|

        # instantiate a new resource
        g ||= {}
        r = self.new(u, g)

        # make a graph of data for this resource's uri
        data_graph = RDF::Graph.new
        repo.query( [RDF::URI.new(u), :predicate, :object] ) do |statement|
          data_graph << statement

          if statement.object.is_a? RDF::Node
            repo.query( [statement.object, :predicate, :object] ) {|s| data_graph << s}
          end
        end

        # use it to hydrate this resource
        r.hydrate!(:graph => data_graph)
        r.new_record = false
        resources << r
      end

      resources
    end
706335
def getPropAllSubs(self, aURI):
        
        aURI = aURI
        try:
            qres = self.rdfgraph.query(
                  """SELECT DISTINCT ?x
                     WHERE {
                         { ?x rdfs:subPropertyOf+ <%s> }
                         FILTER (!isBlank(?x))
                     }
                     """ % (aURI))
        except:
            printDebug("... warning: the 'getPropAllSubs' query failed (maybe missing SPARQL 1.1 support?)")
            qres = []
        return list(qres)
1290241
def __extract_patterns_and_spaces(self):
        

        def __decorate_nodes(nodes, space):
            """
            Performs a backward search from a list of pattern nodes and assigns a set of search spaces
            to all encountered nodes.
            :param nodes: List of pattern nodes that belongs to a search space
            :param space: List of  search space id
            :return:
            """
            for n in nodes:
                if n not in self.__node_spaces:
                    self.__node_spaces[n] = set([])
                self.__node_spaces[n].add(space)
                pred_nodes = self.__plan_graph.subjects(AGORA.next, n)
                __decorate_nodes(pred_nodes, space)

        # Extract all search spaces in the plan and build a dictionary of subjects-to-ignore per each of them.
        # Ignored subjects are those that won't be dereferenced due to a explicit graph pattern (object) filter,
        # e.g. ?s doap:name "jenkins" -> All ?s that don't match the filter will be ignored.
        self.__spaces = set(self.__plan_graph.subjects(RDF.type, AGORA.SearchSpace))
        self.__subjects_to_ignore = dict([(sp, set([])) for sp in self.__spaces])

        patterns = list(self.__plan_graph.subjects(RDF.type, AGORA.TriplePattern))
        for tp in patterns:
            # A triple pattern belongs to a UNIQUE search space
            space = list(self.__plan_graph.subjects(AGORA.definedBy, tp)).pop()
            self.__patterns[tp] = {'space': space}

            # Depending on the format of each triple pattern (either '?s a Concept' or '?s prop O'),
            # it is required to extract different properties.
            tp_pred = list(self.__plan_graph.objects(tp, predicate=AGORA.predicate)).pop()

            if tp_pred == RDF.type:  # ?s a Concept
                self.__patterns[tp]['type'] = list(self.__plan_graph.objects(tp, predicate=AGORA.object)).pop()
                try:
                    check_type = list(self.__plan_graph.objects(tp, predicate=AGORA.checkType)).pop().toPython()
                except IndexError:
                    check_type = True
                self.__patterns[tp]['check'] = check_type
            else:  # ?s prop O
                self.__patterns[tp]['property'] = tp_pred
                tp_obj = list(self.__plan_graph.objects(tp, predicate=AGORA.object)).pop()
                if (tp_obj, RDF.type, AGORA.Literal) in self.__plan_graph:  # In case O is a Literal
                    self.__patterns[tp]['filter_object'] = list(self.__plan_graph.objects(tp_obj, AGORA.value)).pop()
                elif isinstance(tp_obj, URIRef):
                    self.__patterns[tp]['filter_object'] = tp_obj

                tp_sub = list(self.__plan_graph.objects(tp, predicate=AGORA.subject)).pop()
                if isinstance(tp_sub, URIRef):
                    self.__patterns[tp]['filter_subject'] = tp_sub

            # Get all pattern nodes (those that have a byPattern properties) of the search plan and search backwards
            # in order to set the scope of each search space.
            nodes = list(self.__plan_graph.subjects(AGORA.byPattern, tp))
            for n in nodes:
                if n not in self.__node_patterns:
                    self.__node_patterns[n] = set([])
                self.__node_patterns[n].add(tp)
            __decorate_nodes(nodes, space)
244029
def get_example_extractions(fname):
    ""
    with open(fname, 'r') as f:
        sentences = f.read().splitlines()
    rdf_xml_dict = {}
    for sentence in sentences:
        logger.info("Reading \"%s\"..." % sentence)
        html = tc.send_query(sentence, 'cwms')
        try:
            rdf_xml_dict[sentence] = tc.get_xml(html, 'rdf:RDF',
                                                fail_if_empty=True)
        except AssertionError as e:
            logger.error("Got error for %s." % sentence)
            logger.exception(e)
    return rdf_xml_dict
221693
def GetArtifactParserDependencies(rdf_artifact):
  
  deps = set()
  processors = parser.Parser.GetClassesByArtifact(rdf_artifact.name)
  for p in processors:
    deps.update(p.knowledgebase_dependencies)
  return deps
955710
def get_enumeration_sparql(rq, v, endpoint, auth=None):
    
    glogger.info('Retrieving enumeration for variable {}'.format(v))
    vcodes = []
    # tpattern_matcher = re.compile(".*(FROM\s+)?(?P<gnames>.*)\s+WHERE.*[\.\{][\n\t\s]*(?P<tpattern>.*\?" + re.escape(v) + ".*?\.).*", flags=re.DOTALL)
    # tpattern_matcher = re.compile(".*?((FROM\s*)(?P<gnames>(\<.*\>)+))?\s*WHERE\s*\{(?P<tpattern>.*)\}.*", flags=re.DOTALL)

    # WHERE is optional too!!
    tpattern_matcher = re.compile(".*?(FROM\s*(?P<gnames>\<.*\>+))?\s*(WHERE\s*)?\{(?P<tpattern>.*)\}.*",
                                  flags=re.DOTALL)

    glogger.debug(rq)
    tp_match = tpattern_matcher.match(rq)
    if tp_match:
        vtpattern = tp_match.group('tpattern')
        gnames = tp_match.group('gnames')
        glogger.debug("Detected graph names: {}".format(gnames))
        glogger.debug("Detected BGP: {}".format(vtpattern))
        glogger.debug("Matched triple pattern with parameter")
        if gnames:
            codes_subquery = re.sub("SELECT.*\{.*\}.*",
                                    "SELECT DISTINCT ?" + v + " FROM " + gnames + " WHERE { " + vtpattern + " }", rq,
                                    flags=re.DOTALL)
        else:
            codes_subquery = re.sub("SELECT.*\{.*\}.*",
                                    "SELECT DISTINCT ?" + v + " WHERE { " + vtpattern + " }", rq,
                                    flags=re.DOTALL)
        glogger.debug("Codes subquery: {}".format(codes_subquery))
        glogger.debug(endpoint)
        codes_json = requests.get(endpoint, params={'query': codes_subquery},
                                  headers={'Accept': static.mimetypes['json'],
                                           'Authorization': 'token {}'.format(static.ACCESS_TOKEN)}, auth=auth).json()
        for code in codes_json['results']['bindings']:
            vcodes.append(list(code.values())[0]["value"])
    else:
        glogger.debug("No match between variable name and query.")

    return vcodes