From: <jen...@us...> - 2008-02-10 17:52:44
|
Revision: 535 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=535&view=rev Author: jenslehmann Date: 2008-02-10 09:52:40 -0800 (Sun, 10 Feb 2008) Log Message: ----------- started Prolog to OWL mapping for carcinogenesis data Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/core/dl/KB.java trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java trunk/src/dl-learner/org/dllearner/examples/PokerTransformer.java trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java trunk/src/dl-learner/org/dllearner/utilities/Files.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/examples/PokerCard.java trunk/src/dl-learner/org/dllearner/examples/PokerHand.java Removed Paths: ------------- trunk/src/dl-learner/org/dllearner/examples/Card.java trunk/src/dl-learner/org/dllearner/examples/Hand.java Modified: trunk/src/dl-learner/org/dllearner/core/dl/KB.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/dl/KB.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/core/dl/KB.java 2008-02-10 17:52:40 UTC (rev 535) @@ -134,6 +134,24 @@ return tbox; } + /** + * Convenience method, which adds an axiom to ABox, RBox, or + * TBox depending on whether it is an assertional, role, or + * terminological axiom. + * + * @param axiom Axiom to add. + */ + public void addAxiom(Axiom axiom) { + if(axiom instanceof AssertionalAxiom) + addABoxAxiom((AssertionalAxiom) axiom); + else if(axiom instanceof RBoxAxiom) + addRBoxAxiom((RBoxAxiom) axiom); + else if(axiom instanceof TerminologicalAxiom) + addTBoxAxiom((TerminologicalAxiom) axiom); + else + throw new Error(axiom + " has unsupported axiom type."); + } + public void addABoxAxiom(AssertionalAxiom axiom) { abox.add(axiom); } Modified: trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java 2008-02-10 17:52:40 UTC (rev 535) @@ -22,11 +22,25 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import org.dllearner.core.dl.AtomicConcept; +import org.dllearner.core.dl.AtomicRole; +import org.dllearner.core.dl.Axiom; +import org.dllearner.core.dl.Individual; +import org.dllearner.core.dl.KB; +import org.dllearner.core.dl.RoleAssertion; import org.dllearner.parser.ParseException; import org.dllearner.parser.PrologParser; +import org.dllearner.prolog.Atom; +import org.dllearner.prolog.Clause; import org.dllearner.prolog.Program; +import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.utilities.Files; +import org.dllearner.utilities.Helper; /** * This class maps the carcinogenesis Prolog files to an OWL file. In a first @@ -34,23 +48,60 @@ * applying mapping Prolog clauses to OWL axioms through domain specific mapping * rules. * + * The carcinogenesis Prolog files are available here: + * http://web.comlab.ox.ac.uk/oucl/research/areas/machlearn/cancer.html + * + * .f files contain positive and .n files contain negative examples. pte1.n and + * pte.f contain the PTE-1 challenge examples. train.n and train.f contain other + * examples which can be used to train for PTE-1. + * + * The PTE-2 directory contains PTE-2 files, i.e. all substances referred to in + * those files are only those of the PTE-2 challenge. + * * @author Jens Lehmann * */ public class Carcinogenesis { + private static URI ontologyURI = URI.create("http://dl-learner.org/carcinogenesis"); + /** * @param args + * No arguments supported. */ public static void main(String[] args) { + String prologDirectory = "examples/carcinogenesis/prolog/"; + String[] files = new String[] { "ames.pl", "atoms.pl", "bonds.pl", "gentoxprops.pl", + "ind_nos.pl", "ind_pos.pl", "newgroups.pl", + // "train.b" => not a pure Prolog file but Progol/Aleph specific + }; + File owlFile = new File("examples/carcinogenesis/pte.owl"); + + Program program = null; + long startTime, duration; + String time; + try { - String prologFile = "examples/carcinogenesis/prolog/atoms.pl"; - File file = new File(prologFile); - String content = Files.readFile(file); + // reading files + System.out.print("Reading in carcinogenesis Prolog files ... "); + startTime = System.nanoTime(); + String content = ""; + for (String file : files) { + content += Files.readFile(new File(prologDirectory + file)); + } + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + + // parsing files + System.out.print("Parsing Prolog files ... "); + startTime = System.nanoTime(); PrologParser pp = new PrologParser(); - Program program = pp.parseProgram(content); - System.out.println(program.toPLString()); + program = pp.parseProgram(content); + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); } catch (ParseException e) { e.printStackTrace(); } catch (FileNotFoundException e) { @@ -59,6 +110,66 @@ e.printStackTrace(); } + // mapping clauses to axioms + System.out.print("Mapping clauses to axioms ... "); + startTime = System.nanoTime(); + ArrayList<Clause> clauses = program.getClauses(); + KB kb = new KB(); + for (Clause clause : clauses) { + List<Axiom> axioms = mapClause(clause); + for (Axiom axiom : axioms) + kb.addAxiom(axiom); + } + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + + // writing generated knowledge base + System.out.print("Writing OWL file ... "); + startTime = System.nanoTime(); + OWLAPIReasoner.exportKBToOWL(owlFile, kb, ontologyURI); + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); } + private static List<Axiom> mapClause(Clause clause) { + List<Axiom> axioms = new LinkedList<Axiom>(); + Atom head = clause.getHead(); + String headName = head.getName(); + // Body body = clause.getBody(); + // ArrayList<Literal> literals = body.getLiterals(); + if (headName.equals("atm")) { + // System.out.println(clause.toPLString()); + // System.out.println(clause); + String compoundName = head.getArgument(0).toPLString(); + String atomName = head.getArgument(1).toPLString(); + RoleAssertion ra = getRoleAssertion("hasAtom", compoundName, atomName); + axioms.add(ra); + } else { + + } + return axioms; + } + + private static RoleAssertion getRoleAssertion(String role, String i1, String i2) { + Individual ind1 = getIndividual(i1); + Individual ind2 = getIndividual(i2); + AtomicRole ar = getRole(role); + return new RoleAssertion(ar,ind1,ind2); + } + + private static Individual getIndividual(String name) { + return new Individual(ontologyURI + "#" + name); + } + + private static AtomicRole getRole(String name) { + return new AtomicRole(ontologyURI + "#" + name); + } + + @SuppressWarnings({"unused"}) + private static AtomicConcept getAtomicConcept(String name) { + return new AtomicConcept(ontologyURI + "#" + name); + } + } Deleted: trunk/src/dl-learner/org/dllearner/examples/Card.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/Card.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/examples/Card.java 2008-02-10 17:52:40 UTC (rev 535) @@ -1,71 +0,0 @@ -package org.dllearner.examples; - -public class Card { - - private int suit; - private int rank; - - /** - * - * @param suit Ordinal (1-4) representing {Hearts, Spades, Diamonds, Clubs}. - * @param rank Numerical (1-13) representing (Ace, 2, 3, ... , Queen, King) - */ - public Card(int suit, int rank) { - this.suit = suit; - this.rank = rank; - } - - public String getSuitString() { - switch(suit) { - case 1: return "hearts"; - case 2: return "spades"; - case 3: return "diamonds"; - case 4: return "clubs"; - } - throw new Error("Unknown suit code " + suit); - } - - public String getRankString() { - switch(rank) { - case 1: return "ace"; - case 2: return "two"; - case 3: return "three"; - case 4: return "four"; - case 5: return "five"; - case 6: return "six"; - case 7: return "seven"; - case 8: return "eight"; - case 9: return "nine"; - case 10: return "ten"; - case 11: return "jack"; - case 12: return "queen"; - case 13: return "king"; - } - throw new Error("Unknown rank code " + rank); - } - - public boolean hasSameSuit(Card card) { - return (suit == card.getSuit()); - } - - public boolean hasSameRank(Card card) { - return (rank == card.getRank()); - } - - // prüft, ob übergebene Karte den nächsten Rank hat - public boolean hasNextRank(Card card) { - // Spezialfall Ass: Vorgänger von 2 und Nachfolger von König - if(rank+1 == card.getRank() || (rank==13 && card.getRank()==1)) - return true; - else - return false; - } - - public int getRank() { - return rank; - } - - public int getSuit() { - return suit; - } -} Deleted: trunk/src/dl-learner/org/dllearner/examples/Hand.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/Hand.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/examples/Hand.java 2008-02-10 17:52:40 UTC (rev 535) @@ -1,38 +0,0 @@ -package org.dllearner.examples; - -public class Hand { - - private Card[] cards; - int handType; - - /** - * - * @param cards - * @param hand - * Ordinal (0-9) - * - * 0: Nothing in hand; not a recognized poker hand - * 1: One pair; one pair of equal ranks within five cards - * 2: Two pairs; two pairs of equal ranks within five cards - * 3: Three of a kind; three equal ranks within five cards - * 4: Straight; five cards, sequentially ranked with no gaps - * 5: Flush; five cards with the same suit - * 6: Full house; pair + different rank three of a kind - * 7: Four of a kind; four equal ranks within five cards - * 8: Straight flush; straight + flush - * 9: Royal flush; {Ace, King, Queen, Jack, Ten} + flush - */ - public Hand(Card[] cards, int hand) { - this.cards = cards; - this.handType = hand; - } - - public Card[] getCards() { - return cards; - } - - public int getHandType() { - return handType; - } - -} Copied: trunk/src/dl-learner/org/dllearner/examples/PokerCard.java (from rev 533, trunk/src/dl-learner/org/dllearner/examples/Card.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/PokerCard.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/examples/PokerCard.java 2008-02-10 17:52:40 UTC (rev 535) @@ -0,0 +1,76 @@ +package org.dllearner.examples; + +/** + * A poker card has one of 4 suits and 13 ranks. + * @author Jens Lehmann + * + */ +public class PokerCard { + + private int suit; + private int rank; + + /** + * + * @param suit Ordinal (1-4) representing {Hearts, Spades, Diamonds, Clubs}. + * @param rank Numerical (1-13) representing (Ace, 2, 3, ... , Queen, King) + */ + public PokerCard(int suit, int rank) { + this.suit = suit; + this.rank = rank; + } + + public String getSuitString() { + switch(suit) { + case 1: return "hearts"; + case 2: return "spades"; + case 3: return "diamonds"; + case 4: return "clubs"; + } + throw new Error("Unknown suit code " + suit); + } + + public String getRankString() { + switch(rank) { + case 1: return "ace"; + case 2: return "two"; + case 3: return "three"; + case 4: return "four"; + case 5: return "five"; + case 6: return "six"; + case 7: return "seven"; + case 8: return "eight"; + case 9: return "nine"; + case 10: return "ten"; + case 11: return "jack"; + case 12: return "queen"; + case 13: return "king"; + } + throw new Error("Unknown rank code " + rank); + } + + public boolean hasSameSuit(PokerCard card) { + return (suit == card.getSuit()); + } + + public boolean hasSameRank(PokerCard card) { + return (rank == card.getRank()); + } + + // prüft, ob übergebene Karte den nächsten Rank hat + public boolean hasNextRank(PokerCard card) { + // Spezialfall Ass: Vorgänger von 2 und Nachfolger von König + if(rank+1 == card.getRank() || (rank==13 && card.getRank()==1)) + return true; + else + return false; + } + + public int getRank() { + return rank; + } + + public int getSuit() { + return suit; + } +} Copied: trunk/src/dl-learner/org/dllearner/examples/PokerHand.java (from rev 533, trunk/src/dl-learner/org/dllearner/examples/Hand.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/PokerHand.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/examples/PokerHand.java 2008-02-10 17:52:40 UTC (rev 535) @@ -0,0 +1,44 @@ +package org.dllearner.examples; + +/** + * A poker hand consists of 5 cards. + * + * @author Jens Lehmann + * + */ +public class PokerHand { + + private PokerCard[] cards; + int handType; + + /** + * + * @param cards + * @param hand + * Ordinal (0-9) + * + * 0: Nothing in hand; not a recognized poker hand + * 1: One pair; one pair of equal ranks within five cards + * 2: Two pairs; two pairs of equal ranks within five cards + * 3: Three of a kind; three equal ranks within five cards + * 4: Straight; five cards, sequentially ranked with no gaps + * 5: Flush; five cards with the same suit + * 6: Full house; pair + different rank three of a kind + * 7: Four of a kind; four equal ranks within five cards + * 8: Straight flush; straight + flush + * 9: Royal flush; {Ace, King, Queen, Jack, Ten} + flush + */ + public PokerHand(PokerCard[] cards, int hand) { + this.cards = cards; + this.handType = hand; + } + + public PokerCard[] getCards() { + return cards; + } + + public int getHandType() { + return handType; + } + +} Modified: trunk/src/dl-learner/org/dllearner/examples/PokerTransformer.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/PokerTransformer.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/examples/PokerTransformer.java 2008-02-10 17:52:40 UTC (rev 535) @@ -60,27 +60,27 @@ lineNumber++; } - List<Hand> hands = new LinkedList<Hand>(); + List<PokerHand> hands = new LinkedList<PokerHand>(); for(String s : pokerExamples) { // System.out.println(s); StringTokenizer st = new StringTokenizer(s,","); // 5 Karten einlesen - Card[] cards = new Card[5]; + PokerCard[] cards = new PokerCard[5]; for(int i=0; i<=4; i++) { String suit = st.nextToken(); String rank = st.nextToken(); - cards[i] = new Card(new Integer(suit),new Integer(rank)); + cards[i] = new PokerCard(new Integer(suit),new Integer(rank)); } // Klasse auslesen String handType = st.nextToken(); - Hand hand = new Hand(cards, new Integer(handType)); + PokerHand hand = new PokerHand(cards, new Integer(handType)); // prüfen, ob Deck schon existiert (nicht gerade effizient, // aber OK) boolean handExists = false; - for(Hand d : hands) { + for(PokerHand d : hands) { if(equalDecks(d,hand)) { handExists = true; break; @@ -112,7 +112,7 @@ // für jede Karte die entsprechende Ausgabe machen int handNumber = 0; - for(Hand deck : hands) { + for(PokerHand deck : hands) { String handID = "hand" + handNumber; // Ausgabe der Hand (damit für Reasoner klar ist, dass es @@ -120,10 +120,10 @@ write(fos, "deck("+handID+")."); int cardNumber = handNumber*5; - Card[] cards = deck.getCards(); + PokerCard[] cards = deck.getCards(); // for(Card card : deck.getCards()) { for(int i=0; i<5; i++) { - Card card = cards[i]; + PokerCard card = cards[i]; String cardID = "card" + cardNumber; // Ausgabe der Karte (damit klar ist, dass es sich um ein @@ -139,7 +139,7 @@ // sameSuit, nextRank und sameRank ausgeben for(int j=i+1; j<5; j++) { - Card otherCard = cards[j]; + PokerCard otherCard = cards[j]; String otherCardID = "card" + (handNumber*5+j); if(card.hasSameSuit(otherCard)) write(fos, "sameSuit("+cardID+","+otherCardID+")."); @@ -150,7 +150,7 @@ // nextRank ist nicht symmetrisch, deshalb müssen alle // 4 anderen Karten geprüft werden for(int j=0; j<5; j++) { - Card otherCard = cards[j]; + PokerCard otherCard = cards[j]; String otherCardID = "card" + (handNumber*5+j); if(card.hasNextRank(otherCard)) { // Spezialfall: Ass @@ -217,8 +217,8 @@ } } - public boolean equalDecks(Hand deck1, Hand deck2) { - Card[] deck1Cards = deck1.getCards(); + public boolean equalDecks(PokerHand deck1, PokerHand deck2) { + PokerCard[] deck1Cards = deck1.getCards(); for(int i=0; i<5; i++) { if(!isInCards(deck1Cards[i],deck2)) return false; @@ -226,8 +226,8 @@ return true; } - public boolean isInCards(Card card, Hand deck) { - Card[] cards = deck.getCards(); + public boolean isInCards(PokerCard card, PokerHand deck) { + PokerCard[] cards = deck.getCards(); for(int j=0; j<5; j++) { // Test auf Gleichheit if(card.getSuit()==cards[j].getSuit() && card.getRank()==cards[j].getRank()) @@ -236,8 +236,8 @@ return false; } - public boolean hasKing(Hand deck) { - Card[] cards = deck.getCards(); + public boolean hasKing(PokerHand deck) { + PokerCard[] cards = deck.getCards(); for(int j=0; j<5; j++) { if(cards[j].getRank()==13) return true; Modified: trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-02-10 17:52:40 UTC (rev 535) @@ -19,6 +19,7 @@ */ package org.dllearner.reasoning; +import java.io.File; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -83,6 +84,9 @@ import org.semanticweb.owl.model.OWLOntologyChangeException; import org.semanticweb.owl.model.OWLOntologyCreationException; import org.semanticweb.owl.model.OWLOntologyManager; +import org.semanticweb.owl.model.OWLOntologyStorageException; +import org.semanticweb.owl.model.UnknownOWLOntologyException; +import org.semanticweb.owl.util.SimpleURIMapper; /** * Mapping to OWL API reasoner interface. The OWL API currently @@ -521,6 +525,29 @@ return new AtomicConcept(owlClass.getURI().toString()); } + public static void exportKBToOWL(File owlOutputFile, KB kb, URI ontologyURI) { + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + //URI ontologyURI = URI.create("http://example.com"); + URI physicalURI = owlOutputFile.toURI(); + SimpleURIMapper mapper = new SimpleURIMapper(ontologyURI, physicalURI); + manager.addURIMapper(mapper); + OWLOntology ontology; + try { + ontology = manager.createOntology(ontologyURI); + OWLAPIReasoner.fillOWLAPIOntology(manager, ontology, kb); + manager.saveOntology(ontology); + } catch (OWLOntologyCreationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnknownOWLOntologyException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (OWLOntologyStorageException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public static OWLObjectProperty getOWLAPIDescription(AtomicRole role) { return staticFactory.getOWLObjectProperty(URI.create(role.getName())); } Modified: trunk/src/dl-learner/org/dllearner/utilities/Files.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/Files.java 2008-02-10 16:08:51 UTC (rev 534) +++ trunk/src/dl-learner/org/dllearner/utilities/Files.java 2008-02-10 17:52:40 UTC (rev 535) @@ -20,13 +20,11 @@ package org.dllearner.utilities; import java.io.BufferedReader; -import java.io.DataInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; /** * @author Jens Lehmann @@ -42,15 +40,14 @@ * @return Content of the file. */ public static String readFile(File file) throws FileNotFoundException, IOException { - FileInputStream fstream = new FileInputStream(file); - DataInputStream in = new DataInputStream(fstream); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); + BufferedReader br = new BufferedReader(new FileReader(file)); String line; StringBuffer content = new StringBuffer(); while ((line = br.readLine()) != null) { content.append(line); + content.append(System.getProperty("line.separator")); } - in.close(); + br.close(); return content.toString(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |