use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.
the class WonMessageValidatorTest method testInvalidEnvelopeChain.
@Test
@Ignore
public void testInvalidEnvelopeChain() throws IOException {
// test 1
// create invalid dataset by removing a triple that references envelope 1 from envelope 2
Dataset invalidDataset = WonRdfUtils.MessageUtils.copyByDatasetSerialization(new WonMessage(createMessageDataset)).getCompleteDataset();
Model env2Model = invalidDataset.getNamedModel(CREATE_ENV2_NAME);
Model env1Model = invalidDataset.getNamedModel(CREATE_ENV1_NAME);
Statement stmtOld = env2Model.createStatement(ResourceFactory.createResource(CREATE_ENV2_NAME), WONMSG.CONTAINS_ENVELOPE, ResourceFactory.createResource(CREATE_ENV1_NAME));
env2Model.remove(stmtOld);
// validate this invalid dataset
WonMessageValidator validator = new WonMessageValidator();
StringBuilder message = new StringBuilder();
boolean valid = validator.validate(invalidDataset, message);
Assert.assertTrue("validation is expected to fail", !valid);
Assert.assertTrue(message.toString().contains("validation/05_sign/invalid_from_owner_signer.rq"));
// reset for further testing
env2Model.add(stmtOld);
// test 2
// create invalid dataset by adding a triple that references envelope 2 from envelope 1,
// thus creating a cycle in the envelope chain
Statement stmtNew = env1Model.createStatement(ResourceFactory.createResource(CREATE_ENV1_NAME), WONMSG.CONTAINS_ENVELOPE, ResourceFactory.createResource(CREATE_ENV2_NAME));
env1Model.add(stmtNew);
// validate this invalid dataset
valid = validator.validate(invalidDataset, message);
Assert.assertTrue("validation is expected to fail", !valid);
Assert.assertTrue(message.toString().contains("validation/05_sign/invalid_from_owner_signer.rq"));
// reset for further testing
env1Model.remove(stmtNew);
// test 3
// create invalid dataset by adding a triple that references an envelope that is not present in the dataset
stmtNew = env1Model.createStatement(ResourceFactory.createResource(CREATE_ENV1_NAME), WONMSG.CONTAINS_ENVELOPE, ResourceFactory.createResource("test:resource:uri"));
env1Model.add(stmtNew);
// validate this invalid dataset
valid = validator.validate(invalidDataset, message);
Assert.assertTrue("validation is expected to fail", !valid);
Assert.assertTrue(message.toString().contains("validation/05_sign/invalid_from_owner_signer.rq"));
// reset for further testing
env1Model.remove(stmtNew);
}
use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.
the class GetProposalsTests method condenseTestCaseIteratively.
private static void condenseTestCaseIteratively(String filename, String outputPath) throws Exception {
RdfUtils.Pair<Dataset> inputAndExpectedOutput = loadDatasetPair(filename);
try {
if (!passesTest(inputAndExpectedOutput.getFirst(), inputAndExpectedOutput.getSecond())) {
System.out.println("test does not pass, cannot condense: " + filename);
return;
}
} catch (Exception e) {
System.out.println("test throws an Exception, cannot condense: " + filename);
return;
}
Dataset condensedDataset = inputAndExpectedOutput.getFirst();
Dataset expectedOutput = inputAndExpectedOutput.getSecond();
Iterator<String> graphNamesIt = condensedDataset.listNames();
int deletedStatements = 0;
while (graphNamesIt.hasNext()) {
String graphName = graphNamesIt.next();
System.out.println("trying to remove graph: " + graphName);
Dataset backupDataset = RdfUtils.cloneDataset(condensedDataset);
condensedDataset.removeNamedModel(graphName);
if (!passesTest(condensedDataset, expectedOutput)) {
System.out.println("cannot remove graph: " + graphName + ", trying individual triples");
condensedDataset = backupDataset;
// now try to remove triples
Model condensedModel = condensedDataset.getNamedModel(graphName);
Model attepmtedStatements = ModelFactory.createDefaultModel();
boolean done = false;
while (!done) {
Model backupModel = RdfUtils.cloneModel(condensedModel);
StmtIterator it = condensedModel.listStatements();
done = true;
while (it.hasNext()) {
Statement stmt = it.next();
if (attepmtedStatements.contains(stmt)) {
System.out.println("attempted this before");
continue;
}
System.out.println("trying statement: " + stmt);
attepmtedStatements.add(stmt);
it.remove();
deletedStatements++;
done = false;
break;
}
condensedDataset.removeNamedModel(graphName);
if (!condensedModel.isEmpty()) {
condensedDataset.addNamedModel(graphName, condensedModel);
}
if (!passesTest(condensedDataset, expectedOutput)) {
System.out.println("could not delete statement");
condensedModel = backupModel;
condensedDataset.replaceNamedModel(graphName, condensedModel);
deletedStatements--;
} else {
System.out.println("deleted a statement");
}
}
if (!passesTest(condensedDataset, expectedOutput)) {
System.out.println("test does not pass after removing statements!");
condensedDataset = backupDataset;
} else {
System.out.println("removed " + deletedStatements + " statements");
}
} else {
System.out.println("removed graph: " + graphName);
}
System.out.println("dataset has ");
}
RDFDataMgr.write(new FileOutputStream(Paths.get(outputPath + filename).toFile()), condensedDataset, Lang.TRIG);
System.out.println("wrote condensed input file to: " + outputPath + filename);
}
use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.
the class AgreementProtocolTest method condenseTestCaseIteratively.
private static void condenseTestCaseIteratively(String filename, String outputPath) throws Exception {
RdfUtils.Pair<Dataset> inputAndExpectedOutput = loadDatasetPair(filename);
try {
if (!passesTest(inputAndExpectedOutput.getFirst(), inputAndExpectedOutput.getSecond())) {
System.out.println("test does not pass, cannot condense: " + filename);
return;
}
} catch (Exception e) {
System.out.println("test throws an Exception, cannot condense: " + filename);
return;
}
Dataset condensedDataset = inputAndExpectedOutput.getFirst();
Dataset expectedOutput = inputAndExpectedOutput.getSecond();
Iterator<String> graphNamesIt = condensedDataset.listNames();
int deletedStatements = 0;
while (graphNamesIt.hasNext()) {
String graphName = graphNamesIt.next();
System.out.println("trying to remove graph: " + graphName);
Dataset backupDataset = RdfUtils.cloneDataset(condensedDataset);
condensedDataset.removeNamedModel(graphName);
if (!passesTest(condensedDataset, expectedOutput)) {
System.out.println("cannot remove graph: " + graphName + ", trying individual triples");
condensedDataset = backupDataset;
// now try to remove triples
Model condensedModel = condensedDataset.getNamedModel(graphName);
Model attepmtedStatements = ModelFactory.createDefaultModel();
boolean done = false;
while (!done) {
Model backupModel = RdfUtils.cloneModel(condensedModel);
StmtIterator it = condensedModel.listStatements();
done = true;
while (it.hasNext()) {
Statement stmt = it.next();
if (attepmtedStatements.contains(stmt)) {
System.out.println("attempted this before");
continue;
}
System.out.println("trying statement: " + stmt);
attepmtedStatements.add(stmt);
it.remove();
deletedStatements++;
done = false;
break;
}
condensedDataset.removeNamedModel(graphName);
if (!condensedModel.isEmpty()) {
condensedDataset.addNamedModel(graphName, condensedModel);
}
if (!passesTest(condensedDataset, expectedOutput)) {
System.out.println("could not delete statement");
condensedModel = backupModel;
condensedDataset.replaceNamedModel(graphName, condensedModel);
deletedStatements--;
} else {
System.out.println("deleted a statement");
}
}
if (!passesTest(condensedDataset, expectedOutput)) {
System.out.println("test does not pass after removing statements!");
condensedDataset = backupDataset;
} else {
System.out.println("removed " + deletedStatements + " statements");
}
} else {
System.out.println("removed graph: " + graphName);
}
System.out.println("dataset has ");
}
RDFDataMgr.write(new FileOutputStream(Paths.get(outputPath + filename).toFile()), condensedDataset, Lang.TRIG);
System.out.println("wrote condensed input file to: " + outputPath + filename);
}
use of org.apache.jena.rdf.model.Statement in project legato by DOREMUS-ANR.
the class ModelManager method getAllPropFromModels.
/**
*************************************
***Get all properties from two models
**************************************
*/
public static List<Property> getAllPropFromModels(Model srcModel, Model tgtModel) {
List<Property> propList = new ArrayList<Property>();
StmtIterator iter1 = srcModel.listStatements();
while (iter1.hasNext()) {
Statement stmt = iter1.nextStatement();
Property prop = stmt.getPredicate();
if (!propList.contains(prop))
propList.add(prop);
}
StmtIterator iter2 = tgtModel.listStatements();
while (iter2.hasNext()) {
Statement stmt = iter2.nextStatement();
Property prop = stmt.getPredicate();
if (!propList.contains(prop))
propList.add(prop);
}
return propList;
}
use of org.apache.jena.rdf.model.Statement in project legato by DOREMUS-ANR.
the class ModelManager method getFilteredTriples.
/**
******
* Filter triples from an RDF model with specific properties
*******
*/
public static Model getFilteredTriples(Model model, List<Property> listProperties) {
Model newModel = ModelFactory.createDefaultModel();
StmtIterator iter = model.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
Property property = stmt.getPredicate();
if (listProperties.contains(property)) {
newModel.add(stmt);
}
}
return model;
}
Aggregations