Skip to content

Commit

Permalink
feat: UUID class
Browse files Browse the repository at this point in the history
  • Loading branch information
lroffia committed Apr 28, 2024
1 parent f573953 commit 2d45ca1
Show file tree
Hide file tree
Showing 17 changed files with 344 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
package it.unibo.arces.wot.sepa.commons.properties;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -141,8 +139,6 @@ public SPARQL11Properties(String args[]) {
public SPARQL11Properties(String uri,String[] args) throws SEPAPropertiesException {
this();

Path path = Path.of(uri);

Reader in = getReaderFromUri(uri);
parseJSAP(in);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import com.google.gson.JsonPrimitive;
import it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.core.appender.rolling.action.IfAccumulatedFileCount;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -47,8 +49,7 @@ public class BindingsResults {
/**
* Instantiates a new bindings results.
*
* @param results
* the results
* @param results the results
*/
public BindingsResults(JsonObject results) {
this.results = results;
Expand All @@ -57,10 +58,8 @@ public BindingsResults(JsonObject results) {
/**
* Instantiates a new bindings results.
*
* @param varSet
* the var set
* @param solutions
* the solutions
* @param varSet the var set
* @param solutions the solutions
*/
public BindingsResults(ArrayList<String> varSet, List<Bindings> solutions) {
results = new JsonObject();
Expand All @@ -86,8 +85,7 @@ public BindingsResults(ArrayList<String> varSet, List<Bindings> solutions) {
/**
* Instantiates a new bindings results.
*
* @param newBindings
* the new bindings
* @param newBindings the new bindings
*/
public BindingsResults(BindingsResults newBindings) {
results = new JsonObject();
Expand All @@ -113,12 +111,11 @@ public BindingsResults(BindingsResults newBindings) {
public boolean isAskResult() {
return (results.has("boolean") && results.has("head"));
}

public boolean getAskBoolean() {
return results.get("boolean").getAsBoolean();
}



/**
* Gets the variables.
*
Expand Down Expand Up @@ -180,16 +177,16 @@ public boolean isEmpty() {
if (bindings == null)
return true;
if (bindings.size() == 1) {
if (bindings.get(0).getAsJsonObject().entrySet().isEmpty()) return true;
if (bindings.get(0).getAsJsonObject().entrySet().isEmpty())
return true;
}
return (bindings.size() == 0);
}

/**
* Adds the.
*
* @param binding
* the binding
* @param binding the binding
*/
public void add(Bindings binding) {
if (binding == null)
Expand All @@ -204,46 +201,66 @@ public void add(Bindings binding) {
/**
* Contains.
*
* @param solution
* the solution
* @param solution the solution
* @return true, if successful
*/
public boolean contains(Bindings solution) {
// JsonArray solutionSet = getBindingsArray();
// JsonObject bindings = solution.toJson();
//
// for (int i = 0; i < solutionSet.size(); i++) {
// JsonObject solutionX = solutionSet.get(i).getAsJsonObject();
// boolean match = true;
//
// for (String key : bindings.keySet()) {
// if (!solutionX.has(key) || !solutionX.get(key).getAsString().equals(bindings.get(key).getAsString())) {
// match = false;
// break;
// }
// }
//
// if (match) {
// return true;
// }
// }
// return false;

if (solution == null) return false;

JsonArray bindings = getBindingsArray();
if (bindings == null) return false;
if (bindings == null) return false;
if (bindings.size() == 0) return false;

if(getVariables().size() != solution.getVariables().size()) return false;
if(!getVariables().containsAll(solution.getVariables())) return false;


for (JsonElement b : bindings) {
Bindings temp = new Bindings(b.getAsJsonObject());
if (solution.getVariables().size() == temp.getVariables().size()) {
if(solution.getVariables().containsAll(temp.getVariables())) {
boolean found = true;
for(String var : solution.getVariables()) {
try {
// Comparing BNODE between two different queries is not possible
if (solution.isBNode(var) && temp.isBNode(var)) continue;
if(!solution.getRDFTerm(var).equals(temp.getRDFTerm(var))) {
found = false;
break;
}
} catch (SEPABindingsException e) {
Logging.logger.error(e.getMessage());
return false;
}
boolean found = true;
for(String var : solution.getVariables()) {
try {
// Comparing BNODE between two different queries is not possible
if (solution.isBNode(var) && temp.isBNode(var)) continue;
if(!solution.getRDFTerm(var).equals(temp.getRDFTerm(var))) {
found = false;
break;
}
if (found) return true;
}
} catch (SEPABindingsException e) {
Logging.logger.error(e.getMessage());
return false;
}
}
if (found) return true;
}

return false;
}

/**
* Removes the.
*
* @param solution
* the solution
* @param solution the solution
*/
public void remove(Bindings solution) {
if (solution == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class RDFTerm {
protected JsonObject json = null;

public boolean isURI() {
return this.getClass().equals(RDFTermURI.class);
return this.getClass().equals(RDFTermURI.class) || this.getClass().equals(UUID.class);
}

public boolean isLiteral() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.unibo.arces.wot.sepa.commons.sparql;

public class UUID extends RDFTermURI{
public UUID() {
super("urn:uuid:"+java.util.UUID.randomUUID().toString());
}

@Override
public boolean equals(Object t) {
if (t == this)
return true;
if (!t.getClass().equals(UUID.class))
return false;

return this.getValue().equals(((UUID) t).getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static long getTime() {
}

public synchronized static void log(String tag,long start,long stop) {
String message = String.format("%d,%d,%s",System.currentTimeMillis(),stop-start,tag);
String message = String.format("%d,%d,%d,%d,%s",System.currentTimeMillis(),(stop-start)/1000000,(stop-start)/1000,stop-start,tag);
org.apache.logging.log4j.Level level =Logging.getLevel("timing");
if(level==null) {
//default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import it.unibo.arces.wot.sepa.commons.sparql.RDFTerm;
import it.unibo.arces.wot.sepa.logging.Logging;
import it.unibo.arces.wot.sepa.logging.Timings;
import it.unibo.arces.wot.sepa.api.SPARQL11Protocol;
import it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException;
import it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException;
Expand Down Expand Up @@ -81,14 +82,17 @@ public final Response update(long timeout,long nRetry) throws SEPASecurityExcept

public final Response multipleUpdate(long timeout,long nRetry)
throws SEPASecurityException, SEPAPropertiesException, SEPABindingsException, SEPAProtocolException {
long start = Timings.getTime();
UpdateRequest req = new UpdateRequest(appProfile.getUpdateMethod(updateId),
appProfile.getUpdateProtocolScheme(updateId), appProfile.getUpdateHost(updateId),
appProfile.getUpdatePort(updateId), appProfile.getUpdatePath(updateId),
appProfile.addPrefixesAndReplaceMultipleBindings(sparqlUpdate,
addDefaultDatatype(multipleForcedBindings.getBindings(), updateId, false)),
appProfile.getUsingGraphURI(updateId), appProfile.getUsingNamedGraphURI(updateId),
(appProfile.isSecure() ? appProfile.getAuthenticationProperties().getBearerAuthorizationHeader() : null), timeout,nRetry);

long stop = Timings.getTime();
Timings.log("multipleUpdate create UpdateRequest", start, stop);

Logging.logger.trace(req);

Response retResponse = sparql11.update(req);
Expand All @@ -108,6 +112,6 @@ public final void setUpdateBindingValue(String variable, RDFTerm value) throws S
}

public final void setUpdateMultipleBindings(ArrayList<String> variables, ArrayList<ArrayList<RDFTerm>> values) throws SEPABindingsException {
multipleForcedBindings.setUpdateBindings(variables,values);
multipleForcedBindings.add(variables,values);
}
}
Loading

0 comments on commit 2d45ca1

Please sign in to comment.