From: <jen...@us...> - 2008-02-19 14:28:37
|
Revision: 611 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=611&view=rev Author: jenslehmann Date: 2008-02-19 06:28:23 -0800 (Tue, 19 Feb 2008) Log Message: ----------- - continued carcinogenesis mapper - fixed bug in KB file (relevant for component creation in ComponentManager) Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java trunk/src/dl-learner/org/dllearner/kb/KBFile.java Modified: trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java 2008-02-19 13:20:01 UTC (rev 610) +++ trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java 2008-02-19 14:28:23 UTC (rev 611) @@ -73,79 +73,77 @@ public class Carcinogenesis { private static URI ontologyURI = URI.create("http://dl-learner.org/carcinogenesis"); + + // directory of Prolog files + private static String prologDirectory = "examples/carcinogenesis/prolog/"; // mapping of symbols to names of chemical elements - private static Map<String,String> chemElements; - + private static Map<String, String> chemElements; + // types of atoms and bonds private static Set<String> atomTypes = new TreeSet<String>(); private static Set<String> bondTypes = new TreeSet<String>(); - + // we need a counter for bonds, because they are instances in OWL // but not in Prolog private static int bondNr = 0; - + /** * @param args * No arguments supported. + * @throws IOException + * @throws FileNotFoundException + * @throws ParseException */ - public static void main(String[] args) { + public static void main(String[] args) throws FileNotFoundException, IOException, + ParseException { - 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 { - // 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 = 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) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + // 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 = pp.parseProgram(content); + duration = System.nanoTime() - startTime; + time = Helper.prettyPrintNanoSeconds(duration, false, false); + System.out.println("OK (" + time + ")."); + // prepare mapping - KB kb = new KB(); + KB kb = new KB(); createChemElementsMapping(); // create subclasses of atom NamedClass atomClass = getAtomicConcept("Atom"); - for(String element : chemElements.values()) { + for (String element : chemElements.values()) { NamedClass elClass = getAtomicConcept(element); SubClassAxiom sc = new SubClassAxiom(elClass, atomClass); kb.addAxiom(sc); } // define properties including domain and range // ... TODO ... - + // mapping clauses to axioms System.out.print("Mapping clauses to axioms ... "); - startTime = System.nanoTime(); + startTime = System.nanoTime(); ArrayList<Clause> clauses = program.getClauses(); for (Clause clause : clauses) { List<Axiom> axioms = mapClause(clause); @@ -154,7 +152,7 @@ } duration = System.nanoTime() - startTime; time = Helper.prettyPrintNanoSeconds(duration, false, false); - System.out.println("OK (" + time + ")."); + System.out.println("OK (" + time + ")."); // writing generated knowledge base System.out.print("Writing OWL file ... "); @@ -162,7 +160,27 @@ OWLAPIReasoner.exportKBToOWL(owlFile, kb, ontologyURI); duration = System.nanoTime() - startTime; time = Helper.prettyPrintNanoSeconds(duration, false, false); - System.out.println("OK (" + time + ")."); + System.out.println("OK (" + time + ")."); + + // generating conf files + File confTrainFile = new File("examples/carcinogenesis/train.conf"); + String confHeader = ""; + Files.appendFile(confTrainFile, confHeader); + + // generating training examples + File trainingFilePositives = new File(prologDirectory + "train.f"); + File trainingFileNegatives = new File(prologDirectory + "train.n"); + + List<Individual> posTrainExamples = getExamples(trainingFilePositives); + List<Individual> negTrainExamples = getExamples(trainingFileNegatives); + appendPosExamples(confTrainFile, posTrainExamples); + appendNegExamples(confTrainFile, negTrainExamples); + + // generating testExamples +// File confTestFile = new File("examples/carcinogenesis/test.conf"); +// File testFilePositives = new File(prologDirectory + "train.f"); +// File testFileNegatives = new File(prologDirectory + "train.n"); +// } private static List<Axiom> mapClause(Clause clause) { @@ -180,14 +198,14 @@ String type = head.getArgument(3).toPLString(); double charge = Double.parseDouble(head.getArgument(4).toPLString()); // relate compound and atom - ObjectPropertyAssertion ra = getRoleAssertion("hasAtom", compoundName, atomName); + ObjectPropertyAssertion ra = getRoleAssertion("hasAtom", compoundName, atomName); axioms.add(ra); // atom is made instance of the correct class String atomClass = getAtomClass(elementName, type); - ClassAssertionAxiom ca = getConceptAssertion(atomClass,atomName); + ClassAssertionAxiom ca = getConceptAssertion(atomClass, atomName); axioms.add(ca); // write subclass axiom if doesn't exist already - if(!atomTypes.contains(atomClass)) { + if (!atomTypes.contains(atomClass)) { NamedClass subClass = getAtomicConcept(atomClass); NamedClass superClass = getAtomicConcept(getFullElementName(elementName)); SubClassAxiom sc = new SubClassAxiom(subClass, superClass); @@ -195,9 +213,10 @@ atomTypes.add(atomClass); } // charge of atom - DatatypePropertyAssertion dpa = getDoubleDatatypePropertyAssertion(atomName, "charge", charge); + DatatypePropertyAssertion dpa = getDoubleDatatypePropertyAssertion(atomName, "charge", + charge); axioms.add(dpa); - } else if(headName.equals("bond")) { + } else if (headName.equals("bond")) { String compoundName = head.getArgument(0).toPLString(); String atom1Name = head.getArgument(1).toPLString(); String atom2Name = head.getArgument(2).toPLString(); @@ -207,10 +226,10 @@ ObjectPropertyAssertion op = getRoleAssertion("hasBond", compoundName, "bond" + bondNr); axioms.add(op); // make Bond-X subclass of Bond if that hasn't been done already - if(!bondTypes.contains(bondClass)) { + if (!bondTypes.contains(bondClass)) { NamedClass subClass = getAtomicConcept(bondClass); SubClassAxiom sc = new SubClassAxiom(subClass, getAtomicConcept("Bond")); - axioms.add(sc); + axioms.add(sc); bondTypes.add(bondClass); } // make e.g. bond382 instance of Bond-3 @@ -226,46 +245,77 @@ // print clauses which are not supported yet System.out.println("unsupported clause"); System.out.println(clause.toPLString()); - System.out.println(clause); + System.out.println(clause); } return axioms; } + + // takes a *.f or *.n file as input and returns the + // contained examples + private static List<Individual> getExamples(File file) throws FileNotFoundException, IOException, ParseException { + String content = Files.readFile(new File(prologDirectory + file)); + PrologParser pp = new PrologParser(); + Program programPos = pp.parseProgram(content); + List<Individual> ret = new LinkedList<Individual>(); + for(Clause c : programPos.getClauses()) { + String example = c.getHead().getArgument(0).toPLString(); + ret.add(getIndividual(example)); + } + return ret; + } + private static void appendPosExamples(File file, List<Individual> examples) { + StringBuffer content = new StringBuffer(); + for(Individual example : examples) { + content.append("+\""+example.toString()+"\""); + } + Files.appendFile(file, content.toString()); + } + + private static void appendNegExamples(File file, List<Individual> examples) { + StringBuffer content = new StringBuffer(); + for(Individual example : examples) { + content.append("-\""+example.toString()+"\""); + } + Files.appendFile(file, content.toString()); + } + private static String getAtomClass(String element, String atomType) { return getFullElementName(element) + "-" + atomType; } - + private static ClassAssertionAxiom getConceptAssertion(String concept, String i) { Individual ind = getIndividual(i); NamedClass c = getAtomicConcept(concept); - return new ClassAssertionAxiom(c,ind); + return new ClassAssertionAxiom(c, ind); } - + private static ObjectPropertyAssertion getRoleAssertion(String role, String i1, String i2) { Individual ind1 = getIndividual(i1); Individual ind2 = getIndividual(i2); ObjectProperty ar = getRole(role); - return new ObjectPropertyAssertion(ar,ind1,ind2); + return new ObjectPropertyAssertion(ar, ind1, ind2); } - - private static DoubleDatatypePropertyAssertion getDoubleDatatypePropertyAssertion(String individual, String datatypeProperty, double value) { + + private static DoubleDatatypePropertyAssertion getDoubleDatatypePropertyAssertion( + String individual, String datatypeProperty, double value) { Individual ind = getIndividual(individual); DatatypeProperty dp = getDatatypeProperty(datatypeProperty); - return new DoubleDatatypePropertyAssertion(dp,ind,value); - } - + return new DoubleDatatypePropertyAssertion(dp, ind, value); + } + private static Individual getIndividual(String name) { return new Individual(ontologyURI + "#" + name); - } - + } + private static ObjectProperty getRole(String name) { return new ObjectProperty(ontologyURI + "#" + name); - } - + } + private static DatatypeProperty getDatatypeProperty(String name) { return new DatatypeProperty(ontologyURI + "#" + name); - } - + } + private static NamedClass getAtomicConcept(String name) { return new NamedClass(ontologyURI + "#" + name); } @@ -274,15 +324,15 @@ // return corresponding element or throw an error if it // is not in the list String result = chemElements.get(abbreviation); - if(result == null) + if (result == null) throw new Error("Unknown element " + abbreviation); else - return result; - } - + return result; + } + // create chemical element list private static void createChemElementsMapping() { - chemElements = new HashMap<String,String>(); + chemElements = new HashMap<String, String>(); chemElements.put("as", "Arsenic"); chemElements.put("ba", "Barium"); chemElements.put("br", "Bromine"); Modified: trunk/src/dl-learner/org/dllearner/kb/KBFile.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/KBFile.java 2008-02-19 13:20:01 UTC (rev 610) +++ trunk/src/dl-learner/org/dllearner/kb/KBFile.java 2008-02-19 14:28:23 UTC (rev 611) @@ -61,6 +61,13 @@ private KB kb; /** + * Default constructor (needed for reflection in ComponentManager). + */ + public KBFile() { + + } + + /** * Constructor allowing you to treat an already existing KB object * as a KBFile knowledge source. Use it sparingly, because the * standard way to create components is via This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |