Java Code Examples for org.apache.jena.rdf.model.ResourceFactory#createTypedLiteral()

The following examples show how to use org.apache.jena.rdf.model.ResourceFactory#createTypedLiteral() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OneM2MContentInstanceMapper.java    From SDA with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Literal getTypedContent(String con) {
	try {

		return ResourceFactory.createTypedLiteral(Double.valueOf(con));
		// return "\"" + Double.valueOf(con) + "\"^^xsd:double";
	} catch (java.lang.NumberFormatException e) {
		try {
			return ResourceFactory.createTypedLiteral(Float.valueOf(con));
			// return "\"" + Float.valueOf(con) + "\"^^xsd:float";
		} catch (Exception e2) {
			return ResourceFactory.createTypedLiteral(String.valueOf(con));
			// return "\"" + con + "\"^^xsd:string";
		}
	}

}
 
Example 2
Source File: OneM2MContentInstanceMapper.java    From SDA with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Literal getTypedContent(String con) {
	try {

		return ResourceFactory.createTypedLiteral(Double.valueOf(con));
		// return "\"" + Double.valueOf(con) + "\"^^xsd:double";
	} catch (java.lang.NumberFormatException e) {
		try {
			return ResourceFactory.createTypedLiteral(Float.valueOf(con));
			// return "\"" + Float.valueOf(con) + "\"^^xsd:float";
		} catch (Exception e2) {
			return ResourceFactory.createTypedLiteral(String.valueOf(con));
			// return "\"" + con + "\"^^xsd:string";
		}
	}

}
 
Example 3
Source File: RDFNodeFactory.java    From Processor with Apache License 2.0 6 votes vote down vote up
public static final RDFNode createTyped(String value, Resource valueType)
   {
if (value == null) throw new IllegalArgumentException("Param value cannot be null");

       // without value type, return default xsd:string value
       if (valueType == null) return ResourceFactory.createTypedLiteral(value, XSDDatatype.XSDstring);

       // if value type is from XSD namespace, value is treated as typed literal with XSD datatype
       if (valueType.getNameSpace().equals(XSD.getURI()))
       {
           RDFDatatype dataType = NodeFactory.getType(valueType.getURI());
           return ResourceFactory.createTypedLiteral(value, dataType);
       }
       // otherwise, value is treated as URI resource
       else
           return ResourceFactory.createResource(value);
   }
 
Example 4
Source File: OneM2MContentInstanceMapper.java    From SDA with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * typed content type 가져오기
 * @param con
 * @return Literal
 */
public Literal getTypedContent(String con) {
	try {

		return ResourceFactory.createTypedLiteral(Double.valueOf(con));
	} catch (java.lang.NumberFormatException e) {
		try {
			return ResourceFactory.createTypedLiteral(Float.valueOf(con));
		} catch (Exception e2) {
			return ResourceFactory.createTypedLiteral(String.valueOf(con));
		}
	}

}
 
Example 5
Source File: OneM2MContentInstanceMapper.java    From SDA with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * typed content type 가져오기
 * @param con
 * @return Literal
 */
public Literal getTypedContent(String con) {
	try {

		return ResourceFactory.createTypedLiteral(Double.valueOf(con));
	} catch (java.lang.NumberFormatException e) {
		try {
			return ResourceFactory.createTypedLiteral(Float.valueOf(con));
		} catch (Exception e2) {
			return ResourceFactory.createTypedLiteral(String.valueOf(con));
		}
	}

}
 
Example 6
Source File: SPARQLSubstitutions.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Literal withSubstitutions(Literal template, QuerySolution bindings, Function<RDFNode,String> labelFunction) {
	StringBuffer buffer = new StringBuffer();
	String labelTemplate = template.getLexicalForm();
	for(int i = 0; i < labelTemplate.length(); i++) {
		if(i < labelTemplate.length() - 3 && labelTemplate.charAt(i) == '{' && (labelTemplate.charAt(i + 1) == '?' || labelTemplate.charAt(i + 1) == '$')) {
			int varEnd = i + 2;
			while(varEnd < labelTemplate.length()) {
				if(labelTemplate.charAt(varEnd) == '}') {
					String varName = labelTemplate.substring(i + 2, varEnd);
					RDFNode varValue = bindings.get(varName);
					if(varValue != null) {
						if(labelFunction != null) {
							buffer.append(labelFunction.apply(varValue));
						}
						else if(varValue instanceof Resource) {
							buffer.append(RDFLabels.get().getLabel((Resource)varValue));
						}
						else if(varValue instanceof Literal) {
							buffer.append(varValue.asNode().getLiteralLexicalForm());
						}
					}
					break;
				}
				else {
					varEnd++;
				}
			}
			i = varEnd;
		}
		else {
			buffer.append(labelTemplate.charAt(i));
		}
	}
	if(template.getLanguage().isEmpty()) {
		return ResourceFactory.createTypedLiteral(buffer.toString());
	}
	else {
		return ResourceFactory.createLangLiteral(buffer.toString(), template.getLanguage());
	}
}
 
Example 7
Source File: TriplePatternElementParserForJena.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 *
 * @param label
 * @param typeURI
 * @return
 */
@Override
public RDFNode createTypedLiteral( final String label,
                                   final String typeURI )
{
    final RDFDatatype dt = TypeMapper.getInstance()
                                     .getSafeTypeByName( typeURI );
    return ResourceFactory.createTypedLiteral( label, dt );
}
 
