Skip to content

Commit

Permalink
Merge pull request #18 from gessi-chatbots/getAllFeaturesByMobileAppl…
Browse files Browse the repository at this point in the history
…ication

Get all features by mobile application
  • Loading branch information
mtiessler authored Jun 17, 2024
2 parents 806debe + 244dced commit 03c0071
Show file tree
Hide file tree
Showing 14 changed files with 521 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface InductiveKnowledgeAPI extends BaseAPI {
int getLastReview();

@PostMapping("/derived-nl-features")
int derivedNLFeatures(@RequestParam(value = "documentType") DocumentType documentType,
ResponseEntity<String> derivedNLFeatures(@RequestParam(value = "documentType") DocumentType documentType,
@RequestParam(value = "batch-size", defaultValue = "0") Integer batchSize,
@RequestParam(value = "from", defaultValue = "0") Integer from,
@RequestParam(value = "feature-model", defaultValue = "transfeatex") String featureModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import upc.edu.gessi.repo.controller.InductiveKnowledgeAPI;
import upc.edu.gessi.repo.dto.Document;
import upc.edu.gessi.repo.dto.DocumentType;
import upc.edu.gessi.repo.dto.SimilarityAlgorithm;
import upc.edu.gessi.repo.dto.SimilarityApp;
import upc.edu.gessi.repo.service.FeatureService;
import upc.edu.gessi.repo.service.InductiveKnowledgeService;
import upc.edu.gessi.repo.service.ServiceFactory;
import upc.edu.gessi.repo.service.SimilarityService;
import upc.edu.gessi.repo.service.impl.GraphDBServiceImpl;
import upc.edu.gessi.repo.service.impl.SimilarityServiceImpl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -50,37 +46,36 @@ public int getLastReview() {
}

@Override
public int derivedNLFeatures(final DocumentType documentType,
final Integer batchSize,
final Integer from,
final String featureModel) {
public ResponseEntity<String> derivedNLFeatures(final DocumentType documentType,
final Integer batchSize,
final Integer from,
final String featureModel) {
logger.info("Generating derived deductive knowledge from natural language documents");
logger.info("Document type: " + documentType);
if (documentType.equals(DocumentType.REVIEWS)) {
logger.info("Deducting features from reviews...");
try {
return ((FeatureService) useService(FeatureService.class)).extractFeaturesFromReviews(batchSize, from, featureModel);
} catch (Exception e) {
return dbConnection.getCount();
}
} else if (!documentType.equals(DocumentType.ALL)) {
if (documentType.equals(DocumentType.ALL)
|| documentType.equals(DocumentType.DESCRIPTION)) {
logger.info("Deducting features from " + documentType.getName());
((FeatureService) useService(FeatureService.class)).extractFeaturesByDocument(documentType, batchSize);
} else {
logger.info("Deducting features from descriptions...");
((FeatureService) useService(FeatureService.class)).extractFeaturesByDocument(DocumentType.DESCRIPTION, batchSize);

}
if (documentType.equals(DocumentType.ALL)
|| documentType.equals(DocumentType.CHANGELOG)) {
logger.info("Deducting features from changelogs...");
((FeatureService) useService(FeatureService.class)).extractFeaturesByDocument(DocumentType.CHANGELOG, batchSize);

}
if (documentType.equals(DocumentType.ALL)
|| documentType.equals(DocumentType.SUMMARY)) {
logger.info("Deducting features from summaries...");
((FeatureService) useService(FeatureService.class)).extractFeaturesByDocument(DocumentType.SUMMARY, batchSize);


}
if (documentType.equals(DocumentType.ALL)
|| documentType.equals(DocumentType.REVIEWS)) {
logger.info("Deducting features from reviews...");
//dbConnection.extractFeaturesFromReviews();
try {
((FeatureService) useService(FeatureService.class)).extractFeaturesFromReviews(batchSize, from, featureModel);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return -1;
return new ResponseEntity<>(HttpStatus.OK);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package upc.edu.gessi.repo.repository;



import upc.edu.gessi.repo.dto.DigitalDocumentDTO;

import java.util.List;
import java.util.Map;


public interface DocumentRepository extends RDFCRUDRepository<DigitalDocumentDTO> {
Map<String, Integer> findAllDocumentTypeFeaturesWithOccurrences(String documentType);

List<String> findAllDistinctDocumentTypeFeatures(String documentType);
}

16 changes: 16 additions & 0 deletions src/main/java/upc/edu/gessi/repo/repository/FeatureRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package upc.edu.gessi.repo.repository;



import upc.edu.gessi.repo.dto.Review.FeatureDTO;

import java.util.List;
import java.util.Map;


public interface FeatureRepository extends RDFCRUDRepository<FeatureDTO> {
Map<String, Integer> findAllWithOccurrences();

List<String> findAllDistinct();
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import upc.edu.gessi.repo.exception.MobileApplications.NoMobileApplicationsFoundException;

import java.util.List;
import java.util.Map;

public interface MobileApplicationRepository extends RDFCRUDRepository<MobileApplicationFullDataDTO> {
void addFeature(MobileApplicationFullDataDTO completeApplicationDataDTO,
Expand All @@ -22,4 +23,10 @@ void addReviewToMobileApplication(String packageName,
List<MobileApplicationBasicDataDTO> findAllBasicDataPaginated(Integer page, Integer size) throws NoMobileApplicationsFoundException;

List<MobileApplicationBasicDataDTO> findAllApplicationsBasicData() throws NoMobileApplicationsFoundException;

Map<String, Integer> findAllMobileApplicationFeaturesWithOccurrences(String applicationIdentifier);

List<String> findAllDistinctMobileApplicationFeatures(String applicationIdentifier);

List<String> findAllIdentifiers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

package upc.edu.gessi.repo.repository.impl;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.repository.http.HTTPRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import upc.edu.gessi.repo.dto.DigitalDocumentDTO;
import upc.edu.gessi.repo.dto.Review.FeatureDTO;
import upc.edu.gessi.repo.exception.NoObjectFoundException;
import upc.edu.gessi.repo.exception.ObjectNotFoundException;
import upc.edu.gessi.repo.repository.DocumentRepository;
import upc.edu.gessi.repo.repository.FeatureRepository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DocumentRepositoryImpl implements DocumentRepository {

private final HTTPRepository repository;


@Autowired
public DocumentRepositoryImpl(final @Value("${db.url}") String url,
final @Value("${db.username}") String username,
final @Value("${db.password}") String password) {
repository = new HTTPRepository(url);
repository.setUsernameAndPassword(username, password);


}

@Override
public Map<String, Integer> findAllDocumentTypeFeaturesWithOccurrences(String documentType) {
return new HashMap<>();
}

@Override
public List<String> findAllDistinctDocumentTypeFeatures(String documentType) {
return new ArrayList<>();
}

@Override
public DigitalDocumentDTO findById(String id) throws ObjectNotFoundException {
return null;
}

@Override
public List<DigitalDocumentDTO> findAll() throws NoObjectFoundException {
return null;
}

@Override
public List<DigitalDocumentDTO> findAllPaginated(Integer page, Integer size) throws NoObjectFoundException {
return null;
}

@Override
public IRI insert(DigitalDocumentDTO dto) {
return null;
}

@Override
public DigitalDocumentDTO update(DigitalDocumentDTO entity) {
return null;
}

@Override
public void delete(String id) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package upc.edu.gessi.repo.repository.impl;

import org.apache.commons.text.WordUtils;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.http.HTTPRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import upc.edu.gessi.repo.dto.DocumentType;
import upc.edu.gessi.repo.dto.Feature;
import upc.edu.gessi.repo.dto.MobileApplication.MobileApplicationBasicDataDTO;
import upc.edu.gessi.repo.dto.MobileApplication.MobileApplicationFullDataDTO;
import upc.edu.gessi.repo.dto.Review.FeatureDTO;
import upc.edu.gessi.repo.dto.Review.ReviewDTO;
import upc.edu.gessi.repo.dto.SimilarityApp;
import upc.edu.gessi.repo.dto.graph.GraphApp;
import upc.edu.gessi.repo.exception.MobileApplications.MobileApplicationNotFoundException;
import upc.edu.gessi.repo.exception.MobileApplications.NoMobileApplicationsFoundException;
import upc.edu.gessi.repo.exception.NoObjectFoundException;
import upc.edu.gessi.repo.exception.ObjectNotFoundException;
import upc.edu.gessi.repo.repository.FeatureRepository;
import upc.edu.gessi.repo.repository.MobileApplicationRepository;
import upc.edu.gessi.repo.repository.ReviewRepository;
import upc.edu.gessi.repo.util.*;

import java.sql.Date;
import java.util.*;

@Repository
public class FeatureRepositoryImpl implements FeatureRepository {

private final HTTPRepository repository;

private final FeatureQueryBuilder featureQueryBuilder;


@Autowired
public FeatureRepositoryImpl(final @Value("${db.url}") String url,
final @Value("${db.username}") String username,
final @Value("${db.password}") String password,
final FeatureQueryBuilder featureQueryBuild) {
repository = new HTTPRepository(url);
repository.setUsernameAndPassword(username, password);
featureQueryBuilder = featureQueryBuild;


}


@Override
public FeatureDTO findById(String id) throws ObjectNotFoundException {
return null;
}

@Override
public List<FeatureDTO> findAll() throws NoObjectFoundException {
return null;
}

@Override
public List<FeatureDTO> findAllPaginated(Integer page, Integer size) throws NoObjectFoundException {
return null;
}

@Override
public IRI insert(FeatureDTO dto) {
return null;
}

@Override
public FeatureDTO update(FeatureDTO entity) {
return null;
}

@Override
public void delete(String id) {

}

private TupleQueryResult runSparqlQuery(final String query) {
RepositoryConnection repoConnection = repository.getConnection();
TupleQuery tupleQuery = repoConnection.prepareTupleQuery(query);
return tupleQuery.evaluate();
}

@Override
public Map<String, Integer> findAllWithOccurrences() {
TupleQueryResult result = runSparqlQuery(featureQueryBuilder.findAllFeaturesWithOccurrencesQuery());
Map<String, Integer> featureOcurrencesDict = new HashMap<>();
while (result.hasNext()) {
BindingSet bindings = result.next();
if(bindings.getBinding("feature") != null
&& bindings.getBinding("feature").getValue() != null
&& bindings.getBinding("count") != null
&& bindings.getBinding("count").getValue() != null) {
featureOcurrencesDict.put(
bindings.getBinding("feature").getValue().stringValue(),
Integer.valueOf(bindings.getBinding("count").getValue().stringValue())
);
}
}
return featureOcurrencesDict;
}

@Override
public List<String> findAllDistinct() {
TupleQueryResult result = runSparqlQuery(featureQueryBuilder.findAllDistinctFeaturesQuery());
List<String> featuresList = new ArrayList<>();
while (result.hasNext()) {
BindingSet bindings = result.next();
if(bindings.getBinding("feature") != null
&& bindings.getBinding("feature").getValue() != null) {
featuresList.add(
bindings.getBinding("feature").getValue().stringValue());
}
}
return featuresList;
}
}
Loading

0 comments on commit 03c0071

Please sign in to comment.