Skip to content

Commit

Permalink
fixed tests, more renderers, default StringRenderer is now Manchester…
Browse files Browse the repository at this point in the history
…Renderer
  • Loading branch information
stefborg committed Jan 12, 2016
1 parent 7f53a28 commit 36224a8
Show file tree
Hide file tree
Showing 22 changed files with 355 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import de.tudresden.inf.lat.uel.type.api.Equation;
import de.tudresden.inf.lat.uel.type.api.Goal;
import de.tudresden.inf.lat.uel.type.api.Subsumption;
import de.tudresden.inf.lat.uel.type.cons.KRSSKeyword;
import de.tudresden.inf.lat.uel.type.cons.RendererKeywords;
import de.tudresden.inf.lat.uel.type.impl.ExistentialRestriction;

/**
Expand Down Expand Up @@ -62,28 +62,28 @@ private void updateProgram() {
encoding.append("relevant(x");
encoding.append(var);
encoding.append(").");
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}
program = encoding.toString();
}

private void encodeDefinition(StringBuilder encoding, Definition d, int index) {
encoding.append("%definition ");
encoding.append(index);
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
// lhs
encodePositiveAtom(encoding, d.getDefiniendum(), 0, index);
// rhs
for (Integer rightId : d.getRight()) {
encodePositiveAtom(encoding, rightId, 1, index);
}
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodeEquation(StringBuilder encoding, Equation e, int index) {
encoding.append("%equation ");
encoding.append(index);
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
// lhs
for (Integer leftId : e.getLeft()) {
encodePositiveAtom(encoding, leftId, 0, index);
Expand All @@ -92,13 +92,13 @@ private void encodeEquation(StringBuilder encoding, Equation e, int index) {
for (Integer rightId : e.getRight()) {
encodePositiveAtom(encoding, rightId, 1, index);
}
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodeSubsumption(StringBuilder encoding, Subsumption s, int index) {
encoding.append("%subsumption ");
encoding.append(index);
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
// lhs
for (Integer leftId : s.getLeft()) {
encodePositiveAtom(encoding, leftId, 0, index);
Expand All @@ -108,13 +108,13 @@ private void encodeSubsumption(StringBuilder encoding, Subsumption s, int index)
for (Integer rightId : s.getRight()) {
encodePositiveAtom(encoding, rightId, 1, index);
}
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodeDisequation(StringBuilder encoding, Disequation e, int index) {
encoding.append("%disequation ");
encoding.append(index);
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
// lhs
for (Integer leftId : e.getLeft()) {
encodeNegativeAtom(encoding, leftId, 0, index);
Expand All @@ -123,13 +123,13 @@ private void encodeDisequation(StringBuilder encoding, Disequation e, int index)
for (Integer rightId : e.getRight()) {
encodeNegativeAtom(encoding, rightId, 1, index);
}
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodeDissubsumption(StringBuilder encoding, Dissubsumption s, int index) {
encoding.append("%disequation ");
encoding.append(index);
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
// lhs
for (Integer leftId : s.getLeft()) {
encodeNegativeAtom(encoding, leftId, 0, index);
Expand All @@ -138,11 +138,11 @@ private void encodeDissubsumption(StringBuilder encoding, Dissubsumption s, int
for (Integer rightId : s.getRight()) {
encodeNegativeAtom(encoding, rightId, 1, index);
}
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
encoding.append("lefttoright(");
encoding.append(index);
encoding.append(").");
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodePositiveAtom(StringBuilder encoding, Integer atomId, int side, int equationId) {
Expand All @@ -153,7 +153,7 @@ private void encodePositiveAtom(StringBuilder encoding, Integer atomId, int side
encoding.append(", ");
encoding.append(equationId);
encoding.append(").");
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodeNegativeAtom(StringBuilder encoding, Integer atomId, int side, int disequationId) {
Expand All @@ -164,7 +164,7 @@ private void encodeNegativeAtom(StringBuilder encoding, Integer atomId, int side
encoding.append(", ");
encoding.append(disequationId);
encoding.append(").");
encoding.append(KRSSKeyword.newLine);
encoding.append(RendererKeywords.newLine);
}

private void encodeAtom(StringBuilder encoding, Atom atom) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.tudresden.inf.lat.uel.core.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -123,11 +123,10 @@ public static void main(String[] args) {
}

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
AlternativeUelStarter starter = new AlternativeUelStarter(loadOntology(mainFilename, manager));
starter.setVerbose(printInfo);
if (!owlThingAliasName.isEmpty()) {
starter.setOwlThingAlias(factory.getOWLClass(IRI.create(owlThingAliasName)));
starter.setOwlThingAlias(OWLManager.getOWLDataFactory().getOWLClass(IRI.create(owlThingAliasName)));
}

OWLOntology subsumptions = loadOntology(subsFilename, manager);
Expand All @@ -138,7 +137,7 @@ public static void main(String[] args) {
if (dissubsumptions == null) {
return;
}
Set<OWLClass> variables = loadVariables(varFilename, factory);
Set<OWLClass> variables = loadVariables(varFilename);
if (variables == null) {
return;
}
Expand Down Expand Up @@ -179,38 +178,35 @@ private static void printSyntax() {
"Usage: uel [-s subsumptions.owl] [-d dissubsumptions.owl] [-v variables.txt] [-t owl:Thing_alias] [-a algorithmIndex] [-h] [-i] [ontology.owl]");
}

public static Set<OWLClass> loadVariables(String filename, OWLDataFactory factory) {
if (filename.isEmpty()) {
return Collections.emptySet();
}
static Set<OWLClass> loadVariables(String filename) {
try {
if (filename.isEmpty()) {
return Collections.emptySet();
}

BufferedReader input = new BufferedReader(new FileReader(new File(filename)));
OWLDataFactory factory = OWLManager.getOWLDataFactory();
Set<OWLClass> variables = new HashSet<OWLClass>();
String line = "";
while (line != null) {
line = input.readLine();
if ((line != null) && !line.isEmpty()) {

for (String line : Files.readAllLines(Paths.get(filename))) {
if (!line.isEmpty()) {
variables.add(factory.getOWLClass(IRI.create(line)));
}
}
input.close();

return variables;
} catch (FileNotFoundException e) {
System.err.println("Could not find file '" + filename + "'.");
return null;
} catch (IOException e) {
System.err.println("Unknown I/O error while reading file '" + filename + "'.");
System.err.println("Error while reading file '" + filename + "'.");
System.err.println(e.getMessage());
return null;
}
}

public static OWLOntology loadOntology(String filename, OWLOntologyManager manager) {
private static OWLOntology loadOntology(String filename, OWLOntologyManager manager) {
try {
if (filename.isEmpty()) {
return manager.createOntology();
}

InputStream input = new FileInputStream(new File(filename));
return manager.loadOntologyFromOntologyDocument(input);
} catch (FileNotFoundException e) {
Expand Down Expand Up @@ -264,7 +260,7 @@ private Iterator<Set<OWLEquivalentClassesAxiom>> modifyOntologyAndSolve(UelModel
System.out.println("Final number of subsumptions: " + goal.getSubsumptions().size());
System.out.println("Final number of dissubsumptions: " + goal.getDissubsumptions().size());
System.out.println("(Dis-)Unification problem:");
System.out.println(uelModel.printGoal(true));
System.out.println(uelModel.printGoal());
}

uelModel.initializeUnificationAlgorithm(algorithmName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public UnifierIterator(UelModel uelModel) {
this.uelModel = uelModel;
}

UelModel getUelModel() {
return uelModel;
}

private void compute() {
UnificationAlgorithm algorithm = uelModel.getUnificationAlgorithm();
if (!isComputed) {
Expand All @@ -44,6 +40,10 @@ private void compute() {
}
}

UelModel getUelModel() {
return uelModel;
}

@Override
public boolean hasNext() {
compute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ private static OWLOntology createEmptyOntology() {
}
}

public static String removeQuotes(String str) {
String ret = str;
if ((str.startsWith("\"") && str.endsWith("\"")) || (str.startsWith("'") && str.endsWith("'"))) {
ret = str.substring(1, str.length() - 1);
}
return ret;
}

private boolean allUnifiersFound;
private AtomManager atomManager;
private int currentUnifierIndex;
Expand All @@ -65,7 +73,7 @@ public UelModel(OntologyProvider provider) {
private void addAllShortForms(OWLOntology ontology, Set<? extends OWLEntity> entities) {
for (OWLEntity entity : entities) {
String shortForm = getShortForm(entity, ontology);
shortFormMap.put(entity.toStringID(), StringRenderer.removeQuotes(shortForm));
shortFormMap.put(entity.toStringID(), removeQuotes(shortForm));
}
}

Expand Down Expand Up @@ -148,9 +156,8 @@ private String getShortForm(OWLEntity entity, OWLOntology ontology) {
return entity.getIRI().getShortForm();
}

// TODO check if the first parameter is still needed
public StringRenderer getStringRenderer(boolean shortForm, Set<Definition> background) {
return StringRenderer.createInstance(atomManager, shortForm ? shortFormMap : null, background);
public StringRenderer getStringRenderer(Set<Definition> background) {
return StringRenderer.createInstance(atomManager, shortFormMap, background);
}

public UnificationAlgorithm getUnificationAlgorithm() {
Expand Down Expand Up @@ -222,20 +229,20 @@ public void markUndefAsUserVariables() {
}
}

public String printCurrentUnifier(boolean shortForm) {
public String printCurrentUnifier() {
Unifier unifier = getCurrentUnifier();
if (unifier == null) {
return "";
}
return printUnifier(shortForm, unifier);
return printUnifier(unifier);
}

public String printGoal(boolean shortForm) {
return getStringRenderer(shortForm, null).renderGoal(goal);
public String printGoal() {
return getStringRenderer(null).renderGoal(goal);
}

public String printUnifier(boolean shortForm, Unifier unifier) {
return getStringRenderer(shortForm, unifier.getDefinitions()).renderDefinitions(unifier.getDefinitions());
public String printUnifier(Unifier unifier) {
return getStringRenderer(unifier.getDefinitions()).renderUnifier(unifier);
}

public void recomputeShortFormMap() {
Expand All @@ -254,8 +261,12 @@ public Set<OWLAxiom> renderCurrentUnifier() {
return renderUnifier(unifier);
}

public Set<OWLAxiom> renderDefinitions() {
return getOWLRenderer(null).renderAxioms(goal.getDefinitions());
}

public Set<OWLAxiom> renderUnifier(Unifier unifier) {
return getOWLRenderer(unifier.getDefinitions()).renderDefinitions(unifier.getDefinitions());
return getOWLRenderer(unifier.getDefinitions()).renderUnifier(unifier);
}

public void setCurrentUnifierIndex(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ public Set<Integer> processClassExpression(OWLClassExpression expression, Set<De
Integer nameId = toVisit.iterator().next();

if (!visited.contains(nameId)) {
createFlatDefinition(nameId, newDefinitions, toVisit);
loadFlatDefinition(nameId, newDefinitions, toVisit);
visited.add(nameId);
toVisit.remove(nameId);
}
}
return conjunction;
}

private void createFlatDefinition(Integer id, Set<Definition> newDefinitions, Set<Integer> toVisit) {
private void loadFlatDefinition(Integer id, Set<Definition> newDefinitions, Set<Integer> toVisit) {
OWLClass cls = nameMap.get(id);
if (cls.equals(top)) {
// do not expand definitions beyond top
Expand Down Expand Up @@ -165,8 +165,6 @@ private void createFlatDefinition(Integer id, Set<Definition> newDefinitions, Se

Set<Integer> right = flattenClassExpression(expression, newDefinitions, toVisit);
atomManager.makeDefinitionVariable(id);
// TODO introduce new 'undef' variables for primitive definitions
// already here?
newDefinitions.add(new Definition(id, right, primitive));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.tudresden.inf.lat.uel.core.renderer;

import java.util.Map;
import java.util.Set;

import de.tudresden.inf.lat.uel.type.api.AtomManager;
import de.tudresden.inf.lat.uel.type.api.Definition;
import de.tudresden.inf.lat.uel.type.cons.RendererKeywords;

public class FunctionalRenderer extends StringRenderer {

protected FunctionalRenderer(AtomManager atomManager, Map<String, String> shortFormMap,
Set<Definition> background) {
super(atomManager, shortFormMap, background);
}

@Override
protected String translateExistentialRestriction(String roleName, Integer childId) {
sb.append(RendererKeywords.objectSomeValuesFrom);
sb.append(RendererKeywords.open);
sb.append(roleName);
sb.append(RendererKeywords.space);
translateChild(childId);
sb.append(RendererKeywords.close);
return "";
}

@Override
protected String translateTop() {
sb.append(RendererKeywords.owlThing);
return "";
}

@Override
protected String translateTrueConjunction(Set<Integer> atomIds) {
sb.append(RendererKeywords.objectIntersectionOf);
sb.append(RendererKeywords.open);

for (Integer atomId : atomIds) {
translateAtom(atomId);
sb.append(RendererKeywords.space);
}

sb.setLength(sb.length() - RendererKeywords.space.length());
sb.append(RendererKeywords.close);
return "";
}

}
Loading

0 comments on commit 36224a8

Please sign in to comment.