owlapi how to add a OWLDatatype to OWLClass - owl-api

So I have been able to add some classes to an ontology and save them to a file. Now I would like to be able to add a Datatype to my class but I am confused about how to do this probably very simple thing. This is what I have been trying:
OWLClass currentClass =df.getOWLClass(IRI.create("Base"));
OWLDataProperty owlAttr = df.getOWLDataProperty(IRI.create("#" + "name");
OWLLiteralImplString lit = new OWLLiteralImplString("test"); //This is probably on the wrong path
DefaultPrefixManager defaultPrefixManager = new DefaultPrefixManager();
OWLDatatype datatype = df.getOWLDatatype("xsd:string",defaultPrefixManager);
OWLAxiom axiom = df.getOWLDatatypeDefinitionAxiom(datatype, ?); //having trouble find a range.
Edit #1 So I'm a little concerned my question isn't clear. What I am trying to do would be similar to this in Java:
public class Car{
}
I currently am able to create a class using the owlapi but what I am looking to do would be like adding a datamember to my Java class:
public class Car{
public String manufacturer;
}
Using Protege I can produce this which I think is what I want to be able to make with the owlapi:
<!-- http://www.co-ode.org/ontologies/ont.owl#manufacturer -->
<DatatypeProperty rdf:about="http://www.co-ode.org/ontologies/ont.owl#manufacturer">
<rdfs:domain rdf:resource="http://www.co-ode.org/ontologies/ont.owl#Car"/>
<rdfs:range rdf:resource="&xsd;string"/>
</DatatypeProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.co-ode.org/ontologies/ont.owl#Car -->
<Class rdf:about="http://www.co-ode.org/ontologies/ont.owl#Car"/>

Something like this might help:
OWLDatatype datatype = factory.getOWLDatatype("xsd:string",pm);
OWLLiteral lit= factory.getOWLLiteral("1", datatype);
Maybe you want to define min and max restriction:
OWLDataUnionOf union = factory.getOWLDataUnionOf( factory.getOWLDatatypeMinInclusiveRestriction(1), factory.getOWLDatatypeMaxInclusiveRestriction(10));
OWLDatatypeDefinitionAxiom axiom = factory.getOWLDatatypeDefinitionAxiom(datatype, union);
Edit #1: I have added new code based on the edited question.
PrefixManager pm= new DefaultPrefixManager("http://www.co-ode.org/ontologies/ont.owl#");
OWLDataPropertyExpression man= factory.getOWLDataProperty("manufacturer", pm);
OWLClass car= factory.getOWLClass("Car", pm);
OWLDatatype dt = factory.getOWLDatatype("xsd:string",pm);
OWLDataPropertyDomainAxiom domain=factory.getOWLDataPropertyDomainAxiom(man, car);
OWLDataPropertyRangeAxiom range= factory.getOWLDataPropertyRangeAxiom(man, dt);

Related

Serializing PropertyBusinessObject to XML

I'm trying to upgrade the code quality of a part of my app which can display data within dynamic Form that are configured via an XML string translated into an object. Until now, I was doing parsing and serializing with custom methods only, nearly writing each line of XML myself. That's obviously the wrong approach and I fully understand it now that I'm very familiar with Codename One.
So I rewrote my objects to use the PropertyBusinessObject interface and here are the resulting classes :
public class CForm implements PropertyBusinessObject
{
Property<String, CForm> type = new Property<>("type");
Property<String, CForm> label = new Property<>("label");
IntProperty<CForm> currentStep = new IntProperty<>("currentstep");
IntProperty<CForm> maxsteps = new IntProperty<>("maxsteps");
ListProperty<CFormField, CForm> fields = new ListProperty<>("fields", CFormField.class);
ListProperty<CFormStep, CForm> steps = new ListProperty<>("steps", CFormStep.class);
public CForm()
{
}
PropertyIndex index;
#Override
public PropertyIndex getPropertyIndex()
{
if(index == null)
index = new PropertyIndex(this, "CForm",
new PropertyBase[] {type, label, currentStep, maxsteps, fields, steps});
return index;
}
}
This class is the main one, describing the general structure of the process. Type and label are used internally to identify the model used and the job to do at the customer place. Fields are the dynamic fields that will be shown at each step, these are global fields, like remarks or so. The steps are the differents dynamic Forms that will be shown to the user. maxSteps and currentStep are used to know where the user is in the process.
public class CFormStep implements PropertyBusinessObject
{
Property<String, CFormStep> name = new Property<String, CFormStep>("name");
Property<String, CFormStep> label = new Property<String, CFormStep>("label");
IntProperty<CFormStep> value = new IntProperty<CFormStep>("value");
ListProperty<CFormField, CFormStep> fields = new ListProperty<>("fields", CFormField.class);
public CFormStep() {}
PropertyIndex index;
#Override
public PropertyIndex getPropertyIndex()
{
if(index == null)
index = new PropertyIndex(this, "CFormStep",
new PropertyBase[] { label, name, value, fields});
return index;
}
}
Each step is described with an unique name, a label used as the title of the dynamic form and a value to order the steps within the CForm. Like it's CForm parent, it has a set of fields.
public class CFormField implements PropertyBusinessObject
{
Property<String, CFormField> label = new Property<String, CFormField>("label");
Property<String, CFormField> name = new Property<String, CFormField>("name");
Property<String, CFormField> type = new Property<String, CFormField>("type");
Property<String, CFormField> value = new Property<String, CFormField>("value");
Property<String, CFormField> parent = new Property<String, CFormField>("parent");
public CFormField() {}
PropertyIndex index;
#Override
public PropertyIndex getPropertyIndex()
{
if(index == null)
index = new PropertyIndex(this, "CFormField",
new Property[] { label, name, type, value, parent });
return index;
}
}
The fields are composed of a label, an unique name, a type used to define which component will be used to render it, the value selected by the user formatted as a String and finally it's optionnal parent name.
The old implementation was fonctionnal but laggy and hard to evolve, which is why I came to write this new one.
But I'm stuck with one problem : I already rewroted the PropertyIndex.fromXML(Element e) since it was creating NPE's when calling
public void fromXml(Element e) {
Hashtable atts = e.getAttributes();
for(Object a : atts.keySet()) { <--- NPE there if no attributes
and was not supporting ListProperties.
Reading my CForm from XML works fine but when I try to serialize it back to XML I just got a <CForm><CForm/> and don't fully understand how the PropertyXMLElement works... I've already debugged the process and the PropertyBusinessObject is not empty at all. Any idea on how to fill the XML with those values ?
Here is an example of XML used :
<CForm>
<type>delivery-install</type>
<label>Intervention Test</label>
<currentstep/>
<maxsteps>3</maxsteps>
<fields>
<CFormField>
<label>Remarques</label>
<name>globalTEXTAREA6</name>
<type>TEXTAREA</type>
<value/>
</CFormField>
</fields>
<steps>
<CFormStep>
<name>step1</name>
<label>Arrivée sur place</label>
<value>1</value>
<fields>
<CFormField>
<label>Heure d'arrivée</label>
<name>step1TIME3</name>
<type>TIME</type>
<value/>
</CFormField>
</fields>
</CFormStep>
<CFormStep>
<name>step2</name>
<label>Sur place</label>
<value>2</value>
<fields>
<CFormField>
<label>Produits livrés / utilisés</label>
<name>step2PRODUCTTABLE7</name>
<type>PRODUCTTABLE</type>
<value/>
</CFormField>
</fields>
</CFormStep>
<CFormStep>
<name>step3</name>
<label>Départ</label>
<value>3</value>
<fields>
<CFormField>
<label>Heure de départ</label>
<name>step3TIME4</name>
<type>TIME</type>
<value/>
</CFormField>
<CFormField>
<label>Signature client</label>
<name>step3SIGN5</name>
<type>SIGN</type>
<value/>
</CFormField>
</fields>
</CFormStep>
</steps>
</CForm>
The XML support in property business object is still a work in progress which is why I didn't blog about it. I fixed the NPE issue and added support for list properties to fix the two issues raised. Notice I only did this for reading so there might be issues writing these objects as we didn't have time to do QA on this feature.

Why Spring is turning my object into an array of attributes? [duplicate]

I'm developing a Spring Boot application with Spring Data JPA. I'm using a custom JPQL query to group by some field and get the count. Following is my repository method.
#Query(value = "select count(v) as cnt, v.answer from Survey v group by v.answer")
public List<?> findSurveyCount();
It's working and result is obtained as follows:
[
[1, "a1"],
[2, "a2"]
]
I would like to get something like this:
[
{ "cnt":1, "answer":"a1" },
{ "cnt":2, "answer":"a2" }
]
How can I achieve this?
Solution for JPQL queries
This is supported for JPQL queries within the JPA specification.
Step 1: Declare a simple bean class
package com.path.to;
public class SurveyAnswerStatistics {
private String answer;
private Long cnt;
public SurveyAnswerStatistics(String answer, Long cnt) {
this.answer = answer;
this.count = cnt;
}
}
Step 2: Return bean instances from the repository method
public interface SurveyRepository extends CrudRepository<Survey, Long> {
#Query("SELECT " +
" new com.path.to.SurveyAnswerStatistics(v.answer, COUNT(v)) " +
"FROM " +
" Survey v " +
"GROUP BY " +
" v.answer")
List<SurveyAnswerStatistics> findSurveyCount();
}
Important notes
Make sure to provide the fully-qualified path to the bean class, including the package name. For example, if the bean class is called MyBean and it is in package com.path.to, the fully-qualified path to the bean will be com.path.to.MyBean. Simply providing MyBean will not work (unless the bean class is in the default package).
Make sure to call the bean class constructor using the new keyword. SELECT new com.path.to.MyBean(...) will work, whereas SELECT com.path.to.MyBean(...) will not.
Make sure to pass attributes in exactly the same order as that expected in the bean constructor. Attempting to pass attributes in a different order will lead to an exception.
Make sure the query is a valid JPA query, that is, it is not a native query. #Query("SELECT ..."), or #Query(value = "SELECT ..."), or #Query(value = "SELECT ...", nativeQuery = false) will work, whereas #Query(value = "SELECT ...", nativeQuery = true) will not work. This is because native queries are passed without modifications to the JPA provider, and are executed against the underlying RDBMS as such. Since new and com.path.to.MyBean are not valid SQL keywords, the RDBMS then throws an exception.
Solution for native queries
As noted above, the new ... syntax is a JPA-supported mechanism and works with all JPA providers. However, if the query itself is not a JPA query, that is, it is a native query, the new ... syntax will not work as the query is passed on directly to the underlying RDBMS, which does not understand the new keyword since it is not part of the SQL standard.
In situations like these, bean classes need to be replaced with Spring Data Projection interfaces.
Step 1: Declare a projection interface
package com.path.to;
public interface SurveyAnswerStatistics {
String getAnswer();
int getCnt();
}
Step 2: Return projected properties from the query
public interface SurveyRepository extends CrudRepository<Survey, Long> {
#Query(nativeQuery = true, value =
"SELECT " +
" v.answer AS answer, COUNT(v) AS cnt " +
"FROM " +
" Survey v " +
"GROUP BY " +
" v.answer")
List<SurveyAnswerStatistics> findSurveyCount();
}
Use the SQL AS keyword to map result fields to projection properties for unambiguous mapping.
This SQL query return List< Object[] > would.
You can do it this way:
#RestController
#RequestMapping("/survey")
public class SurveyController {
#Autowired
private SurveyRepository surveyRepository;
#RequestMapping(value = "/find", method = RequestMethod.GET)
public Map<Long,String> findSurvey(){
List<Object[]> result = surveyRepository.findSurveyCount();
Map<Long,String> map = null;
if(result != null && !result.isEmpty()){
map = new HashMap<Long,String>();
for (Object[] object : result) {
map.put(((Long)object[0]),object[1]);
}
}
return map;
}
}
I know this is an old question and it has already been answered, but here's another approach:
#Query("select new map(count(v) as cnt, v.answer) from Survey v group by v.answer")
public List<?> findSurveyCount();
define a custom pojo class say sureveyQueryAnalytics and store the query returned value in your custom pojo class
#Query(value = "select new com.xxx.xxx.class.SureveyQueryAnalytics(s.answer, count(sv)) from Survey s group by s.answer")
List<SureveyQueryAnalytics> calculateSurveyCount();
I do not like java type names in query strings and handle it with a specific constructor.
Spring JPA implicitly calls constructor with query result in HashMap parameter:
#Getter
public class SurveyAnswerStatistics {
public static final String PROP_ANSWER = "answer";
public static final String PROP_CNT = "cnt";
private String answer;
private Long cnt;
public SurveyAnswerStatistics(HashMap<String, Object> values) {
this.answer = (String) values.get(PROP_ANSWER);
this.count = (Long) values.get(PROP_CNT);
}
}
#Query("SELECT v.answer as "+PROP_ANSWER+", count(v) as "+PROP_CNT+" FROM Survey v GROUP BY v.answer")
List<SurveyAnswerStatistics> findSurveyCount();
Code needs Lombok for resolving #Getter
#Repository
public interface ExpenseRepo extends JpaRepository<Expense,Long> {
List<Expense> findByCategoryId(Long categoryId);
#Query(value = "select category.name,SUM(expense.amount) from expense JOIN category ON expense.category_id=category.id GROUP BY expense.category_id",nativeQuery = true)
List<?> getAmountByCategory();
}
The above code worked for me.
I used custom DTO (interface) to map a native query to - the most flexible approach and refactoring-safe.
The problem I had with this - that surprisingly, the order of fields in the interface and the columns in the query matters. I got it working by ordering interface getters alphabetically and then ordering the columns in the query the same way.
I just solved this problem :
Class-based Projections doesn't work with query native(#Query(value = "SELECT ...", nativeQuery = true)) so I recommend to define custom DTO using interface .
Before using DTO should verify the query syntatically correct or not
Get data with column name and its values (in key-value pair) using JDBC:
/*Template class with a basic set of JDBC operations, allowing the use
of named parameters rather than traditional '?' placeholders.
This class delegates to a wrapped {#link #getJdbcOperations() JdbcTemplate}
once the substitution from named parameters to JDBC style '?' placeholders is
done at execution time. It also allows for expanding a {#link java.util.List}
of values to the appropriate number of placeholders.
The underlying {#link org.springframework.jdbc.core.JdbcTemplate} is
exposed to allow for convenient access to the traditional
{#link org.springframework.jdbc.core.JdbcTemplate} methods.*/
#Autowired
protected NamedParameterJdbcTemplate jdbc;
#GetMapping("/showDataUsingQuery/{Query}")
public List<Map<String,Object>> ShowColumNameAndValue(#PathVariable("Query")String Query) throws SQLException {
/* MapSqlParameterSource class is intended for passing in a simple Map of parameter values
to the methods of the {#link NamedParameterJdbcTemplate} class*/
MapSqlParameterSource msp = new MapSqlParameterSource();
// this query used for show column name and columnvalues....
List<Map<String,Object>> css = jdbc.queryForList(Query,msp);
return css;
}
//in Service
`
public List<DevicesPerCustomer> findDevicesPerCustomer() {
LOGGER.info(TAG_NAME + " :: inside findDevicesPerCustomer : ");
List<Object[]> list = iDeviceRegistrationRepo.findDevicesPerCustomer();
List<DevicesPerCustomer> out = new ArrayList<>();
if (list != null && !list.isEmpty()) {
DevicesPerCustomer mDevicesPerCustomer = null;
for (Object[] object : list) {
mDevicesPerCustomer = new DevicesPerCustomer();
mDevicesPerCustomer.setCustomerId(object[0].toString());
mDevicesPerCustomer.setCount(Integer.parseInt(object[1].toString()));
out.add(mDevicesPerCustomer);
}
}
return out;
}`
//In Repo
` #Query(value = "SELECT d.customerId,count(*) FROM senseer.DEVICE_REGISTRATION d where d.customerId is not null group by d.customerId", nativeQuery=true)
List<Object[]> findDevicesPerCustomer();`

Request server using Axis2 RPC way, parameter order in xml packets not correct

For example, I will send an object Fruits to server side.
The code like this:
public static <T> T call(String url, String ns, String method, Fruits fruits, Class<T> clz) throws AxisFault
{
RPCServiceClient client = new RPCServiceClient();
Options option = client.getOptions();
EndpointReference erf = new EndpointReference(url);
option.setTo(erf);
QName name = new QName(ns, method);
Object[] object = new Object[]{fruits};
Class[] returnTypes = new Class[]{clz};
Object[] reto = client.invokeBlocking(name, object, returnTypes);
T t = (T)reto[0];
return t;
}
The object like this:
public class Fruits implements Serializable
{
private int pear;
private int banana;
private int apple;
public int setPear(int pear){this.pear=pear;}
public int getPear(){return this.pear;}
...
}
The xml part should be this:
...
<fruits>
<pear>10</pear>
<banana>20</banana>
<apple>60</apple>
</fruits>
...
But in fact like this:
...
<fruits>
<apple>60</apple>
<banana>20</banana>
<pear>10</pear>
</fruits>
...
Axis2 makes object's property alphabetical order, but the server doesn't accept. I can't modify the serverside, it is ESB.
The only way to do a success request is to use the Axis2 generated code, I used to use WSDL2Java, but too many redundant code and difficult to maintain. So I want refactor.
I have also tried to use CXF, but it also makes object's property alphabetical order, not followed with WSDL/XSD or DTO defined style.
I've find the reason why CXF makes the ordering, it uses java.beans.BeanInfo to get properties of object, such as:
...
BeanInfo beanInfo = Introspector.getBeanInfo(Fruits.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
...
The property in the array has already alphabetical order.
Who knows how to let Axis2 to serialize the Fruits' property to be correct ordering.
Thank you, the first!
Not sure on Axis2, but if you are using CXF with the JAXB databinding, you can add an annotation like:
#XmlType(name = "fruits", propOrder = { "apple", "banana", "pear" }})
to the Fruits class to tell JAXB what order you need/want them output.

GAE/JPA/DataNucleus: Strange exception while trying to persist entity (IllegalArgumentException: out of field index :-1)

I'm getting an exception after I added this embedded field in my entity:
#Entity
public class Team extends DataObject
{
#Embedded
private TeamEvolution teamEvolution = new TeamEvolution();
// NEW FIELD:
#Embedded
// #AttributeOverrides({ #AttributeOverride(name = "buffer", column = #Column) })
// #Enumerated
private ScoutBuffer scoutBuffer;
...
This guy is very simple:
#Embeddable
public class ScoutBuffer
{
private static final int BUFFER_SIZE = 150;
#Basic
private List<String> buffer;
... // from here on there are only methods...
When I try to merge my modifications I get the following exception:
java.lang.IllegalArgumentException: out of field index :-1
at com.olympya.futweb.datamodel.model.ScoutBuffer.jdoProvideField(ScoutBuffer.java)
at org.datanucleus.state.JDOStateManagerImpl.provideField(JDOStateManagerImpl.java:2585)
at org.datanucleus.state.JDOStateManagerImpl.provideField(JDOStateManagerImpl.java:2555)
at org.datanucleus.store.mapped.mapping.CollectionMapping.postUpdate(CollectionMapping.java:185)
at org.datanucleus.store.mapped.mapping.EmbeddedPCMapping.postUpdate(EmbeddedPCMapping.java:133)
// etc, etc...
I don't think there's anything to do, but I had to use JDOHelper.makeDirty before merging the entity for it to perceive that I modified scoutBuffer:
team.getScoutBuffer().add(playerIds);
JDOHelper.makeDirty(team, "scoutBuffer");
em.merge(team);
As you can see commented in the code, I tried the workaround described here, without success. Strange thing is that is from 2009... I'm using GAE 1.7.0, by the way. Also, I tried cleaning/re-enhancing the datamodel.

BLToolkit Complex Mapping Using SELECT JOIN

Trying to use BLToolkit mapper in my project. There is a thing that I really don't know how to solve , the deal is that I have 2 classes Content and Comment.
[MapField("CommentItemId","CommentContent.ContentId")]
public class Comment
{
[MapField("Comment.CommentId")]
public int CommentId;
[Relation(typeof(Content))]
public Content CommentContent;
}
public class Content
{
[MapField("ContentId"),PrimaryKey]
public int ContentId;
public IList<Comment> Comments = new List<Comment>();
}
Well , looks as simple Complex Mapping as example that everybody saw on BLToolkit.net .
But , I'm trying to use SELECT JOIN query. This is my query.
MapResultSet[] sets = new MapResultSet[2];
sets[0] = new MapResultSet(typeof(Content));
sets[1] = new MapResultSet(typeof(Comment));
command = String.Format(#"SELECT con.ContentId,com.CommentId,com.CommentItemId
FROM Content as con,Comments as com
WHERE (con.ContentId = com.CommentItemId AND con.ContentId {0})", itemid, type);
This query returns correct data ( checked with ExecuteReader ). But in MapResultSets there is only Content data , no Comment data ... If i will interchange Content and Comment sets ... I will get only Comment data , and no Content data ... I don't even care about realtions right now , i just want to explain to BLToolkit , that there is data of 2 classes in 1 row.

Resources