Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
VIPSCore
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIPS
VIPSCore
Commits
4d1c2799
Commit
4d1c2799
authored
11 years ago
by
Tor-Einar Skog
Browse files
Options
Downloads
Patches
Plain Diff
Added sample config, and a bit of cleanup
parent
286155ec
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/no/bioforsk/vips/core/service/ModelResource.java
+130
-48
130 additions, 48 deletions
...ain/java/no/bioforsk/vips/core/service/ModelResource.java
with
130 additions
and
48 deletions
src/main/java/no/bioforsk/vips/core/service/ModelResource.java
+
130
−
48
View file @
4d1c2799
package
no.bioforsk.vips.core.service
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
...
...
@@ -11,9 +10,6 @@ import javax.ws.rs.PathParam;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.Response
;
import
no.bioforsk.vips.entity.ModelConfiguration
;
import
no.bioforsk.vips.entity.Result
;
import
no.bioforsk.vips.entity.ResultImpl
;
import
no.bioforsk.vips.entity.Weather
;
import
no.bioforsk.vips.model.Model
;
import
no.bioforsk.vips.model.factory.ModelFactory
;
...
...
@@ -26,19 +22,106 @@ import no.bioforsk.vips.model.factory.ModelFactory;
public
class
ModelResource
{
/**
* Lists all models available in this instance
* @return
* Lists all models available in this instance
, using default language (English)
* @return
list of all models available
*/
@GET
@Path
(
"models"
)
@Produces
(
"text/plain;charset=UTF-8"
)
public
Response
printModels
()
public
Response
printModelList
()
{
return
this
.
printModelList
(
null
);
/*
StringBuilder retVal = new StringBuilder();
ModelFactory mF = ModelFactory.getInstance();
for(String key : mF.getModelList().keySet())
{
try {
retVal.append(key).append(" ").append(mF.getModelInstance(key).getModelName()).append("\n");
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(ModelResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
return Response.ok().entity(retVal.toString()).build();*/
}
/**
* Lists all models available in this instance, using default language (English)
* @return list of all models available
*/
@GET
@Path
(
"models/json"
)
@Produces
(
"application/json;charset=UTF-8"
)
public
Response
printModelListJSON
()
{
/*
StringBuilder retVal = new StringBuilder();
ModelFactory mF = ModelFactory.getInstance();
for(String key : mF.getModelList().keySet())
{
try {
retVal.append(key).append(" ").append(mF.getModelInstance(key).getModelName()).append("\n");
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(ModelResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
return Response.ok().entity(retVal.toString()).build();
* */
return
this
.
printModelListJSON
(
null
);
}
/**
* Lists all models available in this instance, using default language (English)
* @return list of all models available
*/
@GET
@Path
(
"models/json/{language}"
)
@Produces
(
"application/json;charset=UTF-8"
)
public
Response
printModelListJSON
(
@PathParam
(
"language"
)
String
language
)
{
StringBuilder
retVal
=
new
StringBuilder
();
ModelFactory
mF
=
ModelFactory
.
getInstance
();
retVal
.
append
(
"["
);
boolean
first
=
true
;
for
(
String
key
:
mF
.
getModelList
().
keySet
())
{
try
{
if
(
first
)
first
=
false
;
else
retVal
.
append
(
","
);
retVal
.
append
(
"{"
);
retVal
.
append
(
"\"modelId\":\""
).
append
(
key
).
append
(
"\", \"modelName\":\""
).
append
(
mF
.
getModelInstance
(
key
).
getModelName
(
language
)).
append
(
"\""
);
retVal
.
append
(
"}"
);
}
catch
(
InstantiationException
|
IllegalAccessException
ex
)
{
Logger
.
getLogger
(
ModelResource
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
retVal
.
append
(
"]"
);
return
Response
.
ok
().
entity
(
retVal
.
toString
()).
build
();
}
/**
* Lists all models available in this instance, using default language (English)
* @return list of all models available
*/
@GET
@Path
(
"models/{language}"
)
@Produces
(
"text/plain;charset=UTF-8"
)
public
Response
printModelList
(
@PathParam
(
"language"
)
String
language
)
{
StringBuilder
retVal
=
new
StringBuilder
();
ModelFactory
mF
=
ModelFactory
.
getInstance
();
for
(
String
key
:
mF
.
getModelList
().
keySet
())
{
retVal
.
append
(
key
).
append
(
"\n"
);
try
{
retVal
.
append
(
key
).
append
(
" "
).
append
(
mF
.
getModelInstance
(
key
).
getModelName
(
language
)).
append
(
"\n"
);
}
catch
(
InstantiationException
|
IllegalAccessException
ex
)
{
Logger
.
getLogger
(
ModelResource
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
return
Response
.
ok
().
entity
(
retVal
.
toString
()).
build
();
}
...
...
@@ -72,66 +155,65 @@ public class ModelResource {
return
this
.
printModelUsage
(
modelId
,
null
);
}
/** Tests for XML and JSON output */
@GET
@Path
(
"models/resulttest"
)
@Produces
(
"application/json"
)
public
Response
printResultTest
()
{
List
<
Result
>
allResults
=
new
ArrayList
();
Result
retVal
=
new
ResultImpl
();
retVal
.
setResultValidTime
(
new
Date
());
retVal
.
setValue
(
"Parameter1"
,
"Verdi1"
);
retVal
.
setValue
(
"Parameter2"
,
"Verdi2"
);
allResults
.
add
(
retVal
);
retVal
=
new
ResultImpl
();
retVal
.
setResultValidTime
(
new
Date
());
retVal
.
setValue
(
"Parameter1"
,
"Verdi5"
);
retVal
.
setValue
(
"Parameter2"
,
"Verdi7"
);
allResults
.
add
(
retVal
);
return
Response
.
ok
().
entity
(
allResults
).
build
();
}
@GET
@Path
(
"
weather/xml
"
)
@Produces
(
"
application/xml
"
)
public
Response
print
WeatherXML
(
)
@Path
(
"
models/{modelId}/sampleconfig
"
)
@Produces
(
"
text/plain;charset=UTF-8
"
)
public
Response
print
ModelSampleConfig
(
@PathParam
(
"modelId"
)
String
modelId
)
{
return
Response
.
ok
().
entity
(
new
Weather
(
new
Date
(),
"Goddag, goddag. Dette her er Meteorologisk institutt.æøåæøå"
)).
build
();
try
{
String
sampleConfig
=
ModelFactory
.
getInstance
().
getModelInstance
(
modelId
).
getSampleConfig
();
return
Response
.
ok
().
entity
(
sampleConfig
).
build
();
}
catch
(
InstantiationException
|
IllegalAccessException
ex
)
{
Logger
.
getLogger
(
ModelResource
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
return
Response
.
serverError
().
build
();
}
}
/**
* Marshalling fra POJO til JSON gjøres automagisk (med Jackson, tror jeg)
* @return gladmelding
* Instantiates and runs the requested model
* @param config input data for the model
* @return list of result objects
*/
@GET
@Path
(
"weather/json"
)
@POST
@Path
(
"models/run"
)
@Consumes
(
"application/json"
)
@Produces
(
"application/json"
)
public
Response
printWeatherJSON
(
)
public
Response
runModel
(
ModelConfiguration
config
)
{
return
Response
.
ok
().
entity
(
new
Weather
(
new
Date
(),
"Goddag, goddag. Dette her er Meteorologisk institutt.æøåæøå"
)).
build
();
try
{
Model
calledModel
=
ModelFactory
.
getInstance
().
getModelInstance
(
config
.
getModelId
());
calledModel
.
setConfiguration
(
config
);
return
Response
.
ok
().
entity
(
calledModel
.
getResult
()).
build
();
}
catch
(
InstantiationException
|
IllegalAccessException
ex
)
{
Logger
.
getLogger
(
ModelResource
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
return
Response
.
serverError
().
build
();
}
}
/**
* Test for input / POST
* Instantiates and runs the requested model
* @param modelId
* @param config input data for the model
* @return list of result objects
*/
@POST
@Path
(
"models/run"
)
@Path
(
"models/
{modelId}/
run"
)
@Consumes
(
"application/json"
)
@Produces
(
"application/json"
)
//@Produces("text/plain;charset=UTF-8")
public
Response
runModel
(
ModelConfiguration
config
)
public
Response
runModel
(
@PathParam
(
"modelId"
)
String
modelId
,
ModelConfiguration
config
)
{
//return Response.ok().entity("runModell called").build();
try
{
Model
calledModel
=
ModelFactory
.
getInstance
().
getModelInstance
(
config
.
getM
odelId
()
);
Model
calledModel
=
ModelFactory
.
getInstance
().
getModelInstance
(
m
odelId
);
calledModel
.
setConfiguration
(
config
);
return
Response
.
ok
().
entity
(
calledModel
.
getResult
()).
build
();
}
catch
(
InstantiationException
|
IllegalAccessException
ia
e
)
catch
(
InstantiationException
|
IllegalAccessException
e
x
)
{
Logger
.
getLogger
(
ModelResource
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
return
Response
.
serverError
().
build
();
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment