com.cyc.km.query.export.ResultsExporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyc-query-client Show documentation
Show all versions of cyc-query-client Show documentation
Query API implementation for requesting and handling answers to arbitrarily complex questions
posed to a Cyc server.
/*
* Class for exporting query results.
*/
package com.cyc.km.query.export;
/*
* #%L
* File: ResultsExporter.java
* Project: Query Client
* %%
* Copyright (C) 2013 - 2017 Cycorp, Inc.
* %%
* 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.
* #L%
*/
import com.cyc.base.exception.CycConnectionException;
import com.cyc.baseclient.exception.ExportException;
import com.cyc.baseclient.export.PrintWriterExporter;
import com.cyc.query.Query;
import com.cyc.query.QueryImpl;
import com.cyc.query.QueryAnswer;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;
/**
* An abstract class for exporting {@link QueryImpl} results.
*
* As with any {@link PrintWriterExporter}, the output can be sent to a
* {@link PrintWriter}, a {@link PrintStream}, or a {@link String}.
*
* The results are structured as a document that is sent to the specified
* destination, and has sections for - A header
- QueryImpl
data
- QueryImpl answers
- A footer
*
* @author baxter
*/
public abstract class ResultsExporter extends PrintWriterExporter {
/**
*
*/
public ResultsExporter() {
super();
}
public ResultsExporter(PrintWriter printWriter) {
super(printWriter);
}
/**
* Construct a new exporter whose output will go to the specified stream.
*
* The exporter will not close the stream when it is done, or even when
* its {@link #close()} method is invoked. Thus this constructor can be safely
* used on {@link System#out} and similar streams that one usually does not
* want to close.
*
* @param printStream
*/
public ResultsExporter(PrintStream printStream) {
super(printStream);
}
@Override
final protected void doExport() throws ExportException {
try {
printHeader();
printQueryData();
printAnswers(object.getAnswers());
printFooter();
finishExport();
} catch (Exception e) {
throw new ExportException("Caught exception while exporting " + object, e);
}
}
/**
* Print any data that goes at the beginning of the export.
*
* @throws ExportException
* @throws com.cyc.base.exception.CycConnectionException
*/
protected void printHeader() throws ExportException, CycConnectionException {
}
/**
* Print any data that goes at the end of the export.
*
* @throws ExportException
*/
protected void printFooter() throws ExportException {
}
/**
* Print data about the query itself.
*
* @throws ExportException
*/
protected void printQueryData() throws ExportException {
}
/**
* Export the answers of the query.
*
* @param answers
* @throws ExportException
* @throws CycConnectionException
*/
final protected void printAnswers(List answers) throws CycConnectionException, ExportException {
heraldStartOfAnswers();
for (final QueryAnswer answer : answers) {
printAnswer(answer);
}
heraldEndOfAnswers();
}
/**
* Print something right before printing all the answer data.
*/
protected void heraldStartOfAnswers() {
}
/**
* Print one answer
*
* @param answer the answer
* @throws com.cyc.base.exception.CycConnectionException
* @throws ExportException
*/
protected abstract void printAnswer(QueryAnswer answer) throws CycConnectionException, ExportException;
/**
* Print something right after printing all the answer data.
*/
protected void heraldEndOfAnswers() {
}
/**
* After we've printed everything, finish things up.
*
* @throws ExportException
*/
protected void finishExport() throws ExportException {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy