protected static Resource _processProperty(
      String propertyID, NativeObject propertyNO, String baseURL, SailConnection sailConnection)
      throws SailException {
    String label = _getStringProperty(propertyNO, "label");
    String pluralLabel = _getStringProperty(propertyNO, "pluralLabel");
    String uri = _getStringProperty(propertyNO, "uri");
    String valueType = _getStringProperty(propertyNO, "valueType");

    if (label == null) {
      label = propertyID;
    }
    if (pluralLabel == null) {
      pluralLabel = label;
    }
    if (valueType == null) {
      valueType = "item";
    }

    Resource resource;
    try {
      resource = new URIImpl(uri);
    } catch (Exception e) {
      uri = baseURL + _encode(propertyID);
      resource = new URIImpl(uri);
    }

    sailConnection.addStatement(resource, RDF.TYPE, RDF.PROPERTY);
    sailConnection.addStatement(resource, ExhibitOntology.ID, new LiteralImpl(propertyID));
    sailConnection.addStatement(resource, RDFS.LABEL, new LiteralImpl(label));
    sailConnection.addStatement(
        resource, ExhibitOntology.PLURAL_LABEL, new LiteralImpl(pluralLabel));
    sailConnection.addStatement(resource, ExhibitOntology.VALUE_TYPE, new LiteralImpl(valueType));

    return resource;
  }
Esempio n. 2
0
  @Override
  public void remove(Iterable<Statement> triples, String namedGraphURI) {
    for (Statement stmt : triples)
      try {
        if (namedGraphURI == null) {
          connection.removeStatements(stmt.getSubject(), stmt.getPredicate(), stmt.getObject());
        } else {
          connection.removeStatements(
              stmt.getSubject(),
              stmt.getPredicate(),
              stmt.getObject(),
              new ValueFactoryImpl().createURI(namedGraphURI));
        }
      } catch (SailException e) {
        try {
          connection.rollback();
        } catch (SailException e1) {
          e1.printStackTrace();
        }
      }

    try {
      connection.commit();
    } catch (SailException e) {
      e.printStackTrace();
    }
  }
 public void commit() throws SailException {
   if (uncommittedChanges) {
     cacheConnection.commit();
     baseSailConnection.commit();
     uncommittedChanges = false;
   }
 }
  protected static Resource _processType(
      String typeID, NativeObject typeNO, String baseURL, SailConnection sailConnection)
      throws SailException {
    String label = _getStringProperty(typeNO, "label");
    String pluralLabel = _getStringProperty(typeNO, "pluralLabel");
    String uri = _getStringProperty(typeNO, "uri");

    if (label == null) {
      label = typeID;
    }
    if (pluralLabel == null) {
      pluralLabel = label;
    }
    if (uri == null) {
      uri = baseURL + _encode(typeID);
    }

    Resource resource = new URIImpl(uri);
    sailConnection.addStatement(resource, RDF.TYPE, OWL.CLASS);
    sailConnection.addStatement(resource, ExhibitOntology.ID, new LiteralImpl(typeID));
    sailConnection.addStatement(resource, RDFS.LABEL, new LiteralImpl(label));
    sailConnection.addStatement(
        resource, ExhibitOntology.PLURAL_LABEL, new LiteralImpl(pluralLabel));

    return resource;
  }
 // Note: adding statements does not change the configuration of cached
 // values.
 public void addStatement(
     final Resource subj, final URI pred, final Value obj, final Resource... contexts)
     throws SailException {
   cacheConnection.addStatement(subj, pred, obj, contexts);
   baseSailConnection.addStatement(subj, pred, obj, contexts);
   uncommittedChanges = true;
 }
  public CloseableIteration<? extends Statement, SailException> getStatements(
      final Resource subj,
      final URI pred,
      final Value obj,
      final boolean includeInferred,
      final Resource... context)
      throws SailException {
    if (null != subj && cacheSubject) {
      if (!cachedSubjects.contains(subj)) {
        cacheStatements(subj, null, null);
        cachedSubjects.add(subj);
      }

      return cacheConnection.getStatements(subj, pred, obj, includeInferred, context);
    } else if (null != obj && cacheObject) {
      if (!cachedObjects.contains(obj)) {
        cacheStatements(null, null, obj);
        cachedObjects.add(obj);
      }

      return cacheConnection.getStatements(subj, pred, obj, includeInferred, context);
    } else if (null != pred && cachePredicate) {
      if (!cachedPredicates.contains(pred)) {
        cacheStatements(null, pred, null);
        cachedPredicates.add(pred);
      }

      return cacheConnection.getStatements(subj, pred, obj, includeInferred, context);
    } else {
      return baseSailConnection.getStatements(subj, pred, obj, includeInferred, context);
    }
  }
 @Override
 public void executeUpdate(
     final UpdateExpr updateExpr,
     final Dataset dataset,
     final BindingSet bindingSet,
     final boolean b)
     throws SailException {
   cacheConnection.executeUpdate(updateExpr, dataset, bindingSet, b);
   baseSailConnection.executeUpdate(updateExpr, dataset, bindingSet, b);
   uncommittedChanges = true;
 }
 public CloseableIteration<? extends Namespace, SailException> getNamespaces()
     throws SailException {
   if (config.logReadOperations) {
     queryHandler.handle(new GetNamespacesCall(id));
     return new RecorderIteration<Namespace, SailException>(
         (CloseableIteration<Namespace, SailException>) baseSailConnection.getNamespaces(),
         nextIterationId(),
         queryHandler);
   } else {
     return baseSailConnection.getNamespaces();
   }
 }
