use of org.apache.uima.resource.metadata.TypeDescription in project uima-uimaj by apache.
the class MultiPageEditor method doJCasGen.
/**
* Do J cas gen.
*
* @param monitor
* the monitor
*/
public void doJCasGen(IProgressMonitor monitor) {
if (0 < mergedTypesAddingFeatures.size()) {
if (Window.CANCEL == Utility.popOkCancel("Type feature merging extended features", "Before generating the JCas classes for the CAS types, please note that " + "the following types were generated by merging different type descriptors, " + "where the resulting number of features is larger than that of the components. " + "Although the resulting generated JCas classes are correct, " + "doing this kind of merging makes reuse of this component more difficult." + makeMergeMessage(mergedTypesAddingFeatures) + "\n Press OK to generate the JCas classes anyway, or cancel to skip generating the JCas classes.", MessageDialog.WARNING)) {
return;
}
}
final JCasGenThrower jCasGenThrower = new JCasGenThrower();
try {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final Jg jg = new Jg();
final TypeDescription[] types = mergedTypeSystemDescription.getTypes();
final String outputDirectory = getPrimarySourceFolder().getLocation().toOSString();
// path to descriptor file
final String inputFile = file.getLocation().toOSString();
IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor progressMonitor) {
try {
jg.mainForCde(new MergerImpl(), new JCasGenProgressMonitor(progressMonitor), jCasGenThrower, inputFile, outputDirectory, types, (CASImpl) getCurrentView(), // https://issues.apache.org/jira/browse/UIMA-5715
getProject().getLocation().toString(), // /default/project.name etc
limitJCasGenToProjectScope, mergedTypesAddingFeatures);
} catch (IOException e) {
// $NON-NLS-1$
Utility.popMessage(// $NON-NLS-1$
Messages.getString("MultiPageEditor.25"), // $NON-NLS-1$
Messages.getString("MultiPageEditor.26") + getMessagesToRootCause(e), MessageDialog.ERROR);
}
}
};
workspace.run(runnable, monitor);
getPrimarySourceFolder().refreshLocal(IResource.DEPTH_INFINITE, null);
String jcasMsg = jCasGenThrower.getMessage();
if (null != jcasMsg && jcasMsg.length() > 0) {
// $NON-NLS-1$
Utility.popMessage(// $NON-NLS-1$
Messages.getString("MultiPageEditor.JCasGenErrorTitle"), // $NON-NLS-1$
Messages.getFormattedString(// $NON-NLS-1$
"MultiPageEditor.jcasGenErr", new String[] { jcasMsg }), MessageDialog.ERROR);
System.out.println(jcasMsg);
}
} catch (Exception ex) {
// $NON-NLS-1$
Utility.popMessage(// $NON-NLS-1$
Messages.getString("MultiPageEditor.JCasGenErrorTitle"), // $NON-NLS-1$
Messages.getFormattedString(// $NON-NLS-1$
"MultiPageEditor.jcasGenErr", new String[] { jCasGenThrower.getMessage() }), MessageDialog.ERROR);
ex.printStackTrace();
}
}
use of org.apache.uima.resource.metadata.TypeDescription in project uima-uimaj by apache.
the class MultiPageEditor method checkForNewlyDirtyTypes.
/**
* Check for newly dirty types.
*
* @param oldTsd
* the old tsd
*/
private void checkForNewlyDirtyTypes(TypeSystemDescription oldTsd) {
// an array of TypeDescription objects (not CAS), including imported ones
TypeDescription[] oldTypes = (null == oldTsd || null == oldTsd.getTypes()) ? new TypeDescription[0] : oldTsd.getTypes();
HashMap oldTypeHash = new HashMap(oldTypes.length);
for (int i = 0, length = oldTypes.length; i < length; i++) {
TypeDescription oldType = oldTypes[i];
oldTypeHash.put(oldType.getName(), oldType);
}
TypeDescription[] newTypes = mergedTypeSystemDescription.getTypes();
for (int i = 0; i < newTypes.length; i++) {
TypeDescription newType = newTypes[i];
TypeDescription oldType = (TypeDescription) oldTypeHash.get(newType.getName());
if (newType.equals(oldType)) {
oldTypeHash.remove(oldType.getName());
} else {
addDirtyTypeName(newType.getName());
if (oldType != null) {
oldTypeHash.remove(oldType.getName());
}
}
}
Set deletedTypes = oldTypeHash.keySet();
Iterator deletedTypeIterator = deletedTypes.iterator();
while (deletedTypeIterator.hasNext()) {
removeDirtyTypeName((String) deletedTypeIterator.next());
}
}
use of org.apache.uima.resource.metadata.TypeDescription in project uima-uimaj by apache.
the class DefinedTypesWithSupers method update.
/**
* Update.
*/
private void update() {
cachedResult.clear();
// for aggregates, this is the fully-merged type system
// for all systems, it is the type system with imports resolved
TypeSystemDescription typeSystemDescription = modelRoot.getMergedTypeSystemDescription();
if (typeSystemDescription == null)
// cleared table
return;
TypeDescription[] types = typeSystemDescription.getTypes();
TypeSystem typeSystem = modelRoot.descriptorCAS.get().getTypeSystem();
String typeName;
Map allTypes = modelRoot.allTypes.get();
for (int i = 0; i < types.length; i++) {
cachedResult.add(typeName = types[i].getName());
Type nextType = (Type) allTypes.get(typeName);
while (nextType != null) {
nextType = typeSystem.getParent(nextType);
if (nextType != null)
cachedResult.add(nextType.getName());
}
}
}
use of org.apache.uima.resource.metadata.TypeDescription in project uima-uimaj by apache.
the class Jg method generateAllTypesFromTemplates.
/**
* Generate all types from templates.
*
* @param outputDirectory
* the output directory
* @param tds
* the tds
* @param aCas
* the a cas
* @param jcasTypeClass
* the jcas type class
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws InstantiationException
* the instantiation exception
* @throws IllegalAccessException
* the illegal access exception
*/
// This is also the interface for CDE
private void generateAllTypesFromTemplates(String outputDirectory, TypeDescription[] tds, CASImpl aCas, Class<JCasTypeTemplate> jcasTypeClass) throws IOException, InstantiationException, IllegalAccessException {
// Create instances of Template classes
IJCasTypeTemplate jcasTypeInstance = (IJCasTypeTemplate) jcasTypeClass.newInstance();
Set<String> generatedBuiltInTypes = new TreeSet<>();
this.cas = aCas;
this.typeSystem = cas.getTypeSystem();
this.casStringType = typeSystem.getType(CAS.TYPE_NAME_STRING);
this.tcasAnnotationType = typeSystem.getType(CAS.TYPE_NAME_ANNOTATION);
for (int i = 0; i < tds.length; i++) {
TypeDescription td = tds[i];
// System.out.println("Description: " + td.getDescription() );
if (noGenTypes.contains(td.getName()))
continue;
if (td.getSupertypeName().equals("uima.cas.String"))
continue;
if (limitJCasGenToProjectScope && isOutOfScope(td, projectPathDir)) {
Set<String> mt = mergedTypesAddingFeatures.get(td.getName());
if (null == mt) {
continue;
}
StringBuilder sb = new StringBuilder("\n");
for (String p : mt) {
sb.append(" ").append(p).append('\n');
}
error.newError(IError.ERROR, getString("limitingButTypeWasExtended", new Object[] { td.getName(), sb.toString() }), null);
continue;
}
// if the type is built-in - augment it with the built-in's features
FeatureDescription[] builtInFeatures = (FeatureDescription[]) extendableBuiltInTypes.get(td.getName());
if (null != builtInFeatures) {
generatedBuiltInTypes.add(td.getName());
List<FeatureDescription> newFeatures = setDifference(td.getFeatures(), builtInFeatures);
int newFeaturesSize = newFeatures.size();
if (newFeaturesSize > 0) {
int newSize = builtInFeatures.length + newFeaturesSize;
FeatureDescription[] newFds = new FeatureDescription[newSize];
System.arraycopy(builtInFeatures, 0, newFds, 0, builtInFeatures.length);
for (int j = builtInFeatures.length, k = 0; k < newFeaturesSize; j++, k++) newFds[j] = (FeatureDescription) newFeatures.get(k);
td.setFeatures(newFds);
} else {
// In this case, skip generation
continue;
}
}
generateClassesFromTemplate(td, outputDirectory, jcasTypeInstance);
}
/*
* This code was supposed to generate extendable built-in types that were not extended by the
* input types But the only extendable built-in type is DocumentAnnotation, and it should not be
* generated by default - there's one provided by the framework.
*
* for (Iterator it = extendableBuiltInTypes.entrySet().iterator(); it.hasNext();) { Map.Entry
* entry = (Map.Entry) it.next(); String typeName = (String) entry.getKey(); if
* (noGenTypes.contains(typeName) || generatedBuiltInTypes.contains(typeName)) continue;
* TypeDescription td = createTdFromType(typeName); generateClasses(td, outputDirectory); }
*/
}
use of org.apache.uima.resource.metadata.TypeDescription in project uima-uimaj by apache.
the class Jg method main1.
/**
* Arguments are: -jcasgeninput xxxx -jcasgenoutput xxxx -jcasgenclasspath xxxx
*
* @param arguments
* the arguments
* @return the int
*/
public int main1(String[] arguments) {
boolean hadError = false;
try {
try {
if (null == progressMonitor) {
progressMonitor = new UimaLoggerProgressMonitor();
}
if (null == error)
error = new LogThrowErrorImpl();
String inputFile = null;
String outputDirectory = null;
TypeSystemDescription typeSystemDescription = null;
TypeDescription[] tds = null;
// init to default value
projectPathDir = "";
limitJCasGenToProjectScope = false;
for (int i = 0; i < arguments.length - 1; i++) {
if (arguments[i].equalsIgnoreCase("-jcasgeninput")) {
inputFile = arguments[++i];
continue;
}
if (arguments[i].equalsIgnoreCase("-jcasgenoutput")) {
outputDirectory = arguments[++i];
continue;
}
// This is used by the jcasgen maven plugin
if (// https://issues.apache.org/jira/browse/UIMA-3044
arguments[i].equalsIgnoreCase("=jcasgenclasspath") || arguments[i].equalsIgnoreCase("-jcasgenclasspath")) {
// https://issues.apache.org/jira/browse/UIMA-3044
classPath = arguments[++i];
continue;
}
if (arguments[i].equalsIgnoreCase("-limitToDirectory")) {
projectPathDir = arguments[++i];
limitJCasGenToProjectScope = (projectPathDir.length() > 0);
continue;
}
}
xmlSourceFileName = inputFile.replaceAll("\\\\", "/");
URL url;
if (inputFile.substring(0, 4).equalsIgnoreCase("jar:")) {
// https://issues.apache.org/jira/browse/UIMA-1793 get things out of Jars
try {
url = new URL(inputFile);
// }
if (null == outputDirectory || outputDirectory.equals("")) {
error.newError(IError.ERROR, getString("sourceArgNeedsDirectory", new Object[] { inputFile }), null);
}
} catch (MalformedURLException e) {
error.newError(IError.ERROR, getString("fileNotFound", new Object[] { inputFile }), null);
// never get here, the previous statement throws. Needed, though for java
url = null;
// path analysis.
}
} else {
File file = new File(inputFile);
if (!file.exists()) {
error.newError(IError.ERROR, getString("fileNotFound", new Object[] { inputFile }), null);
}
if (null == outputDirectory || outputDirectory.equals("")) {
File dir = file.getParentFile();
if (null == dir) {
error.newError(IError.ERROR, getString("sourceArgNeedsDirectory", new Object[] { inputFile }), null);
}
outputDirectory = dir.getPath() + File.separator + "JCas" + ((null != merger) ? "" : "New");
}
url = file.toURI().toURL();
}
progressMonitor.beginTask("", 5);
progressMonitor.subTask("Output going to '" + outputDirectory + "'");
progressMonitor.subTask(getString("ReadingDescriptorAndCreatingTypes", new Object[] { inputFile }));
// code to read xml and make cas type instance
CASImpl casLocal = null;
// handle classpath
try {
XMLInputSource in = new XMLInputSource(url);
XMLizable specifier = UIMAFramework.getXMLParser().parse(in);
mergedTypesAddingFeatures.clear();
if (specifier instanceof AnalysisEngineDescription) {
AnalysisEngineDescription aeSpecifier = (AnalysisEngineDescription) specifier;
if (!aeSpecifier.isPrimitive())
typeSystemDescription = CasCreationUtils.mergeDelegateAnalysisEngineTypeSystems(aeSpecifier, createResourceManager(), mergedTypesAddingFeatures);
else
typeSystemDescription = mergeTypeSystemImports(aeSpecifier.getAnalysisEngineMetaData().getTypeSystem());
} else if (specifier instanceof TypeSystemDescription)
typeSystemDescription = mergeTypeSystemImports(((TypeSystemDescription) specifier));
else {
error.newError(IError.ERROR, getString("fileDoesntParse", new Object[] { inputFile }), null);
}
if (mergedTypesAddingFeatures.size() > 0) {
error.newError(IError.WARN, getString("typesHaveFeaturesAdded", new Object[] { makeMergeMessage(mergedTypesAddingFeatures) }), null);
}
TypePriorities typePriorities = null;
FsIndexDescription[] fsIndexDescription = null;
try {
// no ResourceManager, since everything has been
// imported/merged by previous actions
casLocal = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, typePriorities, fsIndexDescription);
} catch (ResourceInitializationException e) {
error.newError(IError.WARN, getString("resourceInitializationException", new Object[] { e.getLocalizedMessage() }), e);
// continue with null cas, anyway
casLocal = null;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidXMLException e) {
error.newError(IError.ERROR, getString("invalidXML", new Object[] { inputFile }), e);
} catch (ResourceInitializationException e) {
error.newError(IError.ERROR, getString("resourceInitializationExceptionError", new Object[] {}), e);
}
progressMonitor.worked(1);
tds = typeSystemDescription.getTypes();
// Generate type classes from DEFAULT templates
generateAllTypesFromTemplates(outputDirectory, tds, casLocal, JCasTypeTemplate.class);
} catch (IOException e) {
error.newError(IError.ERROR, getString("IOException", new Object[] {}), e);
} catch (ErrorExit e) {
hadError = true;
} catch (InstantiationException e) {
error.newError(IError.ERROR, getString("InstantiationException", new Object[] {}), e);
} catch (IllegalAccessException e) {
error.newError(IError.ERROR, getString("IllegalAccessException", new Object[] {}), e);
}
} finally {
progressMonitor.done();
}
return (hadError) ? -1 : 0;
}
Aggregations