Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package es.cbgp.upm.logic;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Set;
- import java.util.TreeSet;
- import es.cbgp.upm.objects.Endpoint;
- import es.cbgp.upm.objects.Input;
- import es.cbgp.upm.objects.Output;
- import es.cbgp.upm.objects.OutputInputPair;
- import es.cbgp.upm.objects.Result;
- import es.cbgp.upm.ontstuff.OntologyManagement;
- import es.cbgp.upm.ontstuff.SPARQLQueryEngine;
- public class Logic {
- private SPARQLQueryEngine sqe;
- private LinkedList<OutputInputPair> outAndInPairs;
- private Set<Input> inputsSPARQL;
- private Set<Output> outputsSPARQL;
- private DerbyDBManager dbManager;
- private MyLogger logger;
- private int api;
- private int reasoner;
- private int mode;
- public Logic(int a, int r, int m) throws Exception {
- this.api = a;
- this.reasoner = r;
- this.mode = m;
- this.logger = new MyLogger();
- this.dbManager = new DerbyDBManager(api, reasoner, mode, this.logger);
- }
- public void executeSPARQLQuery() throws Exception {
- Endpoint sadiEp = loadSADIEndpoint();
- sqe = new SPARQLQueryEngine();
- sqe.executeQuery(sadiEp);
- }
- /**
- * Method to create the pairs output-input. Each object OutputInputPair
- * contains: - Input service name - Input class - Output service name -
- * Output class
- */
- public void createOutputInputPairs() {
- this.inputsSPARQL = new TreeSet<Input>();
- this.outputsSPARQL = new TreeSet<Output>();
- this.outAndInPairs = new LinkedList<OutputInputPair>();
- /*
- * We have inputs and outputs mixed. We divided them.
- */
- for (int i = 0; i < sqe.getInOuts().size(); i++) {
- if (sqe.getInOuts().get(i) instanceof Input) {
- inputsSPARQL.add((Input) sqe.getInOuts().get(i));
- }
- if (sqe.getInOuts().get(i) instanceof Output) {
- outputsSPARQL.add((Output) sqe.getInOuts().get(i));
- }
- }
- /*
- * For each output.
- */
- Iterator<Output> itOut = this.outputsSPARQL.iterator();
- while (itOut.hasNext()) {
- Output out = itOut.next();
- Iterator<Input> itIn = this.inputsSPARQL.iterator();
- while (itIn.hasNext()) {
- /*
- * We get an input and create the pair.
- */
- Input in = itIn.next();
- OutputInputPair oip = new OutputInputPair(in.getServiceName(),
- out.getServiceName(), in.getObject(), out.getObject());
- outAndInPairs.add(oip);
- }
- }
- }
- private Endpoint loadSADIEndpoint() throws Exception {
- String epName = ConfigManager
- .getConfig(Constants.SADI_REG_ENDPOINT_NAME);
- String epUrl = ConfigManager.getConfig(Constants.SADI_REG_ENDPOINT_URL);
- return new Endpoint(epName, epUrl);
- }
- /**
- * Method to prepare the database.
- *
- * @return Returns a boolean to see if the preparation was ok.
- * @throws Exception
- * It can throws an exception.
- */
- public Result prepareDB() throws Exception {
- this.dbManager.load();
- Result cs = new Result(true);
- boolean existsSchema = this.dbManager.existsSchema();
- if (!existsSchema) {
- logger.log("Schema doesn't exist. Creating..");
- /*
- * If the schema (main tables) doesn't exist. We create it.
- */
- cs = this.dbManager.createSchema();
- } else {
- logger.log("Schema exists.");
- }
- return cs;
- }
- public Result loadInAndOutsToDB() {
- this.logger.log("Loading inputs and outputs from endpoint ...");
- try {
- this.executeSPARQLQuery();
- this.createOutputInputPairs();
- this.processInputs();
- this.dbManager.close();
- System.exit(0);
- } catch (Exception e) {
- e.printStackTrace();
- return new Result(false, e.getMessage());
- }
- return new Result(true);
- }
- private void processInputs() throws Exception {
- Set<Input> inputsSQL = this.dbManager.getInputsFromDB();
- Set<Input> resultingInputs = new TreeSet<Input>(inputsSPARQL);
- this.logger.log("Inputs found in database (SQL): " + inputsSQL.size());
- this.logger.log("Inputs found in endpoint (SPARQL): " + inputsSPARQL.size());
- resultingInputs.removeAll(inputsSQL);
- this.logger.log("Resulting inputs to add to database: " + resultingInputs.size());
- if (resultingInputs.size() > 0) {
- this.dbManager.insertInputs(resultingInputs);
- Iterator<Input> it = resultingInputs.iterator();
- while (it.hasNext()) {
- Input i = it.next();
- this.logger.log("Input: " + i.getObject());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement