diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java new file mode 100644 index 0000000..2280b12 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java | |||
| @@ -0,0 +1,249 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner.light; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.HashMap; | ||
| 5 | import java.util.HashSet; | ||
| 6 | import java.util.LinkedList; | ||
| 7 | import java.util.Map; | ||
| 8 | import java.util.Queue; | ||
| 9 | import java.util.Set; | ||
| 10 | |||
| 11 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 12 | import org.semanticweb.HermiT.model.Atom; | ||
| 13 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 14 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 15 | import org.semanticweb.HermiT.model.Constant; | ||
| 16 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 17 | import org.semanticweb.HermiT.model.Equality; | ||
| 18 | import org.semanticweb.HermiT.model.Individual; | ||
| 19 | import org.semanticweb.HermiT.model.Inequality; | ||
| 20 | import org.semanticweb.HermiT.model.Term; | ||
| 21 | import org.semanticweb.HermiT.model.Variable; | ||
| 22 | |||
| 23 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 24 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 25 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 26 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 27 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 28 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 29 | import uk.ac.ox.cs.JRDFox.store.Dictionary; | ||
| 30 | import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType; | ||
| 31 | import uk.ac.ox.cs.JRDFox.store.Resource; | ||
| 32 | |||
| 33 | public class RDFoxTripleManager { | ||
| 34 | |||
| 35 | UpdateType m_incrementally; | ||
| 36 | // boolean m_incrementally; | ||
| 37 | |||
| 38 | DataStore m_store; | ||
| 39 | Dictionary m_dict; | ||
| 40 | Set<Atom> triplesByTerm = new HashSet<Atom>(); | ||
| 41 | |||
| 42 | public RDFoxTripleManager(DataStore store, boolean incrementally) { | ||
| 43 | m_store = store; | ||
| 44 | // m_incrementally = incrementally; | ||
| 45 | if (incrementally) | ||
| 46 | m_incrementally = UpdateType.ScheduleForAddition; | ||
| 47 | else | ||
| 48 | m_incrementally = UpdateType.Add; | ||
| 49 | |||
| 50 | try { | ||
| 51 | m_dict = store.getDictionary(); | ||
| 52 | resourceID = m_dict.resolveResources( | ||
| 53 | new String[] {Namespace.RDF_TYPE, Namespace.EQUALITY, Namespace.INEQUALITY}, | ||
| 54 | new int[] {Datatype.IRI_REFERENCE.value(), Datatype.IRI_REFERENCE.value(), Datatype.IRI_REFERENCE.value()} | ||
| 55 | ); | ||
| 56 | } catch (JRDFStoreException e) { | ||
| 57 | e.printStackTrace(); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | public boolean isRdfTypeID(int id) { | ||
| 62 | return id == resourceID[0]; | ||
| 63 | } | ||
| 64 | |||
| 65 | public void addTripleByID(int[] tuple) { | ||
| 66 | try { | ||
| 67 | m_store.addTriplesByResourceIDs(tuple, m_incrementally); | ||
| 68 | } catch (JRDFStoreException e) { | ||
| 69 | e.printStackTrace(); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | public void addTripleByTerm(Atom atom) { | ||
| 74 | try { | ||
| 75 | m_store.addTriples(getRDFoxTriple(atom), m_incrementally); | ||
| 76 | } catch (JRDFStoreException e) { | ||
| 77 | e.printStackTrace(); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | public static GroundTerm[] getRDFoxTriple(Atom instance) { | ||
| 82 | if (instance.getArity() == 1) | ||
| 83 | return new GroundTerm[] { | ||
| 84 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 85 | uk.ac.ox.cs.JRDFox.model.Individual.RDF_TYPE, | ||
| 86 | uk.ac.ox.cs.JRDFox.model.Individual.create(((AtomicConcept) instance.getDLPredicate()).getIRI()) }; | ||
| 87 | else if (instance.getDLPredicate() instanceof Equality || instance.getDLPredicate() instanceof AnnotatedEquality) | ||
| 88 | return new GroundTerm[] { | ||
| 89 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 90 | uk.ac.ox.cs.JRDFox.model.Individual.SAME_AS, | ||
| 91 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(1)).getIRI()) }; | ||
| 92 | else if (instance.getDLPredicate() instanceof Inequality) | ||
| 93 | return new GroundTerm[] { | ||
| 94 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 95 | uk.ac.ox.cs.JRDFox.model.Individual.DIFFERENT_FROM, | ||
| 96 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(1)).getIRI()) }; | ||
| 97 | else | ||
| 98 | return new GroundTerm[] { | ||
| 99 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 100 | uk.ac.ox.cs.JRDFox.model.Individual.create(((AtomicRole) instance.getDLPredicate()).getIRI()), | ||
| 101 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(1)).getIRI()) }; | ||
| 102 | } | ||
| 103 | |||
| 104 | int[] resourceID; // rdf:type, owl:sameAs, owl:differentFrom | ||
| 105 | |||
| 106 | public int[] getInstance(Atom atom, Map<Variable, Integer> assignment) { | ||
| 107 | DLPredicate p = atom.getDLPredicate(); | ||
| 108 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 109 | return new int[] { | ||
| 110 | getResourceID(atom.getArgument(0), assignment), | ||
| 111 | resourceID[1], | ||
| 112 | getResourceID(atom.getArgument(1), assignment) | ||
| 113 | }; | ||
| 114 | else if (p instanceof Inequality) | ||
| 115 | return new int[] { | ||
| 116 | getResourceID(atom.getArgument(0), assignment), | ||
| 117 | resourceID[2], | ||
| 118 | getResourceID(atom.getArgument(1), assignment) | ||
| 119 | }; | ||
| 120 | else if (atom.getArity() == 1) | ||
| 121 | return new int[] { | ||
| 122 | getResourceID(atom.getArgument(0), assignment), | ||
| 123 | resourceID[0], | ||
| 124 | getResourceID(p) | ||
| 125 | }; | ||
| 126 | else | ||
| 127 | return new int[] { | ||
| 128 | getResourceID(atom.getArgument(0), assignment), | ||
| 129 | getResourceID(p), | ||
| 130 | getResourceID(atom.getArgument(1), assignment) | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | |||
| 134 | public String getRawTerm(int id) { | ||
| 135 | Resource[] res = new Resource[1]; | ||
| 136 | try { | ||
| 137 | m_dict.getResources(new int[] {id}, 0, 1, res); | ||
| 138 | } catch (JRDFStoreException e) { | ||
| 139 | e.printStackTrace(); | ||
| 140 | } | ||
| 141 | return getQuotedTerm(res[0]); | ||
| 142 | } | ||
| 143 | |||
| 144 | Map<String, Integer> predicateCache = new HashMap<String, Integer>(); | ||
| 145 | |||
| 146 | public int getResourceID(DLPredicate p) { | ||
| 147 | Integer id; | ||
| 148 | String name = p instanceof AtomicConcept ? ((AtomicConcept) p).getIRI() : ((AtomicRole) p).getIRI(); | ||
| 149 | if ((id = predicateCache.get(name)) != null) return id; | ||
| 150 | try { | ||
| 151 | predicateCache.put(name, id = resolveResource(name, Datatype.IRI_REFERENCE.value())); | ||
| 152 | |||
| 153 | } catch (JRDFStoreException e) { | ||
| 154 | e.printStackTrace(); | ||
| 155 | } | ||
| 156 | return id; | ||
| 157 | } | ||
| 158 | |||
| 159 | public int getResourceID(String name) { | ||
| 160 | Integer id = null; | ||
| 161 | try { | ||
| 162 | id = resolveResource(name, Datatype.IRI_REFERENCE.value()); | ||
| 163 | } catch (JRDFStoreException e) { | ||
| 164 | e.printStackTrace(); | ||
| 165 | } | ||
| 166 | return id; | ||
| 167 | } | ||
| 168 | |||
| 169 | private int resolveResource(String name, int type) throws JRDFStoreException { | ||
| 170 | String[] lexicalForms = new String[] {name}; | ||
| 171 | int[] types = new int[] {type}; | ||
| 172 | return m_dict.resolveResources(lexicalForms, types)[0]; | ||
| 173 | } | ||
| 174 | |||
| 175 | Map<Term, Integer> termCache = new HashMap<Term, Integer>(); | ||
| 176 | Queue<Term> termList = new LinkedList<Term>(); | ||
| 177 | int sizeLimit = 10000; | ||
| 178 | |||
| 179 | private int getResourceID(Term arg, Map<Variable, Integer> assignment) { | ||
| 180 | while (termCache.size() > sizeLimit) | ||
| 181 | termCache.remove(termList.poll()); | ||
| 182 | |||
| 183 | if (arg instanceof Variable) return assignment.get((Variable) arg); | ||
| 184 | Integer id = null; | ||
| 185 | if ((id = termCache.get(arg)) != null) | ||
| 186 | return id; | ||
| 187 | |||
| 188 | // if (arg instanceof Individual) { | ||
| 189 | try { | ||
| 190 | if (arg instanceof Individual) | ||
| 191 | termCache.put(arg, id = resolveResource(((Individual) arg).getIRI(), Datatype.IRI_REFERENCE.value())); | ||
| 192 | else if (arg instanceof Constant) | ||
| 193 | termCache.put(arg, id = resolveResource(((Constant) arg).getLexicalForm(), getDatatypeID(((Constant) arg).getDatatypeURI()))); | ||
| 194 | |||
| 195 | } catch (JRDFStoreException e) { | ||
| 196 | e.printStackTrace(); | ||
| 197 | } | ||
| 198 | // } | ||
| 199 | |||
| 200 | return id; | ||
| 201 | } | ||
| 202 | |||
| 203 | private static int getDatatypeID(String uri) { | ||
| 204 | if (uri.equals("http://www.w3.org/2001/XMLSchema#string")) return Datatype.XSD_STRING.value(); | ||
| 205 | if (uri.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral")) return Datatype.RDF_PLAIN_LITERAL.value(); | ||
| 206 | if (uri.equals("http://www.w3.org/2001/XMLSchema#integer")) return Datatype.XSD_INTEGER.value(); | ||
| 207 | if (uri.equals("http://www.w3.org/2001/XMLSchema#float")) return Datatype.XSD_FLOAT.value(); | ||
| 208 | if (uri.equals("http://www.w3.org/2001/XMLSchema#double")) return Datatype.XSD_DOUBLE.value(); | ||
| 209 | if (uri.equals("http://www.w3.org/2001/XMLSchema#boolean")) return Datatype.XSD_BOOLEAN.value(); | ||
| 210 | if (uri.equals("http://www.w3.org/2001/XMLSchema#dateTime")) return Datatype.XSD_DATE_TIME.value(); | ||
| 211 | if (uri.equals("http://www.w3.org/2001/XMLSchema#time")) return Datatype.XSD_TIME.value(); | ||
| 212 | if (uri.equals("http://www.w3.org/2001/XMLSchema#date")) return Datatype.XSD_DATE.value(); | ||
| 213 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gYearMonth")) return Datatype.XSD_G_YEAR_MONTH.value(); | ||
| 214 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gYear")) return Datatype.XSD_G_YEAR.value(); | ||
| 215 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gMonthDay")) return Datatype.XSD_G_MONTH_DAY.value(); | ||
| 216 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gDay")) return Datatype.XSD_G_DAY.value(); | ||
| 217 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gMonth")) return Datatype.XSD_G_MONTH.value(); | ||
| 218 | if (uri.equals("http://www.w3.org/2001/XMLSchema#duration")) return Datatype.XSD_DURATION.value(); | ||
| 219 | |||
| 220 | return -1; | ||
| 221 | } | ||
| 222 | |||
| 223 | public int[] getResourceIDs(Collection<uk.ac.ox.cs.JRDFox.model.Individual> individuals) { | ||
| 224 | String[] str = new String[individuals.size()]; | ||
| 225 | int[] types = new int[individuals.size()]; | ||
| 226 | int index = 0; | ||
| 227 | for (uk.ac.ox.cs.JRDFox.model.Individual individual : individuals) { | ||
| 228 | types[index] = Datatype.IRI_REFERENCE.value(); | ||
| 229 | str[index++] = individual.getIRI(); | ||
| 230 | } | ||
| 231 | |||
| 232 | try { | ||
| 233 | return m_dict.resolveResources(str, types); | ||
| 234 | } catch (JRDFStoreException e) { | ||
| 235 | e.printStackTrace(); | ||
| 236 | return null; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | public static String getQuotedTerm(Resource r) { | ||
| 241 | if (r.m_datatype.equals(Datatype.IRI_REFERENCE)) | ||
| 242 | return OWLHelper.addAngles(r.m_lexicalForm); | ||
| 243 | if (r.m_datatype.equals(Datatype.XSD_STRING) || r.m_datatype.equals(Datatype.RDF_PLAIN_LITERAL)) | ||
| 244 | return "\"" + r.m_lexicalForm + "\""; | ||
| 245 | else | ||
| 246 | return "\"" + r.m_lexicalForm + "\"^^<" + r.m_datatype.getIRI() + ">"; | ||
| 247 | } | ||
| 248 | |||
| 249 | } | ||
