io.konig.core.path.PathImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konig-core Show documentation
Show all versions of konig-core Show documentation
A library for core classes (Graph, Vertex, Edge, etc.)
package io.konig.core.path;
/*
* #%L
* konig-core
* %%
* Copyright (C) 2015 - 2016 Gregory McFall
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import io.konig.core.Graph;
import io.konig.core.Path;
import io.konig.core.SPARQLBuilder;
import io.konig.core.Traverser;
import io.konig.core.Vertex;
public class PathImpl implements Path {
private List stepList;
public PathImpl() {
stepList =new ArrayList<>();
}
public PathImpl(List list) {
stepList = list;
}
void add(Step step) {
stepList.add(step);
}
List getStepList() {
return stepList;
}
@Override
public Path out(URI predicate) {
stepList.add(new OutStep(predicate));
return this;
}
@Override
public Path in(URI predicate) {
stepList.add(new InStep(predicate));
return this;
}
@Override
public List asList() {
return stepList;
}
@Override
public Path copy() {
return new PathImpl(new ArrayList<>(stepList));
}
@Override
public Path has(URI predicate, Value value) {
stepList.add(new HasStep(predicate, value));
return this;
}
@Override
public Path v(Resource... resource) {
add(new VertexStep(resource));
return this;
}
@Override
public Set traverse(Vertex source) {
Set input = new HashSet<>();
input.add(source.getId());
Graph graph = source.getGraph();
Traverser traverser = new Traverser(graph, input);
for (Step step : stepList) {
traverser.visit(step);
}
return traverser.getResultSet();
}
@Override
public Set traverse(Traverser traverser) {
for (Step step : stepList) {
traverser.visit(step);
}
return traverser.getResultSet();
}
@Override
public void visit(SPARQLBuilder builder) {
for (Step s : stepList) {
s.visit(builder);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (Step step : stepList) {
builder.append(step.toString());
}
return builder.toString();
}
}