예제 #1
0
  @Override
  public int compare(Statement first, Statement second) {
    // Cannot use Statement.equals as it does not take Context into account,
    // but can check for reference equality (==)
    if (first == second) {
      return EQUAL;
    }

    if (first.getSubject().equals(second.getSubject())) {
      if (first.getPredicate().equals(second.getPredicate())) {
        if (first.getObject().equals(second.getObject())) {
          // Context is the only part of a statement that should legitimately be null
          if (first.getContext() == null) {
            if (second.getContext() == null) {
              return EQUAL;
            } else {
              return BEFORE;
            }
          } else if (second.getContext() == null) {
            return AFTER;
          } else {
            return ValueComparator.getInstance().compare(first.getContext(), second.getContext());
          }
        } else {
          return ValueComparator.getInstance().compare(first.getObject(), second.getObject());
        }
      } else {
        return ValueComparator.getInstance().compare(first.getPredicate(), second.getPredicate());
      }
    } else {
      return ValueComparator.getInstance().compare(first.getSubject(), second.getSubject());
    }
  }
예제 #2
0
  @Override
  public void handleStatement(Statement st) throws RDFHandlerException {
    if (!writingStarted) {
      throw new RuntimeException("Document writing has not yet been started");
    }

    try {
      // SUBJECT
      NTriplesUtil.append(st.getSubject(), writer);
      writer.write(" ");

      // PREDICATE
      NTriplesUtil.append(st.getPredicate(), writer);
      writer.write(" ");

      // OBJECT
      NTriplesUtil.append(st.getObject(), writer);

      if (null != st.getContext()) {
        writer.write(" ");
        NTriplesUtil.append(st.getContext(), writer);
      }

      writer.write(" .\n");
    } catch (IOException e) {
      throw new RDFHandlerException(e);
    }
  }
예제 #3
0
 @Override
 public synchronized void removeStatements(
     Resource subj, URI pred, Value obj, Resource... contexts) throws SailException {
   if (sail.isArchiving()) {
     CloseableIteration<? extends Statement, SailException> stmts;
     stmts = super.getStatements(subj, pred, obj, false, contexts);
     try {
       while (stmts.hasNext()) {
         Statement st = stmts.next();
         Resource s = st.getSubject();
         URI p = st.getPredicate();
         Value o = st.getObject();
         Resource ctx = st.getContext();
         removeRevision(s, p);
         if (ctx instanceof URI && !ctx.equals(trx)) {
           if (modified.add(ctx)) {
             super.addStatement(getTrx(), MODIFIED, ctx, getTrx());
           }
           BNode node = vf.createBNode();
           super.addStatement(ctx, CONTAINED, node, getTrx());
           super.addStatement(node, RDF.SUBJECT, s, getTrx());
           super.addStatement(node, RDF.PREDICATE, p, getTrx());
           super.addStatement(node, RDF.OBJECT, o, getTrx());
         }
       }
     } finally {
       stmts.close();
     }
     super.removeStatements(subj, pred, obj, contexts);
   } else {
     if (sail.getMaxArchive() > 0 && arch.size() <= sail.getMaxArchive()) {
       CloseableIteration<? extends Statement, SailException> stmts;
       stmts = super.getStatements(subj, pred, obj, false, contexts);
       try {
         int maxArchive = sail.getMaxArchive();
         while (stmts.hasNext() && arch.size() <= maxArchive) {
           Statement st = stmts.next();
           Resource ctx = st.getContext();
           if (ctx instanceof URI && !ctx.equals(trx)) {
             arch.add(st);
           }
         }
       } finally {
         stmts.close();
       }
     }
     super.removeStatements(subj, pred, obj, contexts);
     removeRevision(subj, pred);
     if (contexts != null && contexts.length > 0) {
       for (Resource ctx : contexts) {
         if (ctx != null && modified.add(ctx)) {
           addMetadata(currentTrx, MODIFIED, ctx, currentTrx);
         }
       }
     }
   }
 }
예제 #4
0
 private void removeInforming(
     URI activity, URI entity, Resource subj, URI pred, Value obj, Resource... contexts)
     throws SailException {
   if (contexts != null && contexts.length == 0) {
     CloseableIteration<? extends Statement, SailException> stmts;
     stmts = super.getStatements(subj, pred, obj, false, contexts);
     try {
       while (stmts.hasNext()) {
         Statement st = stmts.next();
         Resource ctx = st.getContext();
         subj = st.getSubject();
         pred = st.getPredicate();
         obj = st.getObject();
         removeInformingGraph(activity, entity, subj, pred, obj, ctx);
       }
     } finally {
       stmts.close();
     }
   } else if (contexts == null) {
     removeInformingGraph(activity, entity, subj, pred, obj, null);
   } else {
     for (Resource ctx : contexts) {
       removeInformingGraph(activity, entity, subj, pred, obj, ctx);
     }
   }
 }
