comparison OPPL/src/OPPLQueryGalaxy.java @ 15:622cde484f4c draft

Add new tool to perform OPPL queries
author Mikel Egana Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Thu, 12 Jul 2012 17:12:00 +0200
parents
children 6c25e717c896
comparison
equal deleted inserted replaced
14:68c4ae500a13 15:622cde484f4c
1 /**
2 *
3 */
4 package es.upm.cbgp.opplquery.oppl.galaxy;
5
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.util.Iterator;
9 import java.util.Scanner;
10 import java.util.Set;
11 import java.util.logging.Logger;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import org.semanticweb.owlapi.expression.ParserException;
16 import org.semanticweb.owlapi.model.IRI;
17 import org.semanticweb.owlapi.model.OWLObject;
18 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
19
20 import es.upm.fi.dia.oeg.oppl.galaxy.GalaxyOWLAPI;
21
22 import org.coode.oppl.ChangeExtractor;
23 import org.coode.oppl.ConstraintSystem;
24 import org.coode.oppl.OPPLParser;
25 import org.coode.oppl.OPPLScript;
26 import org.coode.oppl.ParserFactory;
27 import org.coode.parsers.ErrorListener;
28 import org.coode.parsers.LoggerErrorListener;
29 import org.coode.oppl.bindingtree.Assignment;
30 import org.coode.oppl.bindingtree.BindingNode;
31 import org.coode.oppl.exceptions.QuickFailRuntimeExceptionHandler;
32 import org.coode.oppl.exceptions.RuntimeExceptionHandler;
33
34 public class OPPLQueryGalaxy {
35
36 /**
37 * @param args
38 * @throws OWLOntologyCreationException
39 * @throws ParserException
40 * @throws FileNotFoundException
41 */
42 public static void main(String[] args) throws OWLOntologyCreationException, ParserException, FileNotFoundException {
43 // Get the arguments from command-line
44 String OWLFilePath = args [0]; // /home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/ontology/single/test.owl
45 String reasoner_type = args [1]; // Pellet|FaCTPlusPlus|HermiT
46 String Answer_render = args [2]; // URI|URIfragment|URIfragment2OBO
47 String OPPL_script_sourcee = args [3];
48
49 // String OPPL_script_file = args [3];
50 // File OPPL_script_file = new File("/home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/oppl_script/test_oppl_query.oppl");
51 // File OPPL_script_file = new File("/home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/oppl_script/test_oppl_query_object_prop.oppl");
52 // File OPPL_script_file = new File("/home/mikel/UPM/OPPL_galaxy/OPPL/OPPL/test-data/oppl_script/test_oppl_query_object_ind.oppl");
53
54 // String OPPL_script_source = "";
55 // Scanner input = new Scanner(OPPL_script_file);
56 // while(input.hasNext()) {
57 // String nextToken = input.next();
58 // OPPL_script_source = OPPL_script_source + " " + nextToken;
59 // }
60 // input.close();
61
62 // Create the manager
63 GalaxyOWLAPI galaxyowlapi = new GalaxyOWLAPI();
64
65 // Load the main ontology and hope for the imported URIs to be resolvable
66 galaxyowlapi.loadMainOntology(OWLFilePath);
67
68 // Set the reasoner
69
70 // Pellet
71 if(reasoner_type.equals("Pellet")){
72 galaxyowlapi.setReasonerPellet();
73 }
74 // FaCTPlusPlus
75 else if (reasoner_type.equals("FaCTPlusPlus")){
76 galaxyowlapi.setReasonerFaCT();
77 }
78 // HermiT
79 else{
80 galaxyowlapi.setReasonerHermit();
81 }
82
83 OPPL_script_source = completeOPPLScript(OPPL_script_source);
84 // System.out.println(OPPL_script_source);
85
86 ParserFactory parserFactory = new ParserFactory(galaxyowlapi.getOWLManager(), galaxyowlapi.getMainOntology(), galaxyowlapi.getReasoner());
87 Logger logger = Logger.getLogger(OPPLQueryGalaxy.class.getName());
88 ErrorListener errorListener = (ErrorListener) new LoggerErrorListener(logger);
89 OPPLParser opplparser = parserFactory.build(errorListener);
90 OPPLScript OPPLscript = opplparser.parse(OPPL_script_source);
91 RuntimeExceptionHandler exceptionhandler = new QuickFailRuntimeExceptionHandler();
92 ChangeExtractor extractor = new ChangeExtractor(exceptionhandler, true);
93 extractor.visit(OPPLscript);
94 ConstraintSystem cs = OPPLscript.getConstraintSystem();
95 Set<BindingNode> nodes = cs.getLeaves();
96
97 Iterator NodesIterator = nodes.iterator();
98 while(NodesIterator.hasNext()){
99 Set Assignments = ((BindingNode)NodesIterator.next()).getAssignments();
100 Iterator AssignmentIterator = Assignments.iterator();
101 while(AssignmentIterator.hasNext()){
102 Assignment assignment = (Assignment)AssignmentIterator.next();
103 System.out.print(assignment.getAssignedVariable().toString());
104 System.out.print("\t");
105 String OWLObjectString = ((OWLObject)assignment.getAssignment()).toString();
106 IRI entityIRI = IRI.create(OWLObjectString.substring(1,OWLObjectString.length()-1));
107 print_result_entity(entityIRI, Answer_render);
108 System.out.print("\n");
109 }
110 }
111 }
112
113 //URI|URIfragment|URIfragment2OBO
114 private static void print_result_entity (IRI iri, String Answer_render){
115 if(Answer_render.equals("URI")){
116 System.out.print(iri);
117 }
118
119 // Weird bug: in eclipse it can print out the IRIs of every entity, but in Galaxy it can't!
120 // done manually
121 else if(Answer_render.equals("URIfragment")){
122 if(iri.toString().contains("#")){
123 System.out.print(iri.getFragment());
124 }
125 else{
126 String [] iri_tokens = iri.toString().split("/");
127 System.out.print(iri_tokens[iri_tokens.length-1]);
128 }
129 }
130 else{
131 if(iri.toString().contains("#")){
132 System.out.print((iri.getFragment()).replace("_", ":"));
133 }
134 else{
135 String [] iri_tokens = iri.toString().split("/");
136 System.out.print((iri_tokens[iri_tokens.length-1]).replace("_", ":"));
137 }
138 }
139 }
140
141 // Very crappy stuff: complete the query to make a whole OPPL script that can be parsed
142 private static String completeOPPLScript (String OPPLQuery){
143
144 // Get the first variable and add at least an axiom to make a whole script
145 Pattern p = Pattern.compile("(\\?\\w+):(CLASS|INDIVIDUAL|OBJECTPROPERTY|DATAPROPERTY|ANNOTATIONPROPERTY|CONSTANT)");
146 Matcher m = p.matcher(OPPLQuery);
147 m.find();
148 if (m.group(2).equals("CLASS")){
149 OPPLQuery = OPPLQuery + " BEGIN ADD " + m.group(1) + " SubClassOf !A END;";
150 }
151 else if (m.group(2).equals("INDIVIDUAL")){
152 OPPLQuery = OPPLQuery + " BEGIN ADD " + m.group(1) + " !p !b END;";
153 }
154 else if (m.group(2).equals("OBJECTPROPERTY")){
155 OPPLQuery = OPPLQuery + " BEGIN ADD Transitive ?p END;";
156 }
157 // else if (m.group(2).equals("DATAPROPERTY")){
158 // OPPLQuery = OPPLQuery + " BEGIN ADD ?whole SubClassOf !A END;";
159 // }
160 // else if (m.group(2).equals("ANNOTATIONPROPERTY")){
161 // OPPLQuery = OPPLQuery + " BEGIN ADD ?whole SubClassOf !A END;";
162 // }
163 // else if (m.group(2).equals("CONSTANT")){
164 // OPPLQuery = OPPLQuery + " BEGIN ADD ?whole SubClassOf !A END;";
165 // }
166 else{
167 OPPLQuery = "Malformed OPPL query";
168 }
169 return OPPLQuery;
170 }
171 }