Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. package es.cbgp.upm.logic;
  2.  
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import java.util.Set;
  6. import java.util.TreeSet;
  7.  
  8. import es.cbgp.upm.objects.Endpoint;
  9. import es.cbgp.upm.objects.Input;
  10. import es.cbgp.upm.objects.Output;
  11. import es.cbgp.upm.objects.OutputInputPair;
  12. import es.cbgp.upm.objects.Result;
  13. import es.cbgp.upm.ontstuff.OntologyManagement;
  14. import es.cbgp.upm.ontstuff.SPARQLQueryEngine;
  15.  
  16. public class Logic {
  17.  
  18.     private SPARQLQueryEngine sqe;
  19.     private LinkedList<OutputInputPair> outAndInPairs;
  20.     private Set<Input> inputsSPARQL;
  21.     private Set<Output> outputsSPARQL;
  22.     private DerbyDBManager dbManager;
  23.     private MyLogger logger;
  24.  
  25.     private int api;
  26.     private int reasoner;
  27.     private int mode;
  28.  
  29.     public Logic(int a, int r, int m) throws Exception {
  30.         this.api = a;
  31.         this.reasoner = r;
  32.         this.mode = m;
  33.         this.logger = new MyLogger();
  34.         this.dbManager = new DerbyDBManager(api, reasoner, mode, this.logger);
  35.  
  36.     }
  37.  
  38.     public void executeSPARQLQuery() throws Exception {
  39.         Endpoint sadiEp = loadSADIEndpoint();
  40.         sqe = new SPARQLQueryEngine();
  41.         sqe.executeQuery(sadiEp);
  42.     }
  43.  
  44.     /**
  45.      * Method to create the pairs output-input. Each object OutputInputPair
  46.      * contains: - Input service name - Input class - Output service name -
  47.      * Output class
  48.      */
  49.     public void createOutputInputPairs() {
  50.         this.inputsSPARQL = new TreeSet<Input>();
  51.         this.outputsSPARQL = new TreeSet<Output>();
  52.         this.outAndInPairs = new LinkedList<OutputInputPair>();
  53.         /*
  54.          * We have inputs and outputs mixed. We divided them.
  55.          */
  56.         for (int i = 0; i < sqe.getInOuts().size(); i++) {
  57.             if (sqe.getInOuts().get(i) instanceof Input) {
  58.                 inputsSPARQL.add((Input) sqe.getInOuts().get(i));
  59.             }
  60.             if (sqe.getInOuts().get(i) instanceof Output) {
  61.                 outputsSPARQL.add((Output) sqe.getInOuts().get(i));
  62.             }
  63.         }
  64.         /*
  65.          * For each output.
  66.          */
  67.         Iterator<Output> itOut = this.outputsSPARQL.iterator();
  68.         while (itOut.hasNext()) {
  69.             Output out = itOut.next();
  70.             Iterator<Input> itIn = this.inputsSPARQL.iterator();
  71.             while (itIn.hasNext()) {
  72.                 /*
  73.                  * We get an input and create the pair.
  74.                  */
  75.                 Input in = itIn.next();
  76.                 OutputInputPair oip = new OutputInputPair(in.getServiceName(),
  77.                         out.getServiceName(), in.getObject(), out.getObject());
  78.                 outAndInPairs.add(oip);
  79.             }
  80.         }
  81.     }
  82.  
  83.     private Endpoint loadSADIEndpoint() throws Exception {
  84.         String epName = ConfigManager
  85.                 .getConfig(Constants.SADI_REG_ENDPOINT_NAME);
  86.         String epUrl = ConfigManager.getConfig(Constants.SADI_REG_ENDPOINT_URL);
  87.         return new Endpoint(epName, epUrl);
  88.     }
  89.  
  90.  
  91.     /**
  92.      * Method to prepare the database.
  93.      *
  94.      * @return Returns a boolean to see if the preparation was ok.
  95.      * @throws Exception
  96.      *             It can throws an exception.
  97.      */
  98.     public Result prepareDB() throws Exception {
  99.         this.dbManager.load();
  100.         Result cs = new Result(true);
  101.         boolean existsSchema = this.dbManager.existsSchema();
  102.         if (!existsSchema) {
  103.             logger.log("Schema doesn't exist. Creating..");
  104.             /*
  105.              * If the schema (main tables) doesn't exist. We create it.
  106.              */
  107.             cs = this.dbManager.createSchema();
  108.         } else {
  109.             logger.log("Schema exists.");
  110.         }
  111.         return cs;
  112.     }
  113.  
  114.     public Result loadInAndOutsToDB() {
  115.         this.logger.log("Loading inputs and outputs from endpoint ...");
  116.         try {
  117.             this.executeSPARQLQuery();
  118.             this.createOutputInputPairs();
  119.             this.processInputs();
  120.             this.dbManager.close();
  121.             System.exit(0);
  122.         } catch (Exception e) {
  123.             e.printStackTrace();
  124.             return new Result(false, e.getMessage());
  125.         }
  126.  
  127.         return new Result(true);
  128.     }
  129.  
  130.     private void processInputs() throws Exception {
  131.         Set<Input> inputsSQL = this.dbManager.getInputsFromDB();
  132.         Set<Input> resultingInputs = new TreeSet<Input>(inputsSPARQL);
  133.         this.logger.log("Inputs found in database (SQL): " + inputsSQL.size());
  134.         this.logger.log("Inputs found in endpoint (SPARQL): " + inputsSPARQL.size());
  135.         resultingInputs.removeAll(inputsSQL);
  136.         this.logger.log("Resulting inputs to add to database: " + resultingInputs.size());
  137.         if (resultingInputs.size() > 0) {
  138.             this.dbManager.insertInputs(resultingInputs);
  139.             Iterator<Input> it = resultingInputs.iterator();
  140.             while (it.hasNext()) {
  141.                 Input i = it.next();
  142.                 this.logger.log("Input: " + i.getObject());
  143.             }
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement