Save model after create individuals - jena

I am trying to create individuals and save them in OWL file. The OWL file was created in Protégé. The size of the file was 10KB but after trying to save the individuals in the ontology the size of the code becomes 7KB.
Then I tried to open the OWL file using Protégé but it will not open.
The code is:
String SOURCE = "http://www.semanticweb.org/ontologies/2012/9/untitled-ontology-19";
String NS = SOURCE + "#";
OntModel onto = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
onto.read("file:/home/tourism.owl", "RDF/XML");
OntClass place = onto.getOntClass(NS+"Mountains");
Individual am1 = onto.createIndividual(NS+Concept1, place);
FileOutputStream output = null;
try {
output = new FileOutputStream( "/home/tourism.owl ");
} catch(Exception e) {}
onto.writeAll(output, "RDF/XML-ABBREV","xmlbase");

Have you checked that the new file does not contain the new information? It may have been written out in a more compact form that than the original file because you used the "RDF/XML-ABBREV" form.
PS "xmlbase" should be a URI.

Jena is purely RDF/XML based API for accessing Ontology or Models. Make a Note of these things: What is the format when you save your OWL file after editing it with Protege? Is it OWL/XML or RDF/XML?? Jena being RDF centric can read OWL files written in RDF/XML format, but cannot read OWL file written in OWL/XML syntax (specifically the OWL2 syntax). Similarly it can write a Model/OntModel from memory to OWL file or an RDF file but ALWAYS in either "RDF/XML" syntax or "N3" or "RDF/XML-ABBREV".
And Since you are using "RDF/XML-ABBREV", which lists your triples in abbreviated format.. possibly that is the reason why your output file is decreasing in size.

Related

Generate files with one input to multiply outputs

I'm trying to create a code generator that takes input a JSON file and generates multiple classes in multiple files.
And my question is, is it possible to create multiple files for one input using build from dart lang?
Yes it is possible. There are currently many tools in available on pub.dev that have code generation. For creating a simple custom code generator, check out the package code_builder provided by the core Dart team.
You can use dart_style as well to format the output of the code_builder results.
Here is a simple example of the package in use (from the package's example):
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
final _dartfmt = DartFormatter();
// The string of the generated code for AnimalClass
String animalClass() {
final animal = Class((b) => b
..name = 'Animal'
..extend = refer('Organism')
..methods.add(Method.returnsVoid((b) => b
..name = 'eat'
..body = refer('print').call([literalString('Yum!')]).code)));
return _dartfmt.format('${animal.accept(DartEmitter())}');
}
In this example you can use the dart:io API to create a File and write the output from animalClass() (from the example) to the file:
final animalDart = File('animal.dart');
// write the new file to the disk
animalDart.createSync();
// write the contents of the class to the file
animalDart.writeAsStringSync(animalClass());
You can use the File API to read a .json from the path, then use jsonDecode on the contents of the file to access the contents of the JSON config.

Reading XML file using DXL(DOORS Scripting) and exporting to EXCEL dynamically

I need to read an XML file using DXL scripting and Exporting to EXCEl.
Is there any script in DXL already available?
Please help me out.
Thanks,
Sri
It can be done but there is no quick function built in to DXL to read XML, you have to build your own. Exporting to Excel is also possible by either going to Comma Separated Values or by using the OLE methods.
There is one library created by Mathias Mamtsch, using it you can create DOM from input string xml, then read,write attributes, values, iterate over attributes ,tags and so on.
http://sourceforge.net/projects/dxlstandardlib
In DXL world you have to write everything on you own.
It uses some helpers dxl files so to use it you have to use more than one additional dxl files.
For example one of the function there:
/*! \memberof XMLDocument
\return true - if the XML document could be loaded and parsed correctly. 'false' if there was an error loading the document.
\param xd The XMLDocument into which the contents shall be loaded.
\param s the contents of the XML document to load
\brief This function loads the XML content of a string into an XMLDocument object.
*/
bool loadXML (XMLDocument xd, string s) {
bool result = false
checkOLE ( oleMethod (getOleHandle_ xd, "LoadXML", oleArgs <- s, result) )
return result
}
These functions use OLE objects so I think they are Windows specific and under Linux the code will not work. But not sure.

How to load N-TRIPLE file in apache jena?

I am quite new to RDF and Jena. I want load a .nt (N- TRIPLE) file to a model. I have tried read(inputStream, "N-TRIPLE") but did not help.
It throws
org.apache.jena.riot.RiotException: Element or attribute do not match QName production: QName::=(NCName':')?NCName.
Can anyone point me out what is wrong?
Here is the link for the N-TRiple file which I tried to load : http://dbpedia.org/data/Berlin.ntriples
read(inputStream, string) uses the string argument as the base URI, not the syntax language. It's trying the default, which is RDF/XML. Check the javadoc for Model#read(InputStream in, String base) and Model#read(InputStream in, String base, String lang) for more information.
model.read(inputStream, null, "N-TRIPLES") ;
or
RDFDataMgr.read(model, inputStream, LANG.NTRIPLES) ;
If you are just opening the stream from a file (or URL) then Apache Jena will sort out the details. E.g.,
RDFDataMgr.read(model, "file:///myfile.nt") ;
There are various related operations. See the javadoc for Model and RDFDataMgr.

Open XML SDK: opening a Word template and saving to a different file-name

This one very simple thing I can't find the right technique. What I want is to open a .dotx template, make some changes and save as the same name but .docx extension. I can save a WordprocessingDocument but only to the place it's loaded from. I've tried manually constructing a new document using the WordprocessingDocument with changes made but nothing's worked so far, I tried MainDocumentPart.Document.WriteTo(XmlWriter.Create(targetPath)); and just got an empty file.
What's the right way here? Is a .dotx file special at all or just another document as far as the SDK is concerned - should i simply copy the template to the destination and then open that and make changes, and save? I did have some concerns if my app is called from two clients at once, if it can open the same .dotx file twice... in this case creating a copy would be sensible anyway... but for my own curiosity I still want to know how to do "Save As".
I would suggest just using File.IO to copy the dotx file to a docx file and make your changes there, if that works for your situation. There's also a ChangeDocumentType function you'll have to call to prevent an error in the new docx file.
File.Copy(#"\path\to\template.dotx", #"\path\to\template.docx");
using(WordprocessingDocument newdoc = WordprocessingDocument.Open(#"\path\to\template.docx", true))
{
newdoc.ChangeDocumentType(WordprocessingDocumentType.Document);
//manipulate document....
}
While M_R_H's answer is correct, there is a faster, less IO-intensive method:
Read the template or document into a MemoryStream.
Within a using statement:
open the template or document on the MemoryStream.
If you opened a template (.dotx) and you want to store it as a document (.docx), you must change the document type to WordprocessingDocumentType.Document. Otherwise, Word will complain when you try to open the document.
Manipulate your document.
Write the contents of the MemoryStream to a file.
For the first step, we can use the following method, which reads a file into a MemoryStream:
public static MemoryStream ReadAllBytesToMemoryStream(string path)
{
byte[] buffer = File.ReadAllBytes(path);
var destStream = new MemoryStream(buffer.Length);
destStream.Write(buffer, 0, buffer.Length);
destStream.Seek(0, SeekOrigin.Begin);
return destStream;
}
Then, we can use that in the following way (replicating as much of M_R_H's code as possible):
// Step #1 (note the using declaration)
using MemoryStream stream = ReadAllBytesToMemoryStream(#"\path\to\template.dotx");
// Step #2
using (WordprocessingDocument newdoc = WordprocessingDocument.Open(stream, true)
{
// You must do the following to turn a template into a document.
newdoc.ChangeDocumentType(WordprocessingDocumentType.Document);
// Manipulate document (completely in memory now) ...
}
// Step #3
File.WriteAllBytes(#"\path\to\template.docx", stream.GetBuffer());
See this post for a comparison of methods for cloning (or duplicating) Word documents or templates.

How to write a simple .txt content processor in XNA?

I don't really understand how Content importer/processor works in XNA.
I need to read a text file (Content/levels/level1.txt) of the form:
x x
x x
x x
where x's are just integers, into an int[,] array.
Any tips on writting a SIMPLE .txt importer??? By searching google/msdn I only found .x/.fbx file importer examples. And they seem too complicated.
Do you actually need to process the text file? If not, then you can probably skip most of the content pipeline.
Something like:
string filename = "Content/TextFiles/sometext.txt";
string path = Path.Combine(StorageContainer.TitleLocation, filename);
string lineOfText;
StreamReader sr = new StreamReader(path);
while ((lineOfText = sr.ReadLine()) != null)
{
// do something
}
Also, be sure to set the "Build Action" to "None" and the "Copy to Output Directory" to "Copy if newer" on the text files you've added. This tells the content pipeline not to compile the text file but rather copy it to the output directory for use as is.
I got this (more or less) from the RacingGame sample provided by Microsoft. It foregoes much of the content pipeline and simply loads and processes text files (XML) for much of its level data.
XNA 4.0 uses
System.IO.Stream stream = TitleContainer.OpenStream("tilename.txt");
See http://msdn.microsoft.com/en-us/library/bb199094.aspx and also http://blogs.msdn.com/b/shawnhar/archive/2010/12/09/reading-files-in-xna-game-studio-4-0.aspx
There doesn't seem to be a lot of info out there, but this blog post does indicate how you can load .txt files through code using XNA.
Hopefully this can help you get the file into memory, from there it should be straightforward to parse it in any way you like.
XNA 3.0 - Reading Text Files on the Xbox
http://www.ziggyware.com/readarticle.php?article_id=69 is probably a good place to start. It covers creating a basic content processor.

Resources