Search in sources :

Example 86 with Statement

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);
}
Also used : Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) WonMessageValidator(won.protocol.validation.WonMessageValidator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 87 with Statement

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);
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) RdfUtils(won.protocol.util.RdfUtils)

Example 88 with Statement

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);
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) RdfUtils(won.protocol.util.RdfUtils)

Example 89 with Statement

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;
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) Property(org.apache.jena.rdf.model.Property)

Example 90 with Statement

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;
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property)

Aggregations

Statement (org.apache.jena.rdf.model.Statement)141 Model (org.apache.jena.rdf.model.Model)56 Resource (org.apache.jena.rdf.model.Resource)46 StmtIterator (org.apache.jena.rdf.model.StmtIterator)46 Test (org.junit.Test)39 RDFNode (org.apache.jena.rdf.model.RDFNode)29 Dataset (org.apache.jena.query.Dataset)28 ArrayList (java.util.ArrayList)25 Property (org.apache.jena.rdf.model.Property)18 ReifiedStatement (org.apache.jena.rdf.model.ReifiedStatement)17 Triple (org.apache.jena.graph.Triple)16 WonMessageValidator (won.protocol.validation.WonMessageValidator)14 TextIndexException (org.apache.jena.query.text.TextIndexException)12 Ignore (org.junit.Ignore)10 ReadDeniedException (org.apache.jena.shared.ReadDeniedException)8 Iterator (java.util.Iterator)7 SecuredReifiedStatement (org.apache.jena.permissions.model.SecuredReifiedStatement)7 SecuredStatement (org.apache.jena.permissions.model.SecuredStatement)7 Literal (org.apache.jena.rdf.model.Literal)7 SecuredPrefixMappingTest (org.apache.jena.permissions.graph.SecuredPrefixMappingTest)6