예제 #5
0
 private Object encodeStatement(final Statement statement) {
   return SerializerAvro.newGenericRecord(
       Schemas.STATEMENT,
       encodeIdentifier(statement.getSubject()),
       encodeIdentifier(statement.getPredicate()), //
       encodeValue(statement.getObject()), //
       encodeIdentifier(statement.getContext()));
 }
예제 #6
0
 private URI getTrx() throws SailException {
   if (trx == null) {
     trx = sail.nextTransaction();
     synchronized (metadata) {
       for (Statement st : metadata) {
         storeStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
       }
       metadata.clear();
     }
   }
   return trx;
 }
예제 #7
0
 /**
  * @return string containing human readable statements.
  * @throws RepositoryException
  */
 protected String dumpHumanReadableTriples() throws RepositoryException {
   StringBuilder sb = new StringBuilder();
   RepositoryResult<Statement> result = conn.getStatements(null, null, null, false);
   while (result.hasNext()) {
     Statement statement = result.next();
     sb.append(
         String.format(
             "%s %s %s %s\n",
             statement.getSubject(),
             statement.getPredicate(),
             statement.getObject(),
             statement.getContext()));
   }
   return sb.toString();
 }
예제 #8
0
  @Override
  public void handleStatement(Statement arg0) throws RDFHandlerException {

    try {

      _writer.write("<p>");
      NTriplesUtil.append(arg0.getSubject(), _writer);
      _writer.write(" ");
      NTriplesUtil.append(arg0.getPredicate(), _writer);
      _writer.write(" ");
      NTriplesUtil.append(arg0.getObject(), _writer);

      if (arg0.getContext() != null) {
        _writer.write(" ");
        NTriplesUtil.append(arg0.getContext(), _writer);
      }

      _writer.write(" .");
      _writer.write("<p/>");

    } catch (IOException e) {
      throw new RDFHandlerException(e);
    }
  }
  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();
  }
예제 #10
0
 private void flushArchive() throws SailException {
   if (arch.size() <= sail.getMaxArchive()) {
     for (Statement st : arch) {
       Resource s = st.getSubject();
       URI p = st.getPredicate();
       Value o = st.getObject();
       Resource ctx = st.getContext();
       removeRevision(s, p);
       BNode node = vf.createBNode();
       super.addStatement(ctx, CONTAINED, node, getTrx());
       super.addStatement(node, RDF.SUBJECT, s, getTrx());
       super.addStatement(node, RDF.PREDICATE, p, getTrx());
       super.addStatement(node, RDF.OBJECT, o, getTrx());
       if (ctx instanceof URI && modified.add(ctx)) {
         super.addStatement(getTrx(), MODIFIED, ctx, getTrx());
       }
     }
     arch.clear();
   }
 }
예제 #11
0
 @Override
 public synchronized void commit() throws SailException {
   flushArchive();
   if (trx != null) {
     for (Statement st : arch) {
       Resource ctx = st.getContext();
       if (ctx instanceof URI) {
         modified.add(ctx);
       }
     }
     for (Resource ctx : modified) {
       if (isObsolete(ctx)) {
         super.addStatement(ctx, RDF.TYPE, Audit.OBSOLETE, trx);
       }
     }
     GregorianCalendar cal = new GregorianCalendar();
     XMLGregorianCalendar xgc = factory.newXMLGregorianCalendar(cal);
     Literal now = vf.createLiteral(xgc);
     super.addStatement(trx, RDF.TYPE, TRANSACTION, trx);
     super.addStatement(trx, COMMITTED_ON, now, trx);
     for (Resource predecessor : predecessors) {
       super.addStatement(trx, PREDECESSOR, predecessor, trx);
     }
     sail.recent(trx, getWrappedConnection());
   }
   super.commit();
   metadata.clear();
   revised.clear();
   modified.clear();
   arch.clear();
   if (trx != null) {
     sail.committed(trx, predecessors);
     predecessors = Collections.singleton(trx);
     trx = null;
   }
 }