Esempio n. 9
0
  @Override
  public void disconnect() {
    setConnected(false);

    try {
      if (connection.isOpen()) {
        connection.close();
      }
    } catch (SailException e) {
      e.printStackTrace();
    }
  }
 protected void clearInternal(final Resource... contexts) throws SailException {
   if (0 == contexts.length) {
     baseSailConnection.clear(singleContext);
   } else {
     for (Resource context : contexts) {
       if (null != context && context.equals(singleContext)) {
         baseSailConnection.clear(singleContext);
         break;
       }
     }
   }
 }
  protected long sizeInternal(final Resource... contexts) throws SailException {
    if (0 == contexts.length) {
      return baseSailConnection.size(singleContext);
    } else {
      for (Resource context : contexts) {
        if (null != context && context.equals(singleContext)) {
          return baseSailConnection.size(singleContext);
        }
      }

      return 0;
    }
  }
Esempio n. 12
0
 @Override
 public void connect() {
   if (!isConnected()) {
     setConnected(true);
     try {
       if (connection != null && connection.isOpen()) {
         connection.close();
       }
       connection = sail.getConnection();
     } catch (SailException e) {
       e.printStackTrace();
     }
   }
 }
 protected void addStatementInternal(
     final Resource subj, final URI pred, final Value obj, final Resource... contexts)
     throws SailException {
   if (0 == contexts.length) {
     baseSailConnection.addStatement(subj, pred, obj, singleContext);
   } else {
     for (Resource context : contexts) {
       if (null != context && context.equals(singleContext)) {
         baseSailConnection.addStatement(subj, pred, obj, singleContext);
         break;
       }
     }
   }
 }
  protected static void _addItemProperty(
      Resource itemResource,
      URI predicate,
      Object valueO,
      String baseURL,
      SailConnection dataConnection,
      SailConnection metaConnection,
      Map<String, String> itemIDToURI)
      throws SailException {
    if (valueO == null) return;
    String valueType = _getObjectString(predicate, ExhibitOntology.VALUE_TYPE, metaConnection);
    Value object = null;

    if ("item".equals(valueType)) {
      String id = valueO.toString();
      String uri = itemIDToURI.get(id);
      try {
        object = new URIImpl(uri);
      } catch (Exception e) {
        object = new URIImpl(baseURL + _encode(id));
      }
    } else if (valueO instanceof Double) {
      double d = ((Double) valueO).doubleValue();
      if (d != Math.floor(d)) {
        object = new LiteralImpl(valueO.toString() /*, XMLSchema.DOUBLE*/);
      } else {
        object = new LiteralImpl(Long.toString(((Double) valueO).longValue()) /*, XMLSchema.LONG*/);
      }
    } else {
      object = new LiteralImpl(valueO.toString());
    }

    dataConnection.addStatement(itemResource, predicate, object);
  }
  protected static URI _ensurePropertyExists(
      String propertyID, String baseURL, SailConnection sailConnection) throws SailException {
    URI predicate =
        (URI) _getSubject(ExhibitOntology.ID, new LiteralImpl(propertyID), sailConnection);
    if (predicate == null) {
      String label = propertyID;
      String uri = baseURL + _encode(propertyID);

      predicate = new URIImpl(uri);
      sailConnection.addStatement(predicate, RDF.TYPE, OWL.CLASS);
      sailConnection.addStatement(predicate, ExhibitOntology.ID, new LiteralImpl(propertyID));
      sailConnection.addStatement(predicate, RDFS.LABEL, new LiteralImpl(label));
      sailConnection.commit();
    }
    return predicate;
  }
  private void cacheStatements(final Resource subj, final URI pred, final Value obj)
      throws SailException {
    boolean includeInferred = false;

    CloseableIteration<? extends Statement, SailException> iter =
        baseSailConnection.getStatements(subj, pred, obj, includeInferred);

    while (iter.hasNext()) {
      Statement st = iter.next();
      cacheConnection.addStatement(
          st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
    }

    iter.close();

    cacheConnection.commit();
  }
 // Note: adding statements does not change the configuration of cached
 // values.
 public void addStatement(
     final Resource subj, final URI pred, final Value obj, final Resource... contexts)
     throws SailException {
   if (config.logWriteOperations) {
     queryHandler.handle(new AddStatementCall(id, subj, pred, obj, contexts));
   }
   baseSailConnection.addStatement(subj, pred, obj, contexts);
 }
  protected static Resource _ensureTypeExists(
      String typeID, String baseURL, SailConnection sailConnection) throws SailException {
    Resource resource = _getSubject(ExhibitOntology.ID, new LiteralImpl(typeID), sailConnection);
    if (resource == null) {
      String label = typeID;
      String pluralLabel = label;
      String uri = baseURL + _encode(typeID);

      resource = new URIImpl(uri);
      sailConnection.addStatement(resource, RDF.TYPE, OWL.CLASS);
      sailConnection.addStatement(resource, ExhibitOntology.ID, new LiteralImpl(typeID));
      sailConnection.addStatement(resource, RDFS.LABEL, new LiteralImpl(label));
      sailConnection.addStatement(
          resource, ExhibitOntology.PLURAL_LABEL, new LiteralImpl(pluralLabel));
      sailConnection.commit();
    }
    return resource;
  }
 public CloseableIteration<? extends Statement, SailException> getStatements(
     final Resource subj,
     final URI pred,
     final Value obj,
     final boolean includeInferred,
     final Resource... contexts)
     throws SailException {
   if (config.logReadOperations) {
     queryHandler.handle(new GetStatementsCall(id, subj, pred, obj, includeInferred, contexts));
     return new RecorderIteration<Statement, SailException>(
         (CloseableIteration<Statement, SailException>)
             baseSailConnection.getStatements(subj, pred, obj, includeInferred, contexts),
         nextIterationId(),
         queryHandler);
   } else {
     return baseSailConnection.getStatements(subj, pred, obj, includeInferred, contexts);
   }
 }
 public CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate(
     final TupleExpr tupleExpr,
     final Dataset dataSet,
     final BindingSet bindingSet,
     final boolean includeInferred)
     throws SailException {
   // Note: there is no EvaluateCall, nor is there a recording iterator for evaluate() results
   return baseSailConnection.evaluate(tupleExpr, dataSet, bindingSet, includeInferred);
 }
Esempio n. 21
0
 @Override
 public void delete(String graphURI) {
   try {
     if (graphURI == null) {
       connection.clear();
       connection.commit();
     } else {
       connection.clear(new ValueFactoryImpl().createURI(graphURI));
       connection.commit();
     }
   } catch (SailException e) {
     e.printStackTrace();
     try {
       connection.rollback();
     } catch (SailException e1) {
       e1.printStackTrace();
     }
   }
 }
  protected CloseableIteration<? extends Statement, SailException> getStatementsInternal(
      final Resource subj,
      final URI pred,
      final Value obj,
      final boolean includeInferred,
      final Resource... contexts)
      throws SailException {

    if (0 == contexts.length) {
      return baseSailConnection.getStatements(subj, pred, obj, includeInferred, singleContext);
    } else {
      for (Resource context : contexts) {
        if (null != context && context.equals(singleContext)) {
          return baseSailConnection.getStatements(subj, pred, obj, includeInferred, singleContext);
        }
      }

      return new EmptyCloseableIteration<Statement, SailException>();
    }
  }
 protected static Value _getObject(Resource subject, URI predicate, SailConnection c)
     throws SailException {
   CloseableIteration<? extends Statement, SailException> i =
       c.getStatements(subject, predicate, null, true);
   try {
     if (i.hasNext()) {
       return i.next().getObject();
     } else {
       return null;
     }
   } finally {
     i.close();
   }
 }
Esempio n. 24
0
  protected boolean containsGraph(String graph) {
    connect();

    try {
      for (CloseableIteration<? extends Resource, SailException> res = connection.getContextIDs();
          res.hasNext(); ) {
        Resource r = res.next();
        if (r.stringValue().equals(graph.toString())) return true;
      }
    } catch (SailException e) {
      e.printStackTrace();
    }

    disconnect();

    return false;
  }
  protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(
      final TupleExpr tupleExpr,
      final Dataset dataSet,
      final BindingSet bindingSet,
      final boolean includeInferred)
      throws SailException {

    // ignore the given dataset and restrict everything to the single context we have been setup
    // with
    DatasetImpl singleContextDataset = new DatasetImpl();
    if (singleContext instanceof URI) {
      singleContextDataset.setDefaultInsertGraph((URI) singleContext);
      singleContextDataset.addDefaultGraph((URI) singleContext);
      singleContextDataset.addNamedGraph((URI) singleContext);
      singleContextDataset.addDefaultRemoveGraph((URI) singleContext);
    }

    return baseSailConnection.evaluate(
        tupleExpr, singleContextDataset, bindingSet, includeInferred);
  }
Esempio n. 26
0
  public void handleStatement(Statement arg0) {

    try {
      // avoid self-cycles
      if (arg0.getSubject().stringValue().equals(arg0.getObject().stringValue())) return;

      sc.addStatement(arg0.getSubject(), arg0.getPredicate(), arg0.getObject());
      manager.incrCounter();
      if (manager.atCommit()) System.out.print(".");
    } catch (SailException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(
          "Subject: "
              + arg0.getSubject().toString()
              + " Predicate: "
              + arg0.getPredicate().toString()
              + " Object: "
              + arg0.getObject().toString());
    }
  }
Esempio n. 27
0
  @Override
  public IResultSet<Value, URI, Literal> selectQuery(String query, String graph) {
    connect();

    final SPARQLParser parser = new SPARQLParser();

    CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults = null;
    ParsedQuery parsedQuery = null;

    try {
      parsedQuery = parser.parseQuery(query, null);
    } catch (MalformedQueryException e) {
      e.printStackTrace();
    }
    try {
      sparqlResults =
          connection.evaluate(
              parsedQuery.getTupleExpr(), parsedQuery.getDataset(), new EmptyBindingSet(), false);
    } catch (SailException e) {
      e.printStackTrace();
    }

    return new SailResultSet(sparqlResults);
  }
 public void commit() throws SailException {
   if (config.logTransactions) {
     queryHandler.handle(new CommitCall(id));
   }
   baseSailConnection.commit();
 }
 public void clearNamespaces() throws SailException {
   if (config.logWriteOperations) {
     queryHandler.handle(new ClearNamespacesCall(id));
   }
   baseSailConnection.clearNamespaces();
 }
 // Note: clearing statements does not change the configuration of cached
 // values.
 public void clear(final Resource... contexts) throws SailException {
   if (config.logWriteOperations) {
     queryHandler.handle(new ClearCall(id, contexts));
   }
   baseSailConnection.clear(contexts);
 }