/** * Tries to evaluate the input that this instance was created with. Success of evaluation can be * queried using {@link #errorOccurred()}. If an error occurred, the exception can be obtained * using {@link #getLastError()}. If evaluation was successful, the result can be obtained using * {@link #getRoot()}. * * @param rootNs The namespace to which ne declarations will be stored. * @param workingNs The initial namespace to work with. */ public void evaluate(Namespace rootNs, Namespace workingNs) { try { final InputParser parser = new InputParser(this.input, this.encoding, this.reporter); this.lastResult = parser.parse(); if (this.lastResult == null) { return; } // set parent attributes for all nodes final ASTVisitor parentSetter = new ParentSetter(); this.lastResult.visit(parentSetter); final ProblemReporter reporter = this.reporter.subReporter(this.lastResult.getPosition()); // resolve types TypeResolver.resolveAST(this.lastResult, workingNs, reporter); if (!this.reporter.hasProblems()) { final ASTVisitor executor = getExecutor(rootNs, workingNs, reporter); this.lastResult.visit(executor); } } catch (UnsupportedEncodingException e) { throw new RuntimeException("This should not have happened", e); // $NON-NLS-1$ } catch (ASTTraversalException e) { e.printStackTrace(); this.lastError = e; } }
protected ParsedCommand parse(String[] input) { if (isMissingArguments(input)) { return createParsedCommandError(ERROR_MISSING_ARGS); } else { String inputArgs = input[INDEX_FOR_ARGS]; try { String parsedTitle = getTitleWithDateKeywords(inputArgs); Calendar[] parsedTimes = getStandardDatesTimes(inputArgs); if (mustRemoveDateKeywordSection(parsedTimes, inputArgs)) { parsedTitle = removeDateKeywordSection(parsedTitle); } String parsedDescription = getDescriptionFromString(inputArgs); ArrayList<String> parsedTags = getTagsFromString(inputArgs); ParsedCommand pc = new ParsedCommand.ParsedCommandBuilder(MyParser.CommandType.ADD) .title(parsedTitle) .times(parsedTimes) .description(parsedDescription) .tags(parsedTags) .build(); return pc; } catch (InvalidArgumentsForParsedCommandException e) { return InputParser.createParsedCommandError(e.getMessage()); } } }
public static void main(String[] args) throws IOException { WeightedGraph wg = InputParser.parseAssignmentFile(); MaxSpaceGreedyClustering algo = new MaxSpaceGreedyClustering(wg); int spacing = algo.findClusters(4); System.out.println("Computed max spacing: " + spacing); }
/** * Creates a new <code>RequestCtx</code> by parsing XML from an input stream. Note that this a * convenience method, and it will not do schema validation by default. You should be parsing the * data yourself, and then providing the root node to the other <code>getInstance</code> method. * If you use this convenience method, you probably want to turn on validation by setting the * context schema file (see the programmer guide for more information on this). * * @param input a stream providing the XML data * @return a new <code>RequestCtx</code> * @throws ParsingException if there is an error parsing the input */ public static RequestCtx getInstance(InputStream input) throws ParsingException { return getInstance(InputParser.parseInput(input, "Request")); }
public static void main(String[] args) throws IOException, Exception { // break into cases for each data set // for congressional, partition into training and test sets int whichDataSet = -1; Scanner in = new Scanner(System.in); String whichMonk = ""; boolean IG = false; int tempIG = -1; // check input parameters try { if (args.length > 3) throw new NumberFormatException(); tempIG = Integer.parseInt(args[0]); whichDataSet = Integer.parseInt(args[1]); // System.out.println(tempIG + " " + whichDataSet); if (args.length < 2 || whichDataSet < 0 || whichDataSet > 2 || tempIG < 0 || tempIG > 1) { throw new NumberFormatException(); } // System.out.println(tempIG + " " + whichDataSet); if (args.length < 3 && whichDataSet == 1) { throw new NumberFormatException(); } // System.out.println(tempIG + "* " + whichDataSet); if (args.length == 3) { int whichMonkInt = Integer.parseInt(args[2]); if (whichMonkInt == 1) whichMonk = "monks-1"; else if (whichMonkInt == 2) whichMonk = "monks-2"; else if (whichMonkInt == 3) whichMonk = "monks-3"; else throw new NumberFormatException(); } if (tempIG == 0) IG = false; else IG = true; } catch (NumberFormatException e) { System.out.println( "ERROR: \n\nUsage: ./run_traditional <IG or GR> <whichDataSet> <whichMonkDataSet> \n" + "IG or GR: (0) for information gain, (1) for gain ratio" + "\nwhichDataSet can be: (0=Congressional, 1=MONK, 2=Mushroom)" + "\nwhichMonkDataSet is only relevant if Monk chosen can be: (1 2 or 3)"); System.exit(1); } // cases for each data set switch (whichDataSet) { case 0: { fileName = "house-votes-84.data"; System.out.println("\n\nRunning data set: " + fileName); parser = new CongressionalInputParser(); outputClasses = parser.initializeOutputClasses(); masterAttributes = parser.initializeMasterAttributesAndValues(); attributes = parser.getAttributesSet(); examples = parser.readExamples(fileName); partitionIntoTestAndTraining(examples); traditionalDecisionTreeBuilder = new GreedyInformationGainDecisionTree( trainingExamples, masterAttributes, attributes, outputClasses, IG); System.out.println("\n\nnumber of congressional Examples: " + examples.size()); break; } case 1: { String fileName1 = whichMonk + ".train"; String fileName2 = whichMonk + ".test"; System.out.println("\n\nRunning data set: " + fileName1); parser = new MonkInputParser(); outputClasses = parser.initializeOutputClasses(); masterAttributes = parser.initializeMasterAttributesAndValues(); attributes = parser.getAttributesSet(); trainingExamples = parser.readExamples(fileName1); testExamples = parser.readExamples(fileName2); traditionalDecisionTreeBuilder = new GreedyInformationGainDecisionTree( trainingExamples, masterAttributes, attributes, outputClasses, IG); System.out.println( "\n\nnumber of monk " + whichMonk + " Examples: " + trainingExamples.size()); break; } case 2: { String fileName1 = "agaricus-lepiota.data"; System.out.println("\n\nRunning data set: " + fileName1); parser = new MushroomInputParser(); outputClasses = parser.initializeOutputClasses(); masterAttributes = parser.initializeMasterAttributesAndValues(); attributes = parser.getAttributesSet(); examples = parser.readExamples(fileName1); // testExamples = parser.readExamples(fileName2); partitionIntoTestAndTraining(examples); traditionalDecisionTreeBuilder = new GreedyInformationGainDecisionTree( trainingExamples, masterAttributes, attributes, outputClasses, IG); System.out.println("\n\nnumber of mushroom Examples: " + examples.size()); break; } } // System.out.println(trainingExamples.get(1).getAttributeList()); finalDecisionTree = traditionalDecisionTreeBuilder.makeDecisionTree(trainingExamples, attributes, null); // System.out.println(finalDecisionTree.toString()); System.out.println("\nTesting the tree on the TRAINING examples..."); tester = new DecisionTreeTester(trainingExamples, masterAttributes, attributes, outputClasses); tester.test(finalDecisionTree); String out = tester.printPerformanceMetrics(); System.out.println(out); System.out.println("\nTesting the tree on the " + testExamples.size() + " TEST examples..."); tester = new DecisionTreeTester(testExamples, masterAttributes, attributes, outputClasses); tester.test(finalDecisionTree); out = tester.printPerformanceMetrics(); System.out.println(out); System.out.println("Successful completion of program"); }