All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clarkparsia.pellint.rdfxml.RDFModel Maven / Gradle / Ivy

The newest version!
// Copyright (c) 2006 - 2008, Clark & Parsia, LLC. 
// This source code is available under the terms of the Affero General Public License v3.
//
// Please see LICENSE.txt for full license terms, including the availability of proprietary exceptions.
// Questions, comments, or requests for clarification: [email protected]

package com.clarkparsia.pellint.rdfxml;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.clarkparsia.pellint.util.CollectionUtil;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;

/**
 * 

* Title: *

*

* Description: *

*

* Copyright: Copyright (c) 2008 *

*

* Company: Clark & Parsia, LLC. *

* * @author Harris Lin */ public class RDFModel { private List m_Comments; private Map m_Namespaces; private List m_AllStatements; private Map>> m_Statements; private Map> m_StatementsByPredicate; private Map> m_StatementsByObject; private Set m_BNodes; public RDFModel() { m_Comments = CollectionUtil.makeList(); m_Namespaces = CollectionUtil.makeMap(); m_AllStatements = CollectionUtil.makeList(); m_Statements = CollectionUtil.makeMap(); m_StatementsByPredicate = CollectionUtil.makeMap(); m_StatementsByObject = CollectionUtil.makeMap(); m_BNodes = CollectionUtil.makeSet(); } public void add(RDFModel other) { m_Comments.addAll(other.m_Comments); m_Namespaces.putAll(other.m_Namespaces); addAllStatements(other.getStatements()); m_BNodes.addAll(other.m_BNodes); } public void add(Model jenaModel) { addAllStatements(jenaModel.listStatements()); } public void addComment(String comment) { m_Comments.add(comment); } public List getComments() { return m_Comments; } public void addNamespace(String prefix, String uri) { m_Namespaces.put(prefix, uri); } public Map getNamespaces() { return m_Namespaces; } public void addAllStatements(Iterator stmts) { while (stmts.hasNext()) { addStatement(stmts.next()); } } public void addAllStatements(List stmts) { addAllStatements(stmts.iterator()); } public void addAllStatementsWithExistingBNodesOnly(List stmts) { for (Statement stmt : stmts) { Resource s = stmt.getSubject(); if (s.isAnon() && !m_BNodes.contains(s)) continue; RDFNode o = stmt.getObject(); if (o.isAnon() && !m_BNodes.contains(o)) continue; addStatement(stmt); } } public void addStatement(Statement stmt) { m_AllStatements.add(stmt); addToStatements(stmt); addToStatementsByPredicate(stmt); addToStatementsByObject(stmt); addToBNodes(stmt.getSubject()); addToBNodes(stmt.getObject()); } private void addToBNodes(RDFNode v) { if (v.isAnon()) m_BNodes.add(v); } public List getStatements() { return m_AllStatements; } public Collection getStatementsByPredicate(Property predicate) { List list = m_StatementsByPredicate.get(predicate); if (list == null) { return Collections.emptyList(); } else { return list; } } public Collection getStatementsByObject(RDFNode object) { List list = m_StatementsByObject.get(object); if (list == null) { return Collections.emptyList(); } else { return list; } } public Collection getValues(Resource subject, Property predicate) { Map> pMap = m_Statements.get(subject); if (pMap == null) { return Collections.emptyList(); } Set list = pMap.get(predicate); if (list == null) { return Collections.emptyList(); } else { return list; } } public RDFNode getUniqueObject(Resource subject, Property predicate) { Collection values = getValues(subject, predicate); if (values.isEmpty()) { return null; } else { return values.iterator().next(); } } public boolean containsStatement(Resource subject, Property predicate, RDFNode object) { Collection values = getValues(subject, predicate); if (values.isEmpty()) { return false; } else { return values.contains(object); } } private void addToStatements(Statement stmt) { Resource s = stmt.getSubject(); Property p = stmt.getPredicate(); RDFNode v = stmt.getObject(); Map> pMap = m_Statements.get(s); if (pMap == null) { pMap = CollectionUtil.makeMap(); m_Statements.put(s, pMap); } Set values = pMap.get(p); if (values == null) { values = CollectionUtil.makeSet(); pMap.put(p, values); } values.add(v); } private void addToStatementsByPredicate(Statement stmt) { Property p = stmt.getPredicate(); List list = m_StatementsByPredicate.get(p); if (list == null) { list = CollectionUtil.makeList(); m_StatementsByPredicate.put(p, list); } list.add(stmt); } private void addToStatementsByObject(Statement stmt) { RDFNode o = stmt.getObject(); List list = m_StatementsByObject.get(o); if (list == null) { list = CollectionUtil.makeList(); m_StatementsByObject.put(o, list); } list.add(stmt); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy