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/main/java/uk/ac/ox/cs/pagoda/MyPrefixes.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/main/java/uk/ac/ox/cs/pagoda/MyPrefixes.java')
| -rw-r--r-- | src/main/java/uk/ac/ox/cs/pagoda/MyPrefixes.java | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/main/java/uk/ac/ox/cs/pagoda/MyPrefixes.java b/src/main/java/uk/ac/ox/cs/pagoda/MyPrefixes.java new file mode 100644 index 0000000..98a4e97 --- /dev/null +++ b/src/main/java/uk/ac/ox/cs/pagoda/MyPrefixes.java | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda; | ||
| 2 | |||
| 3 | import java.util.Map; | ||
| 4 | |||
| 5 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 6 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 7 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 8 | |||
| 9 | public class MyPrefixes { | ||
| 10 | |||
| 11 | public final static MyPrefixes PAGOdAPrefixes = new MyPrefixes(); | ||
| 12 | |||
| 13 | private org.semanticweb.HermiT.Prefixes hermit = new org.semanticweb.HermiT.Prefixes(); | ||
| 14 | private uk.ac.ox.cs.JRDFox.Prefixes rdfox = new uk.ac.ox.cs.JRDFox.Prefixes(); | ||
| 15 | |||
| 16 | public MyPrefixes() { | ||
| 17 | hermit.declareSemanticWebPrefixes(); | ||
| 18 | hermit.declarePrefix("anony:", Namespace.PAGODA_ANONY); | ||
| 19 | hermit.declarePrefix("aux:", Namespace.PAGODA_AUX); | ||
| 20 | |||
| 21 | rdfox.declareSemanticWebPrefixes(); | ||
| 22 | rdfox.declarePrefix("anony:", Namespace.PAGODA_ANONY); | ||
| 23 | rdfox.declarePrefix("aux:", Namespace.PAGODA_AUX); | ||
| 24 | } | ||
| 25 | |||
| 26 | public org.semanticweb.HermiT.Prefixes getHermiTPrefixes() { | ||
| 27 | return hermit; | ||
| 28 | } | ||
| 29 | |||
| 30 | public uk.ac.ox.cs.JRDFox.Prefixes getRDFoxPrefixes() { | ||
| 31 | return rdfox; | ||
| 32 | } | ||
| 33 | |||
| 34 | public void declarePrefix(String prefixName, String prefixIRI) { | ||
| 35 | Utility.logDebug("Prefix declared: " + prefixName + "=" + prefixIRI); | ||
| 36 | hermit.declarePrefix(prefixName, prefixIRI); | ||
| 37 | rdfox.declarePrefix(prefixName, prefixIRI); | ||
| 38 | } | ||
| 39 | |||
| 40 | public static boolean isColon4Prefixes(String text, int index) { | ||
| 41 | if (index >= text.length() - 1) return false; | ||
| 42 | if (index > 0 && text.charAt(index - 1) == '_') return false; // _: blank node | ||
| 43 | char nextCharacter = text.charAt(index + 1); | ||
| 44 | if (nextCharacter == '-') return false; // :- | ||
| 45 | if (nextCharacter == '/') return false; // :/ | ||
| 46 | return true; | ||
| 47 | } | ||
| 48 | |||
| 49 | public String expandText(String text) { | ||
| 50 | for (int index = 0; ;) { | ||
| 51 | index = text.indexOf(':', index); | ||
| 52 | if (!isColon4Prefixes(text, index)) { | ||
| 53 | ++index; | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | if (index == -1) return text; | ||
| 57 | int start = index - 1, ends = index + 1; | ||
| 58 | char ch; | ||
| 59 | while (start >= 0 && (ch = text.charAt(start)) != ',' && ch != '(' && ch != ' ') --start; | ||
| 60 | while (ends < text.length() && (ch = text.charAt(ends)) != '(' && ch != ',' && ch != ')') ++ends; | ||
| 61 | |||
| 62 | String sub = text.substring(start + 1, ends); | ||
| 63 | String newSub = OWLHelper.addAngles(hermit.expandAbbreviatedIRI(sub)); | ||
| 64 | |||
| 65 | text = text.replaceAll(sub, newSub); | ||
| 66 | index = ends + newSub.length() - sub.length(); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | public void declareNewPrefix(String iri) { | ||
| 71 | if (!iri.startsWith("<")) return ; | ||
| 72 | int index = splitPoint(iri); | ||
| 73 | if (index >= 0) { | ||
| 74 | String prefixIRI = iri.substring(1, index + 1), prefixName; | ||
| 75 | if ((prefixName = getPrefixName(prefixIRI)) == null) { | ||
| 76 | prefixName = getNewPrefixName(); | ||
| 77 | declarePrefix(prefixName, prefixIRI); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | /** | ||
| 83 | * detect the split point of prefix and name for an <url> | ||
| 84 | * | ||
| 85 | * @param uri | ||
| 86 | * @param index | ||
| 87 | * @return | ||
| 88 | */ | ||
| 89 | private int splitPoint(String uri) { | ||
| 90 | int index = uri.lastIndexOf("#"); | ||
| 91 | if (index != -1) return index; | ||
| 92 | index = uri.lastIndexOf("/"); | ||
| 93 | if (index > 0 && uri.charAt(index - 1) != '/') | ||
| 94 | return index; | ||
| 95 | return -1; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | private int prefixNumber = 0; | ||
| 100 | |||
| 101 | public String getNewPrefixName() { | ||
| 102 | return "prefix" + (prefixNumber ++) + ":"; | ||
| 103 | } | ||
| 104 | |||
| 105 | public String abbreviateIRI(String iri) { | ||
| 106 | declareNewPrefix(iri); | ||
| 107 | |||
| 108 | if (iri.startsWith("<")) | ||
| 109 | return hermit.abbreviateIRI(OWLHelper.removeAngles(iri)); | ||
| 110 | else if (iri.contains("^^")) { | ||
| 111 | int index = iri.indexOf("^^"); | ||
| 112 | if (iri.charAt(index + 2) == '<') | ||
| 113 | return iri.substring(0, index + 2) + abbreviateIRI(iri.substring(index + 2, iri.lastIndexOf('>') + 1)); | ||
| 114 | } | ||
| 115 | return iri; | ||
| 116 | } | ||
| 117 | |||
| 118 | /** | ||
| 119 | * expanded iri without <> | ||
| 120 | * | ||
| 121 | * @param iri | ||
| 122 | * @return | ||
| 123 | */ | ||
| 124 | public String expandIRI(String iri) { | ||
| 125 | if (hermit.canBeExpanded(iri)) | ||
| 126 | return hermit.expandAbbreviatedIRI(iri); | ||
| 127 | else if (iri.startsWith("<")) | ||
| 128 | return iri.substring(1, iri.length() - 1); | ||
| 129 | else | ||
| 130 | return iri; | ||
| 131 | } | ||
| 132 | |||
| 133 | /** | ||
| 134 | * expanded iri with <> | ||
| 135 | * | ||
| 136 | * @param iri | ||
| 137 | * @return | ||
| 138 | */ | ||
| 139 | public String getQuotedIRI(String iri) { | ||
| 140 | if (iri.startsWith("<")) return iri; | ||
| 141 | if (hermit.canBeExpanded(iri)) | ||
| 142 | return OWLHelper.addAngles(hermit.expandAbbreviatedIRI(iri)); | ||
| 143 | return iri; | ||
| 144 | } | ||
| 145 | |||
| 146 | public String getPrefixName(String prefixIRI) { | ||
| 147 | return hermit.getPrefixName(prefixIRI); | ||
| 148 | } | ||
| 149 | |||
| 150 | public Map<String, String> getPrefixIRIsByPrefixName() { | ||
| 151 | return hermit.getPrefixIRIsByPrefixName(); | ||
| 152 | } | ||
| 153 | |||
| 154 | public String prefixesText() { | ||
| 155 | StringBuffer buf = new StringBuffer(); | ||
| 156 | for (Map.Entry<String, String> entry: getPrefixIRIsByPrefixName().entrySet()) | ||
| 157 | buf.append("PREFIX ").append(entry.getKey()).append(" <").append(entry.getValue()).append(">").append(Utility.LINE_SEPARATOR); | ||
| 158 | return buf.toString(); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | } | ||
