Mercurial > hg > openmind
changeset 38:cb25e343e317
more comments and refactorings.
author | casties |
---|---|
date | Wed, 12 Oct 2016 20:51:35 +0200 |
parents | 3485498af8c3 |
children | 86c343109257 |
files | src/main/java/org/mpi/openmind/cache/CacheService.java src/main/java/org/mpi/openmind/cache/WrapperService.java src/main/java/org/mpi/openmind/repository/bo/Entity.java src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java src/main/java/org/mpi/openmind/repository/utils/OMUtils.java |
diffstat | 5 files changed, 293 insertions(+), 198 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/cache/CacheService.java Wed Oct 12 15:33:56 2016 +0200 +++ b/src/main/java/org/mpi/openmind/cache/CacheService.java Wed Oct 12 20:51:35 2016 +0200 @@ -23,6 +23,7 @@ private Map<Long, Map<Long, Attribute>> defAttrs = new HashMap<Long, Map<Long, Attribute>>(); private Map<Long, Relation> defRels = new HashMap<Long, Relation>(); + private boolean defsLoaded = false; public CacheService(){ super(); @@ -32,26 +33,33 @@ this.hashCode()); } - public void initDefinitions(List<Entity> defs, List<Attribute> atts, List<Relation> rels){ - for(Entity def : defs){ - this.lwDefs.put(def.getOwnValue(), def); - } - for(Attribute att : atts){ - if(!this.defAttrs.containsKey(att.getSourceId())){ - this.defAttrs.put(att.getSourceId(), new HashMap<Long, Attribute>()); - } - this.defAttrs.get(att.getSourceId()).put(att.getId(), att); - } - - for(Relation rel : rels){ - this.defRels.put(rel.getId(), rel); - } - - this.defsLoaded = true; - } + /** + * Set up definitions. + * + * @param defs + * @param atts + * @param rels + */ + public void initDefinitions(List<Entity> defs, List<Attribute> atts, List<Relation> rels) { + + for (Entity def : defs) { + this.lwDefs.put(def.getOwnValue(), def); + } + + for (Attribute att : atts) { + if (!this.defAttrs.containsKey(att.getSourceId())) { + this.defAttrs.put(att.getSourceId(), new HashMap<Long, Attribute>()); + } + this.defAttrs.get(att.getSourceId()).put(att.getId(), att); + } + + for (Relation rel : rels) { + this.defRels.put(rel.getId(), rel); + } + + this.defsLoaded = true; + } - private boolean defsLoaded = false; - public void saveLWDefinition(Entity definition){ synchronized (lwDefs) { this.lwDefs.put(definition.getOwnValue(), definition); @@ -182,44 +190,60 @@ return null; } + /** + * Returns all (lightweight) definition entities. + * + * @return + */ public List<Entity> getLWDefinitions(){ List<Entity> list = new ArrayList<Entity>(this.lwDefs.values()); Collections.sort(list); return list; } - public Entity getLWDefinition(String ownValue){ - Entity def = this.lwDefs.get(ownValue); + /** + * Returns the (lightweight) definition entity for the given type. + * + * @param objectClass + * @return + */ + public Entity getLWDefinition(String objectClass){ + Entity def = this.lwDefs.get(objectClass); return def; } private void throwDefLoadingException(){ try { - throw new Exception("the definitions were loaded"); + throw new Exception("the definitions were not loaded"); } catch (Exception e) { - e.printStackTrace(); - logger.error(e.getMessage(), e); + logger.error(e); } } - public List<Attribute> getDefAttributes(String ow){ - if(!this.defsLoaded){ - this.throwDefLoadingException(); - return null; - } - Entity lwDef = getLWDefinition(ow); - if(lwDef != null){ - List<Attribute> list = null; - if(this.defAttrs.containsKey(lwDef.getId())){ - list = new ArrayList<Attribute>(this.defAttrs.get(lwDef.getId()).values()); - Collections.sort(list); - }else{ - list = new ArrayList<Attribute>(); - } - return list; - } - return new ArrayList<Attribute>(); - } + /** + * Returns the attributes of the definition of the given type. + * + * @param objectClass + * @return + */ + public List<Attribute> getDefAttributes(String objectClass) { + if (!this.defsLoaded) { + this.throwDefLoadingException(); + return null; + } + Entity lwDef = getLWDefinition(objectClass); + if (lwDef != null) { + List<Attribute> list = null; + if (this.defAttrs.containsKey(lwDef.getId())) { + list = new ArrayList<Attribute>(this.defAttrs.get(lwDef.getId()).values()); + Collections.sort(list); + } else { + list = new ArrayList<Attribute>(); + } + return list; + } + return new ArrayList<Attribute>(); + } public List<Attribute> getDefAttributesById(Long id){ if(!this.defsLoaded){ @@ -236,12 +260,18 @@ return new ArrayList<Attribute>(); } - public List<Relation> getDefSourceRelations(String ow){ + /** + * Returns the source relations of the definition of the given type. + * + * @param objectClass + * @return + */ + public List<Relation> getDefSourceRelations(String objectClass){ if(!this.defsLoaded){ this.throwDefLoadingException(); return null; } - Entity lwDef = getLWDefinition(ow); + Entity lwDef = getLWDefinition(objectClass); List<Relation> list = new ArrayList<Relation>(); if(lwDef != null){ for(Relation rel : this.defRels.values()){ @@ -254,15 +284,21 @@ return list; } - public List<Relation> getDefTargetRelations(String ow){ + /** + * Returns the target relations of the definition of the given type. + * + * @param objectClass + * @return + */ + public List<Relation> getDefTargetRelations(String objectClass){ if(!this.defsLoaded){ try { - throw new Exception("the definitions were loaded when getDefTargetRelations"); + throw new Exception("the definitions were not loaded when getDefTargetRelations"); } catch (Exception e) { e.printStackTrace(); } } - Entity lwDef = getLWDefinition(ow); + Entity lwDef = getLWDefinition(objectClass); List<Relation> list = new ArrayList<Relation>(); if(lwDef != null){ for(Relation rel : this.defRels.values()){
--- a/src/main/java/org/mpi/openmind/cache/WrapperService.java Wed Oct 12 15:33:56 2016 +0200 +++ b/src/main/java/org/mpi/openmind/cache/WrapperService.java Wed Oct 12 20:51:35 2016 +0200 @@ -123,18 +123,16 @@ // Print Maximum available memory logger.info("Max Memory:" + runtime.maxMemory() / mb + "\n"); } catch (Exception e) { - e.printStackTrace(); + logger.error(e); } if (!this.cache.areDefsLoaded()) { - // List<Entity> list = this.searchServ.getEntities(null, - // Node.SYS_STATUS_CURRENT_VERSION, Node.TYPE_TBOX, null); - // this.cache.initDefs(list); try { - this.cache.initDefinitions(getPS().getLWDefinitions(), getPS() - .getDefAttributes(), getPS().getDefRelations()); + this.cache.initDefinitions(getPS().getLWDefinitions(), + getPS().getDefAttributes(), + getPS().getDefRelations()); } catch (Exception e) { - e.printStackTrace(); + logger.error(e); } } } @@ -730,41 +728,58 @@ public List<Attribute> getDefRelationAttributes(Long id) { return this.cache.getDefAttributesById(id); } - - public List<Attribute> getDefAttributes(String ow) { - return this.cache.getDefAttributes(ow); + + /** + * Returns the attributes of the definition of the given type. + * + * @param objectClass + * @return + */ + public List<Attribute> getDefAttributes(String objectClass) { + return this.cache.getDefAttributes(objectClass); } - public List<Relation> getDefSourceRelations(String ow) { - return this.cache.getDefSourceRelations(ow); + /** + * Returns the source relations of the definition of the given type. + * + * @param objectClass + * @return + */ + public List<Relation> getDefSourceRelations(String objectClass) { + return this.cache.getDefSourceRelations(objectClass); } - public List<Relation> getDefTargetRelations(String ow) { - return this.cache.getDefTargetRelations(ow); + /** + * Returns the target relations of the definition of the given type. + * + * @param objectClass + * @return + */ + public List<Relation> getDefTargetRelations(String objectClass) { + return this.cache.getDefTargetRelations(objectClass); } public Entity getDefinitionById(Long id) { return this.cache.getLWDefinitionById(id); - /* - * Entity def = cache.getDefinitionById(id); if(def == null){ - * List<Entity> list = this.searchServ.getEntities(id, - * Node.SYS_STATUS_CURRENT_VERSION, Node.TYPE_TBOX, null); if - * (list.size() > 0) { def = list.get(0); cache.saveDefinition(def); } } - * return def; - */ } + /** + * Returns all (lightweight) definition entities. + * + * @return + */ public List<Entity> getLWDefinitions() { return this.cache.getLWDefinitions(); - /* - * List<Entity> defs = this.cache.getDefinitions(); - * Collections.sort(defs, new EntitySortByNormalizedOwnValue()); return - * defs; - */ } - public Entity getDefinition(String ownValue) { - return this.cache.getLWDefinition(ownValue); + /** + * Returns the (lightweight) definition entity for the given type. + * + * @param objectClass + * @return + */ + public Entity getDefinition(String objectClass) { + return this.cache.getLWDefinition(objectClass); } public List<Relation> getRelation(String name, String srcOC, String tarOC) @@ -1048,6 +1063,13 @@ null, Node.TYPE_ABOX, objectClass, ownValue, true, maxResult); } + /** + * Save the entity. Calls {@link #saveEntity(String, String)}. + * + * @param entity + * @param user + * @throws Exception + */ public void saveAssertion(Entity entity, String user) throws Exception { this.saveEntity(entity, user); }
--- a/src/main/java/org/mpi/openmind/repository/bo/Entity.java Wed Oct 12 15:33:56 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/bo/Entity.java Wed Oct 12 20:51:35 2016 +0200 @@ -23,6 +23,10 @@ * * @author jurzua */ +/** + * @author casties + * + */ @javax.persistence.Entity @DiscriminatorValue("ENTITY") @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) @@ -108,40 +112,61 @@ } - public boolean containsSourceRelation(String relationName, Long targetId){ - try { + /** + * Returns if this entity has a source relation of the given type to the given id. + * + * @param relationName + * @param targetId + * @return + */ + public boolean containsSourceRelation(String relationName, Long targetId) { + try { if (this.lightweight) { - throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + throw new IllegalAccessException( + "This Entity is lightweight, so its relations and attributes were not loaded from the DB."); } for (Relation relation : this.getSourceRelations()) { - if (relation.getOwnValue().equals(relationName) && - OMUtils.equals(relation.getTargetId(), targetId)) { + if (relation.getOwnValue().equals(relationName) && OMUtils.equals(relation.getTargetId(), targetId)) { return true; } } } catch (Exception e) { - logger.error(e.getMessage(), e); + logger.error(e.getMessage(), e); } return false; } - public boolean containsTargetRelation(String relationName, Long sourceId){ - try { + /** + * Returns if this entity has a target relation of the given type from the given id. + * + * @param relationName + * @param sourceId + * @return + */ + public boolean containsTargetRelation(String relationName, Long sourceId) { + try { if (this.lightweight) { - throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + throw new IllegalAccessException( + "This Entity is lightweight, so its relations and attributes were not loaded from the DB."); } for (Relation relation : this.getTargetRelations()) { - if (relation.getOwnValue().equals(relationName) && - OMUtils.equals(relation.getSourceId(), sourceId)) { + if (relation.getOwnValue().equals(relationName) && OMUtils.equals(relation.getSourceId(), sourceId)) { return true; } } } catch (Exception e) { - logger.error(e.getMessage(), e); + logger.error(e.getMessage(), e); } return false; } + /** + * Returns if the two Longs are equal and not null. + * + * @param l1 + * @param l2 + * @return + */ public boolean equals(Long l1, Long l2){ if(l1 != null && l2 != null){ return l1.equals(l2); @@ -288,38 +313,48 @@ } } - public void addSourceRelation(String relName, Entity tar){ - if(!containsSourceRelation(relName, tar.getId()) && - !tar.containsTargetRelation(relName, this.getId())){ - new Relation(this, tar, relName); - }else if(!containsSourceRelation(relName, tar.getId())){ - Relation rel = tar.getTargetRelation(relName, this.getId()); - if(rel != null){ - this.sourceRelations.add(rel); - } - }else if(!tar.containsTargetRelation(relName, this.getId())){ - Relation rel = this.getSourceRelation(relName, tar.getId()); - if(rel != null){ - tar.getTargetRelations().add(rel); - } - } + /** + * Add a source relation from this entity to another target entity. + * + * @param relName + * @param tar + */ + public void addSourceRelation(String relName, Entity tar) { + if (!containsSourceRelation(relName, tar.getId()) && !tar.containsTargetRelation(relName, this.getId())) { + new Relation(this, tar, relName); + } else if (!containsSourceRelation(relName, tar.getId())) { + Relation rel = tar.getTargetRelation(relName, this.getId()); + if (rel != null) { + this.sourceRelations.add(rel); + } + } else if (!tar.containsTargetRelation(relName, this.getId())) { + Relation rel = this.getSourceRelation(relName, tar.getId()); + if (rel != null) { + tar.getTargetRelations().add(rel); + } + } } - - public void addTargetRelation(String relName, Entity src){ - if(!containsTargetRelation(relName, src.getId()) && - !src.containsSourceRelation(relName, getId())){ - new Relation(src, this, relName); - }else if(!containsTargetRelation(relName, src.getId())){ - Relation rel = src.getSourceRelation(relName, getId()); - if(rel != null){ - this.targetRelations.add(rel); - } - }else if(!src.containsSourceRelation(relName, getId())){ - Relation rel = this.getTargetRelation(relName, src.getId()); - if(rel != null){ - src.getSourceRelations().add(rel); - } - } + + /** + * Add a target relation from another source entity to this entity. + * + * @param relName + * @param src + */ + public void addTargetRelation(String relName, Entity src) { + if (!containsTargetRelation(relName, src.getId()) && !src.containsSourceRelation(relName, getId())) { + new Relation(src, this, relName); + } else if (!containsTargetRelation(relName, src.getId())) { + Relation rel = src.getSourceRelation(relName, getId()); + if (rel != null) { + this.targetRelations.add(rel); + } + } else if (!src.containsSourceRelation(relName, getId())) { + Relation rel = this.getTargetRelation(relName, src.getId()); + if (rel != null) { + src.getSourceRelations().add(rel); + } + } } public Relation getTargetRelation(String relName, Long srcId){ @@ -379,16 +414,6 @@ } - private void testLightweightState(Entity ent){ - try { - if (ent.lightweight) { - throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB. " + ent); - } - }catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - private void testLightweightState(){ try { if (this.lightweight) {
--- a/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java Wed Oct 12 15:33:56 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java Wed Oct 12 20:51:35 2016 +0200 @@ -348,48 +348,43 @@ } /** + * Saves all given entities without attributes and relations without creating new versions. + * * @param nodeList */ - public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) - throws Exception { - long start = System.currentTimeMillis(); - - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - Sequence idSequence = this.getIdSequence(session); - DecimalFormat df = new DecimalFormat("#.##"); - int counter = 0; - - logger.debug("##### Saving Node List: " + nodeList.size() + " #####"); + public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) throws Exception { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + session.getTransaction().begin(); + Sequence idSequence = this.getIdSequence(session); + + logger.debug("START saving Node list of size " + nodeList.size()); - for (Entity node : nodeList) { - node.autoNormalize(); - if (node.getId() == null) - node.setId(idSequence.generateId()); - if (node.getRowId() == null) { - logger.debug("Saving node from EntityListAsNodeWithoutContent: "+node); - txLog.info("save node: "+node.toEncString()); - session.save(node); - } else { - txLog.info("merge node: "+node.toEncString()); - session.merge(node); - } - double percent = ((double) counter / (double) nodeList.size()) * 100.0; - long diff = System.currentTimeMillis() - start; - double min = (double) diff / (double) (60 * 1000); - - if ((percent % 10) < 0.005) { - logger.debug("\n[" + df.format(percent) + " %] counter: " - + counter + " / " + nodeList.size()); - - logger.debug("Tempo: " + (double) counter / min); - } - counter++; - } - - session.save(idSequence); - session.getTransaction().commit(); + for (Entity node : nodeList) { + node.autoNormalize(); + if (node.getId() == null) { + node.setId(idSequence.generateId()); + } + if (node.getRowId() == null) { + // node is new + logger.debug("Saving node from EntityListAsNodeWithoutContent: " + node); + txLog.info("save node: " + node.toEncString()); + session.save(node); + } else { + txLog.info("merge node: " + node.toEncString()); + session.merge(node); + } + } + // update sequence + session.save(idSequence); + + logger.debug("END saving Node list."); + + } catch (Exception e) { + logger.error(e); + } finally { + session.getTransaction().commit(); + } } public void deleteEntityList(List<Entity> entities) { @@ -964,7 +959,7 @@ } /** - * Returns entities with their corresponding content. + * Returns entities with their content. * * If a parameter is null then the condition is omitted from the query * returning all entities matching the remaining conditions. @@ -1548,10 +1543,12 @@ this.importMode = importModus; } - // ################################################################# - // ################################################################# - // ################################################################# - + /** + * Returns all definition relations. + * + * @return + * @throws Exception + */ public List<Relation> getDefRelations() { List<Relation> list = new ArrayList<Relation>(); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -1565,22 +1562,20 @@ query.setString("type", Node.TYPE_TBOX); list = query.list(); - /* - * for(Relation rel : list){ List<Node> attrs = - * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), - * rel.getModificationTime(), null, null); for(Node attNode : - * attrs){ if(attNode instanceof Attribute){ - * rel.addAttribute((Attribute)attNode); } } } - */ - } catch (Exception e) { - logger.error(e.getMessage(), e); + logger.error(e); } finally { session.getTransaction().commit(); } return list; } + /** + * Returns all definition attributes. + * + * @return + * @throws Exception + */ public List<Attribute> getDefAttributes() { List<Attribute> list = new ArrayList<Attribute>(); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); @@ -1596,33 +1591,43 @@ list = query.list(); } catch (Exception e) { - logger.error(e.getMessage(), e); + logger.error(e); } finally { session.getTransaction().commit(); } return list; } - public List<Entity> getLWDefinitions() throws Exception { - List<Entity> list = new ArrayList<Entity>(); - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + /** + * Returns all (lightweight) definition entities. + * + * @return + * @throws Exception + */ + public List<Entity> getLWDefinitions() throws Exception { + List<Entity> list = new ArrayList<Entity>(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + session.getTransaction().begin(); - session.getTransaction().begin(); - - String hql = "from Entity where systemStatus = :systemStatus and type = :type"; - Query query = session.createQuery(hql); + String hql = "from Entity where systemStatus = :systemStatus and type = :type"; + Query query = session.createQuery(hql); - query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); - query.setString("type", Node.TYPE_TBOX); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("type", Node.TYPE_TBOX); - list = query.list(); - for (Entity def : list) { - def.setLightweight(true); - } - session.getTransaction().commit(); + list = query.list(); + for (Entity def : list) { + def.setLightweight(true); + } + } catch (Exception e) { + logger.error(e); + } finally { + session.getTransaction().commit(); + } - return list; - } + return list; + } // ################################################################# // #################################################################
--- a/src/main/java/org/mpi/openmind/repository/utils/OMUtils.java Wed Oct 12 15:33:56 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/utils/OMUtils.java Wed Oct 12 20:51:35 2016 +0200 @@ -104,6 +104,13 @@ return rs; } + /** + * Returns if the two Longs are equal or both null. + * + * @param one + * @param two + * @return + */ public static boolean equals(Long one, Long two){ if(one == null && two == null){ return true;