/*
 * Copyright 2016 Thomas Krause.
 *
 * 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 org.corpus_tools.graphannis;

import java.util.LinkedHashSet;
import java.util.Set;
import org.corpus_tools.salt.common.SDocumentGraph;
import org.corpus_tools.salt.common.SDominanceRelation;
import org.corpus_tools.salt.common.SPointingRelation;
import org.corpus_tools.salt.common.SToken;
import org.corpus_tools.salt.core.SAnnotation;
import org.corpus_tools.salt.core.SLayer;
import org.corpus_tools.salt.core.SNode;

/**
 * A class which helps to import salt documents into graphANNIS.
 * 
 * @author Thomas Krause <thomaskrause@posteo.de>
 */
public class SaltImport
{
  public static API.GraphUpdate map(SDocumentGraph g)
  {
    API.GraphUpdate u = new API.GraphUpdate();
    
    for(SToken t : g.getTokens())
    {
      // use the unique name
      String name = t.getPath().fragment();
      u.addNode(name);
      // add all annotations
      for(SAnnotation anno : t.getAnnotations())
      {
        u.addNodeLabel(name, anno.getNamespace(), anno.getName(), anno.getValue_STEXT());
      }
    }
    
    for(SDominanceRelation rel : g.getDominanceRelations())
    {
      String sourceName = rel.getSource().getPath().fragment();
      String targetName = rel.getTarget().getPath().fragment();
     
      for(String l : getLayerNames(rel.getLayers()))
      {
        // add an edge both for the named component and for the un-named
        u.addEdge(sourceName, targetName, l, "DOMINANCE", rel.getType());
        u.addEdge(sourceName, targetName, l, "DOMINANCE", "");
      }
    }
    
    for(SPointingRelation rel : g.getPointingRelations())
    {
      String sourceName = rel.getSource().getPath().fragment();
      String targetName = rel.getTarget().getPath().fragment();
      
      for(String l : getLayerNames(rel.getLayers()))
      {
        // add an edge both for the named component and for the un-named
        u.addEdge(sourceName, targetName, l, "POINTING", rel.getType());
      }
    }
    
    return u;
  }
  
  private static Set<String> getLayerNames(Set<SLayer> layers)
  {
    Set<String> result = new LinkedHashSet<>();
    
    if(layers == null || layers.isEmpty())
    {
      // add the edge to the default empty layer
      result.add("");
    }
    else
    {
      for(SLayer l : layers)
      {
        result.add(l.getName());
      }
    }
    
    return result;
  }
}

@@ -736,16 +736,74 @@
 tGraph;%0A
+import org.corpus_tools.salt.common.SDocumentGraphObject;%0A
 import o
@@ -897,32 +897,224 @@
 intingRelation;%0A
+import org.corpus_tools.salt.common.SSpan;%0Aimport org.corpus_tools.salt.common.SStructure;%0Aimport org.corpus_tools.salt.common.SStructuredNode;%0Aimport org.corpus_tools.salt.common.STextualDS;%0A
 import org.corpu
@@ -1548,215 +1548,31 @@
 or(S
-Token t : g.getTokens())%0A    %7B%0A      // use the unique name%0A      String name = t.getPath().fragment();%0A      u.addNode(name);%0A      // add all annotations%0A      for(SAnnotation anno : t.getAnnotation
+Node n : g.getNode
 s())%0A
-  
     
@@ -1583,99 +1583,22 @@
     
-  u.
 addNode
-Label(name, anno.getNamespace(), anno.getName(), anno.getValue_STEXT());%0A      %7D
+(n, u);
 %0A   
@@ -2914,10 +2914,408 @@
 lt;%0A  %7D%0A
+  %0A  private static void addNode(SNode n, API.GraphUpdate u)%0A  %7B%0A    if(n instanceof SStructuredNode)%0A    %7B%0A      // use the unique name%0A      String name = n.getPath().fragment();%0A      u.addNode(name);%0A      // add all annotations%0A      for(SAnnotation anno : n.getAnnotations())%0A      %7B%0A        u.addNodeLabel(name, anno.getNamespace(), anno.getName(), anno.getValue_STEXT());%0A      %7D%0A    %7D%0A  %7D%0A
 %7D%0A

Use common function to map not only token but also other nodes.