@Override public String asSparqlTerm(PrefixLogger pl) { String lang = (language.equals("none") ? "" : language); RDFDatatype dt = datatype.length() == 0 ? null : TypeMapper.getInstance().getSafeTypeByName(datatype); Node n = NodeFactory.createLiteral(spelling, lang, dt); if (datatype.length() > 0) pl.present(datatype); String lf = FmtUtils.stringForNode(n, RDFUtils.noPrefixes); return lf; }
protected Literal jenaLiteralFromDataPropertyStatement( DataPropertyStatement dataPropertyStatement, OntModel ontModel) { Literal l = null; if ((dataPropertyStatement.getLanguage()) != null && (dataPropertyStatement.getLanguage().length() > 0)) { l = ontModel.createLiteral( dataPropertyStatement.getData(), dataPropertyStatement.getLanguage()); } else if ((dataPropertyStatement.getDatatypeURI() != null) && (dataPropertyStatement.getDatatypeURI().length() > 0)) { l = ontModel.createTypedLiteral( dataPropertyStatement.getData(), TypeMapper.getInstance().getSafeTypeByName(dataPropertyStatement.getDatatypeURI())); } else { l = ontModel.createLiteral(dataPropertyStatement.getData()); } return l; }
/** * Convert a Mulgara Value to a Jena graph node. * * @param obj The Mulgara value to convert. * @return A new Jena graph node. */ static Node o2n(org.jrdf.graph.Node obj) { if (obj == null) return Node.ANY; // testing for org.jrdf.graph.URIReference if (obj.isURIReference()) { URIReference uRef = (URIReference) obj; if (skolemizedBlankNodes) { String x = uRef.getURI().toString(); if (x.startsWith(bNodeScheme)) { x = x.substring(bNodeScheme.length()); Node n = Node.createAnon(new AnonId(x)); return n; } } return Node.createURI(uRef.getURI().toString()); } // testing for org.jrdf.graph.Literal if (obj.isLiteral()) { Literal literal = (Literal) obj; if (literal.getDatatypeURI() != null) { RDFDatatype type = TypeMapper.getInstance().getSafeTypeByName(literal.getDatatypeURI().toString()); return Node.createLiteral(literal.getLexicalForm(), null, type); } return Node.createLiteral(literal.getLexicalForm(), literal.getLanguage(), null); } if (obj.isBlankNode()) { BlankNodeImpl b = (BlankNodeImpl) obj; // check if this was a Jena-allocated node Node jenaNode = valuesToNodes.get(b); // if not known, then create a Jena node from the Mulgara ID return jenaNode != null ? jenaNode : Node.createAnon(new AnonId(b.getID())); } throw new RuntimeException("Can't convert to Jena Node : " + obj); }
public static synchronized Literal getLiteralMatchingDataPropertyRange( OntModel m, String rnguri, Object v) throws Exception { Literal val = null; String errMsg = null; RDFDatatype rdftype = TypeMapper.getInstance().getSafeTypeByName(rnguri); if (rdftype != null && !rdftype.getURI().equals(XSD.xboolean.getURI()) && !rdftype.getURI().equals(XSD.date.getURI()) && !rdftype.getURI().equals(XSD.dateTime.getURI()) && !rdftype.getURI().equals(XSD.time.getURI())) { val = m.createTypedLiteral(v, rdftype); if (val != null) { return val; } } if (rnguri != null) { if (rnguri.contains("float")) { if (v instanceof String) { v = Double.parseDouble(stripQuotes((String) v)); } if (v instanceof Double) { v = new Float(((Double) v).floatValue()); val = m.createTypedLiteral(v); } else if (v instanceof Float) { val = m.createTypedLiteral(v); } else if (v instanceof Integer) { v = new Float(((Integer) v).floatValue()); val = m.createTypedLiteral(v); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range float"; } } else if (rnguri.contains("double")) { if (v instanceof String) { v = Double.parseDouble(stripQuotes((String) v)); } if (v instanceof Double) { val = m.createTypedLiteral(v); } else if (v instanceof Float) { v = new Double(((Float) v).doubleValue()); val = m.createTypedLiteral(v); } else if (v instanceof Integer) { v = new Double(((Integer) v).doubleValue()); val = m.createTypedLiteral(v); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range double"; } } else if (rnguri.contains("decimal")) { if (v instanceof String) { v = Double.parseDouble(stripQuotes((String) v)); } if (v instanceof Double) { v = new BigDecimal(((Double) v).doubleValue()); } else if (v instanceof Float) { v = new BigDecimal(((Float) v).doubleValue()); } else if (v instanceof Integer) { v = new BigDecimal(((Integer) v).doubleValue()); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range decimal"; } val = m.createTypedLiteral(v); } else if (rnguri.contains("int")) { if (v instanceof String) { v = Integer.parseInt(stripQuotes((String) v)); } if (v instanceof Integer) { val = m.createTypedLiteral(v); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range int"; } } else if (rnguri.contains("long")) { if (v instanceof String) { v = Long.parseLong(stripQuotes((String) v)); } if (v instanceof Long) { val = m.createTypedLiteral(v); } else if (v instanceof Integer) { val = m.createTypedLiteral(new Long(((Integer) v).longValue())); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range long"; } } else if (rnguri.contains("string")) { if (v instanceof String) { v = stripQuotes((String) v); val = m.createTypedLiteral(v); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range string"; } } else if (rnguri.endsWith("date")) { if (v instanceof String) { v = stripQuotes((String) v); DateTime dt = new DateTime((String) v); String xsdFormat = "yyyy-MM-dd"; String modifiedV = dt.toString(xsdFormat); val = m.createTypedLiteral(modifiedV, rnguri); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range date/dateTime/time"; } } else if (rnguri.endsWith("dateTime")) { if (v instanceof String) { v = stripQuotes((String) v); if (v != null && ((String) v).length() > 0) { DateTime dt = new DateTime((String) v); String xsdFormat = "yyyy-MM-dd'T'HH:mm:ssZZ"; String modifiedV = dt.toString(xsdFormat); val = m.createTypedLiteral(modifiedV, rnguri); } } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range date/dateTime/time"; } } else if (rnguri.endsWith("time")) { if (v instanceof String) { v = stripQuotes((String) v); val = m.createTypedLiteral("\"" + v + "\"", rnguri); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range date/dateTime/time"; } } else if (rnguri.endsWith("boolean")) { if (v instanceof String) { v = Boolean.parseBoolean(stripQuotes((String) v)); } if (v instanceof Boolean) { val = m.createTypedLiteral(v); } else { errMsg = "Unexpected value '" + v.toString() + "' (" + v.getClass().getSimpleName() + ") doesn't match range boolean"; } } else { errMsg = "Unhandled range " + rnguri; } } else { errMsg = "Range should not be null."; } if (errMsg != null) { throw new Exception(errMsg); } return val; }
private void reorderIndividuals( String[] individualUris, VitroRequest vreq, String rankPredicate) { // Testing new mechanism OntModel writeModel = vreq.getOntModelSelector().getABoxModel(); Model additions = ModelFactory.createDefaultModel(); Model retractions = ModelFactory.createDefaultModel(); Property rankPredicateProperty = ResourceFactory.createProperty(rankPredicate); DataProperty dp = vreq.getWebappDaoFactory().getDataPropertyDao().getDataPropertyByURI(rankPredicate); String datapropURI = dp.getRangeDatatypeURI(); int counter = 1; for (String individualUri : individualUris) { Resource individualResource = ResourceFactory.createResource(individualUri); // Deletions are all old statements with rank predicate retractions.add( writeModel.listStatements(individualResource, rankPredicateProperty, (RDFNode) null)); log.debug("retractions = " + retractions); // New statement is new literal with the data property from Literal dataLiteral = null; if (datapropURI != null && datapropURI.length() > 0) { dataLiteral = ResourceFactory.createTypedLiteral( String.valueOf(counter), TypeMapper.getInstance().getSafeTypeByName(datapropURI)); } else { dataLiteral = ResourceFactory.createPlainLiteral(String.valueOf(counter)); } additions.add(individualResource, rankPredicateProperty, dataLiteral); log.debug("additions = " + additions); counter++; } Lock lock = null; try { lock = writeModel.getLock(); lock.enterCriticalSection(Lock.WRITE); writeModel.getBaseModel().notifyEvent(new EditEvent(null, true)); writeModel.remove(retractions); writeModel.add(additions); } catch (Throwable t) { log.error("error adding edit change n3required model to in memory model \n" + t.getMessage()); } finally { writeModel.getBaseModel().notifyEvent(new EditEvent(null, false)); lock.leaveCriticalSection(); } // old code that for some reason doesn't seem to actually commit the changes /* * int counter = 1; for (String individualUri : individualUris) { // Retract all existing rank statements for this individual dpsDao.deleteDataPropertyStatementsForIndividualByDataProperty(individualUri, rankPredicate); // Then add the new rank statement for this individual // insertNewDataPropertyStatement will insert the rangeDatatype of the property, so we don't need to set that here. dpsDao.insertNewDataPropertyStatement(new DataPropertyStatementImpl(individualUri, rankPredicate, String.valueOf(counter))); counter++; } */ }
/** Static initializer. Adds builtin datatypes to the mapper. */ static { theTypeMap = new TypeMapper(); theTypeMap.registerDatatype(XMLLiteralType.theXMLLiteralType); XSDDatatype.loadXSDSimpleTypes(theTypeMap); }
// Temporary development code public static void main(String[] args) { for (Iterator iter = theTypeMap.listTypes(); iter.hasNext(); ) { RDFDatatype dt = (RDFDatatype) iter.next(); System.out.println(" - " + dt); } }