001package org.dllearner.examples; 002 003import org.apache.log4j.Level; 004import org.apache.log4j.Logger; 005import org.dllearner.algorithms.celoe.CELOE; 006import org.dllearner.algorithms.celoe.OEHeuristicRuntime; 007import org.dllearner.core.*; 008import org.dllearner.core.StringRenderer.Rendering; 009import org.dllearner.kb.OWLAPIOntology; 010import org.dllearner.learningproblems.PosNegLPStandard; 011import org.dllearner.reasoning.ClosedWorldReasoner; 012import org.dllearner.reasoning.ExistentialRestrictionMaterialization; 013import org.dllearner.reasoning.OWLAPIReasoner; 014import org.dllearner.refinementoperators.RhoDRDown; 015import org.semanticweb.elk.owlapi.ElkReasoner; 016import org.semanticweb.owlapi.apibinding.OWLManager; 017import org.semanticweb.owlapi.model.*; 018import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl; 019 020import java.io.File; 021import java.util.*; 022 023public class ReactomeMinimal { 024 private static final Logger logger = Logger.getLogger(ReactomeMinimal.class); 025 private static final String kbPathStr = "/tmp/tr_cbd.owl"; 026 private static final List<String> posExampleUris = new ArrayList<>(Arrays.asList( 027 "http://www.reactome.org/biopax/48887#BiochemicalReaction670", 028 "http://www.reactome.org/biopax/48887#BiochemicalReaction1968", 029 "http://www.reactome.org/biopax/48887#BiochemicalReaction1331", 030 "http://www.reactome.org/biopax/48887#BiochemicalReaction3743", 031 "http://www.reactome.org/biopax/48887#BiochemicalReaction3244" 032 )); 033 private static final List<String> negExampleUris = new ArrayList<>(Arrays.asList( 034 "http://www.reactome.org/biopax/48887#BiochemicalReaction2588", 035 "http://www.reactome.org/biopax/48887#BiochemicalReaction4826", 036 "http://www.reactome.org/biopax/48887#Degradation10", 037 "http://www.reactome.org/biopax/48887#BiochemicalReaction2187", 038 "http://www.reactome.org/biopax/48887#BiochemicalReaction1273" 039 )); 040 041 public static void main(String[] args) throws Exception { 042 setUp(); 043 run(); 044 } 045 046 private static void run() throws OWLOntologyCreationException, ComponentInitException { 047 logger.debug("Starting..."); 048 049 logger.debug("creating positive and negative examples..."); 050 Set<OWLIndividual> posExamples = makeIndividuals(posExampleUris); 051 Set<OWLIndividual> negExamples = makeIndividuals(negExampleUris); 052 logger.debug("finished creating positive and negative examples"); 053 054 logger.debug("reading ontology..."); 055 OWLOntologyManager man = OWLManager.createOWLOntologyManager(); 056 OWLOntology ontology = man.loadOntologyFromOntologyDocument(new File(kbPathStr)); 057 logger.debug("read " + ontology.getAxiomCount() + " axioms"); 058 logger.debug("finished reading the ontology"); 059 060 ExistentialRestrictionMaterialization mat = new ExistentialRestrictionMaterialization(ontology); 061 System.out.println(mat.materialize("http://purl.obolibrary.org/obo/CHEBI_33560")); 062 063 logger.debug("initializing knowledge source..."); 064 KnowledgeSource ks = new OWLAPIOntology(ontology); 065 ks.init(); 066 logger.debug("finished initializing knowledge source"); 067 068 logger.debug("initializing reasoner..."); 069 OWLAPIReasoner baseReasoner = new OWLAPIReasoner(ks); 070 baseReasoner.setUseFallbackReasoner(true); 071 baseReasoner.init(); 072 Logger.getLogger(ElkReasoner.class).setLevel(Level.OFF); 073 074 ClosedWorldReasoner cwReasoner = new ClosedWorldReasoner(ks); 075 cwReasoner.setReasonerComponent(baseReasoner); 076 cwReasoner.setHandlePunning(false); 077 cwReasoner.setUseMaterializationCaching(false); 078 cwReasoner.setMaterializeExistentialRestrictions(true); 079 cwReasoner.init(); 080 logger.debug("finished initializing reasoner component"); 081 082 AbstractReasonerComponent rc = cwReasoner; 083 084 logger.debug("initializing learning problem..."); 085 PosNegLPStandard lp = new PosNegLPStandard(rc); 086 lp.setPositiveExamples(posExamples); 087 lp.setNegativeExamples(negExamples); 088 lp.init(); 089 logger.debug("finished initializing learning problem"); 090 091 092 logger.debug("initializing learning algorithm..."); 093 AbstractCELA la; 094 095 OEHeuristicRuntime heuristic = new OEHeuristicRuntime(); 096 heuristic.setExpansionPenaltyFactor(0.1); 097 098 CELOE celoe = new CELOE(lp, rc); 099 celoe.setHeuristic(heuristic); 100 celoe.setMaxExecutionTimeInSeconds(60*60*12); 101 celoe.setNoisePercentage(80); 102 celoe.setMaxNrOfResults(50); 103 celoe.setSearchTreeFile("log/reactome-minimal.log"); 104// celoe.setWriteSearchTree(true); 105 celoe.setReplaceSearchTree(true); 106 107// ELLearningAlgorithm elLa = new ELLearningAlgorithm(lp, rc); 108// elLa.setNoisePercentage(1.0); 109// elLa.setWriteSearchTree(true); 110// elLa.setReplaceSearchTree(true); 111// la = elLa; // celoe; 112 113 la = celoe; 114// Description startClass = new NamedClass("http://dl-learner.org/smallis/Allelic_info"); 115 logger.debug("finished initializing learning algorithm"); 116 logger.debug("initializing operator..."); 117 RhoDRDown op = new RhoDRDown(); 118 op.setInstanceBasedDisjoints(true); 119 op.setUseNegation(false); 120// op.setStartClass(new NamedClass("http://dl-learner.org/smallis/Allelic_info")); 121 op.setUseHasValueConstructor(false); 122 op.setUseAllConstructor(false); 123 op.setReasoner(rc); 124 op.setSubHierarchy(rc.getClassHierarchy()); 125 op.setObjectPropertyHierarchy(rc.getObjectPropertyHierarchy()); 126 op.setDataPropertyHierarchy(rc.getDatatypePropertyHierarchy()); 127 op.init(); 128 logger.debug("finished initializing operator"); 129 if(la instanceof CELOE) 130 ((CELOE) la).setOperator(op); 131 132 la.init(); 133 la.start(); 134 135 logger.debug("Finished"); 136 } 137 138 private static void setUp() { 139 logger.setLevel(Level.DEBUG); 140 Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF); 141 StringRenderer.setRenderer(Rendering.DL_SYNTAX); 142 } 143 144 private static Set<OWLIndividual> makeIndividuals(List<String> uris) { 145 Set<OWLIndividual> individuals = new HashSet<>(); 146 for (String uri : uris) { 147 individuals.add(new OWLNamedIndividualImpl(IRI.create(uri))); 148 } 149 150 return individuals; 151 } 152}