org.apache.jena.rdf.model.RDFList Java Examples

The following examples show how to use org.apache.jena.rdf.model.RDFList. 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: RDFLabels.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the label for a given Resource.
 * @param resource  the Resource to get the label of
 * @return the label (never null)
 */
public String getLabel(Resource resource) {
	if(resource.isURIResource() && resource.getModel() != null) {
		String qname = resource.getModel().qnameFor(resource.getURI());
		if(qname != null) {
			return qname;
		}
		else {
			return "<" + resource.getURI() + ">";
		}
	}
	else if(resource.isAnon() && resource.getModel() != null && resource.hasProperty(RDF.first)) {
		StringBuffer buffer = new StringBuffer("[");
		Iterator<RDFNode> members = resource.as(RDFList.class).iterator();
		while(members.hasNext()) {
			RDFNode member = members.next();
			buffer.append(RDFLabels.get().getNodeLabel(member));
			if(members.hasNext()) {
				buffer.append(", ");
			}
		}
		buffer.append("]");
		return buffer.toString();
	}
	else if(resource.isAnon()) {
		return getBlankNodeLabel(resource);
	}
	else {
		return resource.toString();
	}
}
 
Example #2
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static RDFList getListProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().canAs(RDFList.class)) {
		return s.getResource().as(RDFList.class);
	}
	else {
		return null;
	}
}
 
Example #3
Source File: ClosedConstraintExecutor.java    From shacl with Apache License 2.0 5 votes vote down vote up
ClosedConstraintExecutor(Constraint constraint) {
	this.closed = constraint.getShapeResource().hasProperty(SH.closed, JenaDatatypes.TRUE);
	RDFList list = JenaUtil.getListProperty(constraint.getShapeResource(), SH.ignoredProperties);
	if(list != null) {
		list.iterator().forEachRemaining(allowedProperties::add);
	}
	for(Resource ps : JenaUtil.getResourceProperties(constraint.getShapeResource(), SH.property)) {
		Resource path = ps.getPropertyResourceValue(SH.path);
		if(path.isURIResource()) {
			allowedProperties.add(path);
		}
	}
}
 
Example #4
Source File: W3CTestRunner.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void collectItems(File manifestFile, String baseURI) throws IOException {
	
	String filePath = manifestFile.getAbsolutePath().replaceAll("\\\\", "/");
	int coreIndex = filePath.indexOf("core/");
	if(coreIndex > 0 && !filePath.contains("sparql/core")) {
		filePath = filePath.substring(coreIndex);
	}
	else {
		int sindex = filePath.indexOf("sparql/");
		if(sindex > 0) {
			filePath = filePath.substring(sindex);
		}
	}
	
	Model model = JenaUtil.createMemoryModel();
	model.read(new FileInputStream(manifestFile), baseURI, FileUtils.langTurtle);
	
	for(Resource manifest : model.listSubjectsWithProperty(RDF.type, MF.Manifest).toList()) {
		for(Resource include : JenaUtil.getResourceProperties(manifest, MF.include)) {
			String path = include.getURI().substring(baseURI.length());
			File includeFile = new File(manifestFile.getParentFile(), path);
			if(path.contains("/")) {
				String addURI = path.substring(0, path.indexOf('/'));
				collectItems(includeFile, baseURI + addURI + "/");
			}
			else {
				collectItems(includeFile, baseURI + path);
			}
		}
		for(Resource entries : JenaUtil.getResourceProperties(manifest, MF.entries)) {
			for(RDFNode entry : entries.as(RDFList.class).iterator().toList()) {
				items.add(new Item(entry.asResource(), filePath, manifestFile));
			}
		}
	}
}
 
