Structured Prediction Theory Based on Factor Graph Complexity