You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by st...@apache.org on 2018/02/15 01:54:58 UTC

[03/17] commons-rdf git commit: Parser refactoring

Parser refactoring


Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/7d05cc0b
Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/7d05cc0b
Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/7d05cc0b

Branch: refs/heads/fluent-parser
Commit: 7d05cc0bea6a13a7959813c532ea27500b447039
Parents: 96c21ea
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Tue Feb 21 01:05:46 2017 +0000
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Wed Feb 14 18:57:54 2018 +0000

----------------------------------------------------------------------
 .../commons/rdf/experimental/ParserFactory.java | 142 +++++++++++++++++++
 .../apache/commons/rdf/experimental/Test.java   | 130 +++++++++++++++++
 2 files changed, 272 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/7d05cc0b/api/src/main/java/org/apache/commons/rdf/experimental/ParserFactory.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/experimental/ParserFactory.java b/api/src/main/java/org/apache/commons/rdf/experimental/ParserFactory.java
new file mode 100644
index 0000000..b80ac91
--- /dev/null
+++ b/api/src/main/java/org/apache/commons/rdf/experimental/ParserFactory.java
@@ -0,0 +1,142 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed  this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * OUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.experimental;
+
+import java.io.InputStream;
+import java.nio.file.Path;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.function.Consumer;
+
+import org.apache.commons.rdf.api.Dataset;
+import org.apache.commons.rdf.api.Graph;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Quad;
+import org.apache.commons.rdf.api.RDF;
+import org.apache.commons.rdf.api.RDFSyntax;
+import org.apache.commons.rdf.experimental.ParserFactory.NeedSourceBased;
+import org.apache.commons.rdf.experimental.ParserFactory.NeedSourceOrBase;
+import org.apache.commons.rdf.experimental.ParserFactory.OptionalTarget;
+import org.apache.commons.rdf.experimental.ParserFactory.Sync;
+import org.apache.commons.rdf.experimental.ParserFactory.Source;
+import org.apache.commons.rdf.experimental.ParserFactory.Target;
+
+public interface ParserFactory extends _NeedTargetOrRDF {
+    
+    interface Async<T, S> {
+        <V> Async<T, S> option(Option<V> option, V value);
+
+        Future<Parsed<T, S>> parseAsync();
+    }
+
+    interface NeedSourceBased<T> extends _NeedIdentifiedSource<T> {
+        <V> NeedSourceBased<T> option(Option<V> option, V value);
+
+        Sync<T, InputStream> source(InputStream is);
+    }
+
+    interface NeedSourceOrBase<T> extends _OptionalBase<T>, _NeedIdentifiedSource<T> {
+        <V> NeedSourceOrBase<T> option(Option<V> option, V value);
+    }
+
+    interface NeedSourceOrBaseOrSyntax<T> extends _OptionalBase<T>, _NeedIdentifiedSource<T> {
+        <V> NeedSourceOrBaseOrSyntax<T> option(Option<V> option, V value);
+
+        NeedSourceOrBase<T> syntax(RDFSyntax syntax);
+    }
+
+    interface NeedTarget extends _NeedTarget {
+        <V> NeedTarget option(Option<V> option, V value);
+    }
+
+    interface NeedTargetOrRDF extends _NeedTargetOrRDF {
+        <V> NeedTargetOrRDF option(Option<V> option, V value);
+    }
+
+    interface NeedTargetOrRDFOrSyntax extends _NeedTargetOrRDF {
+        <V> NeedTargetOrRDFOrSyntax option(Option<V> option, V value);
+    }
+
+    interface Option<V> {
+    }
+
+    interface OptionalTarget<T> extends _NeedTarget, NeedSourceOrBase<T> {
+        <V> OptionalTarget<T> option(Option<V> option, V value);
+    }
+
+    interface Parsed<T, S> {
+        long count();
+
+        Source<S> source();
+
+        Target<T> target();
+    }
+
+    interface Sync<T, S> {
+        Async<T, S> async();
+
+        Async<T, S> async(ExecutorService executor);
+
+        <V> Sync<T, S> option(Option<V> option, V value);
+
+        Parsed<T, S> parse();
+    }
+
+    interface Source<T> {
+        T source();
+    }
+
+    interface Target<T> {
+        T target();
+    }
+
+    NeedTargetOrRDF syntax(RDFSyntax syntax);
+}
+
+interface _NeedIdentifiedSource<T> {
+    Sync<T, IRI> source(IRI iri);
+
+    Sync<T, Path> source(Path path);
+
+    <S> Sync<T, S> source(Source<S> source);
+
+    Sync<T, IRI> source(String iri);
+}
+
+interface _NeedTarget {
+    NeedSourceOrBase<Consumer<Quad>> target(Consumer<? super Quad> consumer);
+
+    NeedSourceOrBase<Dataset> target(Dataset dataset);
+
+    NeedSourceOrBase<Graph> target(Graph graph);
+
+    <T> NeedSourceOrBase<T> target(Target<T> target);
+}
+
+interface _NeedTargetOrRDF extends _NeedTarget, _OptionalRDF {
+}
+
+interface _OptionalBase<T> {
+    NeedSourceBased<T> base(IRI iri);
+
+    NeedSourceBased<T> base(String iri);
+}
+
+interface _OptionalRDF {
+    OptionalTarget rdf(RDF rdf);
+}

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/7d05cc0b/api/src/test/java/org/apache/commons/rdf/experimental/Test.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/experimental/Test.java b/api/src/test/java/org/apache/commons/rdf/experimental/Test.java
new file mode 100644
index 0000000..cee0854
--- /dev/null
+++ b/api/src/test/java/org/apache/commons/rdf/experimental/Test.java
@@ -0,0 +1,130 @@
+package org.apache.commons.rdf.experimental;
+
+import static org.junit.Assert.*;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.commons.rdf.api.Graph;
+import org.apache.commons.rdf.api.RDFSyntax;
+import org.slf4j.impl.SimpleLoggerFactory;
+
+interface State { 
+    Path source();
+    Graph target();
+    RDFSyntax syntax();
+    State withSource(Path p);
+    State withTarget(Graph g);
+    State withSyntax(RDFSyntax s);
+}
+
+abstract class AbstractState implements State { 
+    @Override
+    public State withSource(Path p) { 
+       return new WithSource(p, this); 
+    }
+    @Override
+    public State withSyntax(RDFSyntax s) {
+        return new WithSyntax(s, this);
+    }
+    @Override
+    public State withTarget(Graph g) {
+        return new WithTarget(g, this);
+    }
+    
+}
+
+final class DefaultState extends AbstractState {
+    @Override
+    public Path source() {
+        throw new IllegalStateException("Source not set");
+    }
+    @Override
+    public Graph target() {
+        throw new IllegalStateException("Target not set");
+    }
+    @Override
+    public RDFSyntax syntax() {
+        throw new IllegalStateException("Syntax not set");
+    }
+}
+
+abstract class Inherited extends AbstractState  {
+    private final State parent;
+    public Inherited() {
+        this(new DefaultState());
+    }
+    public Inherited(State state) {
+        parent = state;
+    }    
+    @Override
+    public Path source() {
+        return parent.source();
+    }
+    @Override
+    public Graph target() {
+        return parent.target();
+    }
+    @Override
+    public RDFSyntax syntax() {
+        return parent.syntax();
+    }
+    
+    
+}
+
+final class WithSource extends Inherited {    
+    private final Path source;   
+    public WithSource(final Path path) {
+        this.source = path;
+    }
+    public WithSource(final Path path, final State state) {
+        super(state);
+        this.source = path;
+    }    
+    @Override
+    public Path source() {
+        return source;
+    }
+}
+
+
+final class WithTarget extends Inherited {    
+    private final Graph target;   
+    public WithTarget(final Graph g) {
+        this.target = g;
+    }
+    public WithTarget(final Graph g, final State state) {
+        super(state);
+        this.target = g;
+    }    
+    @Override
+    public Graph target() {
+        return target;
+    }
+}
+
+final class WithSyntax extends Inherited {    
+    private final RDFSyntax syntax;   
+    public WithSyntax(final RDFSyntax s) {
+        syntax = s;
+    }
+    public WithSyntax(final RDFSyntax s, final State state) {
+        super(state);
+        syntax = s;
+    }    
+    @Override
+    public RDFSyntax syntax() {
+        return syntax;
+    }
+}
+
+public class Test {
+    @org.junit.Test
+    public void testName() throws Exception {
+        Path p = Paths.get("/tmp/f.txt");
+        Graph g = null;
+        State s = new DefaultState().withSource(p).withTarget(g).withSyntax(RDFSyntax.JSONLD);
+        
+    }
+}