예제 #12
0
  private static PredicateObjectMap extractPredicateObjectMap(
      SesameDataSet r2rmlMappingGraph,
      Resource triplesMapSubject,
      Resource predicateObject,
      Set<GraphMap> savedGraphMaps,
      Map<Resource, TriplesMap> triplesMapResources)
      throws InvalidR2RMLStructureException, R2RMLDataError, InvalidR2RMLSyntaxException {
    log.debug("[RMLMappingFactory:extractPredicateObjectMap] Extract predicate-object map..");
    // Extract predicate maps
    URI p = r2rmlMappingGraph.URIref(Vocab.R2RML_NAMESPACE + R2RMLTerm.PREDICATE_MAP);

    List<Statement> statements = r2rmlMappingGraph.tuplePattern(predicateObject, p, null);

    if (statements.size() < 1) {
      throw new InvalidR2RMLStructureException(
          "[RMLMappingFactory:extractSubjectMap] "
              + predicateObject.stringValue()
              + " has no predicate map defined : one or more is required.");
    }

    Set<PredicateMap> predicateMaps = new HashSet<PredicateMap>();
    try {
      for (Statement statement : statements) {
        log.info("[RMLMappingFactory] saved Graphs " + savedGraphMaps);
        PredicateMap predicateMap =
            extractPredicateMap(
                r2rmlMappingGraph, (Resource) statement.getObject(), savedGraphMaps);
        predicateMaps.add(predicateMap);
      }
    } catch (ClassCastException e) {
      throw new InvalidR2RMLStructureException(
          "[RMLMappingFactory:extractPredicateObjectMaps] "
              + "A resource was expected in object of predicateMap of "
              + predicateObject.stringValue());
    }
    // Extract object maps
    URI o = r2rmlMappingGraph.URIref(Vocab.R2RML_NAMESPACE + R2RMLTerm.OBJECT_MAP);
    statements = r2rmlMappingGraph.tuplePattern(predicateObject, o, null);
    if (statements.size() < 1) {
      throw new InvalidR2RMLStructureException(
          "[RMLMappingFactory:extractPredicateObjectMap] "
              + predicateObject.stringValue()
              + " has no object map defined : one or more is required.");
    }
    Set<ObjectMap> objectMaps = new HashSet<ObjectMap>();
    Set<ReferencingObjectMap> refObjectMaps = new HashSet<ReferencingObjectMap>();
    try {
      for (Statement statement : statements) {
        log.debug("[RMLMappingFactory:extractPredicateObjectMap] Try to extract object map..");
        ReferencingObjectMap refObjectMap =
            extractReferencingObjectMap(
                r2rmlMappingGraph,
                (Resource) statement.getObject(),
                savedGraphMaps,
                triplesMapResources);
        if (refObjectMap != null) {
          refObjectMaps.add(refObjectMap);
          // Not a simple object map, skip to next.
          continue;
        }
        ObjectMap objectMap =
            extractObjectMap(
                r2rmlMappingGraph,
                (Resource) statement.getObject(),
                savedGraphMaps,
                triplesMapResources);
        objectMap.setOwnTriplesMap(triplesMapResources.get(triplesMapSubject));
        log.debug(
            "[RMLMappingFactory:extractPredicateObjectMap] ownTriplesMap attempted "
                + triplesMapResources.get(statement.getContext())
                + " for object "
                + statement.getObject().stringValue());
        objectMaps.add(objectMap);
      }
    } catch (ClassCastException e) {
      throw new InvalidR2RMLStructureException(
          "[RMLMappingFactory:extractPredicateObjectMaps] "
              + "A resource was expected in object of objectMap of "
              + predicateObject.stringValue());
    }
    PredicateObjectMap predicateObjectMap =
        new StdPredicateObjectMap(predicateMaps, objectMaps, refObjectMaps);

    // Add graphMaps
    Set<GraphMap> graphMaps = new HashSet<GraphMap>();
    Set<Value> graphMapValues =
        extractValuesFromResource(r2rmlMappingGraph, predicateObject, R2RMLTerm.GRAPH_MAP);

    if (graphMapValues != null) {
      graphMaps = extractGraphMapValues(r2rmlMappingGraph, graphMapValues, savedGraphMaps);
      log.info("[RMLMappingFactory] graph Maps returned " + graphMaps);
    }
    /*Set<GraphMap> graphMaps = new HashSet<GraphMap>();
    log.debug("[RMLMappingFactory] GraphMaps " + graphMaps);
    if (graphMapValues != null) {
        for (Value graphMap : graphMapValues) {
            log.info("[RMLMappingFactory] graph map + " + graphMap);
            // Create associated graphMap if it has not already created
            boolean found = false;
            GraphMap graphMapFound = null;
            /*
             * for (GraphMap savedGraphMap : savedGraphMaps) if
             * (savedGraphMap.getGraph().equals(graphMap)) { found = true;
             * graphMapFound = savedGraphMap; }
             */
    /*        if (found) {
                log.info("[RMLMappingFactory] graph map + " + graphMap);
                graphMaps.add(graphMapFound);
            } else {
                GraphMap newGraphMap = extractGraphMap(r2rmlMappingGraph,
                        (Resource) graphMap);
                savedGraphMaps.add(newGraphMap);
                graphMaps.add(newGraphMap);
                log.info("[RMLMappingFactory] new graph map + " + newGraphMap);
            }
        }
    }*/
    predicateObjectMap.setGraphMaps(graphMaps);
    log.debug("[RMLMappingFactory:extractPredicateObjectMap] Extract predicate-object map done.");
    return predicateObjectMap;
  }