def init_state(self, computed_trace, params, cstate, diag): """I implement :meth:`.abstract.AbstractMonosourceMethod.init_state """ domains = {} src_model = computed_trace.source_traces[0].model for atype in src_model.iter_attribute_types(): dtypes = atype.data_types if len(dtypes) == 1: domains[unicode(atype.uri)] = dtypes[0] rules = params['rules'] bgps = [] for rulepos, rule in enumerate(rules): if not rule.get('visible', True): continue new_type = rule["id"] for subrule in rule['rules']: rank = 0 bgp = [] old_type = subrule.get("type", "") if old_type: rank += 1000000 bgp.append("?obs a <%s>." % old_type) for attno, att in enumerate(subrule.get("attributes", ())): rank += 1000 if isinstance(att['value'], unicode): dtype = domains.get(att['uri'], XSD.string) value = Literal(att['value'], datatype=dtype) else: dtype = att['value'].get('@datatype', XSD.string) value = Literal(att['value']['@value'], datatype=dtype) att['sparql_value'] = value.n3() if att['operator'] == '==': att['sparql_op'] = '=' else: att['sparql_op'] = att['operator'] att['var'] = '?att%s' % attno bgp.append( '?obs <%(uri)s> %(var)s. ' 'FILTER(%(var)s %(sparql_op)s %(sparql_value)s).' % att) bgp = "".join(bgp) rank -= rulepos bgps.append([rank, new_type, bgp]) bgps.sort(reverse=True) cstate.update([ ("bgps", bgps), ("last_seen_u", None), ("last_seen_b", None), ])
def change_label(self, entity_uri, label): entity_uri = URIRef(entity_uri) label = Literal(label) self.graph.remove((entity_uri, RDFS.label, None)) self.graph.add((entity_uri, RDFS.label, label)) db = get_db() db.execute( 'UPDATE knowledge SET object = ? ' ' WHERE subgraph_id = ? AND subject = ? AND predicate = ?', (label.n3(), self.id, entity_uri.n3(), RDFS.label.n3())) db.commit() self.root = None # forces a rebuild of the root entity
def new_value(self, entity_uri, property_uri): if type(entity_uri) == str: entity_uri = URIRef(entity_uri) if type(property_uri) == str: property_uri = URIRef(property_uri) index = self.get_property_free_index(entity_uri, property_uri) value = Literal('') self.graph.add((entity_uri, property_uri, value)) self.properties[(entity_uri, property_uri)][index] = value db = get_db() db.execute( 'INSERT INTO knowledge (subgraph_id, subject, predicate, object, property_index) VALUES (?, ?, ?, ?, ?)', (self.id, entity_uri.n3(), property_uri.n3(), value.n3(), index)) db.commit() self.root = None # forces a rebuild of the root entity return index
# the Free Software Foundation; either version 3, or (at your option) # any later version. # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with n3; see the file LICENSE. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301 USA, # Serializing a single term to N3 from rdflib import Graph, URIRef, Literal, BNode from rdflib.namespace import FOAF, NamespaceManager person = URIRef('http://xmlns.com/foaf/0.1/Person') print(person.n3()) g = Graph() print(g.bind("foaf", FOAF)) print(person.n3(g.namespace_manager)) l = Literal(2) print(l.n3()) print(l.n3(g.namespace_manager))
def test_issue_939(self): lit = Literal(Fraction('2/3')) assert lit.datatype == URIRef('http://www.w3.org/2002/07/owl#rational') assert lit.n3() == '"2/3"^^<http://www.w3.org/2002/07/owl#rational>'