Example #5
Source File: SHACLPaths.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static void appendPathBlankNode(StringBuffer sb, Resource path, String separator) {
	if(path.hasProperty(RDF.first)) {
		Iterator<RDFNode> it = path.as(RDFList.class).iterator();
		while(it.hasNext()) {
			Resource item = (Resource) it.next();
			appendNestedPath(sb, item, SEQUENCE_PATH_SEPARATOR);
			if(it.hasNext()) {
				sb.append(" ");
				sb.append(separator);
				sb.append(" ");
			}
		}
	}
	else if(path.hasProperty(SH.inversePath)) {
		sb.append("^");
		if(path.getProperty(SH.inversePath).getObject().isAnon()) {
			sb.append("(");
			appendPath(sb, path.getPropertyResourceValue(SH.inversePath));
			sb.append(")");
		}
		else {
			appendPath(sb, path.getPropertyResourceValue(SH.inversePath));
		}
	}
	else if(path.hasProperty(SH.alternativePath)) {
		appendNestedPath(sb, path.getPropertyResourceValue(SH.alternativePath), ALTERNATIVE_PATH_SEPARATOR);
	}
	else if(path.hasProperty(SH.zeroOrMorePath)) {
		appendNestedPath(sb, path.getPropertyResourceValue(SH.zeroOrMorePath), SEQUENCE_PATH_SEPARATOR);
		sb.append("*");
	}
	else if(path.hasProperty(SH.oneOrMorePath)) {
		appendNestedPath(sb, path.getPropertyResourceValue(SH.oneOrMorePath), SEQUENCE_PATH_SEPARATOR);
		sb.append("+");
	}
	else if(path.hasProperty(SH.zeroOrOnePath)) {
		appendNestedPath(sb, path.getPropertyResourceValue(SH.zeroOrOnePath), SEQUENCE_PATH_SEPARATOR);
		sb.append("?");
	}
}
 
Example #6
Source File: TemplateImpl.java    From Processor with Apache License 2.0 5 votes vote down vote up
protected List<Locale> getLanguages(Property property)
{
    if (property == null) throw new IllegalArgumentException("Property cannot be null");
    
    List<Locale> languages = new ArrayList<>();
    Resource langs = getPropertyResourceValue(property);
    if (langs != null)
    {
        if (!langs.canAs(RDFList.class))
        {
            if (log.isErrorEnabled()) log.error("ldt:lang value is not an rdf:List on template '{}'", getURI());
            throw new OntologyException("ldt:lang value is not an rdf:List on template  '" + getURI() +"'");
        }

        // could use list order as priority (quality value q=)
        RDFList list = langs.as(RDFList.class);
        ExtendedIterator<RDFNode> it = list.iterator();
        try
        {
            while (it.hasNext())
            {
                RDFNode langTag = it.next();
                if (!langTag.isLiteral())
                {
                    if (log.isErrorEnabled()) log.error("Non-literal language tag (ldt:lang member) on template '{}'", getURI());
                    throw new OntologyException("Non-literal language tag (ldt:lang member) on template '" + getURI() +"'");
                }

                languages.add(Locale.forLanguageTag(langTag.asLiteral().getString()));
            }
        }
        finally
        {
            it.close();
        }
    }
    
    return languages;
}
 
Example #7
Source File: RdfListUtils.java    From RDFUnit with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to return the items of a list or empty if th elist is empty or not an rdf:List
 */
public static Collection<RDFNode> getListItemsOrEmpty(RDFNode node) {
    ImmutableList.Builder<RDFNode> items = ImmutableList.builder();

    if (isList(node)) {
        RDFList rdfList = node.as(RDFList.class);
        rdfList.iterator().forEachRemaining(items::add);
    }

    return items.build();
}
 
Example #8
Source File: InConstraintExecutor.java    From shacl with Apache License 2.0 4 votes vote down vote up
InConstraintExecutor(Constraint constraint) {
	RDFList list = constraint.getParameterValue().as(RDFList.class);
	this.ins = list.iterator().toSet();
}
 
Example #9
Source File: AbstractShapeListConstraintExecutor.java    From shacl with Apache License 2.0 4 votes vote down vote up
AbstractShapeListConstraintExecutor(Constraint constraint) {
	RDFList list = constraint.getParameterValue().as(RDFList.class);
	ExtendedIterator<RDFNode> sit = list.iterator();
	shapes = sit.mapWith(n -> n.asResource()).toList();
}
 
Example #10
Source File: LanguageInConstraintExecutor.java    From shacl with Apache License 2.0 4 votes vote down vote up
LanguageInConstraintExecutor(Constraint constraint) {
	RDFList list = constraint.getParameterValue().as(RDFList.class);
	this.langs = list.iterator().mapWith(n -> n.asLiteral().getString()).toSet();
}
 
Example #11
Source File: RdfListUtils.java    From RDFUnit with Apache License 2.0 4 votes vote down vote up
public static boolean isList(RDFNode node) {
    return node.isAnon() && node.canAs(RDFList.class);
}