2

I've been coding Perl and Python a lot and this time I got an assignment to code in Java instead. So I'm not too familiar with handling data in Java.

My task involves having a input file where I need to check dependencies and then output those with transitive dependencies. Clearer ideas below:

Input File:

A: B C
B: C E
C: G
D: A

Output File:

A: B C E G
B: C E G
C: G
D: A B C E G

So far this is what I've got (separating the first and second token):

import java.util.StringTokenizer;
import java.io.*;

public class TestDependency {

    public static void main(String[] args) {

        try{

        FileInputStream fstream = new FileInputStream("input-file");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;

        while ((strLine = br.readLine()) != null)   {
            StringTokenizer items = new StringTokenizer(strLine, ":");

            System.out.println("I: " + items.nextToken().trim());

            StringTokenizer depn = new StringTokenizer(items.nextToken().trim(), " ");

                while(depn.hasMoreTokens()) {
                    System.out.println( "D: " + depn.nextToken().trim() );
            }
        }

        } catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        }
    }
}

Any help appreciated. I can imagine Perl or Python to handle this easily. Just need to implement it in Java.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
LynxLee
  • 321
  • 1
  • 6
  • 17

2 Answers2

2

This is not very efficient memory-wise and requires good input but should run fine.

public class NodeParser {

    // Map holding references to nodes
    private Map<String, List<String>> nodeReferenceMap;

    /**
     * Parse file and create key/node array pairs
     * @param inputFile
     * @return
     * @throws IOException
     */
    public Map<String, List<String>> parseNodes(String inputFile) throws IOException {

        // Reset list if reusing same object
        nodeReferenceMap = new HashMap<String, List<String>>();

        // Read file
        FileInputStream fstream = new FileInputStream(inputFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        // Parse nodes into reference mapping
        while((strLine = br.readLine()) != null) {
            // Split key from nodes
            String[] tokens = strLine.split(":");
            String key = tokens[0].trim();
            String[] nodes = tokens[1].trim().split(" ");
            // Set nodes as an array list for key
            nodeReferenceMap.put(key, Arrays.asList(nodes));
        }

        // Recursively build node mapping
        Map<String, Set<String>> parsedNodeMap = new HashMap<String, Set<String>>();
        for(Map.Entry<String, List<String>> entry : nodeReferenceMap.entrySet()) {
            String key = entry.getKey();
            List<String> nodes = entry.getValue();
            // Create initial node set
            Set<String> outSet = new HashSet<String>();
            parsedNodeMap.put(key, outSet);
            // Start recursive call
            addNode(outSet, nodes);
        }

        // Sort keys
        List<String> sortedKeys = new ArrayList<String>(parsedNodeMap.keySet());
        Collections.sort(sortedKeys);

        // Sort nodes
        Map<String, List<String>> sortedParsedNodeMap = new LinkedHashMap<String, List<String>>();
        for(String key : sortedKeys) {
            List<String> sortedNodes = new ArrayList<String>(parsedNodeMap.get(key));
            Collections.sort(sortedNodes);
            sortedParsedNodeMap.put(key, sortedNodes);
        }

        // Return sorted key/node mapping
        return sortedParsedNodeMap;
    }

    /**
     * Recursively add nodes by referencing the previously generated list mapping
     * @param outSet
     * @param nodes
     */
    private void addNode(Set<String> outSet, List<String> nodes) {
        // Add each node to the set mapping
        for(String node : nodes) {
            outSet.add(node);
            // Get referenced nodes
            List<String> nodeList = nodeReferenceMap.get(node);
            if(nodeList != null) {
                // Create array list from abstract list for remove support
                List<String> referencedNodes = new ArrayList<String>(nodeList);
                // Remove already searched nodes to prevent infinite recursion
                referencedNodes.removeAll(outSet);
                // Recursively search more node paths
                if(!referencedNodes.isEmpty()) {
                    addNode(outSet, referencedNodes);
                }
            }
        }
    }
}

Then, you can call this from your program like so:

    public static void main(String[] args) {
        try {
            NodeParser nodeParser = new NodeParser();
            Map<String, List<String>> nodeSet = nodeParser.parseNodes("./res/input.txt");
            for(Map.Entry<String, List<String>> entry : nodeSet.entrySet()) {
                String key = entry.getKey();
                List<String> nodes = entry.getValue();
                System.out.println(key + ": " + nodes);
            }
        } catch (IOException e){
            System.err.println("Error: " + e.getMessage());
        }
    }

Also, the output is not sorted but that should be trivial.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Chase
  • 11,161
  • 8
  • 42
  • 39
  • I'm gonna need some time to digest this stuff. Haha.. Nope, just wanted to learn to program. Looks complicated.. – LynxLee Oct 01 '11 at 18:30
  • There you go. I added sorting and fixed an infinite recursion bug. Good luck. – Chase Oct 02 '11 at 15:28
1
String s = "A: B C D";
String i = s.split(":")[0];
String dep[] = s.split(":")[1].trim().split(" ");
System.out.println("i = "+i+", dep = "+Arrays.toString(dep));
blackcompe
  • 3,180
  • 16
  • 27