?? rdfschemamodel.java
字號:
package edu.stanford.db.rdf.schema;import org.w3c.rdf.model.*;import org.w3c.rdf.implementation.model.NodeFactoryImpl;import org.w3c.rdf.util.SetOperations;import java.util.*;import org.w3c.rdf.vocabulary.rdf_syntax_19990222.RDF;import org.w3c.rdf.vocabulary.rdf_schema_19990303.RDFS;// for testing onlyimport org.w3c.rdf.util.*;/** * RDF schema model supports subclassing and validates RDF schemas. */public class RDFSchemaModel implements VirtualModel { static final int MAX_CLOSURE_DEPTH = 50; NodeFactory nodeFactory; Model instances, closure; public RDFSchemaModel() { this(new NodeFactoryImpl()); } /** * Creates a schema model, <tt>closure</tt> must contain transitive closures of <tt>subClassOf</tt> and <tt>subPropertyOf</tt> */ public RDFSchemaModel(Model instances, Model closure) { this(); this.instances = instances; this.closure = closure; } public RDFSchemaModel(NodeFactory f) { nodeFactory = f; } public RDFSchemaModel(NodeFactory f, Model instances, Model closure) { this(f); this.instances = instances; this.closure = closure; } public String getLabel() throws ModelException { return instances.getLabel(); } public String getURI() throws ModelException { return instances.getURI(); } public String getLocalName() throws ModelException { return instances.getLocalName(); } public String getNamespace() throws ModelException { return instances.getNamespace(); } /** * @return model contains the fact basis of this model */ public Model getGroundModel() throws ModelException { return instances; } /** * Set a base URI for the model. * Affects creating of new resources and serialization syntax. * Inherited method getURI returns the URI set in this method */ public void setSourceURI(String uri) throws ModelException { instances.setSourceURI(uri); } /** * Returns current base URI setting. */ public String getSourceURI() throws ModelException { return instances.getSourceURI(); } // Model access /** * Number of triples in the model * * @return number of triples, -1 if unknown * * @seeAlso org.w3c.rdf.model.VirtualModel */ public int size() throws ModelException { return -1; } /** * true if the model contains no triples */ public boolean isEmpty() throws ModelException { return instances.isEmpty(); } /** * Enumerate triples */ public Enumeration elements() throws ModelException { return new RDFSchemaModelEnumeration(instances, closure); } /** * Tests if the model contains the given triple. * * @return <code>true</code> if the triple belongs to the model; * <code>false</code> otherwise. */ public boolean contains(Statement t) throws ModelException { // FIXME: efficiency? return !find(t.subject(), t.predicate(), t.object()).isEmpty(); } // Model manipulation: add, remove, find /** * Adds a new triple to the model. */ public void add(Statement t) throws ModelException { instances.add(t); } /** * Removes the triple from the model. */ public void remove(Statement t) throws ModelException { instances.remove(t); } /** * General method to search for triples. * <code>null</code> input for any parameter will match anything. * <p>Example: <code>Model result = m.find( null, RDF.type, new ResourceImpl("http://...#MyClass") )</code> * <p>finds all instances of the class <code>MyClass</code> */ public Model find( Resource subject, Resource predicate, RDFNode object ) throws ModelException { // only two special cases for now // we need it anyway Model res = instances.find(subject, predicate, object); // asking for instances if(object != null && RDF.type.equals(predicate)) { // find instances Model subclass = closure.find(null, RDFS.subClassOf, object); if(!subclass.isEmpty()) { // collect subproperties for(Enumeration en = subclass.elements(); en.hasMoreElements();) { SetOperations.unite( res, instances.find(subject, RDF.type, ((Statement)en.nextElement()).subject()) ); } } } else if(RDFS.subClassOf.equals(predicate)) { res = closure.find(subject, predicate, object); } else if(predicate != null) { // Check for subproperties Model subprop = closure.find(null, RDFS.subPropertyOf, predicate); if(!subprop.isEmpty()) { // collect subproperties for(Enumeration en = subprop.elements(); en.hasMoreElements();) { SetOperations.unite( res, instances.find(subject, ((Statement)en.nextElement()).subject(), object) ); } } } return res; } /** * Search for a single triple. * <code>null</code> input for any parameter will match anything. * * @return a single triple if such was found;<br> * <code>null</code> if nothing was found;<br> * otherwise a RuntimeException is thrown. public Statement find1( Resource subject, Resource predicate, RDFnode object ); */ /** * Clone the model. */ public Model duplicate() throws ModelException { return new RDFSchemaModel(nodeFactory, instances.duplicate(), closure); } /** * Creates empty model of the same Class */ public Model create() throws ModelException { return new RDFSchemaModel(instances.create(), closure); } public boolean isMutable() throws ModelException { return instances.isMutable(); } /** * Returns the node factory for this model */ public NodeFactory getNodeFactory() throws ModelException { return nodeFactory; } public String toString() { try { return "[RDFSchemaModel " + getURI() + "]"; } catch (ModelException exc) { return "[RDFSchemaModel: " + exc + "]"; } } public static Model computeRDFSClosure(Model src) throws ModelException { Model closure = computeClosure(src, RDFS.subClassOf); SetOperations.unite(closure, computeClosure(src, RDFS.subPropertyOf)); return closure; } /** * Compute a transitive closure, disallow loops. */ public static Model computeClosure(Model src, Resource property) throws ModelException { return computeClosure(src, property, false); } /** * Computes a transitive closure on a given predicate. If <tt>allowLoops</tt> is set to false, an exception is thrown * if a loop is encountered. */ public static Model computeClosure(Model src, Resource property, boolean allowLoops) throws ModelException { Model closure = src.create(); // find all roots Model all = src.find(null, property, null); // System.out.println("Found " + all.size() + " triples containing " + property); // compute closure
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -