Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedUSDLExample.java
More file actions
Latest commit
292 lines (236 loc) · 13.2 KB
/
Copy pathLinkedUSDLExample.java
File metadata and controls
292 lines (236 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* Copyright (C) 2012 Jorge Cardoso. All rights reserved.
*
* This program and the accompanying materials are made available under
* the terms of the Common Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/cpl-v10.html
*
* $Id: LinkedUSDLExample.java,v 0.1 2012/11/11 20:02:36 jorge_cardoso Exp $
*/
importjava.io.*;
importjava.io.File;
importjava.io.FileInputStream;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importcom.hp.hpl.jena.ontology.OntClass;
importcom.hp.hpl.jena.ontology.OntModel;
importcom.hp.hpl.jena.query.Query;
importcom.hp.hpl.jena.query.QueryExecution;
importcom.hp.hpl.jena.query.QueryExecutionFactory;
importcom.hp.hpl.jena.query.QueryFactory;
importcom.hp.hpl.jena.query.QuerySolution;
importcom.hp.hpl.jena.rdf.model.*;
importcom.hp.hpl.jena.rdf.model.Model;
importcom.hp.hpl.jena.rdf.model.ModelFactory;
importcom.hp.hpl.jena.rdf.model.Property;
importcom.hp.hpl.jena.rdf.model.RDFNode;
importcom.hp.hpl.jena.rdf.model.Resource;
importcom.hp.hpl.jena.rdf.model.Statement;
importcom.hp.hpl.jena.rdf.model.StmtIterator;
importcom.hp.hpl.jena.util.FileManager;
importcom.hp.hpl.jena.reasoner.*;
importcom.hp.hpl.jena.vocabulary.*;
importcom.hp.hpl.jena.datatypes.xsd.XSDDatatype;
publicclassLinkedUSDLExample {
publicfinalstaticStringUSDL = "http://www.linked-usdl.org/ns/usdl#";
publicfinalstaticStringRDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
publicfinalstaticStringOWL = "http://www.w3.org/2002/07/owl#";
publicfinalstaticStringDC = "http://purl.org/dc/elements/1.1/";
publicfinalstaticStringXSD = "http://www.w3.org/2001/XMLSchema#";
publicfinalstaticStringVANN = "http://purl.org/vocab/vann/";
publicfinalstaticStringFOAF = "http://xmlns.com/foaf/0.1/";
publicfinalstaticStringUSDK = "http://www.linked-usdl.org/ns/usdl#";
publicfinalstaticStringRDFS = "http://www.w3.org/2000/01/rdf-schema#";
publicfinalstaticStringGR = "http://purl.org/goodrelations/v1#";
publicfinalstaticStringSKOS = "http://www.w3.org/2004/02/skos/core#";
publicfinalstaticStringORG = "http://www.w3.org/ns/org#";
publicfinalstaticStringPRICE= "http://www.linked-usdl.org/ns/usdl-price#";
publicfinalstaticStringLEGAL= "http://www.linked-usdl.org/ns/usdl-legal#";
publicfinalstaticStringDEI = "http://dei.uc.pt/rdf/dei#";
publicfinalstaticStringUSDL_Core_Schema_File = "./usdl-core.ttl";
publicfinalstaticStringUSDL_Price_Schema_File = "./usdl-price.ttl";
publicfinalstaticStringUSDL_Instance_File = "./Service_01.ttl";
publicstaticvoidmain (Stringargs[]) {
LinkedUSDLExampleexample = newLinkedUSDLExample();
// create three empty models. One to store usdl-core, one to store usdl-price, and
// one to store the actual service instance we will be creating
ModelusdlCoreSchemaModel = ModelFactory.createDefaultModel();
ModelusdlPriceSchemaModel = ModelFactory.createDefaultModel();
ModelusdlInstanceModel = ModelFactory.createDefaultModel();
// Create a usdl instance just to demonstrate how to model a service
// Here only usdl-core is used. Not all properties and classes were used
usdlInstanceModel = example.populateUSDLmodel(usdlInstanceModel);
// Add pricing information to the service instance created previously
usdlInstanceModel = example.addUSDLPricing(usdlInstanceModel);
// Add legal information to the service instance created previously
// The code is missing for this method (comming soon)
usdlInstanceModel = example.addUSDLLegal(usdlInstanceModel);
// Display the usdl service instance just created
example.displayUSDLmodel(usdlInstanceModel, "TTL");
// Display the usdl service instance again by showing its triples
example.dumpUSDL(usdlInstanceModel);
// Write it to a file
example.writeUSDLmodeltoFile(usdlInstanceModel, USDL_Instance_File, "TTL");
// Load the usdl model. It should be available at linked-usdl.org
// In this example, we use a local copy stored in disk
usdlCoreSchemaModel = example.loadUSDLmodel(usdlCoreSchemaModel, USDL_Core_Schema_File, "TTL");
// Check if the usdl instance we created complies with the model
// Not sure if this function works well
booleanvalid = example.validateUSDLInstance(usdlCoreSchemaModel, usdlInstanceModel);
if(valid) System.out.print("The usdl instance created is valid");
elseSystem.out.print("The usdl instance created is NOT valid!");
// This function was called only once to make the file containing the price schema well formatted
// example.prettyFormating(USDL_Price_Schema_File, "TTL");
}
publicModelloadUSDLmodel(Modelmodel, StringfilePath, Stringlang) {
InputStreamin = FileManager.get().open( filePath );
if (in == null) {
thrownewIllegalArgumentException( "File: " + filePath + " not found");
}
model.read(in, USDL, lang);
returnmodel;
}
publicvoiddisplayUSDLmodel(Modelmodel, Stringlang) {
// write it to standard out
model.write(System.out, lang, USDL);
}
publicbooleanvalidateUSDLInstance(Modelschema, Modelmodel) {
InfModelinfmodel = ModelFactory.createRDFSModel(schema, model);
ValidityReportvalidity = infmodel.validate();
returnvalidity.isValid();
}
publicModelpopulateUSDLmodel(Modelmodel) {
Resourceservice
= model.createResource(DEI+"ServiceDesk").
addProperty(model.createProperty(RDF+"type"), model.createResource(USDL+"Service")).
addProperty(model.createProperty(DC+"title"), model.createLiteral("SugarCRM service instance", "en")).
addProperty(model.createProperty(DC+"created"), ResourceFactory.createTypedLiteral("2012-11-11", XSDDatatype.XSDdate));
ResourcebusinessEntity
= model.createResource(DEI+"Service_Desk_Inc").
addProperty(model.createProperty(RDF+"type"), model.createResource(GR+"BusinessEntity")).
addProperty(model.createProperty(GR+"legalName"), model.createLiteral("Service Desk Inc.", "en")).
addProperty(model.createProperty(FOAF+"page"), model.createLiteral("<http://www.servicedesk.com/>")).
addProperty(model.createProperty(GR+"taxID"), ResourceFactory.createTypedLiteral(newInteger(345543234)));
Resourceparticipant
= model.createResource(DEI+"IT_Engineer").
addProperty(model.createProperty(RDF+"type"), model.createResource(USDL+"Participant")).
addProperty(model.createProperty(USDL+"hasAgent"), businessEntity).
addProperty(model.createProperty(USDL+"hasRole"), model.createResource(USDL+"Provider"));
ResourceinteractionPoint_Register
= model.createResource(DEI+"InteractionPoint_Register").
addProperty(model.createProperty(RDF+"type"), model.createResource(USDL+"InteractionPoint")).
addProperty(model.createProperty(USDL+"receives"), model.createResource(DEI+"Form_850")).
addProperty(model.createProperty(USDL+"yields"), model.createResource(DEI+"Certificate")).
addProperty(model.createProperty(USDL+"hasInteractionSpace"), model.createLiteral("Remote", "en")).
addProperty(model.createProperty(USDL+"hasInteractionType"), model.createLiteral("Human-Machine", "en"));
ResourceinteractionPoint_Download
= model.createResource(DEI+"InteractionPoint_Download").
addProperty(model.createProperty(RDF+"type"), model.createResource(USDL+"InteractionPoint")).
addProperty(model.createProperty(USDL+"receives"), model.createResource(DEI+"Password")).
addProperty(model.createProperty(USDL+"yields"), model.createResource(DEI+"Access")).
addProperty(model.createProperty(USDL+"hasInteractionSpace"), model.createLiteral("Remote", "en")).
addProperty(model.createProperty(USDL+"hasInteractionType"), model.createLiteral("Human-Machine", "en")).
addProperty(model.createProperty(USDL+"hasParticipant"), participant);
service.addProperty(model.createProperty(USDL+"hasInteractionPoint"), interactionPoint_Register);
service.addProperty(model.createProperty(USDL+"hasInteractionPoint"), interactionPoint_Download);
ResourceserviceOffer
= model.createResource(DEI+"ServiceDesk_Offering").
addProperty(model.createProperty(RDF+"type"), model.createResource(USDL+"ServiceOffering")).
addProperty(model.createProperty(USDL+"includes"), service);
returnmodel;
}
publicModeladdUSDLPricing(Modelmodel) {
ResourcequantitativeValue_MB
= model.createResource(GR+"quantitativeValue_MB").
addProperty(model.createProperty(RDF+"type"), model.createResource(GR+"QuantitativeValue")).
addProperty(model.createProperty(GR+"hasUnitOfMeasurement"), model.createTypedLiteral("MB", XSDDatatype.XSDstring)).
addProperty(model.createProperty(GR+"hasValue"), model.createTypedLiteral("1", XSDDatatype.XSDint));
ResourceunitPriceSpecification_MB
= model.createResource(GR+"UnitPriceSpecification_MB").
addProperty(model.createProperty(RDF+"type"), model.createResource(GR+"QuantitativeValue")).
addProperty(model.createProperty(GR+"hasCurrency"), model.createTypedLiteral("USD", XSDDatatype.XSDstring)).
addProperty(model.createProperty(GR+"hasCurrencyValue"), model.createTypedLiteral("9.90", XSDDatatype.XSDfloat)).
addProperty(model.createProperty(GR+"validThrough"), model.createTypedLiteral("2012-12-31T23:59:59Z", XSDDatatype.XSDdate)).
addProperty(model.createProperty(GR+"hasUnitOfMeasurement"), model.createTypedLiteral("MB", XSDDatatype.XSDstring));
ResourcepriceComponent_Setup
= model.createResource(PRICE+"priceComponent_Setup").
addProperty(model.createProperty(RDF+"type"), model.createResource(PRICE+"PriceComponent")).
addProperty(model.createProperty(PRICE+"hasMetrics"), quantitativeValue_MB).
addProperty(model.createProperty(PRICE+"hasPrice"), unitPriceSpecification_MB);
ResourcepriceComponent_MonthlyFee
= model.createResource(PRICE+"priceComponent_MonthlyFee").
addProperty(model.createProperty(RDF+"type"), model.createResource(PRICE+"PriceComponent")).
addProperty(model.createProperty(PRICE+"hasMetrics"), quantitativeValue_MB).
addProperty(model.createProperty(PRICE+"hasPrice"), unitPriceSpecification_MB);
ResourcepricePlan
= model.createResource(PRICE+"PricePlan_ServiceDesk").
addProperty(model.createProperty(RDF+"type"), model.createResource(PRICE+"PricePlan")).
addProperty(model.createProperty(PRICE+"hasPriceComponent"), priceComponent_Setup).
addProperty(model.createProperty(PRICE+"hasPriceComponent"), priceComponent_MonthlyFee);
returnmodel;
}
publicModeladdUSDLLegal(Modelmodel) {
returnmodel;
}
privatevoidwriteUSDLmodeltoFile(Modelm, StringfilePath, Stringlang) {
m.setNsPrefix("usdl", USDL);
m.setNsPrefix("rdf", RDF);
m.setNsPrefix("owl", OWL);
m.setNsPrefix("dc", DC );
m.setNsPrefix("xsd", XSD);
m.setNsPrefix("vann", VANN);
m.setNsPrefix("foaf", FOAF);
m.setNsPrefix("rdfs", RDFS);
m.setNsPrefix("gr", GR );
m.setNsPrefix("skos", SKOS);
m.setNsPrefix("org", ORG );
m.setNsPrefix("price", PRICE );
m.setNsPrefix("legal", LEGAL );
m.setNsPrefix("dei", DEI );
try {
FileoutputFile = newFile(filePath);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
FileOutputStreamout = newFileOutputStream(outputFile);
m.write(out, lang);
out.close();
}
catch (IOExceptione) { System.out.println(e.toString()); }
}
publicvoiddumpUSDL(Modelmodel) {
// list the statements in the graph
StmtIteratoriter = model.listStatements();
// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
Statementstmt = iter.nextStatement(); // get next statement
Resourcesubject = stmt.getSubject(); // get the subject
Propertypredicate = stmt.getPredicate(); // get the predicate
RDFNodeobject = stmt.getObject(); // get the object
System.out.print(subject.toString());
System.out.print(" " + predicate.toString() + " ");
if (objectinstanceofResource) {
System.out.print(object.toString());
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");
}
System.out.println(" .");
}
}
publicModelprettyFormating(StringfilePath, Stringlang) {
Modelmodel = ModelFactory.createDefaultModel();
InputStreamin = FileManager.get().open( filePath );
if (in == null) {
thrownewIllegalArgumentException( "File: " + filePath + " not found");
}
model.read(in, USDL, lang);
try {
OutputStreamout = newFileOutputStream(filePath+"9");
model.write(out, "TTL");
out.close();
}
catch (IOExceptione) { System.out.println(e.toString()); }
returnmodel;
}
}