Example 8
Source File: JenaDatatypes.java    From shacl with Apache License 2.0 4 votes vote down vote up
public static Literal createDecimal(int value) {
	return ResourceFactory.createTypedLiteral("" + value, XSDDatatype.XSDdecimal);
}
 
Example 9
Source File: JenaDatatypes.java    From shacl with Apache License 2.0 4 votes vote down vote up
public static Literal createInteger(int value) {
	return ResourceFactory.createTypedLiteral("" + value, XSDDatatype.XSDinteger);
}
 
Example 10
Source File: OpenAnnotationRDFWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Adds a single {@link GeneAnnotation} to a model
 *
 * @param ann
 */
public void add(GeneAnnotation ann) {
	if (ann == null) {
		return;
	}
	final Bioentity bioentity = ann.getBioentityObject();
	// TODO - why are we trimming here?
	final String isoForm = StringUtils.trimToNull(ann.getGeneProductForm());

	// TODO: don't use blank nodes
	Resource rAnn = model.createResource();
	Resource rBioentity = createResourceFromId(bioentity.getId());
	Resource rDescriptor = createResourceFromId(ann.getCls());

	model.add(rAnn, RDF.type, getAnnotationType());
	model.add(rAnn, getAnnotationToBioentityProperty(), rBioentity);
	model.add(rAnn, getAnnotationToDescriptorProperty(), rDescriptor);

	// TODO
	if (isoForm != null) {
		//Resource x = createResourceFromId(isoForm);
		//model.add(rAnn, getAnnotationToIsoformProperty(), x);

	}

	if (ann.getEcoEvidenceCls() != null) {
		Resource rEvidenceType = createResourceFromId(ann.getEcoEvidenceCls());
		model.add(rAnn, getAnnotationToEvidenceProperty(), rEvidenceType);
	}

	for (String refId : ann.getReferenceIds()) {
		//model.add(rAnn, getAnnotationToReferencesProperty(), rDescriptor);
	}

	/// TODO
	if (ann.isNegated()) {
	}


	for (List<ExtensionExpression> xl : ann.getExtensionExpressions()) {
		for (ExtensionExpression x : xl) {
			if (false) {
				// TODO - relations ontology needs loaded for mapping
				Resource xn = model.createResource(); // TODO - alternative to blank nodes?
				model.add(xn, 
						createPropertyFromId(x.getRelation()), 
						createResourceFromId(x.getCls()));
				model.add(rAnn, getAnnotationToExtensionProperty(), xn);
			}
		}
	}

	Pair<String, String> actsOnTaxon = ann.getActsOnTaxonId();
	if (actsOnTaxon != null) {
		// TODO
	}

	String date = ann.getLastUpdateDate();
	if (date != null) {
		// TODO - convert to xsd:date if use oa
		String isoDate = 
				date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
		Literal xsdDate = ResourceFactory.createTypedLiteral (isoDate, XSDDatatype.XSDdate);
		model.add(rAnn, getAnnotationDateProperty(), xsdDate);
	}

	if (ann.getAssignedBy() != null) {
		// TODO - use ORCID?
		model.add(rAnn, getAssignedByProperty(), ann.getAssignedBy());
	}

	for (String w : ann.getWithInfos()) {
		model.add(rAnn, getAnnotationToWithProperty(), createResourceFromId(w));

	}

}
 
Example 11
Source File: String2Node.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private void assignLiteral(String value, short datatype)
{

if (datatype == TypeMap.IRI_ID)
   {
   node = ResourceFactory.createResource(value);
   return;
   }

if (datatype == TypeMap.SIMPLE_LITERAL_ID || datatype == 0)
   {
   node = ResourceFactory.createPlainLiteral(value);
   return;
   }

if (datatype > TypeMap.DATATYPE_IDS_START && datatype < TypeMap.DATATYPE_IDS_END)
   {
   node = ResourceFactory.createTypedLiteral(value, TypeMapper.getInstance()
         .getTypeByName(TypeMap.getTypedString(datatype)));
   return;
   }

// TODO language literals
if (datatype >= TypeMap.LANG_IDS_START)
   {
   node = ModelFactory.createDefaultModel().createLiteral(value, TypeMap.getLanguageString(datatype));
   return;
   }

throw new RdfStoreException("Unhandled literal type:" + datatype);
/*
 * if (value.contains(Constants.TYPED_LITERAL_DELIMITER) && value.contains(Constants.LITERAL_LANGUAGE)) { // TODO
 * 
 * } else if (value.contains(Constants.LITERAL_LANGUAGE)) { String[] temp = value.split(Constants.LITERAL_LANGUAGE); if
 * (!Pattern.matches("[a-zA-Z]+(-[a-zA-Z0-9]+)*$", value.substring(value .lastIndexOf(Constants.LITERAL_LANGUAGE) + 1))) { if
 * (value.startsWith("mailto:")) { node = ResourceFactory.createResource(value); } else { node =
 * ModelFactory.createDefaultModel().createLiteral( value); } } else if (temp == null || temp.length != 2) { node =
 * ModelFactory.createDefaultModel().createLiteral(value); } else { node = ModelFactory.createDefaultModel().createLiteral(temp[0], temp[1]); }
 * } else { node = ResourceFactory.createResource(value); }
 */
}