diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-10 18:17:06 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-11 12:34:47 +0100 |
| commit | 17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch) | |
| tree | 47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java | |
| parent | 8651164cd632a5db310b457ce32d4fbc97bdc41c (diff) | |
| download | ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.tar.gz ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.zip | |
[pagoda] Move project to Scala
This commit includes a few changes:
- The repository still uses Maven to manage dependency but it is now a
Scala project.
- The code has been ported from OWLAPI 3.4.10 to 5.1.20
- A proof of concept program using both RSAComb and PAGOdA has been
added.
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 | 269 |
1 files changed, 0 insertions, 269 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 deleted file mode 100644 index 62885be..0000000 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java +++ /dev/null | |||
| @@ -1,269 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner.light; | ||
| 2 | |||
| 3 | import net.sf.ehcache.Cache; | ||
| 4 | import net.sf.ehcache.CacheManager; | ||
| 5 | import net.sf.ehcache.Element; | ||
| 6 | import org.semanticweb.HermiT.model.*; | ||
| 7 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 8 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 9 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 10 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 11 | import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType; | ||
| 12 | import uk.ac.ox.cs.JRDFox.store.Dictionary; | ||
| 13 | import uk.ac.ox.cs.JRDFox.store.Resource; | ||
| 14 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 15 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 16 | |||
| 17 | import java.util.Collection; | ||
| 18 | import java.util.HashMap; | ||
| 19 | import java.util.Map; | ||
| 20 | |||
| 21 | public class RDFoxTripleManager { | ||
| 22 | |||
| 23 | private final Cache termsCache; | ||
| 24 | private static final int TERMS_CACHE_SIZE = 10000; | ||
| 25 | private static final int CACHE_TTL_DEFAULT = 0; | ||
| 26 | private static final int CACHE_TTI_DEFAULT = 0; | ||
| 27 | private static final boolean CACHE_ETERNAL = true; | ||
| 28 | private static final boolean CACHE_USE_DISK = false; | ||
| 29 | |||
| 30 | UpdateType m_incrementally; | ||
| 31 | // boolean m_incrementally; | ||
| 32 | |||
| 33 | DataStore m_store; | ||
| 34 | Dictionary m_dict; | ||
| 35 | // Set<Atom> triplesByTerm = new HashSet<Atom>(); | ||
| 36 | |||
| 37 | public RDFoxTripleManager(DataStore store, boolean incrementally) { | ||
| 38 | m_store = store; | ||
| 39 | // m_incrementally = incrementally; | ||
| 40 | |||
| 41 | CacheManager cacheManager = CacheManager.getInstance(); | ||
| 42 | String cacheName = "RDFoxTripleManager_" + store.hashCode(); | ||
| 43 | if(! cacheManager.cacheExists(cacheName)) { | ||
| 44 | termsCache = new Cache(cacheName, | ||
| 45 | TERMS_CACHE_SIZE, CACHE_USE_DISK, CACHE_ETERNAL, | ||
| 46 | CACHE_TTL_DEFAULT, CACHE_TTI_DEFAULT); | ||
| 47 | cacheManager.addCache(termsCache); | ||
| 48 | } | ||
| 49 | else | ||
| 50 | termsCache = cacheManager.getCache(cacheName); | ||
| 51 | |||
| 52 | if (incrementally) | ||
| 53 | m_incrementally = UpdateType.ScheduleForAddition; | ||
| 54 | else | ||
| 55 | m_incrementally = UpdateType.Add; | ||
| 56 | |||
| 57 | try { | ||
| 58 | m_dict = store.getDictionary(); | ||
| 59 | resourceID = m_dict.resolveResources( | ||
| 60 | new String[] {Namespace.RDF_TYPE, Namespace.EQUALITY, Namespace.INEQUALITY}, | ||
| 61 | new int[] {Datatype.IRI_REFERENCE.value(), Datatype.IRI_REFERENCE.value(), Datatype.IRI_REFERENCE.value()} | ||
| 62 | ); | ||
| 63 | } catch (JRDFStoreException e) { | ||
| 64 | e.printStackTrace(); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | public boolean isRdfTypeID(int id) { | ||
| 69 | return id == resourceID[0]; | ||
| 70 | } | ||
| 71 | |||
| 72 | public void addTripleByID(int[] tuple) { | ||
| 73 | // System.out.println(getRawTerm(tuple[0]) + " " + getRawTerm(tuple[1]) + " " + getRawTerm(tuple[2]) + " ."); | ||
| 74 | try { | ||
| 75 | // Resource[] rsc = new Resource[3]; | ||
| 76 | // m_dict.getResources(tuple, 0, 3, rsc); | ||
| 77 | // | ||
| 78 | // GroundTerm[] terms = new GroundTerm[3]; | ||
| 79 | // for (int i = 0; i < 3; ++i) | ||
| 80 | // terms[i] = uk.ac.ox.cs.JRDFox.model.Individual.create(rsc[i].m_lexicalForm); | ||
| 81 | // m_store.addTriples(terms, m_incrementally); | ||
| 82 | |||
| 83 | m_store.addTriplesByResourceIDs(tuple, m_incrementally); | ||
| 84 | } catch (JRDFStoreException e) { | ||
| 85 | e.printStackTrace(); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | public void addTripleByTerm(Atom atom) { | ||
| 90 | try { | ||
| 91 | m_store.addTriples(getRDFoxTriple(atom), m_incrementally); | ||
| 92 | } catch (JRDFStoreException e) { | ||
| 93 | e.printStackTrace(); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | public void removeTripleByTermIncrementally(Atom atom) { | ||
| 98 | try { | ||
| 99 | m_store.addTriples(getRDFoxTriple(atom), UpdateType.ScheduleForDeletion); | ||
| 100 | } catch (JRDFStoreException e) { | ||
| 101 | e.printStackTrace(); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | public static GroundTerm[] getRDFoxTriple(Atom instance) { | ||
| 106 | if (instance.getArity() == 1) | ||
| 107 | return new GroundTerm[] { | ||
| 108 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 109 | uk.ac.ox.cs.JRDFox.model.Individual.RDF_TYPE, | ||
| 110 | uk.ac.ox.cs.JRDFox.model.Individual.create(((AtomicConcept) instance.getDLPredicate()).getIRI()) }; | ||
| 111 | else if (instance.getDLPredicate() instanceof Equality || instance.getDLPredicate() instanceof AnnotatedEquality) | ||
| 112 | return new GroundTerm[] { | ||
| 113 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 114 | uk.ac.ox.cs.JRDFox.model.Individual.SAME_AS, | ||
| 115 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(1)).getIRI()) }; | ||
| 116 | else if (instance.getDLPredicate() instanceof Inequality) | ||
| 117 | return new GroundTerm[] { | ||
| 118 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 119 | uk.ac.ox.cs.JRDFox.model.Individual.DIFFERENT_FROM, | ||
| 120 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(1)).getIRI()) }; | ||
| 121 | else | ||
| 122 | return new GroundTerm[] { | ||
| 123 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(0)).getIRI()), | ||
| 124 | uk.ac.ox.cs.JRDFox.model.Individual.create(((AtomicRole) instance.getDLPredicate()).getIRI()), | ||
| 125 | uk.ac.ox.cs.JRDFox.model.Individual.create(((Individual) instance.getArgument(1)).getIRI()) }; | ||
| 126 | } | ||
| 127 | |||
| 128 | int[] resourceID; // rdf:type, owl:sameAs, owl:differentFrom | ||
| 129 | |||
| 130 | public int[] getInstance(Atom atom, Map<Variable, Integer> assignment) { | ||
| 131 | DLPredicate p = atom.getDLPredicate(); | ||
| 132 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 133 | return new int[] { | ||
| 134 | getResourceID(atom.getArgument(0), assignment), | ||
| 135 | resourceID[1], | ||
| 136 | getResourceID(atom.getArgument(1), assignment) | ||
| 137 | }; | ||
| 138 | else if (p instanceof Inequality) | ||
| 139 | return new int[] { | ||
| 140 | getResourceID(atom.getArgument(0), assignment), | ||
| 141 | resourceID[2], | ||
| 142 | getResourceID(atom.getArgument(1), assignment) | ||
| 143 | }; | ||
| 144 | else if (atom.getArity() == 1) | ||
| 145 | return new int[] { | ||
| 146 | getResourceID(atom.getArgument(0), assignment), | ||
| 147 | resourceID[0], | ||
| 148 | getResourceID(p) | ||
| 149 | }; | ||
| 150 | else | ||
| 151 | return new int[] { | ||
| 152 | getResourceID(atom.getArgument(0), assignment), | ||
| 153 | getResourceID(p), | ||
| 154 | getResourceID(atom.getArgument(1), assignment) | ||
| 155 | }; | ||
| 156 | } | ||
| 157 | |||
| 158 | public String getRawTerm(int id) { | ||
| 159 | Resource[] res = new Resource[1]; | ||
| 160 | try { | ||
| 161 | m_dict.getResources(new int[] {id}, 0, 1, res); | ||
| 162 | } catch (JRDFStoreException e) { | ||
| 163 | e.printStackTrace(); | ||
| 164 | } | ||
| 165 | return getQuotedTerm(res[0]); | ||
| 166 | } | ||
| 167 | |||
| 168 | Map<String, Integer> predicateCache = new HashMap<String, Integer>(); | ||
| 169 | |||
| 170 | public int getResourceID(DLPredicate p) { | ||
| 171 | Integer id; | ||
| 172 | String name = p instanceof AtomicConcept ? ((AtomicConcept) p).getIRI() : ((AtomicRole) p).getIRI(); | ||
| 173 | if ((id = predicateCache.get(name)) != null) return id; | ||
| 174 | try { | ||
| 175 | predicateCache.put(name, id = resolveResource(name, Datatype.IRI_REFERENCE.value())); | ||
| 176 | |||
| 177 | } catch (JRDFStoreException e) { | ||
| 178 | e.printStackTrace(); | ||
| 179 | } | ||
| 180 | return id; | ||
| 181 | } | ||
| 182 | |||
| 183 | public int getResourceID(String name) { | ||
| 184 | Integer id = null; | ||
| 185 | try { | ||
| 186 | id = resolveResource(name, Datatype.IRI_REFERENCE.value()); | ||
| 187 | } catch (JRDFStoreException e) { | ||
| 188 | e.printStackTrace(); | ||
| 189 | } | ||
| 190 | return id; | ||
| 191 | } | ||
| 192 | |||
| 193 | private int resolveResource(String name, int type) throws JRDFStoreException { | ||
| 194 | String[] lexicalForms = new String[] {name}; | ||
| 195 | int[] types = new int[] {type}; | ||
| 196 | return m_dict.resolveResources(lexicalForms, types)[0]; | ||
| 197 | } | ||
| 198 | |||
| 199 | // Map<Term, Integer> termCache = new HashMap<Term, Integer>(); | ||
| 200 | // Queue<Term> termQueue = new LinkedList<Term>(); | ||
| 201 | |||
| 202 | private int getResourceID(Term arg, Map<Variable, Integer> assignment) { | ||
| 203 | if (arg instanceof Variable) return assignment.get(arg); | ||
| 204 | int id = -1; | ||
| 205 | if(termsCache.isKeyInCache(arg)) | ||
| 206 | return ((int) termsCache.get(arg).getObjectValue()); | ||
| 207 | |||
| 208 | // if (arg instanceof Individual) { | ||
| 209 | try { | ||
| 210 | if (arg instanceof Individual) | ||
| 211 | termsCache.put(new Element(arg, id = resolveResource(((Individual) arg).getIRI(), Datatype.IRI_REFERENCE.value()))); | ||
| 212 | else if (arg instanceof Constant) | ||
| 213 | termsCache.put(new Element(arg, id = resolveResource(((Constant) arg).getLexicalForm(), getDatatypeID(((Constant) arg).getDatatypeURI())))); | ||
| 214 | |||
| 215 | } catch (JRDFStoreException e) { | ||
| 216 | e.printStackTrace(); | ||
| 217 | System.exit(1); | ||
| 218 | } | ||
| 219 | // } | ||
| 220 | |||
| 221 | return id; | ||
| 222 | } | ||
| 223 | |||
| 224 | private static int getDatatypeID(String uri) { | ||
| 225 | if (uri.equals("http://www.w3.org/2001/XMLSchema#string")) return Datatype.XSD_STRING.value(); | ||
| 226 | if (uri.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral")) return Datatype.RDF_PLAIN_LITERAL.value(); | ||
| 227 | if (uri.equals("http://www.w3.org/2001/XMLSchema#integer")) return Datatype.XSD_INTEGER.value(); | ||
| 228 | if (uri.equals("http://www.w3.org/2001/XMLSchema#float")) return Datatype.XSD_FLOAT.value(); | ||
| 229 | if (uri.equals("http://www.w3.org/2001/XMLSchema#double")) return Datatype.XSD_DOUBLE.value(); | ||
| 230 | if (uri.equals("http://www.w3.org/2001/XMLSchema#boolean")) return Datatype.XSD_BOOLEAN.value(); | ||
| 231 | if (uri.equals("http://www.w3.org/2001/XMLSchema#dateTime")) return Datatype.XSD_DATE_TIME.value(); | ||
| 232 | if (uri.equals("http://www.w3.org/2001/XMLSchema#time")) return Datatype.XSD_TIME.value(); | ||
| 233 | if (uri.equals("http://www.w3.org/2001/XMLSchema#date")) return Datatype.XSD_DATE.value(); | ||
| 234 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gYearMonth")) return Datatype.XSD_G_YEAR_MONTH.value(); | ||
| 235 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gYear")) return Datatype.XSD_G_YEAR.value(); | ||
| 236 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gMonthDay")) return Datatype.XSD_G_MONTH_DAY.value(); | ||
| 237 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gDay")) return Datatype.XSD_G_DAY.value(); | ||
| 238 | if (uri.equals("http://www.w3.org/2001/XMLSchema#gMonth")) return Datatype.XSD_G_MONTH.value(); | ||
| 239 | if (uri.equals("http://www.w3.org/2001/XMLSchema#duration")) return Datatype.XSD_DURATION.value(); | ||
| 240 | |||
| 241 | return -1; | ||
| 242 | } | ||
| 243 | |||
| 244 | public int[] getResourceIDs(Collection<uk.ac.ox.cs.JRDFox.model.Individual> individuals) { | ||
| 245 | String[] str = new String[individuals.size()]; | ||
| 246 | int[] types = new int[individuals.size()]; | ||
| 247 | int index = 0; | ||
| 248 | for (uk.ac.ox.cs.JRDFox.model.Individual individual : individuals) { | ||
| 249 | types[index] = Datatype.IRI_REFERENCE.value(); | ||
| 250 | str[index++] = individual.getIRI(); | ||
| 251 | } | ||
| 252 | |||
| 253 | try { | ||
| 254 | return m_dict.resolveResources(str, types); | ||
| 255 | } catch (JRDFStoreException e) { | ||
| 256 | e.printStackTrace(); | ||
| 257 | return null; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | public static String getQuotedTerm(Resource r) { | ||
| 262 | if (r.m_datatype.equals(Datatype.IRI_REFERENCE)) | ||
| 263 | return OWLHelper.addAngles(r.m_lexicalForm); | ||
| 264 | if (r.m_datatype.equals(Datatype.XSD_STRING) || r.m_datatype.equals(Datatype.RDF_PLAIN_LITERAL)) | ||
| 265 | return "\"" + r.m_lexicalForm + "\""; | ||
| 266 | else | ||
| 267 | return "\"" + r.m_lexicalForm + "\"^^<" + r.m_datatype.getIRI() + ">"; | ||
| 268 | } | ||
| 269 | } | ||
