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/hermit/RuleHelper.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/hermit/RuleHelper.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java | 173 |
1 files changed, 0 insertions, 173 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java b/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java deleted file mode 100644 index 43c5849..0000000 --- a/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java +++ /dev/null | |||
| @@ -1,173 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.hermit; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.*; | ||
| 4 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 5 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 6 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 7 | |||
| 8 | import java.security.InvalidParameterException; | ||
| 9 | import java.util.ArrayList; | ||
| 10 | |||
| 11 | public class RuleHelper { | ||
| 12 | private static final String OR = "|"; | ||
| 13 | private static final String IF = ":-"; | ||
| 14 | private static final String AND = ","; | ||
| 15 | |||
| 16 | // public static String abbreviateIRI(String text) { | ||
| 17 | // String prefixName, prefixIRI; | ||
| 18 | // int start = -1, ends = -1; | ||
| 19 | // while (true) { | ||
| 20 | // start = text.indexOf('<', ends + 1); | ||
| 21 | // if (start == -1) return text; | ||
| 22 | // ends = text.indexOf('>', start + 1); | ||
| 23 | // if (ends == -1) return text; | ||
| 24 | // String sub = text.substring(start, ends + 1), newSub = text.substring(start + 1, ends); | ||
| 25 | // | ||
| 26 | // int index = splitPoint(newSub); | ||
| 27 | // if (index >= 0) { | ||
| 28 | // prefixIRI = newSub.substring(0, index + 1); | ||
| 29 | // if ((prefixName = MyPrefixes.PAGOdAPrefixes.getPrefixName(prefixIRI)) == null) { | ||
| 30 | // prefixName = getNewPrefixName(); | ||
| 31 | // MyPrefixes.PAGOdAPrefixes.declarePrefix(prefixName, prefixIRI); | ||
| 32 | // } | ||
| 33 | // newSub = newSub.replace(prefixIRI, prefixName); | ||
| 34 | // text = text.replaceAll(sub, newSub); | ||
| 35 | // ends -= sub.length() - newSub.length(); | ||
| 36 | // } | ||
| 37 | // } | ||
| 38 | // } | ||
| 39 | |||
| 40 | public static String getText(DLClause clause) { | ||
| 41 | StringBuffer buf = new StringBuffer(); | ||
| 42 | String atomText; | ||
| 43 | |||
| 44 | boolean lastSpace = true; | ||
| 45 | for (Atom headAtom: clause.getHeadAtoms()) { | ||
| 46 | if ((atomText = getText(headAtom)) == null) continue; | ||
| 47 | if (!lastSpace) buf.append(" ").append(OR).append(" "); | ||
| 48 | buf.append(atomText); | ||
| 49 | lastSpace = false; | ||
| 50 | } | ||
| 51 | buf.append(" ").append(IF).append(" "); | ||
| 52 | lastSpace = true; | ||
| 53 | for (Atom bodyAtom: clause.getBodyAtoms()) { | ||
| 54 | // for (String str: strs[1].split(", ")) { | ||
| 55 | if ((atomText = getText(bodyAtom)) == null) continue; | ||
| 56 | if (!lastSpace) buf.append(AND).append(" "); | ||
| 57 | buf.append(atomText); | ||
| 58 | lastSpace = false; | ||
| 59 | } | ||
| 60 | buf.append('.'); | ||
| 61 | return buf.toString(); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | private static String getText(Atom atom) { | ||
| 66 | if (atom.getDLPredicate() instanceof NodeIDsAscendingOrEqual || | ||
| 67 | atom.getDLPredicate() instanceof NodeIDLessEqualThan) | ||
| 68 | return null; | ||
| 69 | |||
| 70 | StringBuilder builder = new StringBuilder(); | ||
| 71 | if (atom.getArity() == 1) { | ||
| 72 | builder.append(getText(atom.getDLPredicate())); | ||
| 73 | builder.append("("); | ||
| 74 | builder.append(getText(atom.getArgument(0))); | ||
| 75 | builder.append(")"); | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | DLPredicate p = atom.getDLPredicate(); | ||
| 79 | if (p instanceof Equality || p instanceof AnnotatedEquality) builder.append(Namespace.EQUALITY_ABBR); | ||
| 80 | else if (p instanceof Inequality) builder.append(Namespace.INEQUALITY_ABBR); | ||
| 81 | else builder.append(getText(p)); | ||
| 82 | builder.append("("); | ||
| 83 | builder.append(getText(atom.getArgument(0))); | ||
| 84 | builder.append(","); | ||
| 85 | builder.append(getText(atom.getArgument(1))); | ||
| 86 | builder.append(")"); | ||
| 87 | } | ||
| 88 | return builder.toString(); | ||
| 89 | } | ||
| 90 | |||
| 91 | public static String getText(DLPredicate p) { | ||
| 92 | if (p instanceof Equality || p instanceof AnnotatedEquality) return Namespace.EQUALITY_ABBR; | ||
| 93 | if (p instanceof Inequality) return Namespace.INEQUALITY_ABBR; | ||
| 94 | if (p instanceof AtomicRole && ((AtomicRole) p).getIRI().startsWith("?")) | ||
| 95 | return ((AtomicRole) p).getIRI(); | ||
| 96 | return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(p.toString()); | ||
| 97 | } | ||
| 98 | |||
| 99 | public static String getText(Term t) { | ||
| 100 | if (t instanceof Variable) | ||
| 101 | return "?" + ((Variable) t).getName(); | ||
| 102 | return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(t.toString()); | ||
| 103 | } | ||
| 104 | |||
| 105 | public static Term parseTerm(String s) { | ||
| 106 | s = s.trim(); | ||
| 107 | if(s.startsWith("?")) return Variable.create(s.substring(1)); | ||
| 108 | return Individual.create(MyPrefixes.PAGOdAPrefixes.expandIRI(s)); | ||
| 109 | } | ||
| 110 | |||
| 111 | public static Atom parseAtom(String s) { | ||
| 112 | s = s.trim(); | ||
| 113 | |||
| 114 | String[] split = s.split("\\("); | ||
| 115 | String predicateIri = OWLHelper.removeAngles(MyPrefixes.PAGOdAPrefixes.expandText(split[0])); | ||
| 116 | String[] predicateArgs = split[1].substring(0, split[1].length() - 1).split(","); | ||
| 117 | int numOfargs = predicateArgs.length; | ||
| 118 | Term terms[] = new Term[predicateArgs.length]; | ||
| 119 | for (int i = 0; i < terms.length; i++) | ||
| 120 | terms[i] = parseTerm(predicateArgs[i]); | ||
| 121 | if(numOfargs == 1) { | ||
| 122 | AtomicConcept atomicConcept = AtomicConcept.create(predicateIri); | ||
| 123 | return Atom.create(atomicConcept, terms); | ||
| 124 | } | ||
| 125 | else if(numOfargs == 2) { | ||
| 126 | AtomicRole atomicRole = AtomicRole.create(predicateIri); | ||
| 127 | return Atom.create(atomicRole, terms); | ||
| 128 | } | ||
| 129 | else | ||
| 130 | throw new InvalidParameterException(); | ||
| 131 | // TODO? add equality (owl:sameAs)? | ||
| 132 | } | ||
| 133 | |||
| 134 | public static DLClause parseClause(String s) { | ||
| 135 | s = s.trim(); | ||
| 136 | if(s.endsWith(".")) s = s.substring(0, s.length()-1).trim(); | ||
| 137 | |||
| 138 | String[] headAndBody = s.split(IF); | ||
| 139 | String[] headAtomsStr = splitAtoms(headAndBody[0], OR); | ||
| 140 | String[] bodyAtomsStr = splitAtoms(headAndBody[1], AND); | ||
| 141 | |||
| 142 | Atom[] headAtoms = new Atom[headAtomsStr.length]; | ||
| 143 | Atom[] bodyAtoms = new Atom[bodyAtomsStr.length]; | ||
| 144 | for (int i = 0; i < headAtoms.length; i++) | ||
| 145 | headAtoms[i] = parseAtom(headAtomsStr[i]); | ||
| 146 | for (int i = 0; i < bodyAtoms.length; i++) | ||
| 147 | bodyAtoms[i] = parseAtom(bodyAtomsStr[i]); | ||
| 148 | |||
| 149 | return DLClause.create(headAtoms, bodyAtoms); | ||
| 150 | } | ||
| 151 | |||
| 152 | private static String[] splitAtoms(String s, String operator) { | ||
| 153 | char op = operator.charAt(0); | ||
| 154 | ArrayList<String> result = new ArrayList<>(); | ||
| 155 | |||
| 156 | int b = 0; | ||
| 157 | boolean betweenParenthesis = false; | ||
| 158 | for (int i = 0; i < s.length(); i++) { | ||
| 159 | if(s.charAt(i) == '(') | ||
| 160 | betweenParenthesis = true; | ||
| 161 | else if(s.charAt(i) == ')') | ||
| 162 | betweenParenthesis = false; | ||
| 163 | else if(s.charAt(i) == op && !betweenParenthesis) { | ||
| 164 | result.add(s.substring(b, i)); | ||
| 165 | b = i + 1; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | if(b < s.length()) result.add(s.substring(b)); | ||
| 169 | |||
| 170 | return result.toArray(new String[0]); | ||
| 171 | } | ||
| 172 | |||
| 173 | } | ||
