com.bigdata.rdf.graph.impl.BaseGASProgram Maven / Gradle / Ivy
Show all versions of bigdata-gas Show documentation
/**
Copyright (C) SYSTAP, LLC 2006-2012. All rights reserved.
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.
*/
package com.bigdata.rdf.graph.impl;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.apache.log4j.Logger;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import com.bigdata.rdf.graph.BinderBase;
import com.bigdata.rdf.graph.EdgesEnum;
import com.bigdata.rdf.graph.Factory;
import com.bigdata.rdf.graph.FrontierEnum;
import com.bigdata.rdf.graph.IBinder;
import com.bigdata.rdf.graph.IBindingExtractor;
import com.bigdata.rdf.graph.IGASContext;
import com.bigdata.rdf.graph.IGASProgram;
import com.bigdata.rdf.graph.IGASState;
import com.bigdata.rdf.graph.impl.util.VertexDistribution;
/**
* Abstract base class with some useful defaults.
*
* @author Bryan Thompson
* @param
* @param
* @param
*/
abstract public class BaseGASProgram implements
IGASProgram {
private static final Logger log = Logger.getLogger(BaseGASProgram.class);
// /**
// * If the vertex is actually an edge, then return the decoded edge.
// *
// * @see GASUtil#decodeStatement(Value)
// */
// protected Statement decodeStatement(final Value v) {
//
// return GASUtil.decodeStatement(v);
//
// }
/**
* {@inheritDoc}
*
* The default implementation returns {@link #getGatherEdges()} and the
* {@link #getScatterEdges()} if {@link #getGatherEdges()} returns
* {@value EdgesEnum#NoEdges}.
*
* TODO This ignores {@link IGASContext#isDirectedTraversal()}
*/
@Override
public EdgesEnum getSampleEdgesFilter() {
// Assume that a GATHER will be done for each starting vertex.
EdgesEnum edges = getGatherEdges();
if (edges == EdgesEnum.NoEdges) {
// If no GATHER is performed, then use the SCATTER edges.
edges = getScatterEdges();
}
return edges;
}
/**
* {@inheritDoc}
*
* The default gathers on the {@link EdgesEnum#InEdges}.
*/
@Override
public EdgesEnum getGatherEdges() {
return EdgesEnum.InEdges;
}
/**
* {@inheritDoc}
*
* The default scatters on the {@link EdgesEnum#OutEdges}.
*/
@Override
public EdgesEnum getScatterEdges() {
return EdgesEnum.OutEdges;
}
/**
* {@inheritDoc}
*
* The default implementation populates the frontier IFF this is an
* {@link FrontierEnum#AllVertices} {@link IGASProgram}.
*/
@Override
public void before(final IGASContext ctx) {
switch (getInitialFrontierEnum()) {
case AllVertices: {
addAllVerticesToFrontier(ctx);
break;
}
}
}
// /**
// * {@inheritDoc}
// *
// * The default implementation is a NOP.
// */
// @Override
// public IReducer getDefaultAfterOp() {
//
// return null; // NOP
//
// }
/**
* Populate the initial frontier using all vertices in the graph.
*
* @param ctx
* The graph evaluation context.
*
* TODO This has a random number generator whose initial seed is
* not controlled by the caller. However, the desired use case
* here is to produce a distribution over ALL vertices so the
* random number should be ignored - perhaps we should pass it in
* as null
?
*/
private void addAllVerticesToFrontier(final IGASContext ctx) {
final IGASState gasState = ctx.getGASState();
final EdgesEnum sampleEdges = getSampleEdgesFilter();
final VertexDistribution dist = ctx.getGraphAccessor().getDistribution(
new Random());
final Resource[] initialFrontier = dist.getUnweightedSample(
dist.size(), sampleEdges);
if (log.isDebugEnabled())
log.debug("initialFrontier=" + Arrays.toString(initialFrontier));
gasState.setFrontier(ctx, initialFrontier);
}
/**
* {@inheritDoc}
*
* The default is a NOP.
*/
@Override
public void initVertex(final IGASContext ctx,
final IGASState state, final Value u) {
// NOP
}
// public Factory getVertexStateFactory();
/**
* {@inheritDoc}
*
* The default implementation returns null
. Override this if
* the algorithm uses per-edge computation state.
*/
@Override
public Factory getEdgeStateFactory() {
return null;
}
/**
* {@inheritDoc}
*
* The default implementation returns true
. Override this if
* you know whether or not the computation state of this vertex has changed.
*/
@Override
public boolean isChanged(IGASState state, Value u) {
return true;
}
/**
* {@inheritDoc}
*
* The default returns true
.
*/
@Override
public boolean nextRound(IGASContext ctx) {
return true;
}
/**
* {@inheritDoc}
*
*
* - {@value Bindings#VISITED}
* - The visited vertex itself.
*
*/
@Override
public List> getBinderList() {
final List> tmp = new LinkedList>();
tmp.add(new BinderBase() {
@Override
public int getIndex() {
return Bindings.VISITED;
}
@Override
public Value bind(final ValueFactory vf,
final IGASState state, final Value u) {
return u;
}
});
return tmp;
}
/**
* Interface declares symbolic constants for the {@link IBindingExtractor.IBinder}s reported
* by {@link BaseGASProgram#getBinderList()}.
*
* @author Bryan
* Thompson
*/
public interface Bindings {
/**
* The visited vertex identifier.
*/
int VISITED = 0;
}
}